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) {
|
if (self->tq_head == NULL) {
|
||||||
self->tq_head = self->tq_tail = handle;
|
self->tq_head = self->tq_tail = handle;
|
||||||
handle->next = NULL;
|
handle->next = NULL;
|
||||||
|
handle->vrfy = NWMAGIC;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
/* if new one is not earlier than latest one, insert after latest */
|
/* 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->next = handle;
|
||||||
self->tq_tail = handle;
|
self->tq_tail = handle;
|
||||||
handle->next = NULL;
|
handle->next = NULL;
|
||||||
|
handle->vrfy = NWMAGIC;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
/* if new one is not later than earliest one, insert before earliest */
|
/* 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->tv.tv_usec <= self->tq_head->tv.tv_usec)) {
|
||||||
handle->next = self->tq_head;
|
handle->next = self->tq_head;
|
||||||
self->tq_head = handle;
|
self->tq_head = handle;
|
||||||
|
handle->vrfy = NWMAGIC;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -113,8 +116,10 @@ static int NetWatchTimerInsQue(pNetWatch self, pNWTimer handle)
|
|||||||
/* slip new one in between this one and the next one */
|
/* slip new one in between this one and the next one */
|
||||||
handle->next = pNxt->next;
|
handle->next = pNxt->next;
|
||||||
pNxt->next = handle ;
|
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)
|
static int NetWatchTimerRemQue(pNetWatch self, pNWTimer handle)
|
||||||
{
|
{
|
||||||
|
assert(handle->vrfy == NWMAGIC);
|
||||||
/* handle the case of first and possibly only */
|
/* handle the case of first and possibly only */
|
||||||
if (handle == self->tq_head) {
|
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 */
|
||||||
@@ -145,12 +151,14 @@ static int NetWatchTimerRemQue(pNetWatch self, pNWTimer handle)
|
|||||||
if (handle == self->tq_tail)
|
if (handle == self->tq_tail)
|
||||||
self->tq_tail = pNxt;
|
self->tq_tail = pNxt;
|
||||||
}
|
}
|
||||||
|
handle->vrfy = 0;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int NetWatchRegisterTimer(pNWTimer* handle, int mSec,
|
int NetWatchRegisterTimer(pNWTimer* handle, int mSec,
|
||||||
pNWCallback callback, void* context)
|
pNWCallback callback, void* context)
|
||||||
{
|
{
|
||||||
|
assert(callback);
|
||||||
pNetWatch self = instance;
|
pNetWatch self = instance;
|
||||||
if(!self || self->lMagic != NWMAGIC)
|
if(!self || self->lMagic != NWMAGIC)
|
||||||
return 0;
|
return 0;
|
||||||
@@ -168,7 +176,6 @@ int NetWatchRegisterTimer(pNWTimer* handle, int mSec,
|
|||||||
pNew->tick = 0;
|
pNew->tick = 0;
|
||||||
pNew->func = callback;
|
pNew->func = callback;
|
||||||
pNew->cntx = context;
|
pNew->cntx = context;
|
||||||
pNew->vrfy = NWMAGIC;
|
|
||||||
NetWatchTimerInsQue(self, pNew);
|
NetWatchTimerInsQue(self, pNew);
|
||||||
*handle = pNew;
|
*handle = pNew;
|
||||||
return 1;
|
return 1;
|
||||||
@@ -177,6 +184,7 @@ int NetWatchRegisterTimer(pNWTimer* handle, int mSec,
|
|||||||
int NetWatchRegisterTimerPeriodic(pNWTimer* handle, int mSecInitial, int mSecPeriod,
|
int NetWatchRegisterTimerPeriodic(pNWTimer* handle, int mSecInitial, int mSecPeriod,
|
||||||
pNWCallback callback, void* context)
|
pNWCallback callback, void* context)
|
||||||
{
|
{
|
||||||
|
assert(callback);
|
||||||
if (NetWatchRegisterTimer(handle, mSecInitial, callback, context)) {
|
if (NetWatchRegisterTimer(handle, mSecInitial, callback, context)) {
|
||||||
pNWTimer pNew = *handle;
|
pNWTimer pNew = *handle;
|
||||||
if (pNew == NULL)
|
if (pNew == NULL)
|
||||||
@@ -208,8 +216,9 @@ int NetWatchRemoveTimer(pNWTimer handle)
|
|||||||
pNetWatch self = instance;
|
pNetWatch self = instance;
|
||||||
if (!self || self->lMagic != NWMAGIC)\
|
if (!self || self->lMagic != NWMAGIC)\
|
||||||
return 0;
|
return 0;
|
||||||
|
if (handle == NULL || handle->vrfy != NWMAGIC)
|
||||||
|
return 0;
|
||||||
NetWatchTimerRemQue(self, handle);
|
NetWatchTimerRemQue(self, handle);
|
||||||
handle->vrfy = 0;
|
|
||||||
free(handle);
|
free(handle);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@@ -238,6 +247,7 @@ static int NetWatchContextInsQue(pNetWatch self, pNWContext handle)
|
|||||||
self->cq_tail->next = handle;
|
self->cq_tail->next = handle;
|
||||||
self->cq_tail = handle;
|
self->cq_tail = handle;
|
||||||
}
|
}
|
||||||
|
handle->vrfy = NWMAGIC;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -266,6 +276,7 @@ static void NetWatchContextRemQue(pNetWatch self, pNWContext handle)
|
|||||||
if (handle == self->cq_tail) /* if last */
|
if (handle == self->cq_tail) /* if last */
|
||||||
self->cq_tail = pNxt;
|
self->cq_tail = pNxt;
|
||||||
}
|
}
|
||||||
|
handle->vrfy = 0;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -286,19 +297,19 @@ static void NetWatchContextPrgQue(pNetWatch self)
|
|||||||
free(tmp);
|
free(tmp);
|
||||||
}
|
}
|
||||||
pNxt = self->cq_head;
|
pNxt = self->cq_head;
|
||||||
while (pNxt) {
|
while (pNxt && pNxt->next) {
|
||||||
if (pNxt->next && pNxt->next->sock < 0) {
|
if (pNxt->next->sock < 0) {
|
||||||
pNWContext tmp = NULL;
|
pNWContext tmp = NULL;
|
||||||
tmp = pNxt->next;
|
tmp = pNxt->next;
|
||||||
pNxt->next = pNxt->next->next;
|
pNxt->next = pNxt->next->next;
|
||||||
tmp->vrfy = 0;
|
tmp->vrfy = 0;
|
||||||
free(tmp);
|
free(tmp);
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
pNxt = pNxt->next;
|
pNxt = pNxt->next;
|
||||||
}
|
}
|
||||||
/* if the queue is empty clear the tail */
|
/* if the queue is empty then pNxt is NULL else pNxt points to the tail */
|
||||||
if (self->cq_head == NULL)
|
self->cq_tail = pNxt;
|
||||||
self->cq_tail = pNxt;
|
|
||||||
self->nInvalid = 0;
|
self->nInvalid = 0;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -306,6 +317,7 @@ static void NetWatchContextPrgQue(pNetWatch self)
|
|||||||
int NetWatchRegisterCallback(pNWContext* handle, int iSocket,
|
int NetWatchRegisterCallback(pNWContext* handle, int iSocket,
|
||||||
pNWCallback callback, void* context)
|
pNWCallback callback, void* context)
|
||||||
{
|
{
|
||||||
|
assert(callback);
|
||||||
pNWContext pNew = NULL;
|
pNWContext pNew = NULL;
|
||||||
pNetWatch self = instance;
|
pNetWatch self = instance;
|
||||||
if(!self || self->lMagic != NWMAGIC)
|
if(!self || self->lMagic != NWMAGIC)
|
||||||
@@ -320,7 +332,6 @@ int NetWatchRegisterCallback(pNWContext* handle, int iSocket,
|
|||||||
pNew->mode = nwatch_read;
|
pNew->mode = nwatch_read;
|
||||||
pNew->func = callback;
|
pNew->func = callback;
|
||||||
pNew->cntx = context;
|
pNew->cntx = context;
|
||||||
pNew->vrfy = NWMAGIC;
|
|
||||||
*handle = pNew;
|
*handle = pNew;
|
||||||
NetWatchContextInsQue(self, pNew);
|
NetWatchContextInsQue(self, pNew);
|
||||||
return 1;
|
return 1;
|
||||||
@@ -375,7 +386,7 @@ int NetWatchTask (void* pData)
|
|||||||
if (self->nInvalid > 0)
|
if (self->nInvalid > 0)
|
||||||
NetWatchContextPrgQue(self);
|
NetWatchContextPrgQue(self);
|
||||||
|
|
||||||
/* build the select mask */
|
/* build the select mask */
|
||||||
FD_ZERO(&rMask);
|
FD_ZERO(&rMask);
|
||||||
FD_ZERO(&wMask);
|
FD_ZERO(&wMask);
|
||||||
pNWC = self->cq_head;
|
pNWC = self->cq_head;
|
||||||
@@ -453,7 +464,6 @@ int NetWatchTask (void* pData)
|
|||||||
NetWatchTimerInsQue(self, pNew);
|
NetWatchTimerInsQue(self, pNew);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
pNew->vrfy = 0;
|
|
||||||
free(pNew);
|
free(pNew);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user