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)

View File

@ -17,6 +17,8 @@
Mark Koennecke, October 2001
Changed all timeout units from microseconds to milliseconds
Markus Zolliker August 2004
----------------------------------------------------------------------------*/
#ifndef NNNET
#define NNNET
@ -44,12 +46,12 @@
else a valid mkChannel structure for the port
*/
mkChannel *NETAccept(mkChannel *self, int timeout);
mkChannel *NETAccept(mkChannel *self, long timeout);
/* tries to accept a new connection on the Channel self
until timeout. If a connection can be built a new mkChannel
structure is returned, else NULL. With a negative value or 0 for
timeout this function blocks for an accept.
*/
*/
mkChannel *NETConnect(char *name, int port);
/* tries to open a client connection to the server specified by name
@ -69,17 +71,18 @@
false otherwise.
*/
long NETRead(mkChannel *self, char *buffer, long lLen, int timeout);
long NETRead(mkChannel *self, char *buffer, long lLen, long timeout);
/* reads data from socket self into buffer with max length lLen
waits maximum timeout for data. Returns -1 on error, 0 on no
data, and else the length of the data read. With a negative value
or 0 for timeout this function blocks for the read.
or 0 for timeout this function blocks for the read.
*/
int NETAvailable(mkChannel *self, int timeout);
/*
returns 1 if data is pending on the port, 0 if none is
int NETAvailable(mkChannel *self, long timeout);
/* returns 1 if data is pending on the port, 0 if none is
pending.
*/
int NETReadTillTermNew(mkChannel *self, long timeout,
char *pTerm, char *pBuffer, int iBufLen);
int NETReadTillTerm(mkChannel *self, int timeout,
char *pTerm, char *pBuffer, int iBufLen);
/*
@ -91,6 +94,8 @@
and a negative value if a network error occurred. Beware that
this may not work correctly if the wrong terminator is given.
The last one is really needed.
In the new version, timeout is in MILLIseconds (10 -3 sec).
However, the accuracy is machine dependent (for Linux 10 ms, for Tru64 1 ms)
*/
/* ********************* KILLING FIELD ******************************** */
int NETClosePort(mkChannel *self);

View File

@ -123,10 +123,10 @@ int writeRS232(prs232 self, void *data, int dataLen)
iRet = NETWrite(self->pSock,data,dataLen);
if(self->debug > 0)
{
printf("RS232 OUT: %s",(char *)data);
printf("RS232 OUT : %s",(char *)data);
if(strchr((char *)data,'\n') == NULL)
{
puts("\n");
puts("");
}
fflush(stdout);
}
@ -175,10 +175,10 @@ int readRS232(prs232 self, void *data, int *dataLen)
{
if(self->debug > 0)
{
printf("RS232 IN: %s",(char *)data);
printf("RS232 IN : %s",(char *)data);
if(strchr((char *)data,'\n') == NULL)
{
puts("\n");
puts("");
}
fflush(stdout);
}
@ -211,14 +211,14 @@ int readRS232TillTerm(prs232 self, void *data, int *datalen){
memset(data,0,*datalen);
replylen = *datalen;
iRet = NETReadTillTerm(self->pSock,self->timeout,self->replyTerminator,
iRet = NETReadTillTermNew(self->pSock,self->timeout,self->replyTerminator,
(char *)data, replylen);
if(self->debug > 0)
{
printf("RS232 IN/TERM: %s",(char *)data);
printf("RS232 IN/TERM : %s",(char *)data);
if(strchr((char *)data,'\n') == NULL)
{
puts("\n");
puts("");
}
fflush(stdout);
}
@ -231,6 +231,10 @@ int readRS232TillTerm(prs232 self, void *data, int *datalen){
printf("Incomplete read: %s\n", (char *)data);
return INCOMPLETE;
}
else if (iRet < 0)
{
return iRet;
}
*datalen = strlen((char *)data);
return 1;
}
@ -247,7 +251,7 @@ int availableRS232(prs232 self)
return NOTCONNECTED;
}
return NETAvailable(self->pSock,3);
return NETAvailable(self->pSock,0);
}
/*-----------------------------------------------------------------------*/
int availableNetRS232(prs232 self)
@ -312,14 +316,14 @@ int transactRS232(prs232 self, void *send, int sendLen,
read
*/
memset(reply,0,replyLen);
iRet = NETReadTillTerm(self->pSock,self->timeout,self->replyTerminator,
iRet = NETReadTillTermNew(self->pSock,self->timeout,self->replyTerminator,
reply, replyLen);
if(self->debug > 0)
{
printf("RS232 IN/TRANS: %s",(char *)reply);
if(strchr((char *)reply,'\n') == NULL)
{
puts("\n");
puts("");
}
fflush(stdout);
}
@ -389,6 +393,7 @@ int initRS232(prs232 self)
NetReadRemoveUserSocket(pServ->pReader,self->pSock->sockid);
}
NETClosePort(self->pSock);
free(self->pSock);
self->pSock = NULL;
}
self->pSock = NETConnect(self->pHost, self->iPort);
@ -422,6 +427,7 @@ prs232 createRS232(char *host, int iPort)
pNew->sendTerminator = strdup("\r");
pNew->replyTerminator = strdup("\n");
pNew->timeout = 1000;
pNew->debug = 0;
pNew->pDes = CreateDescriptor("RS232 Controller");
if(!pNew->pDes || !pNew->pHost ||
!pNew->replyTerminator || !pNew->sendTerminator)
@ -457,6 +463,7 @@ prs232 createRS232(char *host, int iPort)
NetReadRemoveUserSocket(pServ->pReader,self->pSock->sockid);
}
NETClosePort(self->pSock);
free(self->pSock);
}
if(self->pHost)
{
@ -761,26 +768,14 @@ int RS232Factory(SConnection *pCon, SicsInterp *pSics,
/*
create data structure and open port
*/
pNew = (prs232)malloc(sizeof(rs232));
pNew = createRS232(argv[2], atoi(argv[3]));
if(!pNew)
{
SCWrite(pCon,"ERROR: out of memory in RS232Factory",eError);
return 0;
}
memset(pNew, 0, sizeof(rs232));
pNew->pHost = strdup(argv[2]);
pNew->iPort = atoi(argv[3]);
pNew->sendTerminator = strdup("\r");
pNew->replyTerminator = strdup("\n");
pNew->timeout = 1000;
pNew->pDes = CreateDescriptor("RS232 Controller");
if(!pNew->pDes || !pNew->pHost ||
!pNew->replyTerminator || !pNew->sendTerminator)
{
SCWrite(pCon,"ERROR: out of memory in RS232Factory",eError);
return 0;
}
status = initRS232(pNew);
if(status != 1)
{

View File

@ -9,6 +9,10 @@
Use internal connection.
Mark Koennecke, July 2002
changed timeout units in NETRead from us to ms
the timing could be optimized (no need for SicsWait
when timeout properly choosen (could be in the region of 1 second )
M.Zolliker, August 04
*/
#include "sics.h"
#include "network.h"
@ -34,7 +38,7 @@ static void syncLogin(void)
connection = NETConnect(hostname, port);
for(i = 0; i < 10; i++)
{
NETRead(connection,pBueffel,1020,10*1000);
NETRead(connection,pBueffel,1020,10); /* 10 ms M.Z */
if(strstr(pBueffel,"OK") != NULL)
{
break;
@ -60,7 +64,7 @@ static void syncLogin(void)
for(i = 0; i < 60; i++)
{
memset(pRead,0,80);
test = NETRead(connection,pRead,70,10*1000);
test = NETRead(connection,pRead,70,10); /* 10 ms M.Z. */
if(test < 0)
{
NETClosePort(connection);
@ -212,7 +216,7 @@ int Synchronize(SConnection *pCon, SicsInterp *pSics, void *pData,
for(i = 0; i < 60; i++)
{
pRead[0] = '\0';
test = NETRead(connection,pRead,75,20*1000);
test = NETRead(connection,pRead,75,20); /* 20 ms M.Z. */
if(test < 0)
{
NETClosePort(connection);