connectiontcpclient.cpp
/*
Copyright (c) 2004-2023 by Jakob Schröter <js@camaya.net>
This file is part of the gloox library. http://camaya.net/gloox
This software is distributed under a license. The full license
agreement can be found in the file LICENSE in this distribution.
This software may not be copied, modified, sold or distributed
other than expressed in the named license agreement.
This software is distributed without any warranty.
*/
#include "config.h"
#include "gloox.h"
#include "connectiontcpclient.h"
#include "dns.h"
#include "logsink.h"
#include "mutexguard.h"
#include "util.h"
#ifdef __MINGW32__
# include <winsock.h>
#endif
#if ( !defined( _WIN32 ) && !defined( _WIN32_WCE ) ) || defined( __SYMBIAN32__ )
# include <sys/types.h>
# include <sys/socket.h>
# include <sys/select.h>
# include <unistd.h>
# include <string.h>
# include <errno.h>
#elif ( defined( _WIN32 ) || defined( _WIN32_WCE ) ) && !defined( __SYMBIAN32__ )
# include <winsock.h>
#endif
#include <cstdlib>
#include <string>
namespace gloox
{
ConnectionTCPClient::ConnectionTCPClient( const LogSink& logInstance,
const std::string& server, int port )
: ConnectionTCPBase( logInstance, server, port )
{
}
ConnectionTCPClient::ConnectionTCPClient( ConnectionDataHandler* cdh, const LogSink& logInstance,
const std::string& server, int port )
: ConnectionTCPBase( cdh, logInstance, server, port )
{
}
ConnectionTCPClient::~ConnectionTCPClient()
{
}
ConnectionBase* ConnectionTCPClient::newInstance() const
{
return new ConnectionTCPClient( m_handler, m_logInstance, m_server, m_port );
}
ConnectionError ConnectionTCPClient::connect()
{
m_sendMutex.lock();
// FIXME CHECKME
if( !m_handler )
{
m_sendMutex.unlock();
return ConnNotConnected;
}
if( m_socket >= 0 && m_state > StateDisconnected )
{
m_sendMutex.unlock();
return ConnNoError;
}
m_state = StateConnecting;
if( m_socket < 0 )
{
if( m_port == -1 )
m_socket = DNS::connect( m_server, m_logInstance );
else
m_socket = DNS::connect( m_server, m_port, m_logInstance );
}
m_sendMutex.unlock();
if( m_socket < 0 )
{
switch( m_socket )
{
case -ConnConnectionRefused:
m_logInstance.err( LogAreaClassConnectionTCPClient,
m_server + ": connection refused" );
break;
case -ConnDnsError:
m_logInstance.err( LogAreaClassConnectionTCPClient,
m_server + ": host not found" );
break;
default:
m_logInstance.err( LogAreaClassConnectionTCPClient,
"Unknown error condition" );
break;
}
m_handler->handleDisconnect( this, static_cast<ConnectionError>( -m_socket ) );
return static_cast<ConnectionError>( -m_socket );
}
else
{
m_state = StateConnected;
}
m_cancel = false;
m_handler->handleConnect( this );
return ConnNoError;
}
ConnectionError ConnectionTCPClient::recv( int timeout )
{
m_recvMutex.lock();
if( m_cancel || m_socket < 0 )
{
m_recvMutex.unlock();
return ConnNotConnected;
}
if( !dataAvailable( timeout ) )
{
m_recvMutex.unlock();
return ConnNoError;
}
#if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
int size = static_cast<int>( ::recv( m_socket, m_buf, m_bufsize, 0 ) );
#else
int size = static_cast<int>( ::recv( m_socket, m_buf, m_bufsize, MSG_DONTWAIT ) );
#endif
if( size > 0 )
m_totalBytesIn += size;
m_recvMutex.unlock();
if( size <= 0 )
{
if( size == -1 )
{
#if defined(__unix__)
if( errno == EAGAIN || errno == EWOULDBLOCK )
return ConnNoError;
#endif
// recv() failed for an unexpected reason
std::string message = "recv() failed. "
#if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
"WSAGetLastError: " + util::int2string( ::WSAGetLastError() );
#else
"errno: " + util::int2string( errno ) + ": " + strerror( errno );
#endif
m_logInstance.err( LogAreaClassConnectionTCPClient, message );
}
ConnectionError error = ( size ? ConnIoError : ConnStreamClosed );
if( m_handler )
m_handler->handleDisconnect( this, error );
return error;
}
m_buf[size] = '\0';
if( m_handler )
m_handler->handleReceivedData( this, std::string( m_buf, size ) );
return ConnNoError;
}
}
gloox::ConnectionBase
An abstract base class for a connection.
Definition connectionbase.h:35
gloox::ConnectionBase::m_state
ConnectionState m_state
Definition connectionbase.h:155
gloox::ConnectionBase::port
int port() const
Definition connectionbase.h:121
gloox::ConnectionBase::server
const std::string & server() const
Definition connectionbase.h:115
gloox::ConnectionBase::m_port
int m_port
Definition connectionbase.h:161
gloox::ConnectionBase::m_handler
ConnectionDataHandler * m_handler
Definition connectionbase.h:152
gloox::ConnectionBase::m_server
std::string m_server
Definition connectionbase.h:158
gloox::ConnectionDataHandler
This is an abstract base class to receive events from a ConnectionBase-derived object.
Definition connectiondatahandler.h:36
gloox::ConnectionTCPBase::ConnectionTCPBase
ConnectionTCPBase(const LogSink &logInstance, const std::string &server, int port=-1)
Definition connectiontcpbase.cpp:53
gloox::ConnectionTCPClient::recv
virtual ConnectionError recv(int timeout=-1)
Definition connectiontcpclient.cpp:125
gloox::ConnectionTCPClient::newInstance
virtual ConnectionBase * newInstance() const
Definition connectiontcpclient.cpp:62
gloox::ConnectionTCPClient::connect
virtual ConnectionError connect()
Definition connectiontcpclient.cpp:67
gloox::ConnectionTCPClient::ConnectionTCPClient
ConnectionTCPClient(const LogSink &logInstance, const std::string &server, int port=-1)
Definition connectiontcpclient.cpp:45
gloox::ConnectionTCPClient::~ConnectionTCPClient
virtual ~ConnectionTCPClient()
Definition connectiontcpclient.cpp:58
gloox::DNS::connect
static int connect(const std::string &host, const LogSink &logInstance)
Definition dns.cpp:247
gloox::LogSink
An implementation of log sink and source.
Definition logsink.h:39
gloox
The namespace for the gloox library.
Definition adhoc.cpp:28
gloox::ConnectionError
ConnectionError
Definition gloox.h:684
gloox::ConnNotConnected
@ ConnNotConnected
Definition gloox.h:715
gloox::ConnConnectionRefused
@ ConnConnectionRefused
Definition gloox.h:698
gloox::ConnDnsError
@ ConnDnsError
Definition gloox.h:700
gloox::ConnNoError
@ ConnNoError
Definition gloox.h:685
gloox::ConnStreamClosed
@ ConnStreamClosed
Definition gloox.h:689
gloox::ConnIoError
@ ConnIoError
Definition gloox.h:696
gloox::LogAreaClassConnectionTCPClient
@ LogAreaClassConnectionTCPClient
Definition gloox.h:1062
gloox::StateDisconnected
@ StateDisconnected
Definition gloox.h:642
gloox::StateConnected
@ StateConnected
Definition gloox.h:644
gloox::StateConnecting
@ StateConnecting
Definition gloox.h:643