17 #include "connectiontcpbase.h"
21 #include "mutexguard.h"
28 #if ( !defined( _WIN32 ) && !defined( _WIN32_WCE ) ) || defined( __SYMBIAN32__ )
29 # include <arpa/inet.h>
30 # include <sys/types.h>
31 # include <sys/socket.h>
32 # include <sys/select.h>
33 # include <netinet/in.h>
37 #elif ( defined( _WIN32 ) || defined( _WIN32_WCE ) ) && !defined( __SYMBIAN32__ )
39 typedef int socklen_t;
51 const std::string& server,
int port )
53 m_logInstance( logInstance ), m_buf( 0 ), m_socket( -1 ), m_totalBytesIn( 0 ),
54 m_totalBytesOut( 0 ), m_bufsize( 8192 ), m_cancel( true )
60 const std::string& server,
int port )
62 m_logInstance( logInstance ), m_buf( 0 ), m_socket( -1 ), m_totalBytesIn( 0 ),
63 m_totalBytesOut( 0 ), m_bufsize( 8192 ), m_cancel( true )
68 void ConnectionTCPBase::init(
const std::string& server,
int port )
73 m_buf = (
char*)calloc( m_bufsize + 1,
sizeof(
char ) );
89 bool ConnectionTCPBase::dataAvailable(
int timeout )
100 FD_SET( m_socket, &fds );
102 tv.tv_sec = timeout / 1000000;
103 tv.tv_usec = timeout % 1000000;
105 return ( ( select( m_socket + 1, &fds, 0, 0, timeout == -1 ? 0 : &tv ) > 0 )
106 && FD_ISSET( m_socket, &fds ) != 0 );
124 if( data.empty() || ( m_socket < 0 ) )
131 for(
size_t num = 0, len = data.length(); sent != -1 && num < len; num += sent )
133 sent =
static_cast<int>(
::send( m_socket, (data.c_str()+num), (
int)(len - num), 0 ) );
136 m_totalBytesOut += (int)data.length();
143 std::string message =
"send() failed. "
144 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
145 "WSAGetLastError: " + util::int2string( ::WSAGetLastError() );
147 "errno: " + util::int2string( errno ) +
": " + strerror( errno );
160 totalIn = m_totalBytesIn;
161 totalOut = m_totalBytesOut;
192 struct sockaddr local;
193 socklen_t len = (socklen_t)
sizeof( local );
194 if( getsockname ( m_socket, &local, &len ) < 0 )
197 return ntohs( ((
struct sockaddr_in *)&local)->sin_port );
202 struct sockaddr_in local;
203 socklen_t len = (socklen_t)
sizeof( local );
204 if( getsockname ( m_socket, (reinterpret_cast<struct sockaddr*>( &local )), &len ) < 0 )
210 return inet_ntoa( local.sin_addr );