Async 1.7.0
AsyncTcpConnection.h
Go to the documentation of this file.
1
29
30
31
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#include <AsyncFdWatch.h>
56
57
58/****************************************************************************
59 *
60 * Local Includes
61 *
62 ****************************************************************************/
63
64
65
66/****************************************************************************
67 *
68 * Forward declarations
69 *
70 ****************************************************************************/
71
72
73
74/****************************************************************************
75 *
76 * Namespace
77 *
78 ****************************************************************************/
79
80namespace Async
81{
82
83/****************************************************************************
84 *
85 * Forward declarations of classes inside of the declared namespace
86 *
87 ****************************************************************************/
88
89class 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
123class TcpConnection : virtual public sigc::trackable
124{
125 public:
140
144 static const int DEFAULT_RECV_BUF_LEN = 1024;
145
149 static const char *disconnectReasonStr(DisconnectReason reason);
150
155 explicit TcpConnection(size_t recv_buf_len = DEFAULT_RECV_BUF_LEN);
156
164 TcpConnection(int sock, const IpAddress& remote_addr,
165 uint16_t remote_port,
166 size_t recv_buf_len = DEFAULT_RECV_BUF_LEN);
167
171 virtual ~TcpConnection(void);
172
183
193 void setRecvBufLen(size_t recv_buf_len);
194
202 virtual void disconnect(void) { closeConnection(); }
203
210 virtual int write(const void *buf, int count);
211
218 const IpAddress& remoteHost(void) const { return remote_addr; }
219
224 uint16_t remotePort(void) const { return remote_port; }
225
231 bool isConnected(void) const { return sock != -1; }
232
239 bool isIdle(void) const { return sock == -1; }
240
246 sigc::signal<void, TcpConnection *, DisconnectReason> disconnected;
247
262 sigc::signal<int, TcpConnection *, void *, int> dataReceived;
263
269 sigc::signal<void, bool> sendBufferFull;
270
271
272 protected:
279 void setSocket(int sock);
280
287 void setRemoteAddr(const IpAddress& remote_addr);
288
295 void setRemotePort(uint16_t remote_port);
296
304 int socket(void) const { return sock; }
305
312 virtual void closeConnection(void);
313
321 virtual void onDisconnected(DisconnectReason reason)
322 {
323 emitDisconnected(reason);
324 }
325
340 virtual int onDataReceived(void *buf, int count)
341 {
342 return dataReceived(this, buf, count);
343 }
344
350 {
351 disconnected(this, reason);
352 }
353
354 private:
355 friend class TcpClientBase;
356
357 IpAddress remote_addr;
358 uint16_t remote_port;
359 size_t recv_buf_len;
360 int sock;
361 FdWatch rd_watch;
362 FdWatch wr_watch;
363 char * recv_buf;
364 size_t recv_buf_cnt;
365
366 void recvHandler(FdWatch *watch);
367 void writeHandler(FdWatch *watch);
368
369}; /* class TcpConnection */
370
371
372} /* namespace */
373
374#endif /* ASYNC_TCP_CONNECTION_INCLUDED */
375
376
377
378/*
379 * This file has not been truncated
380 */
381
Contains a watch for file descriptors.
Platform independent representation of an IP address.
A class for watching file descriptors.
A class for representing an IP address in an OS independent way.
static const char * disconnectReasonStr(DisconnectReason reason)
Translate disconnect reason to a string.
virtual void disconnect(void)
Disconnect from the remote host.
bool isIdle(void) const
Check if the connection is idle.
const IpAddress & remoteHost(void) const
Return the IP-address of the remote host.
uint16_t remotePort(void) const
Return the remote port used.
static const int DEFAULT_RECV_BUF_LEN
The default length of the reception buffer.
DisconnectReason
Reason code for disconnects.
@ DR_HOST_NOT_FOUND
The specified host was not found in the DNS.
@ DR_SYSTEM_ERROR
A system error occured (check errno)
@ DR_BAD_STATE
The connection ended up in a bad state.
@ DR_PROTOCOL_ERROR
Protocol error.
@ DR_REMOTE_DISCONNECTED
The remote host disconnected.
@ DR_RECV_BUFFER_OVERFLOW
Receiver buffer overflow.
@ DR_ORDERED_DISCONNECT
Disconnect ordered locally.
@ DR_SWITCH_PEER
A better peer was found so reconnecting.
virtual ~TcpConnection(void)
Destructor.
virtual void onDisconnected(DisconnectReason reason)
Called when a connection has been terminated.
void setSocket(int sock)
Setup information about the connection.
virtual TcpConnection & operator=(TcpConnection &&other)
Move assignmnt operator.
int socket(void) const
Return the socket file descriptor.
virtual int onDataReceived(void *buf, int count)
Called when data has been received on the connection.
sigc::signal< int, TcpConnection *, void *, int > dataReceived
A signal that is emitted when data has been received on the connection.
void setRecvBufLen(size_t recv_buf_len)
Set a new receive buffer size.
TcpConnection(int sock, const IpAddress &remote_addr, uint16_t remote_port, size_t recv_buf_len=DEFAULT_RECV_BUF_LEN)
Constructor.
sigc::signal< void, bool > sendBufferFull
A signal that is emitted when the send buffer status changes.
void setRemotePort(uint16_t remote_port)
Setup information about the connection.
virtual void emitDisconnected(DisconnectReason reason)
Emit the disconnected signal.
virtual int write(const void *buf, int count)
Write data to the TCP connection.
bool isConnected(void) const
Check if the connection is established or not.
TcpConnection(size_t recv_buf_len=DEFAULT_RECV_BUF_LEN)
Constructor.
sigc::signal< void, TcpConnection *, DisconnectReason > disconnected
A signal that is emitted when a connection has been terminated.
void setRemoteAddr(const IpAddress &remote_addr)
Setup information about the connection.
virtual void closeConnection(void)
Disconnect from the remote peer.
Namespace for the asynchronous programming classes.