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 ) );
159 struct addrinfo hints;
160 struct addrinfo *res;
162 memset( &hints, 0,
sizeof hints );
163 hints.ai_family = AF_UNSPEC;
164 hints.ai_socktype = SOCK_STREAM;
165 hints.ai_flags = AI_PASSIVE;
166 status = getaddrinfo(
m_server.c_str(), util::int2string(
m_port ).c_str(), &hints, &res );
169 std::string message =
"getaddrinfo() for " + (
m_server.empty() ? std::string(
"*" ) :
m_server )
170 +
" (" + util::int2string(
m_port ) +
") failed. "
171 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
172 "WSAGetLastError: " + util::int2string( ::WSAGetLastError() );
174 "errno: " + util::int2string( errno );
179 m_socket =
::socket( res->ai_family, res->ai_socktype, res->ai_protocol );
181 if( bind( m_socket, res->ai_addr, res->ai_addrlen ) < 0 )
183 std::string message =
"bind() to " + (
m_server.empty() ? std::string(
"*" ) :
m_server )
184 +
" (" + util::int2string(
m_port ) +
") failed. "
185 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
186 "WSAGetLastError: " + util::int2string( ::WSAGetLastError() );
188 "errno: " + util::int2string( errno );
195 if( listen( m_socket, 10 ) < 0 )
197 std::string message =
"listen on " + (
m_server.empty() ? std::string(
"*" ) :
m_server )
198 +
" (" +
":" + util::int2string(
m_port ) +
") failed. "
199 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
200 "WSAGetLastError: " + util::int2string( ::WSAGetLastError() );
202 "errno: " + util::int2string( errno );
217 if( m_cancel || m_socket < 0 || !m_connectionHandler )
223 if( !dataAvailable( timeout ) )
229 struct sockaddr_storage they;
230 int addr_size =
sizeof(
struct sockaddr_storage );
231 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
232 int newfd =
static_cast<int>( accept( static_cast<SOCKET>( m_socket ), (
struct sockaddr*)&they, &addr_size ) );
234 int newfd = accept( m_socket, (
struct sockaddr*)&they, (socklen_t*)&addr_size );
239 char buffer[INET6_ADDRSTRLEN];
240 char portstr[NI_MAXSERV];
241 int err = getnameinfo( (
struct sockaddr*)&they, addr_size, buffer,
sizeof( buffer ),
242 portstr,
sizeof( portstr ), NI_NUMERICHOST | NI_NUMERICSERV );
An abstract base class for a connection.
A simple implementation of a mutex guard.
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.