Async  1.5.0
AsyncTcpConnection.h
Go to the documentation of this file.
1 
32 #ifndef ASYNC_TCP_CONNECTION_INCLUDED
33 #define ASYNC_TCP_CONNECTION_INCLUDED
34 
35 
36 /****************************************************************************
37  *
38  * System Includes
39  *
40  ****************************************************************************/
41 
42 #include <sigc++/sigc++.h>
43 #include <stdint.h>
44 
45 #include <string>
46 
47 
48 /****************************************************************************
49  *
50  * Project Includes
51  *
52  ****************************************************************************/
53 
54 #include <AsyncIpAddress.h>
55 
56 
57 /****************************************************************************
58  *
59  * Local Includes
60  *
61  ****************************************************************************/
62 
63 
64 
65 /****************************************************************************
66  *
67  * Forward declarations
68  *
69  ****************************************************************************/
70 
71 
72 
73 /****************************************************************************
74  *
75  * Namespace
76  *
77  ****************************************************************************/
78 
79 namespace Async
80 {
81 
82 /****************************************************************************
83  *
84  * Forward declarations of classes inside of the declared namespace
85  *
86  ****************************************************************************/
87 
88 class FdWatch;
89 class IpAddress;
90 
91 
92 /****************************************************************************
93  *
94  * Defines & typedefs
95  *
96  ****************************************************************************/
97 
98 
99 
100 /****************************************************************************
101  *
102  * Exported Global Variables
103  *
104  ****************************************************************************/
105 
106 
107 
108 /****************************************************************************
109  *
110  * Class definitions
111  *
112  ****************************************************************************/
113 
123 class TcpConnection : public sigc::trackable
124 {
125  public:
129  typedef enum
130  {
138 
142  static const int DEFAULT_RECV_BUF_LEN = 1024;
143 
147  static const char *disconnectReasonStr(DisconnectReason reason);
148 
153  explicit TcpConnection(size_t recv_buf_len = DEFAULT_RECV_BUF_LEN);
154 
162  TcpConnection(int sock, const IpAddress& remote_addr,
163  uint16_t remote_port,
164  size_t recv_buf_len = DEFAULT_RECV_BUF_LEN);
165 
169  virtual ~TcpConnection(void);
170 
180  void setRecvBufLen(size_t recv_buf_len);
181 
189  virtual void disconnect(void);
190 
197  virtual int write(const void *buf, int count);
198 
205  const IpAddress& remoteHost(void) const { return remote_addr; }
206 
211  uint16_t remotePort(void) const { return remote_port; }
212 
218  bool isConnected(void) const { return sock != -1; }
219 
227  bool isIdle(void) const { return sock == -1; }
228 
234  sigc::signal<void, TcpConnection *, DisconnectReason> disconnected;
235 
250  sigc::signal<int, TcpConnection *, void *, int> dataReceived;
251 
257  sigc::signal<void, bool> sendBufferFull;
258 
259 
260  protected:
267  void setSocket(int sock);
268 
275  void setRemoteAddr(const IpAddress& remote_addr);
276 
283  void setRemotePort(uint16_t remote_port);
284 
292  int socket(void) const { return sock; }
293 
301  virtual void onDisconnected(DisconnectReason reason)
302  {
303  disconnected(this, reason);
304  }
305 
320  virtual int onDataReceived(void *buf, int count)
321  {
322  return dataReceived(this, buf, count);
323  }
324 
325  private:
326  friend class TcpClientBase;
327 
328  IpAddress remote_addr;
329  uint16_t remote_port;
330  size_t recv_buf_len;
331  int sock;
332  FdWatch * rd_watch;
333  FdWatch * wr_watch;
334  char * recv_buf;
335  size_t recv_buf_cnt;
336 
337  void recvHandler(FdWatch *watch);
338  void writeHandler(FdWatch *watch);
339 
340 }; /* class TcpConnection */
341 
342 
343 } /* namespace */
344 
345 #endif /* ASYNC_TCP_CONNECTION_INCLUDED */
346 
347 
348 
349 /*
350  * This file has not been truncated
351  */
352 
bool isConnected(void) const
Check if the connection is established or not.
A system error occured (check errno)
The specified host was not found in the DNS.
A class for handling exiting TCP connections.
void setRecvBufLen(size_t recv_buf_len)
Set a new receive buffer size.
virtual ~TcpConnection(void)
Destructor.
A base class for creating a TCP client connection.
sigc::signal< void, TcpConnection *, DisconnectReason > disconnected
A signal that is emitted when a connection has been terminated.
void setRemotePort(uint16_t remote_port)
Setup information about the connection.
bool isIdle(void) const
Check if the connection is idle.
virtual void disconnect(void)
Disconnect from the remote host.
static const char * disconnectReasonStr(DisconnectReason reason)
Translate disconnect reason to a string.
static const int DEFAULT_RECV_BUF_LEN
The default length of the reception buffer.
void setSocket(int sock)
Setup information about the connection.
virtual void onDisconnected(DisconnectReason reason)
Called when a connection has been terminated.
virtual int write(const void *buf, int count)
Write data to the TCP connection.
virtual int onDataReceived(void *buf, int count)
Called when data has been received on the connection.
const IpAddress & remoteHost(void) const
Return the IP-address of the remote host.
A class for watching file descriptors.
Definition: AsyncFdWatch.h:119
void setRemoteAddr(const IpAddress &remote_addr)
Setup information about the connection.
TcpConnection(size_t recv_buf_len=DEFAULT_RECV_BUF_LEN)
Constructor.
uint16_t remotePort(void) const
Return the remote port used.
sigc::signal< void, bool > sendBufferFull
A signal that is emitted when the send buffer status changes.
DisconnectReason
Reason code for disconnects.
int socket(void) const
Return the socket file descriptor.
Namespace for the asynchronous programming classes.
Platform independent representation of an IP address.
A class for representing an IP address in an OS independent way.
sigc::signal< int, TcpConnection *, void *, int > dataReceived
A signal that is emitted when data has been received on the connection.