Async 1.8.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
176 void setMaxRxFrameSize(uint32_t frame_size)
177 {
178 m_max_rx_frame_size = frame_size;
179 }
180
190 void setMaxTxFrameSize(uint32_t frame_size)
191 {
192 m_max_tx_frame_size = frame_size;
193 }
194
203 void setMaxFrameSize(uint32_t frame_size)
204 {
205 setMaxRxFrameSize(frame_size);
206 setMaxTxFrameSize(frame_size);
207 }
208
220 virtual int write(const void *buf, int count) override;
221
227 sigc::signal<void, FramedTcpConnection *, DisconnectReason> disconnected;
228
237 sigc::signal<void, FramedTcpConnection *,
238 std::vector<uint8_t>&> frameReceived;
239
240 protected:
241 sigc::signal<int, TcpConnection*, void*, int> dataReceived;
242 sigc::signal<void, bool> sendBufferFull;
243
245
252 virtual void closeConnection(void) override;
253
261 virtual void onDisconnected(DisconnectReason reason) override;
262
277 virtual int onDataReceived(void *buf, int count) override;
278
283 virtual void emitDisconnected(DisconnectReason reason) override
284 {
285 disconnected(this, reason);
287 }
288
289 private:
290 static const uint32_t DEFAULT_MAX_FRAME_SIZE = 1024 * 1024; // 1MB
291
292 struct QueueItem
293 {
294 char* m_buf;
295 int m_size;
296 int m_pos;
297
298 QueueItem(const void* buf, int count)
299 : m_buf(0), m_size(4+count), m_pos(0)
300 {
301 m_buf = new char[4+count];
302 char *ptr = m_buf;
303 *ptr++ = static_cast<uint32_t>(count) >> 24;
304 *ptr++ = (static_cast<uint32_t>(count) >> 16) & 0xff;
305 *ptr++ = (static_cast<uint32_t>(count) >> 8) & 0xff;
306 *ptr++ = (static_cast<uint32_t>(count)) & 0xff;
307 std::memcpy(ptr, buf, count);
308 }
309 ~QueueItem(void) { delete [] m_buf; }
310 };
311 typedef std::deque<QueueItem*> TxQueue;
312
313 uint32_t m_max_rx_frame_size;
314 uint32_t m_max_tx_frame_size;
315 bool m_size_received;
316 uint32_t m_frame_size;
317 std::vector<uint8_t> m_frame;
318 TxQueue m_txq;
319
321 //void onSendBufferFull(bool is_full);
322 void disconnectCleanup(void);
323
324}; /* class FramedTcpConnection */
325
326
327} /* namespace */
328
329#endif /* ASYNC_FRAMED_TCP_CONNECTION_INCLUDED */
330
331
332
333/*
334 * This file has not been truncated
335 */
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.
void setMaxRxFrameSize(uint32_t frame_size)
Set the maximum RX frame size.
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 setMaxTxFrameSize(uint32_t frame_size)
Set the maximum TX frame size.
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 size of the reception buffer.
DisconnectReason
Reason code for disconnects.
virtual void emitDisconnected(DisconnectReason reason)
Emit the disconnected signal.
Namespace for the asynchronous programming classes.