gloox  1.0.20
connectiontcpbase.h
1 /*
2  Copyright (c) 2004-2017 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 
44  class GLOOX_API ConnectionTCPBase : public ConnectionBase
45  {
46  public:
57  ConnectionTCPBase( const LogSink& logInstance, const std::string& server, int port = -1 );
58 
67  ConnectionTCPBase( ConnectionDataHandler* cdh, const LogSink& logInstance,
68  const std::string& server, int port = -1 );
69 
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 
95  int socket() const { return m_socket; }
96 
103  void setSocket( int socket ) { m_cancel = false; m_state = StateConnected; m_socket = socket; }
104 
109  virtual int localPort() const;
110 
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__
An abstract base class for a connection.
A simple implementation of mutex as a wrapper around a pthread mutex or a win32 critical section...
Definition: mutex.h:33
ConnectionError
Definition: gloox.h:683
This is an abstract base class to receive events from a ConnectionBase-derived object.
This is a base class for a simple TCP connection.
The namespace for the gloox library.
Definition: adhoc.cpp:27
An implementation of log sink and source.
Definition: logsink.h:38