NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: NDN, CCN, CCNx, content centric networks
API Documentation
message.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014, Peter Thorson. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright
7  * notice, this list of conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright
9  * notice, this list of conditions and the following disclaimer in the
10  * documentation and/or other materials provided with the distribution.
11  * * Neither the name of the WebSocket++ Project nor the
12  * names of its contributors may be used to endorse or promote products
13  * derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  */
27 
28 #ifndef WEBSOCKETPP_MESSAGE_BUFFER_MESSAGE_HPP
29 #define WEBSOCKETPP_MESSAGE_BUFFER_MESSAGE_HPP
30 
32 #include <websocketpp/frame.hpp>
33 
34 #include <string>
35 
36 namespace websocketpp {
37 namespace message_buffer {
38 
39 /* # message:
40  * object that stores a message while it is being sent or received. Contains
41  * the message payload itself, the message header, the extension data, and the
42  * opcode.
43  *
44  * # connection_message_manager:
45  * An object that manages all of the message_buffers associated with a given
46  * connection. Implements the get_message_buffer(size) method that returns
47  * a message buffer at least size bytes long.
48  *
49  * Message buffers are reference counted with shared ownership semantics. Once
50  * requested from the manager the requester and it's associated downstream code
51  * may keep a pointer to the message indefinitely at a cost of extra resource
52  * usage. Once the reference count drops to the point where the manager is the
53  * only reference the messages is recycled using whatever method is implemented
54  * in the manager.
55  *
56  * # endpoint_message_manager:
57  * An object that manages connection_message_managers. Implements the
58  * get_message_manager() method. This is used once by each connection to
59  * request the message manager that they are supposed to use to manage message
60  * buffers for their own use.
61  *
62  * TYPES OF CONNECTION_MESSAGE_MANAGERS
63  * - allocate a message with the exact size every time one is requested
64  * - maintain a pool of pre-allocated messages and return one when needed.
65  * Recycle previously used messages back into the pool
66  *
67  * TYPES OF ENDPOINT_MESSAGE_MANAGERS
68  * - allocate a new connection manager for each connection. Message pools
69  * become connection specific. This increases memory usage but improves
70  * concurrency.
71  * - allocate a single connection manager and share a pointer to it with all
72  * connections created by this endpoint. The message pool will be shared
73  * among all connections, improving memory usage and performance at the cost
74  * of reduced concurrency
75  */
76 
77 
79 
83 template <template<class> class con_msg_manager>
84 class message {
85 public:
86  typedef lib::shared_ptr<message> ptr;
87 
88  typedef con_msg_manager<message> con_msg_man_type;
89  typedef typename con_msg_man_type::ptr con_msg_man_ptr;
90  typedef typename con_msg_man_type::weak_ptr con_msg_man_weak_ptr;
91 
93 
96  message(const con_msg_man_ptr manager)
97  : m_manager(manager)
98  , m_prepared(false)
99  , m_fin(true)
100  , m_terminal(false)
101  , m_compressed(false) {}
102 
104 
107  message(const con_msg_man_ptr manager, frame::opcode::value op, size_t size = 128)
108  : m_manager(manager)
109  , m_opcode(op)
110  , m_prepared(false)
111  , m_fin(true)
112  , m_terminal(false)
113  , m_compressed(false)
114  {
115  m_payload.reserve(size);
116  }
117 
119 
125  bool get_prepared() const {
126  return m_prepared;
127  }
128 
130 
135  void set_prepared(bool value) {
136  m_prepared = value;
137  }
138 
140 
143  bool get_compressed() const {
144  return m_compressed;
145  }
146 
148 
156  void set_compressed(bool value) {
157  m_compressed = value;
158  }
159 
161 
169  bool get_terminal() const {
170  return m_terminal;
171  }
172 
174 
181  void set_terminal(bool value) {
182  m_terminal = value;
183  }
185 
195  bool get_fin() const {
196  return m_fin;
197  }
198 
200 
205  void set_fin(bool value) {
206  m_fin = value;
207  }
208 
211  return m_opcode;
212  }
213 
216  m_opcode = op;
217  }
218 
220 
224  std::string const & get_header() const {
225  return m_header;
226  }
227 
229 
234  void set_header(std::string const & header) {
235  m_header = header;
236  }
237 
238  std::string const & get_extension_data() const {
239  return m_extension_data;
240  }
241 
243 
246  std::string const & get_payload() const {
247  return m_payload;
248  }
249 
251 
254  std::string & get_raw_payload() {
255  return m_payload;
256  }
257 
259 
264  void set_payload(std::string const & payload) {
265  m_payload = payload;
266  }
267 
269 
275  void set_payload(void const * payload, size_t len) {
276  m_payload.reserve(len);
277  char const * pl = static_cast<char const *>(payload);
278  m_payload.assign(pl, pl + len);
279  }
280 
282 
287  void append_payload(std::string const & payload) {
288  m_payload.append(payload);
289  }
290 
292 
298  void append_payload(void const * payload, size_t len) {
299  m_payload.reserve(m_payload.size()+len);
300  m_payload.append(static_cast<char const *>(payload),len);
301  }
302 
304 
316  bool recycle() {
317  con_msg_man_ptr shared = m_manager.lock();
318 
319  if (shared) {
320  return shared->recycle(this);
321  } else {
322  return false;
323  }
324  }
325 private:
326  con_msg_man_weak_ptr m_manager;
327  std::string m_header;
328  std::string m_extension_data;
329  std::string m_payload;
330  frame::opcode::value m_opcode;
331  bool m_prepared;
332  bool m_fin;
333  bool m_terminal;
334  bool m_compressed;
335 };
336 
337 } // namespace message_buffer
338 } // namespace websocketpp
339 
340 #endif // WEBSOCKETPP_MESSAGE_BUFFER_MESSAGE_HPP
std::string const & get_extension_data() const
Definition: message.hpp:238
con_msg_manager< message > con_msg_man_type
Definition: message.hpp:88
con_msg_man_type::ptr con_msg_man_ptr
Definition: message.hpp:89
void set_terminal(bool value)
Set the terminal flag.
Definition: message.hpp:181
void set_fin(bool value)
Set the fin bit.
Definition: message.hpp:205
message(const con_msg_man_ptr manager)
Construct an empty message.
Definition: message.hpp:96
std::string const & get_payload() const
Get a reference to the payload string.
Definition: message.hpp:246
void set_opcode(frame::opcode::value op)
Set the opcode.
Definition: message.hpp:215
con_msg_man_type::weak_ptr con_msg_man_weak_ptr
Definition: message.hpp:90
lib::shared_ptr< message > ptr
Definition: message.hpp:86
std::string & get_raw_payload()
Get a non-const reference to the payload string.
Definition: message.hpp:254
frame::opcode::value get_opcode() const
Return the message opcode.
Definition: message.hpp:210
void append_payload(std::string const &payload)
Append payload data.
Definition: message.hpp:287
message(const con_msg_man_ptr manager, frame::opcode::value op, size_t size=128)
Construct a message and fill in some values.
Definition: message.hpp:107
void set_header(std::string const &header)
Set prepared frame header.
Definition: message.hpp:234
Namespace for the WebSocket++ project.
Definition: base64.hpp:41
void set_compressed(bool value)
Set or clear the compression flag.
Definition: message.hpp:156
Represents a buffer for a single WebSocket message.
Definition: message.hpp:84
void append_payload(void const *payload, size_t len)
Append payload data.
Definition: message.hpp:298
bool get_compressed() const
Return whether or not the message is flagged as compressed.
Definition: message.hpp:143
bool get_prepared() const
Return whether or not the message has been prepared for sending.
Definition: message.hpp:125
bool get_fin() const
Read the fin bit.
Definition: message.hpp:195
void set_prepared(bool value)
Set or clear the flag that indicates that the message has been prepared.
Definition: message.hpp:135
std::string const & get_header() const
Return the prepared frame header.
Definition: message.hpp:224
bool recycle()
Recycle the message.
Definition: message.hpp:316
void set_payload(void const *payload, size_t len)
Set payload data.
Definition: message.hpp:275
void set_payload(std::string const &payload)
Set payload data.
Definition: message.hpp:264
bool get_terminal() const
Get whether or not the message is terminal.
Definition: message.hpp:169