📄 ulibip.c
字号:
/*--------------------------------------------------------------------*/
/* Determine if our internal buffer has the data */
/*--------------------------------------------------------------------*/
if (bufsize >= wanted)
{
memmove( output, save, wanted );
bufsize -= wanted;
if ( bufsize ) /* Any data left over? */
memmove( save, &save[wanted], bufsize ); /* Yes --> Save it*/
return wanted + bufsize;
} /* if */
if (connectionDied || connectedSock == INVALID_SOCKET)
{ // Haven't accepted a connection yet?
return 0;
}
/*--------------------------------------------------------------------*/
/* Determine when to stop processing */
/*--------------------------------------------------------------------*/
if ( timeout == 0 )
{
stop_time = 0;
now = 1; /* Any number greater than stop time */
}
else {
time( & now );
stop_time = now + timeout;
}
do {
int received;
int needed = wanted - bufsize;
/*--------------------------------------------------------------------*/
/* Initialize fd_set structure for select() call */
/*--------------------------------------------------------------------*/
FD_ZERO(&readfds);
FD_SET(connectedSock, &readfds);
/*--------------------------------------------------------------------*/
/* Handle an aborted program */
/*--------------------------------------------------------------------*/
if ( terminate_processing )
{
static boolean recurse = FALSE;
if ( ! recurse )
{
printmsg(2,"sread: User aborted processing");
recurse = TRUE;
}
return 0;
}
/*--------------------------------------------------------------------*/
/* Special case for network sockets: block for at least 5 */
/* msec if we have to read at least one character (this */
/* needs to be tuned) */
/*--------------------------------------------------------------------*/
if (stop_time > now )
{
tm.tv_sec = stop_time - now;
tm.tv_usec = 0;
}
else {
tm.tv_usec = 5000;
tm.tv_sec = 0;
}
/*--------------------------------------------------------------------*/
/* Read the data from the socket */
/*--------------------------------------------------------------------*/
nReady = select(1, &readfds, NULL, NULL, &tm);
if (nReady == SOCKET_ERROR)
{
int err = WSAGetLastError();
printmsg(0, "tsread: error in select()");
printWSerror("select", err);
if (IsFatalSocketError(err))
{
shutdown(connectedSock, 2); // Fail both reads and writes
connectionDied = TRUE;
}
bufsize = 0;
return 0;
}
else if (nReady == 0)
{
printmsg(5, "tsread: timeout after %d seconds",timeout);
bufsize = 0;
return 0;
}
else {
received = recv(connectedSock, &save[bufsize], needed, 0);
if (received == SOCKET_ERROR)
{
int wsErr = WSAGetLastError();
printmsg(0, "tsread: recv() failed");
printWSerror("recv", wsErr);
bufsize = 0;
return 0;
}
} /* else */
#ifdef UDEBUG
printmsg(15,"sread: Want %d characters, received %d, total %d in buffer",
(int) wanted, (int) received, (int) bufsize + received);
#endif
/*--------------------------------------------------------------------*/
/* Log the newly received data */
/*--------------------------------------------------------------------*/
traceData( &save[bufsize], received, FALSE );
/*--------------------------------------------------------------------*/
/* If we got the data, return it to the caller */
/*--------------------------------------------------------------------*/
bufsize += received;
if ( bufsize == wanted )
{
memmove( output, save, bufsize);
bufsize = 0;
if (debuglevel > 14)
fwrite(output,1,bufsize,stdout);
return wanted;
} /* if */
/*--------------------------------------------------------------------*/
/* Update the clock for the next pass */
/*--------------------------------------------------------------------*/
if (stop_time > 0)
time( &now );
} while (stop_time > now);
/*--------------------------------------------------------------------*/
/* We don't have enough data; report what we do have */
/*--------------------------------------------------------------------*/
return bufsize;
} /* tsread */
/*--------------------------------------------------------------------*/
/* t s w r i t e */
/* */
/* Write to the open socket */
/* Note: this is non-blocking, so we've got to use select() to */
/* gradually write out the entire buffer */
/*--------------------------------------------------------------------*/
int tswrite(char *data, unsigned int len)
{
int status;
/* Has connection died? */
if (connectionDied || connectedSock == INVALID_SOCKET)
return 0;
status = send(connectedSock, data, len, 0);
if (status == SOCKET_ERROR)
{
int err;
err = WSAGetLastError();
printmsg(0, "tswrite: Error sending data to socket");
printWSerror("send", err);
if (IsFatalSocketError(err))
{
shutdown(connectedSock, 2); // Fail both reads and writes
connectionDied = TRUE;
}
return 0;
}
if (status < len)
{
printmsg(0,"tswrite: Write to network failed.");
return status;
}
/*--------------------------------------------------------------------*/
/* Log the data written */
/*--------------------------------------------------------------------*/
traceData( data, len, TRUE );
/*--------------------------------------------------------------------*/
/* Return byte count transmitted to caller */
/*--------------------------------------------------------------------*/
return len;
} /* tswrite */
/*--------------------------------------------------------------------*/
/* t s s e n d b r k */
/* */
/* Send a break signal over the network */
/*--------------------------------------------------------------------*/
#ifdef __TURBOC__
#pragma argsused
#endif
void tssendbrk(unsigned int duration)
{
printmsg(12, "tsendbrk: ignored break of duration %d", duration);
return;
} /* tssendbrk */
/*--------------------------------------------------------------------*/
/* t c l o s e l i n e */
/* */
/* Close the serial port down */
/*--------------------------------------------------------------------*/
void tcloseline(void)
{
if (!portActive)
panic();
portActive = FALSE; /* flag port closed for error handler */
if (connectedSock != INVALID_SOCKET)
{
closesocket(connectedSock);
connectedSock = INVALID_SOCKET;
}
if (pollingSock != INVALID_SOCKET)
{
closesocket(pollingSock);
pollingSock = INVALID_SOCKET;
}
carrierDetect = FALSE; /* No network connection yet */
traceStop();
} /* tcloseline */
/*--------------------------------------------------------------------*/
/* t h a n g u p */
/* */
/* Break the connection with the remote system. */
/*--------------------------------------------------------------------*/
void thangup( void )
{
if (!hangupNeeded)
return;
hangupNeeded = FALSE;
if (connectedSock != INVALID_SOCKET)
{
closesocket(connectedSock);
connectedSock = INVALID_SOCKET;
}
if (pollingSock != INVALID_SOCKET)
{
closesocket(pollingSock);
pollingSock = INVALID_SOCKET;
}
carrierDetect = FALSE; /* No network connection yet */
} /* thangup */
/*--------------------------------------------------------------------*/
/* t S I O S p e e d */
/* */
/* Re-specify the speed of an opened serial port */
/*--------------------------------------------------------------------*/
#ifdef __TURBOC__
#pragma argsused
#endif
void tSIOSpeed(BPS bps)
{
return; /* Irrelevant on network */
} /* iSIOSpeed */
/*--------------------------------------------------------------------*/
/* t f l o w c o n t r o l */
/* */
/* Enable/Disable in band (XON/XOFF) flow control */
/*--------------------------------------------------------------------*/
#ifdef __TURBOC__
#pragma argsused
#endif
void tflowcontrol( boolean flow )
{
return; /* Irrelevant on network (we hope) */
} /* tflowcontrol */
/*--------------------------------------------------------------------*/
/* t G e t S p e e d */
/* */
/* Report current speed of communications connection */
/*--------------------------------------------------------------------*/
BPS tGetSpeed( void )
{
return 57600; // Arbitary large number to avoid possible
// divide by zero error in caller
} /* GetSpeed */
/*--------------------------------------------------------------------*/
/* t C D */
/* */
/* Report if we have carrier detect and lost it */
/*--------------------------------------------------------------------*/
boolean tCD( void )
{
boolean online = carrierDetect;
return online;
} /* tCD */
/*--------------------------------------------------------------------*/
/* t W a i t F o r N e t C o n n e c t */
/* */
/* Wait for remote system to connect to our waiting server */
/*--------------------------------------------------------------------*/
boolean tWaitForNetConnect(int timeout)
{
fd_set readfds;
int nReady;
struct timeval tm;
tm.tv_sec = timeout;
tm.tv_usec = 0;
FD_ZERO(&readfds);
FD_SET(pollingSock, &readfds);
nReady = select(1, &readfds, NULL, NULL, &tm);
if (nReady == SOCKET_ERROR)
{
int wsErr = WSAGetLastError();
printmsg(0, "WaitForNetConnect: select() failed");
printWSerror("select", wsErr);
return FALSE;
}
else if (nReady == 0)
{
printmsg(5, "WaitForNetConnect: select() timed out");
return FALSE;
}
connectedSock = accept(pollingSock, NULL, NULL);
if (connectedSock == INVALID_SOCKET)
{
int wsErr = WSAGetLastError();
printmsg(0, "WaitForNetConnect: could not accept a connection");
printWSerror("accept", wsErr);
}
carrierDetect = TRUE;
return TRUE;
} /* tWaitForNetConnect */
boolean IsFatalSocketError(int err)
{
if (err == WSAENOTSOCK ||
err == WSAENETDOWN ||
err == WSAENETRESET ||
err == WSAECONNABORTED ||
err == WSAECONNRESET ||
err == WSAENOTCONN ||
err == WSAECONNREFUSED ||
err == WSAEHOSTDOWN ||
err == WSAEHOSTUNREACH)
return TRUE;
else
return FALSE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -