gloox  1.0.1
connectiontcpbase.h
1 /*
2  Copyright (c) 2004-2012 by Jakob Schroeter <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 #include <string>
23 
24 namespace gloox
25 {
26 
27  namespace util
28  {
29  class Mutex;
30  }
31 
40  class GLOOX_API ConnectionTCPBase : public ConnectionBase
41  {
42  public:
53  ConnectionTCPBase( const LogSink& logInstance, const std::string& server, int port = -1 );
54 
63  ConnectionTCPBase( ConnectionDataHandler* cdh, const LogSink& logInstance,
64  const std::string& server, int port = -1 );
65 
69  virtual ~ConnectionTCPBase();
70 
71  // reimplemented from ConnectionBase
72  virtual bool send( const std::string& data );
73 
74  // reimplemented from ConnectionBase
75  virtual ConnectionError receive();
76 
77  // reimplemented from ConnectionBase
78  virtual void disconnect();
79 
80  // reimplemented from ConnectionBase
81  virtual void cleanup();
82 
83  // reimplemented from ConnectionBase
84  virtual void getStatistics( long int &totalIn, long int &totalOut );
85 
91  int socket() const { return m_socket; }
92 
99  void setSocket( int socket ) { m_cancel = false; m_state = StateConnected; m_socket = socket; }
100 
105  virtual int localPort() const;
106 
111  virtual const std::string localInterface() const;
112 
113  protected:
114  ConnectionTCPBase& operator=( const ConnectionTCPBase& );
115  void init( const std::string& server, int port );
116  bool dataAvailable( int timeout = -1 );
117  void cancel();
118 
119  const LogSink& m_logInstance;
120  util::Mutex m_sendMutex;
121  util::Mutex m_recvMutex;
122 
123  char* m_buf;
124  int m_socket;
125  long int m_totalBytesIn;
126  long int m_totalBytesOut;
127  const int m_bufsize;
128  bool m_cancel;
129 
130  };
131 
132 }
133 
134 #endif // CONNECTIONTCPBASE_H__