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 
83 namespace 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 
131 class FramedTcpConnection : public TcpConnection
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 
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  */
Async::FramedTcpConnection::onDataReceived
virtual int onDataReceived(void *buf, int count)
Called when data has been received on the connection.
Async::FramedTcpConnection::dataReceived
sigc::signal< int, FramedTcpConnection *, void *, int > dataReceived
Definition: AsyncFramedTcpConnection.h:263
AsyncTcpConnection.h
Contains a class for handling exiting TCP connections.
Async::FramedTcpConnection::setMaxFrameSize
void setMaxFrameSize(uint32_t frame_size)
Set the maximum frame size.
Definition: AsyncFramedTcpConnection.h:220
Async::FramedTcpConnection::sendBufferFull
sigc::signal< void, bool > sendBufferFull
Definition: AsyncFramedTcpConnection.h:264
Async::FramedTcpConnection::disconnected
sigc::signal< void, FramedTcpConnection *, DisconnectReason > disconnected
A signal that is emitted when a connection has been terminated.
Definition: AsyncFramedTcpConnection.h:249
Async::FramedTcpConnection::onDisconnected
virtual void onDisconnected(DisconnectReason reason)
Called when a connection has been terminated.
Async::FramedTcpConnection::frameReceived
sigc::signal< void, FramedTcpConnection *, std::vector< uint8_t > & > frameReceived
A signal that is emitted when a frame has been received on the connection.
Definition: AsyncFramedTcpConnection.h:260
Async::TcpConnection::DEFAULT_RECV_BUF_LEN
static const int DEFAULT_RECV_BUF_LEN
The default length of the reception buffer.
Definition: AsyncTcpConnection.h:174
Async::FramedTcpConnection::disconnect
virtual void disconnect(void)
Disconnect from the remote host.
Async::FramedTcpConnection::write
virtual int write(const void *buf, int count)
Send a frame on the TCP connection.
Async::FramedTcpConnection::~FramedTcpConnection
virtual ~FramedTcpConnection(void)
Destructor.
Async
Namespace for the asynchronous programming classes.
Definition: AsyncApplication.h:75
Async::FramedTcpConnection
A TCP connection with framed instead of streamed content.
Definition: AsyncFramedTcpConnection.h:151
Async::FramedTcpConnection::FramedTcpConnection
FramedTcpConnection(size_t recv_buf_len=DEFAULT_RECV_BUF_LEN)
Constructor.
Async::TcpConnection::DisconnectReason
DisconnectReason
Reason code for disconnects.
Definition: AsyncTcpConnection.h:161