Skip to content

Commit 260b933

Browse files
committed
Saving some space in websocket messages and container_buffer.
1 parent 9de742d commit 260b933

File tree

8 files changed

+56
-111
lines changed

8 files changed

+56
-111
lines changed

Release/include/cpprest/containerstream.h

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ namespace Concurrency { namespace streams {
120120
/// </summary>
121121
virtual utility::size64_t size() const
122122
{
123-
return utility::size64_t(m_size);
123+
return utility::size64_t(m_data.size());
124124
}
125125

126126
/// <summary>
@@ -153,10 +153,10 @@ namespace Concurrency { namespace streams {
153153
{
154154
// See the comment in seek around the restriction that we do not allow read head to
155155
// seek beyond the current write_end.
156-
_ASSERTE(m_current_position <= m_size);
156+
_ASSERTE(m_current_position <= m_data.size());
157157

158158
SafeSize readhead(m_current_position);
159-
SafeSize writeend(m_size);
159+
SafeSize writeend(m_data.size());
160160
return (size_t)(writeend - readhead);
161161
}
162162

@@ -330,7 +330,7 @@ namespace Concurrency { namespace streams {
330330
// For now, we assume that the current write_end is the end of the buffer. We use this artificial
331331
// end to restrict the read head from seeking beyond what is available.
332332

333-
pos_type end(m_size);
333+
pos_type end(m_data.size());
334334

335335
if (position >= beg)
336336
{
@@ -378,7 +378,7 @@ namespace Concurrency { namespace streams {
378378
{
379379
pos_type beg = 0;
380380
pos_type cur = static_cast<pos_type>(m_current_position);
381-
pos_type end = static_cast<pos_type>(m_size);
381+
pos_type end = static_cast<pos_type>(m_data.size());
382382

383383
switch ( way )
384384
{
@@ -404,8 +404,7 @@ namespace Concurrency { namespace streams {
404404
/// </summary>
405405
basic_container_buffer(std::ios_base::openmode mode)
406406
: streambuf_state_manager<typename _CollectionType::value_type>(mode),
407-
m_current_position(0),
408-
m_size(0)
407+
m_current_position(0)
409408
{
410409
validate_mode(mode);
411410
}
@@ -416,8 +415,7 @@ namespace Concurrency { namespace streams {
416415
basic_container_buffer(_CollectionType data, std::ios_base::openmode mode)
417416
: streambuf_state_manager<typename _CollectionType::value_type>(mode),
418417
m_data(std::move(data)),
419-
m_current_position((mode & std::ios_base::in) ? 0 : m_data.size()),
420-
m_size(m_data.size())
418+
m_current_position((mode & std::ios_base::in) ? 0 : m_data.size())
421419
{
422420
validate_mode(mode);
423421
}
@@ -509,10 +507,8 @@ namespace Concurrency { namespace streams {
509507
/// </summary>
510508
void resize_for_write(size_t newPos)
511509
{
512-
_ASSERTE(m_size <= m_data.size());
513-
514510
// Resize the container if required
515-
if (newPos > m_size)
511+
if (newPos > m_data.size())
516512
{
517513
m_data.resize(newPos);
518514
}
@@ -525,23 +521,14 @@ namespace Concurrency { namespace streams {
525521
{
526522
// The new write head
527523
m_current_position = newPos;
528-
529-
if ( this->can_write() && m_size < m_current_position)
530-
{
531-
// Update the size of the buffer with valid data if required
532-
m_size = m_current_position;
533-
}
534-
535-
_ASSERTE(m_current_position <= m_size);
536-
_ASSERTE(m_size <= m_data.size());
524+
_ASSERTE(m_current_position <= m_data.size());
537525
}
538526

539527
// The actual data store
540528
_CollectionType m_data;
541529

542530
// Read/write head
543531
size_t m_current_position;
544-
size_t m_size;
545532
};
546533

547534
} // namespace details

Release/include/cpprest/ws_msg.h

Lines changed: 20 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -77,46 +77,20 @@ enum class websocket_message_type
7777
pong
7878
};
7979

80-
namespace details
81-
{
82-
class _websocket_message
83-
{
84-
public:
85-
86-
void set_msg_type(websocket_message_type msg_type) { m_msg_type = msg_type; }
87-
88-
void set_length(size_t len) { m_length = len; }
89-
90-
size_t length() const { return m_length; }
91-
92-
websocket_message_type message_type() const { return m_msg_type; }
93-
94-
private:
95-
96-
websocket_message_type m_msg_type;
97-
size_t m_length;
98-
};
99-
}
100-
10180
/// <summary>
10281
/// Represents an outgoing websocket message
10382
/// </summary>
10483
class websocket_outgoing_message
10584
{
10685
public:
10786

108-
/// <summary>
109-
/// Creates an initially empty message for sending.
110-
/// </summary>
111-
websocket_outgoing_message() : _m_impl(std::make_shared<details::_websocket_message>()) {}
112-
11387
/// <summary>
11488
/// Sets a UTF-8 message as the message body.
11589
/// </summary>
11690
/// <param name="data">UTF-8 String containing body of the message.</param>
11791
void set_utf8_message(std::string &&data)
11892
{
119-
this->_set_message(std::move(data), websocket_message_type::text_message);
93+
this->set_message(concurrency::streams::container_buffer<std::string>(std::move(data)));
12094
}
12195

12296
/// <summary>
@@ -125,7 +99,7 @@ class websocket_outgoing_message
12599
/// <param name="data">UTF-8 String containing body of the message.</param>
126100
void set_utf8_message(const std::string &data)
127101
{
128-
this->_set_message(data, websocket_message_type::text_message);
102+
this->set_message(concurrency::streams::container_buffer<std::string>(data));
129103
}
130104

131105
/// <summary>
@@ -135,7 +109,7 @@ class websocket_outgoing_message
135109
/// <remarks>Upon sending, the entire stream may be buffered to determine the length.</remarks>
136110
void set_utf8_message(const concurrency::streams::istream &istream)
137111
{
138-
this->_set_message(istream, SIZE_MAX, websocket_message_type::text_message);
112+
this->set_message(istream, SIZE_MAX, websocket_message_type::text_message);
139113
}
140114

141115
/// <summary>
@@ -145,7 +119,7 @@ class websocket_outgoing_message
145119
/// <param name="len">number of bytes to send.</param>
146120
void set_utf8_message(const concurrency::streams::istream &istream, size_t len)
147121
{
148-
this->_set_message(istream, len, websocket_message_type::text_message);
122+
this->set_message(istream, len, websocket_message_type::text_message);
149123
}
150124

151125
/// <summary>
@@ -155,7 +129,7 @@ class websocket_outgoing_message
155129
/// <param name="len">number of bytes to send.</param>
156130
void set_binary_message(const concurrency::streams::istream &istream, size_t len)
157131
{
158-
this->_set_message(istream, len, websocket_message_type::binary_message);
132+
this->set_message(istream, len, websocket_message_type::binary_message);
159133
}
160134

161135
/// <summary>
@@ -165,17 +139,17 @@ class websocket_outgoing_message
165139
/// <remarks>Upon sending, the entire stream may be buffered to determine the length.</remarks>
166140
void set_binary_message(const concurrency::streams::istream &istream)
167141
{
168-
this->_set_message(istream, SIZE_MAX, websocket_message_type::binary_message);
142+
this->set_message(istream, SIZE_MAX, websocket_message_type::binary_message);
169143
}
170144

171145
private:
172146
friend class details::winrt_client;
173147
friend class details::wspp_client;
174148

175-
std::shared_ptr<details::_websocket_message> _m_impl;
176-
177149
pplx::task_completion_event<void> m_body_sent;
178150
concurrency::streams::streambuf<uint8_t> m_body;
151+
websocket_message_type m_msg_type;
152+
size_t m_length;
179153

180154
void signal_body_sent() const
181155
{
@@ -189,24 +163,17 @@ class websocket_outgoing_message
189163

190164
const pplx::task_completion_event<void> & body_sent() const { return m_body_sent; }
191165

192-
void _set_message(std::string &&data, websocket_message_type msg_type)
193-
{
194-
_m_impl->set_msg_type(msg_type);
195-
_m_impl->set_length(data.length());
196-
m_body = concurrency::streams::container_buffer<std::string>(std::move(data));
197-
}
198-
199-
void _set_message(const std::string &data, websocket_message_type msg_type)
200-
{
201-
_m_impl->set_msg_type(msg_type);
202-
_m_impl->set_length(data.length());
203-
m_body = concurrency::streams::container_buffer<std::string>(data);
204-
}
166+
void set_message(const concurrency::streams::container_buffer<std::string> &buffer)
167+
{
168+
m_msg_type = websocket_message_type::text_message;
169+
m_length = static_cast<size_t>(buffer.size());
170+
m_body = buffer;
171+
}
205172

206-
void _set_message(const concurrency::streams::istream &istream, size_t len, websocket_message_type msg_type)
173+
void set_message(const concurrency::streams::istream &istream, size_t len, websocket_message_type msg_type)
207174
{
208-
_m_impl->set_msg_type(msg_type);
209-
_m_impl->set_length(len);
175+
m_msg_type = msg_type;
176+
m_length = len;
210177
m_body = istream.streambuf();
211178
}
212179
};
@@ -217,9 +184,6 @@ class websocket_outgoing_message
217184
class websocket_incoming_message
218185
{
219186
public:
220-
websocket_incoming_message() : _m_impl(std::make_shared<details::_websocket_message>())
221-
{
222-
}
223187

224188
/// <summary>
225189
/// Extracts the body of the incoming message as a string value, only if the message type is UTF-8.
@@ -250,15 +214,15 @@ class websocket_incoming_message
250214
/// </summary>
251215
size_t length() const
252216
{
253-
return _m_impl->length();
217+
return static_cast<size_t>(m_body.size());
254218
}
255219

256220
/// <summary>
257221
/// Returns the type of the received message.
258222
/// </summary>
259223
websocket_message_type messge_type() const
260224
{
261-
return _m_impl->message_type();
225+
return m_msg_type;
262226
}
263227

264228
private:
@@ -271,8 +235,7 @@ class websocket_incoming_message
271235
// Store message body in a container buffer backed by a string.
272236
// Allows for optimization in the string message cases.
273237
concurrency::streams::container_buffer<std::string> m_body;
274-
275-
std::shared_ptr<details::_websocket_message> _m_impl;
238+
websocket_message_type m_msg_type;
276239
};
277240

278241
}}}

Release/src/websockets/client/ws_client.cpp

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -269,34 +269,31 @@ class wspp_client : public _websocket_client_impl, public std::enable_shared_fro
269269
client.set_message_handler([this](websocketpp::connection_hdl, const websocketpp::config::asio_client::message_type::ptr &msg)
270270
{
271271
_ASSERTE(m_state >= CONNECTED && m_state < CLOSED);
272-
websocket_incoming_message ws_incoming_message;
273-
auto& incmsg = ws_incoming_message._m_impl;
272+
websocket_incoming_message incoming_msg;
274273

275274
switch (msg->get_opcode())
276275
{
277276
case websocketpp::frame::opcode::binary:
278-
incmsg->set_msg_type(websocket_message_type::binary_message);
277+
incoming_msg.m_msg_type = websocket_message_type::binary_message;
279278
break;
280279
case websocketpp::frame::opcode::text:
281-
incmsg->set_msg_type(websocket_message_type::text_message);
280+
incoming_msg.m_msg_type = websocket_message_type::text_message;
282281
break;
283282
default:
284283
// Unknown message type. Since both websocketpp and our code use the RFC codes, we'll just pass it on to the user.
285-
incmsg->set_msg_type(static_cast<websocket_message_type>(msg->get_opcode()));
284+
incoming_msg.m_msg_type = static_cast<websocket_message_type>(msg->get_opcode());
286285
break;
287286
}
288287

289288
// 'move' the payload into a container buffer to avoid any copies.
290289
auto &payload = msg->get_raw_payload();
291-
const auto len = payload.size();
292-
ws_incoming_message.m_body = concurrency::streams::container_buffer<std::string>(std::move(payload));
293-
incmsg->set_length(len);
290+
incoming_msg.m_body = concurrency::streams::container_buffer<std::string>(std::move(payload));
294291

295292
std::unique_lock<std::mutex> lock(m_receive_queue_lock);
296293
if (m_receive_task_queue.empty())
297294
{
298295
// Push message to the queue as no one is waiting to receive
299-
m_receive_msg_queue.push(ws_incoming_message);
296+
m_receive_msg_queue.push(incoming_msg);
300297
return;
301298
}
302299
else
@@ -306,7 +303,7 @@ class wspp_client : public _websocket_client_impl, public std::enable_shared_fro
306303
m_receive_task_queue.pop();
307304
// Unlock the lock before setting the completion event to avoid contention
308305
lock.unlock();
309-
tce.set(ws_incoming_message);
306+
tce.set(incoming_msg);
310307
}
311308
});
312309

@@ -370,7 +367,7 @@ class wspp_client : public _websocket_client_impl, public std::enable_shared_fro
370367
return pplx::task_from_exception<void>(websocket_exception("Client not connected."));
371368
}
372369

373-
switch (msg._m_impl->message_type())
370+
switch (msg.m_msg_type)
374371
{
375372
case websocket_message_type::text_message:
376373
case websocket_message_type::binary_message:
@@ -379,7 +376,7 @@ class wspp_client : public _websocket_client_impl, public std::enable_shared_fro
379376
return pplx::task_from_exception<void>(websocket_exception("Invalid message type"));
380377
}
381378

382-
const auto length = msg._m_impl->length();
379+
const auto length = msg.m_length;
383380
if (length == 0)
384381
{
385382
return pplx::task_from_exception<void>(websocket_exception("Cannot send empty message."));
@@ -449,7 +446,7 @@ class wspp_client : public _websocket_client_impl, public std::enable_shared_fro
449446
{
450447
auto this_client = this->shared_from_this();
451448
auto& is_buf = msg.m_body;
452-
auto length = msg._m_impl->length();
449+
auto length = msg.m_length;
453450

454451
if (length == SIZE_MAX)
455452
{
@@ -475,7 +472,7 @@ class wspp_client : public _websocket_client_impl, public std::enable_shared_fro
475472
{
476473
try
477474
{
478-
msg._m_impl->set_length(t.get());
475+
msg.m_length = t.get();
479476
this_client->send_msg(msg);
480477
}
481478
catch (...)
@@ -528,7 +525,7 @@ class wspp_client : public _websocket_client_impl, public std::enable_shared_fro
528525
{
529526
auto &client = this_client->m_client->client<WebsocketClientType>();
530527
websocketpp::lib::error_code ec;
531-
switch (msg._m_impl->message_type())
528+
switch (msg.m_msg_type)
532529
{
533530
case websocket_message_type::text_message:
534531
client.send(

Release/src/websockets/client/ws_msg.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ std::vector<::utility::string_t> websocket_client_config::subprotocols() const
7171

7272
pplx::task<std::string> websocket_incoming_message::extract_string() const
7373
{
74-
if (_m_impl->message_type() == websocket_message_type::binary_message)
74+
if (m_msg_type == websocket_message_type::binary_message)
7575
{
7676
return pplx::task_from_exception<std::string>(websocket_exception("Invalid message type"));
7777
}

0 commit comments

Comments
 (0)