17 #include "connectiontcpbase.h" 21 #include "mutexguard.h" 25 # include <winsock2.h> 26 # include <ws2tcpip.h> 29 #if ( !defined( _WIN32 ) && !defined( _WIN32_WCE ) ) || defined( __SYMBIAN32__ ) 30 # include <arpa/inet.h> 31 # include <sys/types.h> 32 # include <sys/socket.h> 33 # include <sys/select.h> 34 # include <netinet/in.h> 39 #elif ( defined( _WIN32 ) || defined( _WIN32_WCE ) ) && !defined( __SYMBIAN32__ ) 40 # include <winsock2.h> 41 # include <ws2tcpip.h> 42 typedef int socklen_t;
54 const std::string& server,
int port )
56 m_logInstance( logInstance ), m_buf( 0 ), m_socket( -1 ), m_totalBytesIn( 0 ),
57 m_totalBytesOut( 0 ), m_bufsize( 8192 ), m_cancel( true )
65 m_logInstance( logInstance ), m_buf( 0 ), m_socket( -1 ), m_totalBytesIn( 0 ),
66 m_totalBytesOut( 0 ), m_bufsize( 8192 ), m_cancel( true )
71 void ConnectionTCPBase::init(
const std::string&
server,
int port )
76 m_buf =
static_cast<char*
>( calloc( m_bufsize + 1,
sizeof(
char ) ) );
92 bool ConnectionTCPBase::dataAvailable(
int timeout )
103 FD_SET( m_socket, &fds );
105 tv.tv_sec = timeout / 1000000;
106 tv.tv_usec = timeout % 1000000;
108 return ( ( select( m_socket + 1, &fds, 0, 0, timeout == -1 ? 0 : &tv ) > 0 )
109 && FD_ISSET( m_socket, &fds ) != 0 );
127 if( data.empty() || ( m_socket < 0 ) )
134 for(
size_t num = 0, len = data.length(); sent != -1 && num < len; num += sent )
136 sent =
static_cast<int>(
::send( m_socket, (data.c_str()+num), static_cast<int>( len - num ), 0 ) );
139 m_totalBytesOut += data.length();
146 std::string message =
"send() failed. " 147 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ ) 148 "WSAGetLastError: " + util::int2string( ::WSAGetLastError() );
150 "errno: " + util::int2string( errno ) +
": " + strerror( errno );
163 totalIn = m_totalBytesIn;
164 totalOut = m_totalBytesOut;
195 struct sockaddr local;
196 socklen_t len =
static_cast<socklen_t
>(
sizeof( local ) );
197 if( getsockname ( m_socket, &local, &len ) < 0 )
200 return ntohs( (reinterpret_cast<struct sockaddr_in*>( &local ) )->sin_port );
205 struct sockaddr_storage local;
206 socklen_t len =
static_cast<socklen_t
>(
sizeof( local ) );
207 if( getsockname( m_socket, reinterpret_cast<struct sockaddr*>( &local ), &len ) < 0 )
211 char buffer[INET6_ADDRSTRLEN];
212 int err = getnameinfo( reinterpret_cast<struct sockaddr*>( &local ), len, buffer,
sizeof( buffer ),
213 0, 0, NI_NUMERICHOST );
An abstract base class for a connection.
virtual ConnectionError receive()
A simple implementation of a mutex guard.
static void closeSocket(int fd, const LogSink &logInstance)
virtual int localPort() const
ConnectionTCPBase(const LogSink &logInstance, const std::string &server, int port=-1)
This is an abstract base class to receive events from a ConnectionBase-derived object.
virtual void handleDisconnect(const ConnectionBase *connection, ConnectionError reason)=0
The namespace for the gloox library.
virtual bool send(const std::string &data)
virtual void disconnect()
virtual ConnectionError recv(int timeout=-1)=0
bool idna(const std::string &domain, std::string &out)
ConnectionDataHandler * m_handler
virtual const std::string localInterface() const
const std::string & server() const
virtual ~ConnectionTCPBase()
An implementation of log sink and source.
void err(LogArea area, const std::string &message) const
const std::string EmptyString
virtual void getStatistics(long int &totalIn, long int &totalOut)