Async 1.5.0
AsyncFramedTcpConnection.h
Go to the documentation of this file.
1
36#ifndef ASYNC_FRAMED_TCP_CONNECTION_INCLUDED
37#define ASYNC_FRAMED_TCP_CONNECTION_INCLUDED
38
39
40/****************************************************************************
41 *
42 * System Includes
43 *
44 ****************************************************************************/
45
46#include <stdint.h>
47#include <vector>
48#include <deque>
49#include <cstring>
50
51
52/****************************************************************************
53 *
54 * Project Includes
55 *
56 ****************************************************************************/
57
58#include <AsyncTcpConnection.h>
59
60
61/****************************************************************************
62 *
63 * Local Includes
64 *
65 ****************************************************************************/
66
67
68
69/****************************************************************************
70 *
71 * Forward declarations
72 *
73 ****************************************************************************/
74
75
76
77/****************************************************************************
78 *
79 * Namespace
80 *
81 ****************************************************************************/
82
83namespace Async
84{
85
86
87/****************************************************************************
88 *
89 * Forward declarations of classes inside of the declared namespace
90 *
91 ****************************************************************************/
92
93
94
95/****************************************************************************
96 *
97 * Defines & typedefs
98 *
99 ****************************************************************************/
100
101
102
103/****************************************************************************
104 *
105 * Exported Global Variables
106 *
107 ****************************************************************************/
108
109
110
111/****************************************************************************
112 *
113 * Class definitions
114 *
115 ****************************************************************************/
116
132{
133 public:
138 explicit FramedTcpConnection(size_t recv_buf_len = DEFAULT_RECV_BUF_LEN);
139
147 FramedTcpConnection(int sock, const IpAddress& remote_addr,
148 uint16_t remote_port,
149 size_t recv_buf_len = DEFAULT_RECV_BUF_LEN);
150
154 virtual ~FramedTcpConnection(void);
155
164 void setMaxFrameSize(uint32_t frame_size) { m_max_frame_size = frame_size; }
165
173 virtual void disconnect(void);
174
186 virtual int write(const void *buf, int count);
187
193 sigc::signal<void, FramedTcpConnection *, DisconnectReason> disconnected;
194
203 sigc::signal<void, FramedTcpConnection *,
204 std::vector<uint8_t>&> frameReceived;
205
206 protected:
207 sigc::signal<int, FramedTcpConnection *, void *, int> dataReceived;
208 sigc::signal<void, bool> sendBufferFull;
209
217 virtual void onDisconnected(DisconnectReason reason);
218
233 virtual int onDataReceived(void *buf, int count);
234
235 private:
236 static const uint32_t DEFAULT_MAX_FRAME_SIZE = 1024 * 1024; // 1MB
237
238 struct QueueItem
239 {
240 char* m_buf;
241 int m_size;
242 int m_pos;
243
244 QueueItem(const void* buf, int count)
245 : m_buf(0), m_size(4+count), m_pos(0)
246 {
247 m_buf = new char[4+count];
248 char *ptr = m_buf;
249 *ptr++ = static_cast<uint32_t>(count) >> 24;
250 *ptr++ = (static_cast<uint32_t>(count) >> 16) & 0xff;
251 *ptr++ = (static_cast<uint32_t>(count) >> 8) & 0xff;
252 *ptr++ = (static_cast<uint32_t>(count)) & 0xff;
253 std::memcpy(ptr, buf, count);
254 }
255 ~QueueItem(void) { delete [] m_buf; }
256 };
257 typedef std::deque<QueueItem*> TxQueue;
258
259 uint32_t m_max_frame_size;
260 bool m_size_received;
261 uint32_t m_frame_size;
262 std::vector<uint8_t> m_frame;
263 TxQueue m_txq;
264
265 FramedTcpConnection(const FramedTcpConnection&);
266 FramedTcpConnection& operator=(const FramedTcpConnection&);
267 void onSendBufferFull(bool is_full);
268 void disconnectCleanup(void);
269
270}; /* class FramedTcpConnection */
271
272
273} /* namespace */
274
275#endif /* ASYNC_FRAMED_TCP_CONNECTION_INCLUDED */
276
277
278
279/*
280 * This file has not been truncated
281 */
Contains a class for handling exiting TCP connections.
A TCP connection with framed instead of streamed content.
virtual ~FramedTcpConnection(void)
Destructor.
virtual int write(const void *buf, int count)
Send a frame on the TCP connection.
void setMaxFrameSize(uint32_t frame_size)
Set the maximum frame size.
sigc::signal< void, FramedTcpConnection *, std::vector< uint8_t > & > frameReceived
A signal that is emitted when a frame has been received on the connection.
sigc::signal< void, bool > sendBufferFull
virtual void onDisconnected(DisconnectReason reason)
Called when a connection has been terminated.
sigc::signal< void, FramedTcpConnection *, DisconnectReason > disconnected
A signal that is emitted when a connection has been terminated.
FramedTcpConnection(size_t recv_buf_len=DEFAULT_RECV_BUF_LEN)
Constructor.
FramedTcpConnection(int sock, const IpAddress &remote_addr, uint16_t remote_port, size_t recv_buf_len=DEFAULT_RECV_BUF_LEN)
Constructor.
virtual void disconnect(void)
Disconnect from the remote host.
sigc::signal< int, FramedTcpConnection *, void *, int > dataReceived
virtual int onDataReceived(void *buf, int count)
Called when data has been received on the connection.
A class for representing an IP address in an OS independent way.
A class for handling exiting TCP connections.
static const int DEFAULT_RECV_BUF_LEN
The default length of the reception buffer.
DisconnectReason
Reason code for disconnects.
Namespace for the asynchronous programming classes.