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 
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 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.
SigC::Signal2< void, TcpConnection *, DisconnectReason > disconnected
A signal that is emitted when a connection has been terminated.
int write(const void *buf, int count)
Write data to the TCP connection.
SigC::Signal3< int, TcpConnection *, void *, int > dataReceived
A signal that is emitted when data has been received on the 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.
DisconnectReason
Reason code for disconnects.
SigC::Signal1< void, bool > sendBufferFull
A signal that is emitted when the send buffer status changes.
~TcpConnection(void)
Destructor.
Platform independent representation of an IP address.
A class for representing an IP address in an OS independent way.