detect when server closes connection. can only be done reliably with recv, not with send

This commit is contained in:
2019-08-29 17:07:20 +02:00
parent 12ce14b6e5
commit d602c1cdcb

View File

@@ -228,6 +228,23 @@ void epicsShareAPI logClientFlush ( logClientId id )
epicsMutexMustLock ( pClient->mutex );
while (1) {
struct timeval timeout = { 0, 0 };
fd_set set;
char buffer[256];
FD_ZERO ( &set );
FD_SET ( pClient->sock, &set );
if ( select ( pClient->sock + 1, &set, NULL, NULL, &timeout ) == 0)
break;
if ( recv ( pClient->sock, buffer, sizeof ( buffer ), 0 ) == 0 ) {
fprintf ( stderr, "log client: connection closed by server \"%s\"\n",
pClient->name );
logClientClose ( pClient );
break;
}
}
while ( nSent < pClient->nextMsgIndex && pClient->connected ) {
int status = send ( pClient->sock, pClient->msgBuf + nSent,
pClient->nextMsgIndex - nSent, 0 );
@@ -359,20 +376,6 @@ static void logClientConnect (logClient *pClient)
fprintf (stderr, "log client: unable to enable keepalive option because \"%s\"\n", sockErrBuf);
}
/*
* we don't need full-duplex (clients only write), so we shutdown
* the read end of our socket
*/
status = shutdown (pClient->sock, SHUT_RD);
if (status < 0) {
char sockErrBuf[64];
epicsSocketConvertErrnoToString (
sockErrBuf, sizeof ( sockErrBuf ) );
fprintf (stderr, "%s:%d shutdown(%d,SHUT_RD) error was \"%s\"\n",
__FILE__, __LINE__, pClient->sock, sockErrBuf);
/* not fatal (although it shouldn't happen) */
}
/*
* set how long we will wait for the TCP state machine
* to clean up when we issue a close(). This