17 #include "connectiontcpbase.h"
21 #include "mutexguard.h"
27 #if ( !defined( _WIN32 ) && !defined( _WIN32_WCE ) ) || defined( __SYMBIAN32__ )
28 # include <arpa/inet.h>
29 # include <sys/types.h>
30 # include <sys/socket.h>
31 # include <sys/select.h>
32 # include <netinet/in.h>
34 #elif ( defined( _WIN32 ) || defined( _WIN32_WCE ) ) && !defined( __SYMBIAN32__ )
36 typedef int socklen_t;
48 const std::string& server,
int port )
50 m_logInstance( logInstance ), m_buf( 0 ), m_socket( -1 ), m_totalBytesIn( 0 ),
51 m_totalBytesOut( 0 ), m_bufsize( 1024 ), m_cancel( true )
57 const std::string& server,
int port )
59 m_logInstance( logInstance ), m_buf( 0 ), m_socket( -1 ), m_totalBytesIn( 0 ),
60 m_totalBytesOut( 0 ), m_bufsize( 1024 ), m_cancel( true )
65 void ConnectionTCPBase::init(
const std::string& server,
int port )
70 m_buf = (
char*)calloc( m_bufsize + 1,
sizeof(
char ) );
86 bool ConnectionTCPBase::dataAvailable(
int timeout )
97 FD_SET( m_socket, &fds );
99 tv.tv_sec = timeout / 1000000;
100 tv.tv_usec = timeout % 1000000;
102 return ( ( select( m_socket + 1, &fds, 0, 0, timeout == -1 ? 0 : &tv ) > 0 )
103 && FD_ISSET( m_socket, &fds ) != 0 );
121 if( data.empty() || ( m_socket < 0 ) )
128 for(
size_t num = 0, len = data.length(); sent != -1 && num < len; num += sent )
130 sent =
static_cast<int>(
::send( m_socket, (data.c_str()+num), (
int)(len - num), 0 ) );
133 m_totalBytesOut += (int)data.length();
145 totalIn = m_totalBytesIn;
146 totalOut = m_totalBytesOut;
177 struct sockaddr local;
178 socklen_t len = (socklen_t)
sizeof( local );
179 if( getsockname ( m_socket, &local, &len ) < 0 )
182 return ntohs( ((
struct sockaddr_in *)&local)->sin_port );
187 struct sockaddr_in local;
188 socklen_t len = (socklen_t)
sizeof( local );
189 if( getsockname ( m_socket, (reinterpret_cast<struct sockaddr*>( &local )), &len ) < 0 )
195 return inet_ntoa( local.sin_addr );