corrected bad specified timeouts (ms versus us) M.Z.

This commit is contained in:
cvs
2004-11-17 11:25:43 +00:00
parent db94826248
commit 4e06a17518
4 changed files with 66 additions and 55 deletions

View File

@ -159,7 +159,7 @@ CreateSocketAdress(
}
/*---------------------------------------------------------------------------*/
mkChannel *NETAccept(mkChannel *self, int timeout)
mkChannel *NETAccept(mkChannel *self, long timeout)
{
mkChannel *pRes = NULL;
int iRet;
@ -173,7 +173,8 @@ CreateSocketAdress(
if(timeout > 0)
{
/* select first */
tmo.tv_usec = timeout;
tmo.tv_usec = (timeout % 1000) * 1000;
tmo.tv_sec = timeout / 1000;
FD_ZERO(&lMask);
FD_SET(self->sockid,&lMask);
if((self->sockid >= FD_SETSIZE) || (self->sockid < 0) ) /* invalid descriptor */
@ -336,7 +337,8 @@ CreateSocketAdress(
return 0;
}
/* setup for select first */
#ifndef CYGNUS
/* setup for select first */
tmo.tv_usec = 100;
FD_ZERO(&lMask);
FD_SET(self->sockid,&lMask);
@ -345,7 +347,6 @@ CreateSocketAdress(
{
return -1; /* eof */
}
#ifndef CYGNUS
iRet = select( (self->sockid + 1),NULL, &lMask, NULL,&tmo);
if( iRet <= 0)
{
@ -375,7 +376,7 @@ CreateSocketAdress(
}
/* -------------------------------------------------------------------------*/
long NETRead(mkChannel *self, char *buffer, long lLen, int timeout)
long NETRead(mkChannel *self, char *buffer, long lLen, long timeout)
{
fd_set lMask;
struct timeval tmo ={0,1};
@ -389,8 +390,8 @@ CreateSocketAdress(
if(timeout > 0)
{
/* setup for select first */
tmo.tv_usec = timeout % 1000000;
tmo.tv_sec = timeout / 1000000;
tmo.tv_usec = (timeout % 1000) *1000;
tmo.tv_sec = timeout / 1000;
FD_ZERO(&lMask);
FD_SET(self->sockid,&lMask);
if((self->sockid >= FD_SETSIZE) || (self->sockid < 0) ) /* invalid descriptor */
@ -424,7 +425,7 @@ CreateSocketAdress(
}
}
/*---------------------------------------------------------------------*/
int NETAvailable(mkChannel *self, int timeout)
int NETAvailable(mkChannel *self, long timeout)
{
fd_set lMask;
struct timeval tmo ={0,1};
@ -436,8 +437,8 @@ CreateSocketAdress(
}
/* setup for select */
tmo.tv_usec = timeout % 1000000;
tmo.tv_sec = timeout / 1000000;
tmo.tv_usec = (timeout % 1000) * 1000;
tmo.tv_sec = timeout / 1000;
FD_ZERO(&lMask);
FD_SET(self->sockid,&lMask);
if((self->sockid >= FD_SETSIZE) || (self->sockid < 0) )
@ -460,23 +461,25 @@ CreateSocketAdress(
}
}
/*-------------------------------------------------------------------------*/
int NETReadTillTermNew(mkChannel *self, int timeout,
int NETReadTillTermNew(mkChannel *self, long timeout,
char *pTerm, char *pBuffer, int iBufLen)
{
time_t maxTime, now;
struct timeval start, now;
int bufPtr = 0, status, i, length;
char c;
long dif;
if(!VerifyChannel(self))
{
return -1;
}
maxTime = time(NULL) + (time_t)ceil((double)timeout/1000 +1);
gettimeofday(&start, NULL);
length = strlen(pTerm);
memset(pBuffer,0,iBufLen);
status = NETAvailable(self,timeout*1000); /* first time: full timeout */
status = NETAvailable(self,timeout); /* first time: full timeout */
if(status <= 0) /* return on error or on timeout */
{
return status;
@ -484,7 +487,7 @@ int NETReadTillTermNew(mkChannel *self, int timeout,
while (status > 0)
{
status = recv(self->sockid,&c,1,0);
if(status < 0)
if(status <= 0)
{
return status;
}
@ -492,7 +495,7 @@ int NETReadTillTermNew(mkChannel *self, int timeout,
{
if(c == pTerm[i])
{
return 1;
return 1;
}
}
if(c != 0) /* skip null characters */
@ -509,14 +512,17 @@ int NETReadTillTermNew(mkChannel *self, int timeout,
/*
wait for more data
*/
status = NETAvailable(self,1); /* minimal timeout */
while (status == 0)
status = NETAvailable(self,0); /* timeout 0 (just poll) */
if (status == 0 && timeout > 0)
{
if(time(NULL) > maxTime)
gettimeofday(&now, NULL);
dif = (now.tv_sec-start.tv_sec)*1000+(now.tv_usec-start.tv_usec)/1000;
if(dif > timeout)
{
return 0; /* timeout */
}
status = NETAvailable(self,100000); /* 0.1 s timeout */
status = NETAvailable(self,timeout-dif);
};
};
return status;
@ -737,7 +743,8 @@ This old version may be removed in some stage. It has two problems:
if(timeout > 0)
{
/* setup for select first */
tmo.tv_usec = timeout;
tmo.tv_usec = (timeout % 1000) *1000;
tmo.tv_sec = timeout / 1000;
lMask = (1 << self->sockid);
iRet = select( (self->sockid + 1),(fd_set *)&lMask, NULL, NULL,&tmo);
if( iRet <= 0)