From f44ac6d4102521c58f5e16942e337c03f56a8ac1 Mon Sep 17 00:00:00 2001 From: darkeye Date: Sun, 25 Feb 2007 16:38:33 +0000 Subject: [PATCH] added sleeping between reconnection attempts, re #12 --- darkice/trunk/ChangeLog | 1 + darkice/trunk/src/MultiThreadedConnector.cpp | 3 +- darkice/trunk/src/TcpSocket.cpp | 1 + darkice/trunk/src/Util.cpp | 37 ++++++++++++++++++++ darkice/trunk/src/Util.h | 15 +++++++- 5 files changed, 55 insertions(+), 2 deletions(-) diff --git a/darkice/trunk/ChangeLog b/darkice/trunk/ChangeLog index b01b29f..7f2d81c 100644 --- a/darkice/trunk/ChangeLog +++ b/darkice/trunk/ChangeLog @@ -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 o added logging facility - [file-X] targets will cut the saved file diff --git a/darkice/trunk/src/MultiThreadedConnector.cpp b/darkice/trunk/src/MultiThreadedConnector.cpp index 5c331fc..55c6a5e 100644 --- a/darkice/trunk/src/MultiThreadedConnector.cpp +++ b/darkice/trunk/src/MultiThreadedConnector.cpp @@ -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(); diff --git a/darkice/trunk/src/TcpSocket.cpp b/darkice/trunk/src/TcpSocket.cpp index b55ca57..745d08b 100644 --- a/darkice/trunk/src/TcpSocket.cpp +++ b/darkice/trunk/src/TcpSocket.cpp @@ -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; diff --git a/darkice/trunk/src/Util.cpp b/darkice/trunk/src/Util.cpp index b30eb55..ee3bfa9 100644 --- a/darkice/trunk/src/Util.cpp +++ b/darkice/trunk/src/Util.cpp @@ -63,6 +63,24 @@ #error need time.h #endif +#ifdef HAVE_UNISTD_H +#include +#else +#error need unistd.h +#endif + +#ifdef HAVE_SYS_TIME_H +#include +#else +#error need sys/time.h +#endif + +#ifdef HAVE_SIGNAL_H +#include +#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, ×pec, &sigset); +} diff --git a/darkice/trunk/src/Util.h b/darkice/trunk/src/Util.h index 08c8af4..9911cc4 100644 --- a/darkice/trunk/src/Util.h +++ b/darkice/trunk/src/Util.h @@ -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); + };