get it to build

This commit is contained in:
Douglas Clowes
2012-11-27 13:34:05 +11:00
parent ca11a3cfe4
commit 27e89241cf
11 changed files with 502 additions and 196 deletions

View File

@ -67,10 +67,13 @@ typedef struct __netwatchtimer {
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 */
int msec; /* millisecond delay time */
int tick; /* millisecond repeat rate */
long int vrfy; /* integrity check */
} NWTimer;
static pNWTimer activeTimer = NULL;
/*
* \brief private function to insert an entry into the sorted timer queue.
*
@ -83,6 +86,7 @@ static int NetWatchTimerInsQue(pNetWatch self, pNWTimer handle)
if (self->tq_head == NULL) {
self->tq_head = self->tq_tail = handle;
handle->next = NULL;
handle->vrfy = NWMAGIC;
return 1;
}
/* if new one is not earlier than latest one, insert after latest */
@ -92,6 +96,7 @@ static int NetWatchTimerInsQue(pNetWatch self, pNWTimer handle)
self->tq_tail->next = handle;
self->tq_tail = handle;
handle->next = NULL;
handle->vrfy = NWMAGIC;
return 1;
}
/* if new one is not later than earliest one, insert before earliest */
@ -100,6 +105,7 @@ static int NetWatchTimerInsQue(pNetWatch self, pNWTimer handle)
handle->tv.tv_usec <= self->tq_head->tv.tv_usec)) {
handle->next = self->tq_head;
self->tq_head = handle;
handle->vrfy = NWMAGIC;
return 1;
} else {
/* must be in between two so start at the first entry */
@ -113,8 +119,10 @@ static int NetWatchTimerInsQue(pNetWatch self, pNWTimer handle)
/* slip new one in between this one and the next one */
handle->next = pNxt->next;
pNxt->next = handle;
handle->vrfy = NWMAGIC;
return 1;
}
return 1;
return 0;
}
/*
@ -125,6 +133,7 @@ static int NetWatchTimerInsQue(pNetWatch self, pNWTimer handle)
*/
static int NetWatchTimerRemQue(pNetWatch self, pNWTimer handle)
{
assert(handle->vrfy == NWMAGIC);
/* handle the case of first and possibly only */
if (handle == self->tq_head) {
self->tq_head = self->tq_head->next; /* may be NULL */
@ -145,12 +154,14 @@ static int NetWatchTimerRemQue(pNetWatch self, pNWTimer handle)
if (handle == self->tq_tail)
self->tq_tail = pNxt;
}
handle->vrfy = 0;
return 1;
}
int NetWatchRegisterTimer(pNWTimer * handle, int mSec,
pNWCallback callback, void *context)
{
assert(callback);
pNetWatch self = instance;
if (!self || self->lMagic != NWMAGIC)
return 0;
@ -165,10 +176,10 @@ int NetWatchRegisterTimer(pNWTimer * handle, int mSec,
pNew->tv.tv_sec++;
pNew->tv.tv_usec -= 1000000;
}
pNew->msec = mSec;
pNew->tick = 0;
pNew->func = callback;
pNew->cntx = context;
pNew->vrfy = NWMAGIC;
NetWatchTimerInsQue(self, pNew);
*handle = pNew;
return 1;
@ -178,6 +189,7 @@ int NetWatchRegisterTimerPeriodic(pNWTimer * handle, int mSecInitial,
int mSecPeriod, pNWCallback callback,
void *context)
{
assert(callback);
if (NetWatchRegisterTimer(handle, mSecInitial, callback, context)) {
pNWTimer pNew = *handle;
if (pNew == NULL)
@ -189,16 +201,36 @@ int NetWatchRegisterTimerPeriodic(pNWTimer * handle, int mSecInitial,
return 0;
}
pNWTimer NetWatchGetActiveTimer(void)
{
return activeTimer;
}
int NetWatchGetTimerInitial(pNWTimer handle)
{
if (handle == NULL
|| (handle->vrfy != NWMAGIC && handle->vrfy != ~NWMAGIC))
return 0;
return handle->msec;
}
int NetWatchGetTimerDelay(pNWTimer handle)
{
return NetWatchGetTimerInitial(handle);
}
int NetWatchGetTimerPeriod(pNWTimer handle)
{
if (handle == NULL || handle->vrfy != NWMAGIC)
if (handle == NULL
|| (handle->vrfy != NWMAGIC && handle->vrfy != ~NWMAGIC))
return 0;
return handle->tick;
}
int NetWatchSetTimerPeriod(pNWTimer handle, int mSecPeriod)
{
if (handle == NULL || handle->vrfy != NWMAGIC)
if (handle == NULL
|| (handle->vrfy != NWMAGIC && handle->vrfy != ~NWMAGIC))
return 0;
handle->tick = mSecPeriod;
return 1;
@ -209,6 +241,8 @@ int NetWatchRemoveTimer(pNWTimer handle)
pNetWatch self = instance;
if (!self || self->lMagic != NWMAGIC)
return 0;
if (handle == NULL || handle->vrfy != NWMAGIC)
return 0;
NetWatchTimerRemQue(self, handle);
handle->vrfy = 0;
free(handle);
@ -239,35 +273,10 @@ static int NetWatchContextInsQue(pNetWatch self, pNWContext handle)
self->cq_tail->next = handle;
self->cq_tail = handle;
}
handle->vrfy = NWMAGIC;
return 1;
}
/**
* \brief private function to remove entry from unsorted queue
*
* \param self singleton
* \param handle entry to insert
*/
static void NetWatchContextRemQue(pNetWatch self, pNWContext handle)
{
if (handle == self->cq_head) { /* if first */
self->cq_head = self->cq_head->next;
if (handle == self->cq_tail) /* if also last */
self->cq_tail = NULL;
} else {
pNWContext pNxt = self->cq_head;
while (pNxt) {
if (handle == pNxt->next) {
pNxt->next = pNxt->next->next;
break;
}
pNxt = pNxt->next;
}
if (handle == self->cq_tail) /* if last */
self->cq_tail = pNxt;
}
return;
}
/**
* \brief private function to purge invalid entries
@ -286,19 +295,19 @@ static void NetWatchContextPrgQue(pNetWatch self)
free(tmp);
}
pNxt = self->cq_head;
while (pNxt) {
if (pNxt->next && pNxt->next->sock < 0) {
while (pNxt && pNxt->next) {
if (pNxt->next->sock < 0) {
pNWContext tmp = NULL;
tmp = pNxt->next;
pNxt->next = pNxt->next->next;
tmp->vrfy = 0;
free(tmp);
continue;
}
pNxt = pNxt->next;
}
/* if the queue is empty clear the tail */
if (self->cq_head == NULL)
self->cq_tail = pNxt;
/* if the queue is empty then pNxt is NULL else pNxt points to the tail */
self->cq_tail = pNxt;
self->nInvalid = 0;
return;
}
@ -306,6 +315,7 @@ static void NetWatchContextPrgQue(pNetWatch self)
int NetWatchRegisterCallback(pNWContext * handle, int iSocket,
pNWCallback callback, void *context)
{
assert(callback);
pNWContext pNew = NULL;
pNetWatch self = instance;
if (!self || self->lMagic != NWMAGIC)
@ -320,7 +330,6 @@ int NetWatchRegisterCallback(pNWContext * handle, int iSocket,
pNew->mode = nwatch_read;
pNew->func = callback;
pNew->cntx = context;
pNew->vrfy = NWMAGIC;
*handle = pNew;
NetWatchContextInsQue(self, pNew);
return 1;
@ -333,8 +342,11 @@ int NetWatchRemoveCallback(pNWContext handle)
return 0;
if (!self || self->lMagic != NWMAGIC)
return 0;
/* mark as invalid */
handle->sock = -1;
/* increment count of invalid */
self->nInvalid++;
/* leave for garbage collection */
return 1;
}
@ -375,7 +387,7 @@ int NetWatchTask(void *pData)
if (self->nInvalid > 0)
NetWatchContextPrgQue(self);
/* build the select mask */
/* build the select mask */
FD_ZERO(&rMask);
FD_ZERO(&wMask);
pNWC = self->cq_head;
@ -430,7 +442,11 @@ int NetWatchTask(void *pData)
break;
}
NetWatchTimerRemQue(self, pNew);
activeTimer = pNew;
activeTimer->vrfy = ~NWMAGIC;
iStatus = pNew->func(pNew->cntx, 0);
activeTimer->vrfy = 0;
activeTimer = NULL;
/*
* If this is a recurrent timer and the function
* indicates to keep it going, put it back in