19 #include "connectiontcpserver.h"
20 #include "connectiontcpclient.h"
21 #include "connectionhandler.h"
25 #include "mutexguard.h"
29 # include <winsock2.h>
30 # include <ws2tcpip.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>
39 # include <sys/socket.h>
41 # include <sys/select.h>
46 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
47 # include <winsock2.h>
48 # include <ws2tcpip.h>
49 #elif defined( _WIN32_WCE )
50 # include <winsock2.h>
57 # include <sys/types.h>
61 #ifndef INVALID_SOCKET
62 # define INVALID_SOCKET -1
69 const std::string& ip,
int port )
71 m_connectionHandler( ch )
85 int ConnectionTCPServer::getSocket(
int af,
int socktype,
int proto,
const LogSink& logInstance )
87 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
92 if( ( fd = ::
socket( af, socktype, proto ) ) == INVALID_SOCKET )
94 std::string message =
"getSocket( "
95 + util::int2string( af ) +
", "
96 + util::int2string( socktype ) +
", "
97 + util::int2string( proto )
99 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
100 "WSAGetLastError: " + util::int2string( ::WSAGetLastError() );
102 "errno: " + util::int2string( errno ) +
": " + strerror( errno );
106 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
107 if( WSACleanup() != 0 )
110 + util::int2string( ::WSAGetLastError() ) );
117 #ifdef HAVE_SETSOCKOPT
120 setsockopt( fd, SOL_SOCKET, SO_SNDTIMEO, (
char*)&timeout,
sizeof( timeout ) );
121 setsockopt( fd, SOL_SOCKET, SO_REUSEADDR, (
char*)&reuseaddr,
sizeof( reuseaddr ) );
142 #ifdef HAVE_SETSOCKOPT
144 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
145 int bufbytes =
sizeof( int );
147 socklen_t bufbytes =
sizeof( int );
149 if( ( getsockopt( m_socket, SOL_SOCKET, SO_RCVBUF, (
char*)&buf, &bufbytes ) != -1 ) &&
150 ( m_bufsize > buf ) )
151 setsockopt( m_socket, SOL_SOCKET, SO_RCVBUF, (
char*)&m_bufsize,
sizeof( m_bufsize ) );
153 if( ( getsockopt( m_socket, SOL_SOCKET, SO_SNDBUF, (
char*)&buf, &bufbytes ) != -1 ) &&
154 ( m_bufsize > buf ) )
155 setsockopt( m_socket, SOL_SOCKET, SO_SNDBUF, (
char*)&m_bufsize,
sizeof( m_bufsize ) );
160 struct addrinfo hints;
161 struct addrinfo *res;
163 memset( &hints, 0,
sizeof hints );
164 hints.ai_family = AF_UNSPEC;
165 hints.ai_socktype = SOCK_STREAM;
166 hints.ai_flags = AI_PASSIVE;
167 status = getaddrinfo(
m_server.empty() ? 0 :
m_server.c_str(), util::int2string(
m_port ).c_str(), &hints, &res );
171 std::string message =
"getaddrinfo() for " + (
m_server.empty() ? std::string(
"*" ) :
m_server )
172 +
" (" + util::int2string(
m_port ) +
") failed. "
173 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
174 "WSAGetLastError: " + util::int2string( ::WSAGetLastError() );
176 + strerror( err ) +
" (errno: " + util::int2string( err ) +
")";
183 m_socket =
::socket( res->ai_family, res->ai_socktype, res->ai_protocol );
185 if( bind( m_socket, res->ai_addr, res->ai_addrlen ) < 0 )
188 std::string message =
"bind() to " + (
m_server.empty() ? std::string(
"*" ) :
m_server )
189 +
" (" + util::int2string(
m_port ) +
") failed. "
190 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
191 "WSAGetLastError: " + util::int2string( ::WSAGetLastError() );
193 + strerror( err ) +
" (errno: " + util::int2string( err ) +
")";
201 if( listen( m_socket, 10 ) < 0 )
204 std::string message =
"listen() on " + (
m_server.empty() ? std::string(
"*" ) :
m_server )
205 +
" (" +
":" + util::int2string(
m_port ) +
") failed. "
206 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
207 "WSAGetLastError: " + util::int2string( ::WSAGetLastError() );
209 + strerror( err ) +
" (errno: " + util::int2string( err ) +
")";
225 if( m_cancel || m_socket < 0 || !m_connectionHandler )
231 if( !dataAvailable( timeout ) )
237 struct sockaddr_storage they;
238 int addr_size =
sizeof(
struct sockaddr_storage );
239 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
240 int newfd =
static_cast<int>( accept( static_cast<SOCKET>( m_socket ), (
struct sockaddr*)&they, &addr_size ) );
242 int newfd = accept( m_socket, (
struct sockaddr*)&they, (socklen_t*)&addr_size );
247 char buffer[INET6_ADDRSTRLEN];
248 char portstr[NI_MAXSERV];
249 int err = getnameinfo( (
struct sockaddr*)&they, addr_size, buffer,
sizeof( buffer ),
250 portstr,
sizeof( portstr ), NI_NUMERICHOST | NI_NUMERICSERV );
An abstract base class for a connection.
A simple implementation of a mutex guard.
static void closeSocket(int fd, const LogSink &logInstance)
virtual ConnectionError recv(int timeout=-1)
virtual void handleIncomingConnection(ConnectionBase *server, ConnectionBase *connection)=0
This is an implementation of a simple TCP connection.
This is a base class for a simple TCP connection.
virtual ConnectionError connect()
This is an abstract base class to receive incoming connection attempts. Do not confuse this with Conn...
The namespace for the gloox library.
virtual ~ConnectionTCPServer()
virtual ConnectionBase * newInstance() const
void dbg(LogArea area, const std::string &message) const
static int getSocket(const LogSink &logInstance)
ConnectionTCPServer(ConnectionHandler *ch, const LogSink &logInstance, const std::string &ip, int port)
void setSocket(int socket)
An implementation of log sink and source.