19 #include "connectiontcpserver.h"
20 #include "connectiontcpclient.h"
21 #include "connectionhandler.h"
25 #include "mutexguard.h"
32 #if ( !defined( _WIN32 ) && !defined( _WIN32_WCE ) ) || defined( __SYMBIAN32__ )
33 # include <netinet/in.h>
34 # include <arpa/nameser.h>
37 # include <arpa/inet.h>
38 # include <sys/socket.h>
40 # include <sys/select.h>
45 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
47 #elif defined( _WIN32_WCE )
48 # include <winsock2.h>
55 # include <sys/types.h>
59 #ifndef INVALID_SOCKET
60 # define INVALID_SOCKET -1
67 const std::string& ip,
int port )
69 m_connectionHandler( ch )
83 int ConnectionTCPServer::getSocket(
int af,
int socktype,
int proto,
const LogSink& logInstance )
85 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
90 if( ( fd = ::
socket( af, socktype, proto ) ) == INVALID_SOCKET )
92 std::string message =
"getSocket( "
93 + util::int2string( af ) +
", "
94 + util::int2string( socktype ) +
", "
95 + util::int2string( proto )
97 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
98 "WSAGetLastError: " + util::int2string( ::WSAGetLastError() );
100 "errno: " + util::int2string( errno ) +
": " + strerror( errno );
104 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
105 if( WSACleanup() != 0 )
108 + util::int2string( ::WSAGetLastError() ) );
115 #ifdef HAVE_SETSOCKOPT
118 setsockopt( fd, SOL_SOCKET, SO_SNDTIMEO, (
char*)&timeout,
sizeof( timeout ) );
119 setsockopt( fd, SOL_SOCKET, SO_REUSEADDR, (
char*)&reuseaddr,
sizeof( reuseaddr ) );
140 #ifdef HAVE_SETSOCKOPT
142 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
143 int bufbytes =
sizeof( int );
145 socklen_t bufbytes =
sizeof( int );
147 if( ( getsockopt( m_socket, SOL_SOCKET, SO_RCVBUF, (
char*)&buf, &bufbytes ) != -1 ) &&
148 ( m_bufsize > buf ) )
149 setsockopt( m_socket, SOL_SOCKET, SO_RCVBUF, (
char*)&m_bufsize,
sizeof( m_bufsize ) );
151 if( ( getsockopt( m_socket, SOL_SOCKET, SO_SNDBUF, (
char*)&buf, &bufbytes ) != -1 ) &&
152 ( m_bufsize > buf ) )
153 setsockopt( m_socket, SOL_SOCKET, SO_SNDBUF, (
char*)&m_bufsize,
sizeof( m_bufsize ) );
157 struct addrinfo hints;
158 struct addrinfo *res;
160 memset( &hints, 0,
sizeof hints );
161 hints.ai_family = AF_UNSPEC;
162 hints.ai_socktype = SOCK_STREAM;
163 hints.ai_flags = AI_PASSIVE;
164 status = getaddrinfo(
m_server.c_str(), util::int2string(
m_port ).c_str(), &hints, &res );
167 std::string message =
"getaddrinfo() for " + (
m_server.empty() ? std::string(
"*" ) :
m_server )
168 +
" (" + util::int2string(
m_port ) +
") failed. "
169 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
170 "WSAGetLastError: " + util::int2string( ::WSAGetLastError() );
172 "errno: " + util::int2string( errno );
177 m_socket =
::socket( res->ai_family, res->ai_socktype, res->ai_protocol );
179 if( bind( m_socket, res->ai_addr, res->ai_addrlen ) < 0 )
181 std::string message =
"bind() to " + (
m_server.empty() ? std::string(
"*" ) :
m_server )
182 +
" (" + util::int2string(
m_port ) +
") failed. "
183 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
184 "WSAGetLastError: " + util::int2string( ::WSAGetLastError() );
186 "errno: " + util::int2string( errno );
193 if( listen( m_socket, 10 ) < 0 )
195 std::string message =
"listen on " + (
m_server.empty() ? std::string(
"*" ) :
m_server )
196 +
" (" +
":" + util::int2string(
m_port ) +
") failed. "
197 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
198 "WSAGetLastError: " + util::int2string( ::WSAGetLastError() );
200 "errno: " + util::int2string( errno );
215 if( m_cancel || m_socket < 0 || !m_connectionHandler )
221 if( !dataAvailable( timeout ) )
227 struct sockaddr_storage they;
228 int addr_size =
sizeof(
struct sockaddr_storage );
229 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
230 int newfd =
static_cast<int>( accept( static_cast<SOCKET>( m_socket ), (
struct sockaddr_storage*)&they, &addr_size ) );
232 int newfd = accept( m_socket, (
struct sockaddr*)&they, (socklen_t*)&addr_size );
237 char buffer[INET6_ADDRSTRLEN];
238 char portstr[NI_MAXSERV];
239 int err = getnameinfo( (
struct sockaddr*)&they, addr_size, buffer,
sizeof( buffer ),
240 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.