@@ -61,10 +61,10 @@ ref class ReceiveContext sealed
61
61
62
62
private:
63
63
// Public members cannot have native types
64
- ReceiveContext (std::function<void (std::shared_ptr< websocket_incoming_message>)> receive_handler, std::function<void ()> close_handler): m_receive_handler(receive_handler), m_close_handler(close_handler) {}
64
+ ReceiveContext (std::function<void (websocket_incoming_message & >)> receive_handler, std::function<void ()> close_handler): m_receive_handler(receive_handler), m_close_handler(close_handler) {}
65
65
66
66
// Handler to be executed when a message has been received by the client
67
- std::function<void (std::shared_ptr< websocket_incoming_message> )> m_receive_handler;
67
+ std::function<void (websocket_incoming_message & )> m_receive_handler;
68
68
69
69
// Handler to be executed when a close message has been received by the client
70
70
std::function<void ()> m_close_handler;
@@ -108,15 +108,14 @@ class winrt_client : public _websocket_client_impl, public std::enable_shared_fr
108
108
Platform::StringReference (client_config.credentials ().password ().c_str ()));
109
109
}
110
110
111
- m_context = ref new ReceiveContext ([=](std::shared_ptr< websocket_incoming_message> msg)
111
+ m_context = ref new ReceiveContext ([=](websocket_incoming_message & msg)
112
112
{
113
- _ASSERTE (msg != nullptr );
114
113
pplx::task_completion_event<websocket_incoming_message> tce; // This will be set if there are any tasks waiting to receive a message
115
114
{
116
115
std::lock_guard<std::mutex> lock (m_receive_queue_lock);
117
116
if (m_receive_task_queue.empty ()) // Push message to the queue as no one is waiting to receive
118
117
{
119
- m_receive_msg_queue.push (std::move (* msg) );
118
+ m_receive_msg_queue.push (msg);
120
119
return ;
121
120
}
122
121
else // There are tasks waiting to receive a message.
@@ -126,7 +125,7 @@ class winrt_client : public _websocket_client_impl, public std::enable_shared_fr
126
125
}
127
126
}
128
127
// Setting the tce outside the receive lock for better performance
129
- tce.set (* msg);
128
+ tce.set (msg);
130
129
},
131
130
[=]() // Close handler called upon receiving a close frame from the server.
132
131
{
@@ -253,7 +252,7 @@ class winrt_client : public _websocket_client_impl, public std::enable_shared_fr
253
252
void send_msg (websocket_outgoing_message &msg)
254
253
{
255
254
auto this_client = this ->shared_from_this ();
256
- auto & is_buf = msg._m_impl -> body () .create_istream ();
255
+ auto & is_buf = msg.m_body .create_istream ();
257
256
auto length = msg._m_impl ->length ();
258
257
259
258
if (length == SIZE_MAX)
@@ -274,10 +273,9 @@ class winrt_client : public _websocket_client_impl, public std::enable_shared_fr
274
273
else
275
274
{
276
275
// The stream needs to be buffered.
277
- concurrency::streams::container_buffer<std::vector<uint8_t >> stbuf;
278
276
auto is_buf_istream = is_buf.create_istream ();
279
- msg._m_impl -> set_body (stbuf );
280
- is_buf_istream.read_to_end (stbuf ).then ([this_client, msg](pplx::task<size_t > t)
277
+ msg.m_body = concurrency::streams::container_buffer<std::vector< uint8_t >>( );
278
+ is_buf_istream.read_to_end (msg. m_body ).then ([this_client, msg](pplx::task<size_t > t)
281
279
{
282
280
try
283
281
{
@@ -336,7 +334,7 @@ class winrt_client : public _websocket_client_impl, public std::enable_shared_fr
336
334
337
335
// Send the data as one complete message, in WinRT we do not have an option to send fragments.
338
336
return pplx::task<unsigned int >(this_client->m_messageWriter ->StoreAsync ());
339
- }).then ([this_client, msg, acquired, sp_allocated, length] (pplx::task<unsigned int > previousTask)
337
+ }).then ([this_client, msg, is_buf, acquired, sp_allocated, length](pplx::task<unsigned int > previousTask)
340
338
{
341
339
std::exception_ptr eptr;
342
340
unsigned int bytes_written = 0 ;
@@ -360,7 +358,7 @@ class winrt_client : public _websocket_client_impl, public std::enable_shared_fr
360
358
361
359
if (acquired)
362
360
{
363
- msg. _m_impl -> body () .release (sp_allocated.get (), bytes_written);
361
+ is_buf .release (sp_allocated.get (), bytes_written);
364
362
}
365
363
366
364
// Set the send_task_completion_event after calling release.
@@ -420,11 +418,6 @@ class winrt_client : public _websocket_client_impl, public std::enable_shared_fr
420
418
return pplx::create_task (m_close_tce);
421
419
}
422
420
423
- static std::shared_ptr<details::_websocket_message> &get_impl (websocket_incoming_message msg)
424
- {
425
- return msg._m_impl ;
426
- }
427
-
428
421
private:
429
422
430
423
// WinRT MessageWebSocket object
@@ -469,7 +462,7 @@ class winrt_client : public _websocket_client_impl, public std::enable_shared_fr
469
462
void ReceiveContext::OnReceive (MessageWebSocket^ sender, MessageWebSocketMessageReceivedEventArgs^ args)
470
463
{
471
464
websocket_incoming_message ws_incoming_message;
472
- auto &msg = winrt_client::get_impl ( ws_incoming_message) ;
465
+ auto &msg = ws_incoming_message-> _m_impl ;
473
466
474
467
switch (args->MessageType )
475
468
{
@@ -481,18 +474,16 @@ void ReceiveContext::OnReceive(MessageWebSocket^ sender, MessageWebSocketMessage
481
474
break ;
482
475
}
483
476
484
- auto &writebuf = msg->body ();
485
-
486
477
try
487
478
{
488
479
DataReader^ reader = args->GetDataReader ();
489
480
const auto len = reader->UnconsumedBufferLength ;
490
- auto block = writebuf. alloc (len) ;
491
- reader-> ReadBytes (Platform::ArrayReference< uint8_t >(block, len));
492
- writebuf. commit ( len);
493
- writebuf. close ( std::ios::out). wait (); // Since this is an in-memory stream, this call is not blocking.
481
+ std::string payload ;
482
+ payload. resize ( len);
483
+ reader-> ReadBytes (Platform::ArrayReference< uint8_t >(payload. c_str (), len) );
484
+ ws_incoming_message. m_body = concurrency::streams::container_buffer< std::string>( std::move (payload));
494
485
msg->signal_msg_received (len);
495
- m_receive_handler (std::make_shared<websocket_incoming_message>( ws_incoming_message) );
486
+ m_receive_handler (ws_incoming_message);
496
487
}
497
488
catch (...)
498
489
{
0 commit comments