Skip to content

Commit 80e244c

Browse files
committed
Merge branch 'websockets_namespace' of https://git01.codeplex.com/forks/stevetgates/stgatesdev into websockets_namespace
Conflicts: Release/include/cpprest/ws_client.h Release/include/cpprest/ws_msg.h Release/src/websockets/client/ws_winrt.cpp
2 parents 5071237 + 367c488 commit 80e244c

File tree

3 files changed

+30
-18
lines changed

3 files changed

+30
-18
lines changed

Release/include/cpprest/ws_client.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ namespace web
5757
/// WebSocket client is currently in beta.
5858
namespace experimental
5959
{
60+
// In the past namespace was accidentally called 'web_sockets'. To avoid breaking code
61+
// alias it. At our next major release this should be deleted.
62+
namespace web_sockets = websockets;
6063
namespace websockets
6164
{
6265
/// WebSocket client side library.

Release/include/cpprest/ws_msg.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,10 @@ namespace client
5959
namespace details
6060
{
6161
class winrt_client;
62-
class ReceiveContext;
6362
class ws_desktop_client;
63+
#if defined(__cplusplus_winrt)
64+
ref class ReceiveContext;
65+
#endif
6466
}
6567

6668
/// <summary>
@@ -92,6 +94,8 @@ class _websocket_message
9294

9395
websocket_message_type message_type() const { return m_msg_type; }
9496

97+
websocket_message_type message_type() const { return m_msg_type; }
98+
9599
private:
96100

97101
websocket_message_type m_msg_type;
@@ -265,8 +269,10 @@ class websocket_incoming_message
265269
private:
266270
friend class details::winrt_client;
267271
friend class details::ws_desktop_client;
268-
friend class details::ReceiveContext;
269-
272+
#if defined(__cplusplus_winrt)
273+
friend ref class details::ReceiveContext;
274+
#endif
275+
270276
// Store message body in a container buffer backed by a string.
271277
// Allows for optimization in the string message cases.
272278
concurrency::streams::container_buffer<std::string> m_body;

Release/src/websockets/client/ws_winrt.cpp

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ ref class ReceiveContext sealed
6161

6262
private:
6363
// Public members cannot have native types
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) {}
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) {}
6565

6666
// Handler to be executed when a message has been received by the client
6767
std::function<void(websocket_incoming_message &)> m_receive_handler;
@@ -246,13 +246,13 @@ class winrt_client : public _websocket_client_impl, public std::enable_shared_fr
246246
m_outgoing_msg_queue.push(msg);
247247
}
248248
}
249-
return pplx::create_task(msg._m_impl->msg_sent());
249+
return pplx::create_task(msg.body_sent());
250250
}
251251

252252
void send_msg(websocket_outgoing_message &msg)
253253
{
254254
auto this_client = this->shared_from_this();
255-
auto& is_buf = msg.m_body.create_istream();
255+
auto &is_buf = msg.m_body;
256256
auto length = msg._m_impl->length();
257257

258258
if (length == SIZE_MAX)
@@ -264,7 +264,7 @@ class winrt_client : public _websocket_client_impl, public std::enable_shared_fr
264264
auto buf_sz = is_buf.size();
265265
if (buf_sz >= SIZE_MAX)
266266
{
267-
msg.signal_msg_sent(std::make_exception_ptr(websocket_exception("Cannot send messages larger than SIZE_MAX.")));
267+
msg.signal_body_sent(std::make_exception_ptr(websocket_exception("Cannot send messages larger than SIZE_MAX.")));
268268
return;
269269
}
270270
length = static_cast<size_t>(buf_sz);
@@ -275,7 +275,7 @@ class winrt_client : public _websocket_client_impl, public std::enable_shared_fr
275275
// The stream needs to be buffered.
276276
auto is_buf_istream = is_buf.create_istream();
277277
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)
278+
is_buf_istream.read_to_end(msg.m_body).then([this_client, msg](pplx::task<size_t> t) mutable
279279
{
280280
try
281281
{
@@ -284,7 +284,7 @@ class winrt_client : public _websocket_client_impl, public std::enable_shared_fr
284284
}
285285
catch (...)
286286
{
287-
msg.signal_msg_sent(std::current_exception());
287+
msg.signal_body_sent(std::current_exception());
288288
}
289289
});
290290
// We have postponed the call to send_msg() until after the data is buffered.
@@ -334,7 +334,7 @@ class winrt_client : public _websocket_client_impl, public std::enable_shared_fr
334334

335335
// Send the data as one complete message, in WinRT we do not have an option to send fragments.
336336
return pplx::task<unsigned int>(this_client->m_messageWriter->StoreAsync());
337-
}).then([this_client, msg, is_buf, 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) mutable
338338
{
339339
std::exception_ptr eptr;
340340
unsigned int bytes_written = 0;
@@ -364,7 +364,7 @@ class winrt_client : public _websocket_client_impl, public std::enable_shared_fr
364364
// Set the send_task_completion_event after calling release.
365365
if (eptr)
366366
{
367-
msg.signal_msg_sent(eptr);
367+
msg.signal_body_sent(eptr);
368368
}
369369

370370
{
@@ -377,7 +377,7 @@ class winrt_client : public _websocket_client_impl, public std::enable_shared_fr
377377
this_client->send_msg(next_msg);
378378
}
379379
}
380-
msg.signal_msg_sent();
380+
msg.signal_body_sent();
381381
});
382382
}
383383

@@ -462,7 +462,7 @@ class winrt_client : public _websocket_client_impl, public std::enable_shared_fr
462462
void ReceiveContext::OnReceive(MessageWebSocket^ sender, MessageWebSocketMessageReceivedEventArgs^ args)
463463
{
464464
websocket_incoming_message ws_incoming_message;
465-
auto &msg = ws_incoming_message->_m_impl;
465+
auto &msg = ws_incoming_message._m_impl;
466466

467467
switch(args->MessageType)
468468
{
@@ -478,11 +478,14 @@ void ReceiveContext::OnReceive(MessageWebSocket^ sender, MessageWebSocketMessage
478478
{
479479
DataReader^ reader = args->GetDataReader();
480480
const auto len = reader->UnconsumedBufferLength;
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));
485-
msg->signal_msg_received(len);
481+
if (len > 0)
482+
{
483+
std::string payload;
484+
payload.resize(len);
485+
reader->ReadBytes(Platform::ArrayReference<uint8_t>(reinterpret_cast<uint8 *>(&payload[0]), len));
486+
ws_incoming_message.m_body = concurrency::streams::container_buffer<std::string>(std::move(payload));
487+
}
488+
msg->set_length(len);
486489
m_receive_handler(ws_incoming_message);
487490
}
488491
catch(...)

0 commit comments

Comments
 (0)