Async 1.7.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
165 virtual TcpConnection& operator=(TcpConnection&& other_base) override;
166
175 void setMaxFrameSize(uint32_t frame_size) { m_max_frame_size = frame_size; }
176
188 virtual int write(const void *buf, int count) override;
189
195 sigc::signal<void, FramedTcpConnection *, DisconnectReason> disconnected;
196
205 sigc::signal<void, FramedTcpConnection *,
206 std::vector<uint8_t>&> frameReceived;
207
208 protected:
209 sigc::signal<int, TcpConnection*, void*, int> dataReceived;
210 sigc::signal<void, bool> sendBufferFull;
211
213
220 virtual void closeConnection(void) override;
221
229 virtual void onDisconnected(DisconnectReason reason) override;
230
245 virtual int onDataReceived(void *buf, int count) override;
246
251 virtual void emitDisconnected(DisconnectReason reason) override
252 {
253 disconnected(this, reason);
255 }
256
257 private:
258 static const uint32_t DEFAULT_MAX_FRAME_SIZE = 1024 * 1024; // 1MB
259
260 struct QueueItem
261 {
262 char* m_buf;
263 int m_size;
264 int m_pos;
265
266 QueueItem(const void* buf, int count)
267 : m_buf(0), m_size(4+count), m_pos(0)
268 {
269 m_buf = new char[4+count];
270 char *ptr = m_buf;
271 *ptr++ = static_cast<uint32_t>(count) >> 24;
272 *ptr++ = (static_cast<uint32_t>(count) >> 16) & 0xff;
273 *ptr++ = (static_cast<uint32_t>(count) >> 8) & 0xff;
274 *ptr++ = (static_cast<uint32_t>(count)) & 0xff;
275 std::memcpy(ptr, buf, count);
276 }
277 ~QueueItem(void) { delete [] m_buf; }
278 };
279 typedef std::deque<QueueItem*> TxQueue;
280
281 uint32_t m_max_frame_size;
282 bool m_size_received;
283 uint32_t m_frame_size;
284 std::vector<uint8_t> m_frame;
285 TxQueue m_txq;
286
288 void onSendBufferFull(bool is_full);
289 void disconnectCleanup(void);
290
291}; /* class FramedTcpConnection */
292
293
294} /* namespace */
295
296#endif /* ASYNC_FRAMED_TCP_CONNECTION_INCLUDED */
297
298
299
300/*
301 * This file has not been truncated
302 */
Contains a class for handling exiting TCP connections.
A TCP connection with framed instead of streamed content.
virtual void onDisconnected(DisconnectReason reason) override
Called when a connection has been terminated.
virtual ~FramedTcpConnection(void)
Destructor.
virtual int write(const void *buf, int count) override
Send a frame on the TCP connection.
FramedTcpConnection & operator=(const FramedTcpConnection &)=delete
virtual TcpConnection & operator=(TcpConnection &&other_base) override
Move assignmnt operator.
virtual void closeConnection(void) override
Disconnect from the remote peer.
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.
virtual int onDataReceived(void *buf, int count) override
Called when data has been received on the connection.
sigc::signal< void, bool > sendBufferFull
virtual void emitDisconnected(DisconnectReason reason) override
Emit the disconnected signal.
sigc::signal< void, FramedTcpConnection *, DisconnectReason > disconnected
A signal that is emitted when a connection has been terminated.
sigc::signal< int, TcpConnection *, void *, int > dataReceived
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.
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.
virtual void emitDisconnected(DisconnectReason reason)
Emit the disconnected signal.
Namespace for the asynchronous programming classes.