Async  1.4.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  {
137 
141  static const int DEFAULT_RECV_BUF_LEN = 1024;
142 
146  static const char *disconnectReasonStr(DisconnectReason reason);
147 
152  explicit TcpConnection(size_t recv_buf_len = DEFAULT_RECV_BUF_LEN);
153 
161  TcpConnection(int sock, const IpAddress& remote_addr,
162  uint16_t remote_port,
163  size_t recv_buf_len = DEFAULT_RECV_BUF_LEN);
164 
168  virtual ~TcpConnection(void);
169 
179  void setRecvBufLen(size_t recv_buf_len);
180 
188  void disconnect(void);
189 
196  int write(const void *buf, int count);
197 
204  const IpAddress& remoteHost(void) const { return remote_addr; }
205 
210  uint16_t remotePort(void) const { return remote_port; }
211 
217  bool isConnected(void) const { return sock != -1; }
218 
226  bool isIdle(void) const { return sock == -1; }
227 
233  sigc::signal<void, TcpConnection *, DisconnectReason> disconnected;
234 
249  sigc::signal<int, TcpConnection *, void *, int> dataReceived;
250 
256  sigc::signal<void, bool> sendBufferFull;
257 
258 
259  protected:
266  void setSocket(int sock);
267 
274  void setRemoteAddr(const IpAddress& remote_addr);
275 
282  void setRemotePort(uint16_t remote_port);
283 
291  int socket(void) const { return sock; }
292 
293 
294  private:
295  IpAddress remote_addr;
296  uint16_t remote_port;
297  size_t recv_buf_len;
298  int sock;
299  FdWatch * rd_watch;
300  FdWatch * wr_watch;
301  char * recv_buf;
302  size_t recv_buf_cnt;
303 
304  void recvHandler(FdWatch *watch);
305  void writeHandler(FdWatch *watch);
306 
307 }; /* class TcpConnection */
308 
309 
310 } /* namespace */
311 
312 #endif /* ASYNC_TCP_CONNECTION_INCLUDED */
313 
314 
315 
316 /*
317  * This file has not been truncated
318  */
319 
A system error occured (check errno)
int socket(void) const
Return the socket file descriptor.
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.
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 isConnected(void) const
Check if the connection is established or not.
uint16_t remotePort(void) const
Return the remote port used.
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.
int write(const void *buf, int count)
Write data to the TCP connection.
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.
const IpAddress & remoteHost(void) const
Return the IP-address of the remote host.
sigc::signal< void, bool > sendBufferFull
A signal that is emitted when the send buffer status changes.
DisconnectReason
Reason code for disconnects.
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.
bool isIdle(void) const
Check if the connection is idle.