- Adapted indenation to new agreed upon system

- Added support for second generation scriptcontext based counter
This commit is contained in:
koennecke
2009-02-13 09:00:03 +00:00
parent a3dcad2bfa
commit 91d4af0541
405 changed files with 88101 additions and 88173 deletions

121
nwatch.c
View File

@ -25,25 +25,26 @@
/* Net Watcher control structure */
typedef struct __netwatcher_s {
pNWContext cq_head; /* head of socket context queue */
pNWContext cq_tail; /* tail of socket context queue */
int nInvalid; /* number of invalidated entries */
pNWTimer tq_head; /* head of timer context queue */
pNWTimer tq_tail; /* tail of timer context queue */
long lMagic; /* integrity check */
pNWContext cq_head; /* head of socket context queue */
pNWContext cq_tail; /* tail of socket context queue */
int nInvalid; /* number of invalidated entries */
pNWTimer tq_head; /* head of timer context queue */
pNWTimer tq_tail; /* tail of timer context queue */
long lMagic; /* integrity check */
} NetWatch, *pNetWatch;
/* Singleton pattern */
static pNetWatch instance = NULL;
static int NetWatchTask(void* pData);
static int NetWatchTask(void *pData);
/**
* \brief Initialises the Net Watcher singleton and starts the task
*
* \return 1=success, 0=failure
*/
int NetWatchInit(void) {
int NetWatchInit(void)
{
/*
* If the singleton has not yet been created, do so now
*/
@ -62,12 +63,12 @@ int NetWatchInit(void) {
* The timer context object private definition
*/
typedef struct __netwatchtimer {
pNWTimer next; /* chain to next event */
struct timeval tv; /* time when event is due */
pNWCallback func; /* function to call */
void* cntx; /* abstract context to pass to callback */
long int tick; /* millisecond repeat rate */
long int vrfy; /* integrity check */
pNWTimer next; /* chain to next event */
struct timeval tv; /* time when event is due */
pNWCallback func; /* function to call */
void *cntx; /* abstract context to pass to callback */
long int tick; /* millisecond repeat rate */
long int vrfy; /* integrity check */
} NWTimer;
/*
@ -100,20 +101,18 @@ static int NetWatchTimerInsQue(pNetWatch self, pNWTimer handle)
handle->next = self->tq_head;
self->tq_head = handle;
return 1;
}
else
{
} else {
/* must be in between two so start at the first entry */
pNWTimer pNxt = self->tq_head;
/* follow chain until the one after this one is greater than new one */
while (pNxt->next &&
(handle->tv.tv_sec > pNxt->next->tv.tv_sec ||
(handle->tv.tv_sec == pNxt->next->tv.tv_sec &&
handle->tv.tv_usec > pNxt->next->tv.tv_usec)))
(handle->tv.tv_sec > pNxt->next->tv.tv_sec ||
(handle->tv.tv_sec == pNxt->next->tv.tv_sec &&
handle->tv.tv_usec > pNxt->next->tv.tv_usec)))
pNxt = pNxt->next;
/* slip new one in between this one and the next one */
handle->next = pNxt->next;
pNxt->next = handle ;
pNxt->next = handle;
}
return 1;
}
@ -128,7 +127,7 @@ static int NetWatchTimerRemQue(pNetWatch self, pNWTimer handle)
{
/* handle the case of first and possibly only */
if (handle == self->tq_head) {
self->tq_head = self->tq_head->next; /* may be NULL */
self->tq_head = self->tq_head->next; /* may be NULL */
if (handle == self->tq_tail)
self->tq_tail = NULL;
}
@ -149,11 +148,11 @@ static int NetWatchTimerRemQue(pNetWatch self, pNWTimer handle)
return 1;
}
int NetWatchRegisterTimer(pNWTimer* handle, int mSec,
pNWCallback callback, void* context)
int NetWatchRegisterTimer(pNWTimer * handle, int mSec,
pNWCallback callback, void *context)
{
pNetWatch self = instance;
if(!self || self->lMagic != NWMAGIC)
if (!self || self->lMagic != NWMAGIC)
return 0;
pNWTimer pNew = (pNWTimer) malloc(sizeof(NWTimer));
if (pNew == NULL)
@ -163,7 +162,7 @@ int NetWatchRegisterTimer(pNWTimer* handle, int mSec,
pNew->tv.tv_sec += mSec / 1000;
pNew->tv.tv_usec += 1000 * (mSec % 1000);
if (pNew->tv.tv_usec > 1000000) {
pNew->tv.tv_sec ++;
pNew->tv.tv_sec++;
pNew->tv.tv_usec -= 1000000;
}
pNew->tick = 0;
@ -175,8 +174,9 @@ int NetWatchRegisterTimer(pNWTimer* handle, int mSec,
return 1;
}
int NetWatchRegisterTimerPeriodic(pNWTimer* handle, int mSecInitial, int mSecPeriod,
pNWCallback callback, void* context)
int NetWatchRegisterTimerPeriodic(pNWTimer * handle, int mSecInitial,
int mSecPeriod, pNWCallback callback,
void *context)
{
if (NetWatchRegisterTimer(handle, mSecInitial, callback, context)) {
pNWTimer pNew = *handle;
@ -207,7 +207,7 @@ int NetWatchSetTimerPeriod(pNWTimer handle, int mSecPeriod)
int NetWatchRemoveTimer(pNWTimer handle)
{
pNetWatch self = instance;
if (!self || self->lMagic != NWMAGIC)\
if (!self || self->lMagic != NWMAGIC)
return 0;
NetWatchTimerRemQue(self, handle);
handle->vrfy = 0;
@ -217,12 +217,12 @@ int NetWatchRemoveTimer(pNWTimer handle)
/* private data */
typedef struct __netwatchcontext {
pNWContext next; /* chain pointer */
int sock; /* socket to watch */
int mode; /* read or write */
pNWCallback func; /* user supplied callback function */
void* cntx; /* user supplied callback context */
long vrfy; /* integrity check */
pNWContext next; /* chain pointer */
int sock; /* socket to watch */
int mode; /* read or write */
pNWCallback func; /* user supplied callback function */
void *cntx; /* user supplied callback context */
long vrfy; /* integrity check */
} NWContext;
/**
@ -233,7 +233,7 @@ typedef struct __netwatchcontext {
*/
static int NetWatchContextInsQue(pNetWatch self, pNWContext handle)
{
if (self->cq_head == NULL) /* empty */
if (self->cq_head == NULL) /* empty */
self->cq_head = self->cq_tail = handle;
else {
self->cq_tail->next = handle;
@ -250,12 +250,11 @@ static int NetWatchContextInsQue(pNetWatch self, pNWContext handle)
*/
static void NetWatchContextRemQue(pNetWatch self, pNWContext handle)
{
if (handle == self->cq_head) { /* if first */
if (handle == self->cq_head) { /* if first */
self->cq_head = self->cq_head->next;
if (handle == self->cq_tail) /* if also last */
if (handle == self->cq_tail) /* if also last */
self->cq_tail = NULL;
}
else {
} else {
pNWContext pNxt = self->cq_head;
while (pNxt) {
if (handle == pNxt->next) {
@ -264,7 +263,7 @@ static void NetWatchContextRemQue(pNetWatch self, pNWContext handle)
}
pNxt = pNxt->next;
}
if (handle == self->cq_tail) /* if last */
if (handle == self->cq_tail) /* if last */
self->cq_tail = pNxt;
}
return;
@ -304,12 +303,12 @@ static void NetWatchContextPrgQue(pNetWatch self)
return;
}
int NetWatchRegisterCallback(pNWContext* handle, int iSocket,
pNWCallback callback, void* context)
int NetWatchRegisterCallback(pNWContext * handle, int iSocket,
pNWCallback callback, void *context)
{
pNWContext pNew = NULL;
pNetWatch self = instance;
if(!self || self->lMagic != NWMAGIC)
if (!self || self->lMagic != NWMAGIC)
return 0;
if (iSocket < 0 || iSocket > 65535)
return 0;
@ -332,7 +331,7 @@ int NetWatchRemoveCallback(pNWContext handle)
pNetWatch self = instance;
if (handle == NULL || handle->vrfy != NWMAGIC)
return 0;
if(!self || self->lMagic != NWMAGIC)
if (!self || self->lMagic != NWMAGIC)
return 0;
handle->sock = -1;
self->nInvalid++;
@ -357,19 +356,19 @@ int NetWatchSetMode(pNWContext handle, int mode)
/**
* \brief the registered SICS Task to drive all this
*/
int NetWatchTask (void* pData)
int NetWatchTask(void *pData)
{
pNetWatch self = NULL;
pNWContext pNWC = NULL;
fd_set rMask;
fd_set wMask;
struct timeval tmo = {0,0};
struct timeval tmo = { 0, 0 };
int iRet;
int iCount;
/* Check the singleton */
self = (pNetWatch) instance;
if(!self || self->lMagic != NWMAGIC)
if (!self || self->lMagic != NWMAGIC)
return 0;
/* Purge the invalidated */
@ -381,13 +380,13 @@ int NetWatchTask (void* pData)
FD_ZERO(&wMask);
pNWC = self->cq_head;
iCount = -1;
while(pNWC) {
while (pNWC) {
if (pNWC->sock >= 0 && pNWC->sock <= 65535) {
if (pNWC->mode & nwatch_read)
FD_SET(pNWC->sock,&rMask);
FD_SET(pNWC->sock, &rMask);
if (pNWC->mode & nwatch_write)
FD_SET(pNWC->sock,&wMask);
if(pNWC->sock > iCount) {
FD_SET(pNWC->sock, &wMask);
if (pNWC->sock > iCount) {
iCount = pNWC->sock;
}
}
@ -396,14 +395,13 @@ int NetWatchTask (void* pData)
iRet = 0;
if (iCount >= 0)
iRet = uselect(iCount+1, &rMask, &wMask, NULL, &tmo);
iRet = uselect(iCount + 1, &rMask, &wMask, NULL, &tmo);
if(iRet > 0) {
if (iRet > 0) {
/* invoke the active callbacks */
iCount = 0;
pNWC = self->cq_head;
while(pNWC)
{
while (pNWC) {
if (pNWC->sock >= 0 && pNWC->sock <= 65535) {
int action_mode = 0;
if ((pNWC->mode & nwatch_read) && FD_ISSET(pNWC->sock, &rMask))
@ -412,7 +410,7 @@ int NetWatchTask (void* pData)
action_mode |= nwatch_write;
if (action_mode != 0) {
int iStatus;
iStatus = (*pNWC->func)(pNWC->cntx, action_mode);
iStatus = (*pNWC->func) (pNWC->cntx, action_mode);
}
}
pNWC = pNWC->next;
@ -427,7 +425,7 @@ int NetWatchTask (void* pData)
while (self->tq_head) {
pNWTimer pNew = self->tq_head;
if (tv.tv_sec < pNew->tv.tv_sec ||
(tv.tv_sec == pNew->tv.tv_sec &&
(tv.tv_sec == pNew->tv.tv_sec &&
tv.tv_usec < pNew->tv.tv_usec)) {
break;
}
@ -443,8 +441,8 @@ int NetWatchTask (void* pData)
*/
gettimeofday(&tv, NULL);
while (tv.tv_sec > pNew->tv.tv_sec ||
(tv.tv_sec == pNew->tv.tv_sec &&
tv.tv_usec > pNew->tv.tv_usec)) {
(tv.tv_sec == pNew->tv.tv_sec &&
tv.tv_usec > pNew->tv.tv_usec)) {
pNew->tv.tv_usec += 1000 * pNew->tick;
if (pNew->tv.tv_usec > 1000000) {
pNew->tv.tv_sec += pNew->tv.tv_usec / 1000000;
@ -452,8 +450,7 @@ int NetWatchTask (void* pData)
}
}
NetWatchTimerInsQue(self, pNew);
}
else {
} else {
pNew->vrfy = 0;
free(pNew);
}