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 )
99 #ifdef HAVE_SETSOCKOPT 101 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ ) 102 int bufbytes =
sizeof( int );
104 socklen_t bufbytes =
sizeof( int );
106 if( ( getsockopt( m_socket, SOL_SOCKET, SO_RCVBUF, reinterpret_cast<char*>( &buf ), &bufbytes ) != -1 ) && ( m_bufsize > buf ) )
107 setsockopt( m_socket, SOL_SOCKET, SO_RCVBUF, reinterpret_cast<const char*>( &m_bufsize ),
sizeof( m_bufsize ) );
109 if( ( getsockopt( m_socket, SOL_SOCKET, SO_SNDBUF, reinterpret_cast<char*>( &buf ), &bufbytes ) != -1 ) && ( m_bufsize > buf ) )
110 setsockopt( m_socket, SOL_SOCKET, SO_SNDBUF, reinterpret_cast<const char*>( &m_bufsize ),
sizeof( m_bufsize ) );
115 struct addrinfo hints;
116 struct addrinfo *res;
118 memset( &hints, 0,
sizeof hints );
119 hints.ai_family = AF_UNSPEC;
120 hints.ai_socktype = SOCK_STREAM;
121 hints.ai_flags = AI_PASSIVE;
122 status = getaddrinfo(
m_server.empty() ? 0 :
m_server.c_str(), util::int2string(
m_port ).c_str(), &hints, &res );
126 std::string message =
"getaddrinfo() for " + (
m_server.empty() ? std::string(
"*" ) :
m_server )
127 +
" (" + util::int2string(
m_port ) +
") failed. " 128 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ ) 129 "WSAGetLastError: " + util::int2string( ::WSAGetLastError() );
131 + strerror( err ) +
" (errno: " + util::int2string( err ) +
")";
139 if( bind( m_socket, res->ai_addr, res->ai_addrlen ) < 0 )
142 std::string message =
"bind() to " + (
m_server.empty() ? std::string(
"*" ) :
m_server )
143 +
" (" + util::int2string(
m_port ) +
") failed. " 144 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ ) 145 "WSAGetLastError: " + util::int2string( ::WSAGetLastError() );
147 + strerror( err ) +
" (errno: " + util::int2string( err ) +
")";
155 if( listen( m_socket, 10 ) < 0 )
158 std::string message =
"listen() on " + (
m_server.empty() ? std::string(
"*" ) :
m_server )
159 +
" (" +
":" + util::int2string(
m_port ) +
") failed. " 160 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ ) 161 "WSAGetLastError: " + util::int2string( ::WSAGetLastError() );
163 + strerror( err ) +
" (errno: " + util::int2string( err ) +
")";
179 if( m_cancel || m_socket < 0 || !m_connectionHandler )
185 if( !dataAvailable( timeout ) )
191 struct sockaddr_storage they;
192 int addr_size =
sizeof(
struct sockaddr_storage );
193 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ ) 194 int newfd =
static_cast<int>( accept( static_cast<SOCKET>( m_socket ), reinterpret_cast<struct sockaddr*>( &they ), &addr_size ) );
196 int newfd = accept( m_socket, reinterpret_cast<struct sockaddr*>( &they ), reinterpret_cast<socklen_t*>( &addr_size ) );
201 char buffer[INET6_ADDRSTRLEN];
202 char portstr[NI_MAXSERV];
203 int err = getnameinfo( reinterpret_cast<struct sockaddr*>( &they ), addr_size, buffer,
sizeof( buffer ),
204 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)
void dbg(LogArea area, const std::string &message) const
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
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.