added sleeping between reconnection attempts, re #12

This commit is contained in:
darkeye 2007-02-25 16:38:33 +00:00
parent 8c4d10dc13
commit f44ac6d410
5 changed files with 55 additions and 2 deletions

View File

@ -2,6 +2,7 @@ DarkIce next release
o improvements on reconnecting:
added TCP connection keep-alive to TCP sockets
added graceful sleep when trying to reconnect
o added user-defined date formatting for the fileAddDate options,
thanks to dsk <derrick@csociety.org>
o added logging facility - [file-X] targets will cut the saved file

View File

@ -42,6 +42,7 @@
#include "Exception.h"
#include "MultiThreadedConnector.h"
#include "Util.h"
/* =================================================== local data structures */
@ -326,7 +327,7 @@ MultiThreadedConnector :: sinkThread( int ixSink )
// if we're not accepting, try to reopen the sink
try {
sink->close();
sched_yield();
Util::sleep(1L, 0L);
sink->open();
sched_yield();
threadData->accepting = sink->isOpen();

View File

@ -308,6 +308,7 @@ TcpSocket :: read ( void * buf,
case ECONNRESET:
// re-open the socket if it has been reset by the peer
close();
Util::sleep(1L, 0L);
open();
break;

View File

@ -63,6 +63,24 @@
#error need time.h
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#else
#error need unistd.h
#endif
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#else
#error need sys/time.h
#endif
#ifdef HAVE_SIGNAL_H
#include <signal.h>
#else
#error need signal.h
#endif
#include "Util.h"
@ -490,3 +508,22 @@ Util :: conv16 ( unsigned char * pcmBuffer,
}
/*------------------------------------------------------------------------------
* Make a thread sleep for a specified amount of time.
*----------------------------------------------------------------------------*/
void
Util :: sleep ( long sec,
long nsec)
{
struct timespec timespec;
sigset_t sigset;
timespec.tv_sec = sec;
timespec.tv_nsec = nsec;
// mask out SIGUSR1, as we're expecting that signal for other reasons
sigemptyset(&sigset);
sigaddset(&sigset, SIGUSR1);
pselect( 0, NULL, NULL, NULL, &timespec, &sigset);
}

View File

@ -216,7 +216,8 @@ class Util
*/
static char *
fileAddDate ( const char * str,
const char * format = "[%m-%d-%Y-%H-%M-%S]" ) throw ( Exception );
const char * format = "[%m-%d-%Y-%H-%M-%S]" )
throw ( Exception );
/**
* Convert a string into base64 encoding.
@ -305,6 +306,18 @@ class Util
unsigned int channels,
bool isBigEndian ) throw ( Exception );
/**
* Make a thread sleep for specified amount of time.
* Only the thread which this is called in will sleep.
* The SIGUSR1 signal will be blocked during the sleep.
*
* @param sec the number of seconds to sleep.
* @param nsec the number of nano-seconds to sleep.
*/
static void
sleep( long sec,
long nsec);
};