Improve robustness and fix bug when removing last queue entry does not update tail pointer.
r2276 | dcl | 2008-01-15 12:32:20 +1100 (Tue, 15 Jan 2008) | 2 lines
This commit is contained in:
32
nwatch.c
32
nwatch.c
@@ -81,6 +81,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 */
|
||||
@@ -90,6 +91,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 */
|
||||
@@ -98,6 +100,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
|
||||
@@ -113,8 +116,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 +130,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 +151,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;
|
||||
@@ -168,7 +176,6 @@ int NetWatchRegisterTimer(pNWTimer* handle, int mSec,
|
||||
pNew->tick = 0;
|
||||
pNew->func = callback;
|
||||
pNew->cntx = context;
|
||||
pNew->vrfy = NWMAGIC;
|
||||
NetWatchTimerInsQue(self, pNew);
|
||||
*handle = pNew;
|
||||
return 1;
|
||||
@@ -177,6 +184,7 @@ int NetWatchRegisterTimer(pNWTimer* handle, int mSec,
|
||||
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)
|
||||
@@ -208,8 +216,9 @@ 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);
|
||||
return 1;
|
||||
}
|
||||
@@ -238,6 +247,7 @@ static int NetWatchContextInsQue(pNetWatch self, pNWContext handle)
|
||||
self->cq_tail->next = handle;
|
||||
self->cq_tail = handle;
|
||||
}
|
||||
handle->vrfy = NWMAGIC;
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -266,6 +276,7 @@ static void NetWatchContextRemQue(pNetWatch self, pNWContext handle)
|
||||
if (handle == self->cq_tail) /* if last */
|
||||
self->cq_tail = pNxt;
|
||||
}
|
||||
handle->vrfy = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -286,19 +297,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 +317,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 +332,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;
|
||||
@@ -375,7 +386,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;
|
||||
@@ -453,7 +464,6 @@ int NetWatchTask (void* pData)
|
||||
NetWatchTimerInsQue(self, pNew);
|
||||
}
|
||||
else {
|
||||
pNew->vrfy = 0;
|
||||
free(pNew);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user