1 /*
2   Copyright (c) 2004-2019 by Jakob Schröter <js@camaya.net>
3   This file is part of the gloox library. http://camaya.net/gloox
4 
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.
9 
10   This software is distributed without any warranty.
11 */
12 
13 
14 #ifndef CONNECTIONTCPBASE_H__
15 #define CONNECTIONTCPBASE_H__
16 
17 #include "gloox.h"
18 #include "connectionbase.h"
19 #include "logsink.h"
20 #include "mutex.h"
21 
22 #ifdef __MINGW32__
23 #include <ws2tcpip.h>
24 #endif
25 
26 #include <string>
27 
28 namespace gloox
29 {
30 
31   namespace util
32   {
33     class Mutex;
34   }
35 
36   /**
37    * @brief This is a base class for a simple TCP connection.
38    *
39    * You should not need to use this class directly.
40    *
41    * @author Jakob Schröter <js@camaya.net>
42    * @since 0.9
43    */
44   class GLOOX_API ConnectionTCPBase : public ConnectionBase
45   {
46     public:
47       /**
48        * Constructs a new ConnectionTCPBase object.
49        * @param logInstance The log target. Obtain it from ClientBase::logInstance().
50        * @param server A server to connect to.
51        * @param port The port to connect to. The default of -1 means that XMPP SRV records
52        * will be used to find out about the actual host:port.
53        * @note To properly use this object, you have to set a ConnectionDataHandler using
54        * registerConnectionDataHandler(). This is not necessary if this object is
55        * part of a 'connection chain', e.g. with ConnectionHTTPProxy.
56        */
57       ConnectionTCPBase( const LogSink& logInstance, const std::string& server, int port = -1 );
58 
59       /**
60        * Constructs a new ConnectionTCPBase object.
61        * @param cdh An ConnectionDataHandler-derived object that will handle incoming data.
62        * @param logInstance The log target. Obtain it from ClientBase::logInstance().
63        * @param server A server to connect to.
64        * @param port The port to connect to. The default of -1 means that SRV records will be used
65        * to find out about the actual host:port.
66        */
67       ConnectionTCPBase( ConnectionDataHandler* cdh, const LogSink& logInstance,
68                          const std::string& server, int port = -1 );
69 
70       /**
71        * Virtual destructor
72        */
73       virtual ~ConnectionTCPBase();
74 
75       // reimplemented from ConnectionBase
76       virtual bool send( const std::string& data );
77 
78       // reimplemented from ConnectionBase
79       virtual ConnectionError receive();
80 
81       // reimplemented from ConnectionBase
82       virtual void disconnect();
83 
84       // reimplemented from ConnectionBase
85       virtual void cleanup();
86 
87       // reimplemented from ConnectionBase
88       virtual void getStatistics( long int &totalIn, long int &totalOut );
89 
90       /**
91        * Gives access to the raw socket of this connection. Use it wisely. You can
92        * select()/poll() it and use ConnectionTCPBase::recv( -1 ) to fetch the data.
93        * @return The socket of the active connection, or -1 if no connection is established.
94        */
socket()95       int socket() const { return m_socket; }
96 
97       /**
98        * This function allows to set an existing socket with an established
99        * connection to use in this connection. You will still need to call connect() in order to
100        * negotiate the XMPP stream. You should not set a new socket after having called connect().
101        * @param socket The existing socket.
102        */
setSocket(int socket)103       void setSocket( int socket ) { m_cancel = false; m_state = StateConnected; m_socket = socket; }
104 
105       /**
106        * Returns the local port.
107        * @return The local port.
108        */
109       virtual int localPort() const;
110 
111       /**
112        * Returns the locally bound IP address.
113        * @return The locally bound IP address.
114        */
115       virtual const std::string localInterface() const;
116 
117     protected:
118       ConnectionTCPBase& operator=( const ConnectionTCPBase& );
119       void init( const std::string& server, int port );
120       bool dataAvailable( int timeout = -1 );
121       void cancel();
122 
123       const LogSink& m_logInstance;
124       util::Mutex m_sendMutex;
125       util::Mutex m_recvMutex;
126 
127       char* m_buf;
128       int m_socket;
129       long int m_totalBytesIn;
130       long int m_totalBytesOut;
131       const int m_bufsize;
132       bool m_cancel;
133 
134   };
135 
136 }
137 
138 #endif // CONNECTIONTCPBASE_H__
139