19#include "connectiontcpserver.h"
20#include "connectiontcpclient.h"
21#include "connectionhandler.h"
25#include "mutexguard.h"
33#if ( !defined( _WIN32 ) && !defined( _WIN32_WCE ) ) || defined( __SYMBIAN32__ )
34# include <netinet/in.h>
35# include <arpa/nameser.h>
38# include <arpa/inet.h>
40# include <sys/socket.h>
42# include <sys/select.h>
47#if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
50#elif defined( _WIN32_WCE )
58# include <sys/types.h>
63# define INVALID_SOCKET -1
70 const std::string& ip,
int port )
72 m_connectionHandler( ch )
100#ifdef HAVE_SETSOCKOPT
102#if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
103 int bufbytes =
sizeof( int );
105 socklen_t bufbytes =
sizeof( int );
107 if( ( getsockopt( m_socket, SOL_SOCKET, SO_RCVBUF,
reinterpret_cast<char*
>( &buf ), &bufbytes ) != -1 ) && ( m_bufsize > buf ) )
108 setsockopt( m_socket, SOL_SOCKET, SO_RCVBUF,
reinterpret_cast<const char*
>( &m_bufsize ),
sizeof( m_bufsize ) );
110 if( ( getsockopt( m_socket, SOL_SOCKET, SO_SNDBUF,
reinterpret_cast<char*
>( &buf ), &bufbytes ) != -1 ) && ( m_bufsize > buf ) )
111 setsockopt( m_socket, SOL_SOCKET, SO_SNDBUF,
reinterpret_cast<const char*
>( &m_bufsize ),
sizeof( m_bufsize ) );
116 struct addrinfo hints;
117 struct addrinfo *res;
119 memset( &hints, 0,
sizeof hints );
120 hints.ai_family = AF_UNSPEC;
121 hints.ai_socktype = SOCK_STREAM;
122 hints.ai_flags = AI_PASSIVE;
123 status = getaddrinfo(
m_server.empty() ? 0 :
m_server.c_str(), util::int2string(
m_port ).c_str(), &hints, &res );
127 std::string message =
"getaddrinfo() for " + (
m_server.empty() ? std::string(
"*" ) :
m_server )
128 +
" (" + util::int2string(
m_port ) +
") failed. "
129#if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
130 "WSAGetLastError: " + util::int2string( ::WSAGetLastError() );
132 + strerror( err ) +
" (errno: " + util::int2string( err ) +
")";
140 if( bind( m_socket, res->ai_addr, res->ai_addrlen ) < 0 )
143 std::string message =
"bind() to " + (
m_server.empty() ? std::string(
"*" ) :
m_server )
144 +
" (" + util::int2string(
m_port ) +
") failed. "
145#if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
146 "WSAGetLastError: " + util::int2string( ::WSAGetLastError() );
148 + strerror( err ) +
" (errno: " + util::int2string( err ) +
")";
156 if( listen( m_socket, 10 ) < 0 )
159 std::string message =
"listen() on " + (
m_server.empty() ? std::string(
"*" ) :
m_server )
160 +
" (" +
":" + util::int2string(
m_port ) +
") failed. "
161#if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
162 "WSAGetLastError: " + util::int2string( ::WSAGetLastError() );
164 + strerror( err ) +
" (errno: " + util::int2string( err ) +
")";
180 if( m_cancel || m_socket < 0 || !m_connectionHandler )
186 if( !dataAvailable( timeout ) )
192 struct sockaddr_storage they;
193 int addr_size =
sizeof(
struct sockaddr_storage );
194#if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
195 int newfd =
static_cast<int>( accept(
static_cast<SOCKET
>( m_socket ),
reinterpret_cast<struct sockaddr*
>( &they ), &addr_size ) );
197 int newfd = accept( m_socket,
reinterpret_cast<struct sockaddr*
>( &they ),
reinterpret_cast<socklen_t*
>( &addr_size ) );
202 char buffer[INET6_ADDRSTRLEN];
203 char portstr[NI_MAXSERV];
204 int err = getnameinfo(
reinterpret_cast<struct sockaddr*
>( &they ), addr_size, buffer,
sizeof( buffer ),
205 portstr,
sizeof( portstr ), NI_NUMERICHOST | NI_NUMERICSERV );
An abstract base class for a connection.
This is an abstract base class to receive incoming connection attempts. Do not confuse this with Conn...
virtual void handleIncomingConnection(ConnectionBase *server, ConnectionBase *connection)=0
This is a base class for a simple TCP connection.
void setSocket(int socket)
This is an implementation of a simple TCP connection.
ConnectionTCPServer(ConnectionHandler *ch, const LogSink &logInstance, const std::string &ip, int port)
virtual ConnectionError recv(int timeout=-1)
virtual ~ConnectionTCPServer()
virtual ConnectionBase * newInstance() const
virtual ConnectionError connect()
static int getSocket(const LogSink &logInstance)
static void closeSocket(int fd, const LogSink &logInstance)
An implementation of log sink and source.
void dbg(LogArea area, const std::string &message) const
A simple implementation of a mutex guard.
The namespace for the gloox library.
@ LogAreaClassConnectionTCPServer