2
Copyright (c) 2004-2009 by Jakob Schroeter <js@camaya.net>
3
This file is part of the gloox library. http://camaya.net/gloox
5
This software is distributed under a license. The full license
6
agreement can be found in the file LICENSE in this distribution.
7
This software may not be copied, modified, sold or distributed
8
other than expressed in the named license agreement.
10
This software is distributed without any warranty.
14
#ifndef CONNECTIONTCPBASE_H__
15
#define CONNECTIONTCPBASE_H__
18
#include "connectionbase.h"
33
* @brief This is a base class for a simple TCP connection.
35
* You should not need to use this class directly.
37
* @author Jakob Schroeter <js@camaya.net>
40
class GLOOX_API ConnectionTCPBase : public ConnectionBase
44
* Constructs a new ConnectionTCPBase object.
45
* @param logInstance The log target. Obtain it from ClientBase::logInstance().
46
* @param server A server to connect to.
47
* @param port The port to connect to. The default of -1 means that XMPP SRV records
48
* will be used to find out about the actual host:port.
49
* @note To properly use this object, you have to set a ConnectionDataHandler using
50
* registerConnectionDataHandler(). This is not necessary if this object is
51
* part of a 'connection chain', e.g. with ConnectionHTTPProxy.
53
ConnectionTCPBase( const LogSink& logInstance, const std::string& server, int port = -1 );
56
* Constructs a new ConnectionTCPBase object.
57
* @param cdh An ConnectionDataHandler-derived object that will handle incoming data.
58
* @param logInstance The log target. Obtain it from ClientBase::logInstance().
59
* @param server A server to connect to.
60
* @param port The port to connect to. The default of -1 means that SRV records will be used
61
* to find out about the actual host:port.
63
ConnectionTCPBase( ConnectionDataHandler* cdh, const LogSink& logInstance,
64
const std::string& server, int port = -1 );
69
virtual ~ConnectionTCPBase();
71
// reimplemented from ConnectionBase
72
virtual bool send( const std::string& data );
74
// reimplemented from ConnectionBase
75
virtual ConnectionError receive();
77
// reimplemented from ConnectionBase
78
virtual void disconnect();
80
// reimplemented from ConnectionBase
81
virtual void cleanup();
83
// reimplemented from ConnectionBase
84
virtual void getStatistics( long int &totalIn, long int &totalOut );
87
* Gives access to the raw socket of this connection. Use it wisely. You can
88
* select()/poll() it and use ConnectionTCPBase::recv( -1 ) to fetch the data.
89
* @return The socket of the active connection, or -1 if no connection is established.
91
int socket() const { return m_socket; }
94
* This function allows to set an existing socket with an established
95
* connection to use in this connection. You will still need to call connect() in order to
96
* negotiate the XMPP stream. You should not set a new socket after having called connect().
97
* @param socket The existing socket.
99
void setSocket( int socket ) { m_cancel = false; m_state = StateConnected; m_socket = socket; }
102
* Returns the local port.
103
* @return The local port.
105
virtual int localPort() const;
108
* Returns the locally bound IP address.
109
* @return The locally bound IP address.
111
virtual const std::string localInterface() const;
114
ConnectionTCPBase& operator=( const ConnectionTCPBase& );
115
void init( const std::string& server, int port );
116
bool dataAvailable( int timeout = -1 );
119
const LogSink& m_logInstance;
120
util::Mutex m_sendMutex;
121
util::Mutex m_recvMutex;
125
long int m_totalBytesIn;
126
long int m_totalBytesOut;
134
#endif // CONNECTIONTCPBASE_H__