don't reconnect to log server unnecessarily

This commit is contained in:
2019-09-04 10:27:10 +02:00
parent 172f170230
commit b8419ec17d
+34 -21
View File
@@ -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);