added TCP keep-alive, fixes #10

This commit is contained in:
darkeye 2007-02-25 16:09:33 +00:00
parent 2b2ae392e0
commit 8c4d10dc13
3 changed files with 20 additions and 6 deletions

View File

@ -1,5 +1,7 @@
DarkIce next release
o improvements on reconnecting:
added TCP connection keep-alive to TCP sockets
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

@ -192,6 +192,8 @@ TcpSocket :: operator= ( const TcpSocket & ss ) throw ( Exception )
bool
TcpSocket :: open ( void ) throw ( Exception )
{
int optval;
socklen_t optlen;
#ifdef HAVE_ADDRINFO
struct addrinfo hints
struct addrinfo * ptr;
@ -213,7 +215,7 @@ TcpSocket :: open ( void ) throw ( Exception )
snprintf(portstr, sizeof(portstr), "%d", port);
if (getaddrinfo(host , portstr, &hints, &ptr)) {
throw Exception( __FILE__, __LINE__, "getaddrinfo error", errno);
throw Exception( __FILE__, __LINE__, "getaddrinfo error", errno);
}
memcpy ( addr, ptr->ai_addr, ptr->ai_addrlen);
freeaddrinfo(ptr);
@ -222,15 +224,24 @@ TcpSocket :: open ( void ) throw ( Exception )
throw Exception( __FILE__, __LINE__, "gethostbyname error", errno);
}
if ( (sockfd = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1 ) {
throw Exception( __FILE__, __LINE__, "socket error", errno);
}
memset( &addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = *((long*) pHostEntry->h_addr_list[0]);
#endif
if ( (sockfd = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1 ) {
throw Exception( __FILE__, __LINE__, "socket error", errno);
}
// set TCP keep-alive
optval = 1;
optlen = sizeof(optval);
if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen) == -1) {
reportEvent(5, "can't set TCP socket keep-alive mode", errno);
}
// connect
if ( connect( sockfd, (struct sockaddr*)&addr, sizeof(addr)) == -1 ) {
::close( sockfd);
sockfd = 0;

View File

@ -38,6 +38,7 @@
#include "Source.h"
#include "Sink.h"
#include "Reporter.h"
/* ================================================================ constants */
@ -54,7 +55,7 @@
* @author $Author$
* @version $Revision$
*/
class TcpSocket : public Source, public Sink
class TcpSocket : public Source, public Sink, public virtual Reporter
{
private: