17 #include "connectiontcpbase.h"
21 #include "mutexguard.h"
27 #if !defined( _WIN32 ) && !defined( _WIN32_WCE )
28 # include <sys/types.h>
29 # include <sys/socket.h>
30 # include <sys/select.h>
49 const std::string& server,
int port )
51 m_logInstance( logInstance ), m_buf( 0 ), m_socket( -1 ), m_totalBytesIn( 0 ),
52 m_totalBytesOut( 0 ), m_bufsize( 1024 ), m_cancel( true )
58 const std::string& server,
int port )
60 m_logInstance( logInstance ), m_buf( 0 ), m_socket( -1 ), m_totalBytesIn( 0 ),
61 m_totalBytesOut( 0 ), m_bufsize( 1024 ), m_cancel( true )
66 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 )
95 FD_SET( m_socket, &fds );
97 tv.tv_sec = timeout / 1000000;
98 tv.tv_usec = timeout % 1000000;
100 return ( ( select( m_socket + 1, &fds, 0, 0, timeout == -1 ? 0 : &tv ) > 0 )
101 && FD_ISSET( m_socket, &fds ) != 0 );
119 if( data.empty() || ( m_socket < 0 ) )
126 for(
size_t num = 0, len = data.length(); sent != -1 && num < len; num += sent )
129 sent =
::send( m_socket, (
unsigned char*)(data.c_str()+num), len - num, 0 );
131 sent =
::send( m_socket, (data.c_str()+num), len - num, 0 );
135 m_totalBytesOut += data.length();
139 if( sent == -1 && m_handler )
147 totalIn = m_totalBytesIn;
148 totalOut = m_totalBytesOut;