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>
38 #elif ( defined( _WIN32 ) || defined( _WIN32_WCE ) ) && !defined( __SYMBIAN32__ )
40 typedef int socklen_t;
52 const std::string& server,
int port )
54 m_logInstance( logInstance ), m_buf( 0 ), m_socket( -1 ), m_totalBytesIn( 0 ),
55 m_totalBytesOut( 0 ), m_bufsize( 8192 ), m_cancel( true )
61 const std::string& server,
int port )
63 m_logInstance( logInstance ), m_buf( 0 ), m_socket( -1 ), m_totalBytesIn( 0 ),
64 m_totalBytesOut( 0 ), m_bufsize( 8192 ), m_cancel( true )
69 void ConnectionTCPBase::init(
const std::string& server,
int port )
74 m_buf = (
char*)calloc( m_bufsize + 1,
sizeof(
char ) );
90 bool ConnectionTCPBase::dataAvailable(
int timeout )
101 FD_SET( m_socket, &fds );
103 tv.tv_sec = timeout / 1000000;
104 tv.tv_usec = timeout % 1000000;
106 return ( ( select( m_socket + 1, &fds, 0, 0, timeout == -1 ? 0 : &tv ) > 0 )
107 && FD_ISSET( m_socket, &fds ) != 0 );
125 if( data.empty() || ( m_socket < 0 ) )
132 for(
size_t num = 0, len = data.length(); sent != -1 && num < len; num += sent )
134 sent =
static_cast<int>(
::send( m_socket, (data.c_str()+num), (
int)(len - num), 0 ) );
137 m_totalBytesOut += (int)data.length();
144 std::string message =
"send() failed. "
145 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
146 "WSAGetLastError: " + util::int2string( ::WSAGetLastError() );
148 "errno: " + util::int2string( errno ) +
": " + strerror( errno );
161 totalIn = m_totalBytesIn;
162 totalOut = m_totalBytesOut;
193 struct sockaddr local;
194 socklen_t len = (socklen_t)
sizeof( local );
195 if( getsockname ( m_socket, &local, &len ) < 0 )
198 return ntohs( ((
struct sockaddr_in *)&local)->sin_port );
203 struct sockaddr_storage local;
204 socklen_t len = (socklen_t)
sizeof( local );
205 if( getsockname ( m_socket, (reinterpret_cast<struct sockaddr*>( &local )), &len ) < 0 )
209 char buffer[INET6_ADDRSTRLEN];
210 int err = getnameinfo( (
struct sockaddr*)&local, len, buffer,
sizeof( buffer ),
211 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)
void err(LogArea area, const std::string &message) 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 const std::string localInterface() const
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 ~ConnectionTCPBase()
virtual int localPort() const
An implementation of log sink and source.
const std::string EmptyString
virtual void getStatistics(long int &totalIn, long int &totalOut)