From d602c1cdcb623497e7dff09c817ba1e5fbed411b Mon Sep 17 00:00:00 2001 From: Dirk Zimoch Date: Thu, 29 Aug 2019 17:07:20 +0200 Subject: [PATCH] detect when server closes connection. can only be done reliably with recv, not with send --- modules/libcom/src/log/logClient.c | 31 ++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/modules/libcom/src/log/logClient.c b/modules/libcom/src/log/logClient.c index 72a2e1364..0056a21fe 100644 --- a/modules/libcom/src/log/logClient.c +++ b/modules/libcom/src/log/logClient.c @@ -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