From b8419ec17dad02a5f096233739875d3f6e483c25 Mon Sep 17 00:00:00 2001 From: Dirk Zimoch Date: Wed, 4 Sep 2019 10:27:10 +0200 Subject: [PATCH] don't reconnect to log server unnecessarily --- src/libCom/logClient/logClient.c | 55 ++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/src/libCom/logClient/logClient.c b/src/libCom/logClient/logClient.c index 0056a21fe..692ac3105 100644 --- a/src/libCom/logClient/logClient.c +++ b/src/libCom/logClient/logClient.c @@ -25,7 +25,6 @@ #include "dbDefs.h" #include "epicsEvent.h" #include "iocLog.h" -#include "errlog.h" #include "epicsMutex.h" #include "epicsThread.h" #include "epicsTime.h" @@ -162,6 +161,31 @@ static void logClientDestroy (logClientId id) free ( pClient ); } +/* + * logClientCheckConnection + */ +static void logClientCheckConnection( logClient * pClient ) +{ + epicsMutexMustLock ( pClient->mutex ); + while ( pClient->connected ) { + 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; + } + } + epicsMutexUnlock ( pClient->mutex ); +} + /* * This method requires the pClient->mutex be owned already. */ @@ -176,6 +200,7 @@ static void sendMessageChunk(logClient * pClient, const char * message) { if ( msgBufBytesLeft < strSize && pClient->nextMsgIndex != 0u && pClient->connected) { /* buffer is full, thus flush it */ + logClientCheckConnection( pClient ); logClientFlush ( pClient ); msgBufBytesLeft = sizeof ( pClient->msgBuf ) - pClient->nextMsgIndex; } @@ -228,23 +253,6 @@ 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 ); @@ -415,14 +423,19 @@ static void logClientRestart ( logClientId id ) /* SMP safe state inspection */ epicsMutexMustLock ( pClient->mutex ); while ( ! pClient->shutdown ) { - unsigned isConn; + unsigned isConn, dataToSend; + + logClientCheckConnection( pClient ); isConn = pClient->connected; + dataToSend = pClient->nextMsgIndex; epicsMutexUnlock ( pClient->mutex ); - if ( ! isConn ) logClientConnect ( pClient ); - logClientFlush ( pClient ); + if ( dataToSend ) { + if ( ! isConn ) logClientConnect ( pClient ); + logClientFlush ( pClient ); + } epicsEventWaitWithTimeout ( pClient->shutdownNotify, LOG_RESTART_DELAY);