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 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, o added user-defined date formatting for the fileAddDate options,
thanks to dsk <derrick@csociety.org> thanks to dsk <derrick@csociety.org>
o added logging facility - [file-X] targets will cut the saved file 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 bool
TcpSocket :: open ( void ) throw ( Exception ) TcpSocket :: open ( void ) throw ( Exception )
{ {
int optval;
socklen_t optlen;
#ifdef HAVE_ADDRINFO #ifdef HAVE_ADDRINFO
struct addrinfo hints struct addrinfo hints
struct addrinfo * ptr; struct addrinfo * ptr;
@ -213,7 +215,7 @@ TcpSocket :: open ( void ) throw ( Exception )
snprintf(portstr, sizeof(portstr), "%d", port); snprintf(portstr, sizeof(portstr), "%d", port);
if (getaddrinfo(host , portstr, &hints, &ptr)) { 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); memcpy ( addr, ptr->ai_addr, ptr->ai_addrlen);
freeaddrinfo(ptr); freeaddrinfo(ptr);
@ -222,15 +224,24 @@ TcpSocket :: open ( void ) throw ( Exception )
throw Exception( __FILE__, __LINE__, "gethostbyname error", errno); 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)); memset( &addr, 0, sizeof(addr));
addr.sin_family = AF_INET; addr.sin_family = AF_INET;
addr.sin_port = htons(port); addr.sin_port = htons(port);
addr.sin_addr.s_addr = *((long*) pHostEntry->h_addr_list[0]); addr.sin_addr.s_addr = *((long*) pHostEntry->h_addr_list[0]);
#endif #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 ) { if ( connect( sockfd, (struct sockaddr*)&addr, sizeof(addr)) == -1 ) {
::close( sockfd); ::close( sockfd);
sockfd = 0; sockfd = 0;

View File

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