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 
Async::TcpConnection::DR_SYSTEM_ERROR
@ DR_SYSTEM_ERROR
A system error occured (check errno)
Definition: AsyncTcpConnection.h:177
Async::TcpConnection::onDisconnected
virtual void onDisconnected(DisconnectReason reason)
Called when a connection has been terminated.
Definition: AsyncTcpConnection.h:333
Async::TcpConnection::setRemoteAddr
void setRemoteAddr(const IpAddress &remote_addr)
Setup information about the connection.
Async::TcpConnection::DR_REMOTE_DISCONNECTED
@ DR_REMOTE_DISCONNECTED
The remote host disconnected.
Definition: AsyncTcpConnection.h:176
Async::TcpConnection::dataReceived
sigc::signal< int, TcpConnection *, void *, int > dataReceived
A signal that is emitted when data has been received on the connection.
Definition: AsyncTcpConnection.h:282
Async::TcpConnection::disconnect
virtual void disconnect(void)
Disconnect from the remote host.
Async::TcpConnection::DR_ORDERED_DISCONNECT
@ DR_ORDERED_DISCONNECT
Disconnect ordered locally.
Definition: AsyncTcpConnection.h:179
Async::TcpConnection::disconnected
sigc::signal< void, TcpConnection *, DisconnectReason > disconnected
A signal that is emitted when a connection has been terminated.
Definition: AsyncTcpConnection.h:266
Async::TcpConnection::isIdle
bool isIdle(void) const
Check if the connection is idle.
Definition: AsyncTcpConnection.h:259
Async::TcpConnection::DEFAULT_RECV_BUF_LEN
static const int DEFAULT_RECV_BUF_LEN
The default length of the reception buffer.
Definition: AsyncTcpConnection.h:174
AsyncIpAddress.h
Platform independent representation of an IP address.
Async::TcpConnection::~TcpConnection
virtual ~TcpConnection(void)
Destructor.
Async::TcpConnection::disconnectReasonStr
static const char * disconnectReasonStr(DisconnectReason reason)
Translate disconnect reason to a string.
Async::TcpClientBase
A base class for creating a TCP client connection.
Definition: AsyncTcpClientBase.h:141
Async::TcpConnection::isConnected
bool isConnected(void) const
Check if the connection is established or not.
Definition: AsyncTcpConnection.h:250
Async::TcpConnection::DR_HOST_NOT_FOUND
@ DR_HOST_NOT_FOUND
The specified host was not found in the DNS.
Definition: AsyncTcpConnection.h:175
Async
Namespace for the asynchronous programming classes.
Definition: AsyncApplication.h:75
Async::TcpConnection::write
virtual int write(const void *buf, int count)
Write data to the TCP connection.
Async::TcpConnection::TcpConnection
TcpConnection(size_t recv_buf_len=DEFAULT_RECV_BUF_LEN)
Constructor.
Async::TcpConnection::DR_RECV_BUFFER_OVERFLOW
@ DR_RECV_BUFFER_OVERFLOW
Receiver buffer overflow.
Definition: AsyncTcpConnection.h:178
Async::TcpConnection::remoteHost
const IpAddress & remoteHost(void) const
Return the IP-address of the remote host.
Definition: AsyncTcpConnection.h:237
Async::TcpConnection::setSocket
void setSocket(int sock)
Setup information about the connection.
Async::TcpConnection::sendBufferFull
sigc::signal< void, bool > sendBufferFull
A signal that is emitted when the send buffer status changes.
Definition: AsyncTcpConnection.h:289
Async::IpAddress
A class for representing an IP address in an OS independent way.
Definition: AsyncIpAddress.h:124
Async::TcpConnection::setRecvBufLen
void setRecvBufLen(size_t recv_buf_len)
Set a new receive buffer size.
Async::TcpConnection::DisconnectReason
DisconnectReason
Reason code for disconnects.
Definition: AsyncTcpConnection.h:161
Async::TcpConnection::DR_PROTOCOL_ERROR
@ DR_PROTOCOL_ERROR
Protocol error.
Definition: AsyncTcpConnection.h:180
Async::TcpConnection::remotePort
uint16_t remotePort(void) const
Return the remote port used.
Definition: AsyncTcpConnection.h:243
Async::TcpConnection::socket
int socket(void) const
Return the socket file descriptor.
Definition: AsyncTcpConnection.h:324
Async::FdWatch
A class for watching file descriptors.
Definition: AsyncFdWatch.h:139
Async::TcpConnection::onDataReceived
virtual int onDataReceived(void *buf, int count)
Called when data has been received on the connection.
Definition: AsyncTcpConnection.h:352
Async::TcpConnection::setRemotePort
void setRemotePort(uint16_t remote_port)
Setup information about the connection.