getting in shape for 1.2

This commit is contained in:
rafael@riseup.net 2013-05-18 19:00:08 +00:00
parent 4055ebf9ef
commit 3fbfd0f03d
3 changed files with 22 additions and 73 deletions

View File

@ -235,8 +235,8 @@ class DarkIce : public virtual Referable, public virtual Reporter
* @return if shouting was successful. * @return if shouting was successful.
* @exception Exception * @exception Exception
*/ */
//bool bool
//shout ( unsigned int ) throw ( Exception ); shout ( unsigned int ) throw ( Exception );
protected: protected:

View File

@ -83,7 +83,6 @@
#ifdef HAVE_SIGNAL_H #ifdef HAVE_SIGNAL_H
#include <signal.h> #include <signal.h>
#include <netinet/tcp.h>
#else #else
#error need signal.h #error need signal.h
#endif #endif
@ -194,7 +193,6 @@ bool
TcpSocket :: open ( void ) throw ( Exception ) TcpSocket :: open ( void ) throw ( Exception )
{ {
int optval; int optval;
struct timeval optval2 = {5L, 0L};
socklen_t optlen; socklen_t optlen;
#ifdef HAVE_ADDRINFO #ifdef HAVE_ADDRINFO
struct addrinfo hints struct addrinfo hints
@ -223,13 +221,10 @@ TcpSocket :: open ( void ) throw ( Exception )
memcpy ( addr, ptr->ai_addr, ptr->ai_addrlen); memcpy ( addr, ptr->ai_addr, ptr->ai_addrlen);
freeaddrinfo(ptr); freeaddrinfo(ptr);
#else #else
reportEvent(9, "Gonna do gethostbyname()");
if ( !(pHostEntry = gethostbyname( host)) ) { if ( !(pHostEntry = gethostbyname( host)) ) {
sockfd = 0; sockfd = 0;
reportEvent(9, "Fail in gethostbyname()");
throw Exception( __FILE__, __LINE__, "gethostbyname error", errno); throw Exception( __FILE__, __LINE__, "gethostbyname error", errno);
} }
reportEvent(9, "done gethostbyname()");
memset( &addr, 0, sizeof(addr)); memset( &addr, 0, sizeof(addr));
addr.sin_family = AF_INET; addr.sin_family = AF_INET;
@ -246,39 +241,8 @@ TcpSocket :: open ( void ) throw ( Exception )
optval = 1; optval = 1;
optlen = sizeof(optval); optlen = sizeof(optval);
if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen) == -1) { if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen) == -1) {
reportEvent(5, "can't set TCP socket SO_KEEPALIVE mode", errno); reportEvent(5, "can't set TCP socket keep-alive mode", errno);
} }
// set keep alive to some short value, this is a streaming server
// a long value will not work and lead to delay in reconnection
optval=5;
if (setsockopt(sockfd, SOL_TCP, TCP_KEEPIDLE, &optval, optlen) == -1) {
reportEvent(5, "can't set TCP socket keep-alive TCP_KEEPIDLE value", errno);
}
optval=2;
if (setsockopt(sockfd, SOL_TCP, TCP_KEEPCNT, &optval, optlen) == -1) {
reportEvent(5, "can't set TCP socket keep-alive TCP_KEEPCNT value", errno);
}
optval=5;
if (setsockopt(sockfd, SOL_TCP, TCP_KEEPINTVL, &optval, optlen) == -1) {
reportEvent(5, "can't set TCP socket keep-alive TCP_KEEPCNT value", errno);
}
if (-1 == setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, (const char *) &optval2, sizeof (optval2))) {
reportEvent(5,"could not set socket option SO_SNDTIMEO");
}
if (-1 == setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (const char *) &optval2, sizeof (optval2))) {
reportEvent(5,"could not set socket option SO_RCVTIMEO");
}
#ifdef TCP_CORK
// send larger network segments, limit buffer upto 0.2 sec before actual sending
if (-1 == setsockopt(sockfd, IPPROTO_TCP, TCP_CORK, (const char *) &optval, sizeof (optval))) {
reportEvent(5,"could not set socket option TCP_CORK");
}
#endif
// connect // connect
if ( connect( sockfd, (struct sockaddr*)&addr, sizeof(addr)) == -1 ) { if ( connect( sockfd, (struct sockaddr*)&addr, sizeof(addr)) == -1 ) {
@ -365,7 +329,7 @@ TcpSocket :: read ( void * buf,
/*------------------------------------------------------------------------------ /*------------------------------------------------------------------------------
* Check if write() would block * Check wether read() would return anything
*----------------------------------------------------------------------------*/ *----------------------------------------------------------------------------*/
bool bool
TcpSocket :: canWrite ( unsigned int sec, TcpSocket :: canWrite ( unsigned int sec,
@ -414,42 +378,27 @@ TcpSocket :: write ( const void * buf,
if ( !isOpen() ) { if ( !isOpen() ) {
return 0; return 0;
} }
// let us try to write stuff to this socket
// we can not take forever to do it, so the open() call set up #ifdef HAVE_MSG_NOSIGNAL
// a send timeout, of 5 seconds ret = send( sockfd, buf, len, MSG_NOSIGNAL);
// we give it 2 retries and then give up, the stream has #else
// been blocked for 10+ seconds and we need to take action anyway ret = send( sockfd, buf, len, 0);
unsigned int bytesleft = len; #endif
int retries = 2;
errno = 0; if ( ret == -1 ) {
while (bytesleft && (retries)) { if ( errno == EAGAIN ) {
reportEvent(9,"before write\n", retries); ret = 0;
#ifdef HAVE_MSG_NOSIGNAL
ret = send( sockfd, buf, bytesleft, MSG_NOSIGNAL); // no SIGPIPE
#else
ret = send( sockfd, buf, bytesleft, 0);
#endif
if ((ret < 0) && ( errno == EAGAIN )) {
// problem happened, but try again
// try again
retries--;
} else { } else {
// some data was written ::close( sockfd);
bytesleft -= ret; // we
buf = (char*)buf + ret; // move pointer to unsent portion
}
reportEvent(9,"after write\n",ret);
}
if (bytesleft) {
// data not send after this time means serious problem
::close(sockfd);
sockfd = 0; sockfd = 0;
throw Exception( __FILE__, __LINE__, "send error", errno); throw Exception( __FILE__, __LINE__, "send error", errno);
} else {
return len; // all bytes sent
} }
}
return ret;
} }
/*------------------------------------------------------------------------------ /*------------------------------------------------------------------------------
* Close the socket * Close the socket
*----------------------------------------------------------------------------*/ *----------------------------------------------------------------------------*/