Async  0.18.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::Object
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  ~TcpConnection(void);
169 
177  void disconnect(void);
178 
185  int write(const void *buf, int count);
186 
193  const IpAddress& remoteHost(void) const { return remote_addr; }
194 
199  uint16_t remotePort(void) const { return remote_port; }
200 
206  bool isConnected(void) const { return sock != -1; }
207 
213  SigC::Signal2<void, TcpConnection *, DisconnectReason> disconnected;
214 
229  SigC::Signal3<int, TcpConnection *, void *, int> dataReceived;
230 
236  SigC::Signal1<void, bool> sendBufferFull;
237 
238 
239  protected:
246  void setSocket(int sock);
247 
254  void setRemoteAddr(const IpAddress& remote_addr);
255 
262  void setRemotePort(uint16_t remote_port);
263 
271  int socket(void) const { return sock; }
272 
273 
274  private:
275  IpAddress remote_addr;
276  uint16_t remote_port;
277  size_t recv_buf_len;
278  int sock;
279  FdWatch * rd_watch;
280  FdWatch * wr_watch;
281  char * recv_buf;
282  size_t recv_buf_cnt;
283 
284  void recvHandler(FdWatch *watch);
285  void writeHandler(FdWatch *watch);
286 
287 }; /* class TcpConnection */
288 
289 
290 } /* namespace */
291 
292 #endif /* ASYNC_TCP_CONNECTION_INCLUDED */
293 
294 
295 
296 /*
297  * This file has not been truncated
298  */
299