Skip to content

Commit 38075c4

Browse files
committed
Added winrt implementation as well + fixed dev11 issues with new tests
1 parent 3a9033d commit 38075c4

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

Release/src/websockets/client/ws_winrt.cpp

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,9 @@ class winrt_client : public _websocket_client_impl, public std::enable_shared_fr
230230
return pplx::task_from_exception<void>(websocket_exception(_XPLATSTR("Cannot send empty message.")));
231231
}
232232

233-
if (length > UINT_MAX)
233+
if (length >= UINT_MAX && length != SIZE_MAX)
234234
{
235-
return pplx::task_from_exception<void>(websocket_exception(_XPLATSTR("Message size too large. Ensure message length is less than or equal to UINT_MAX.")));
235+
return pplx::task_from_exception<void>(websocket_exception(_XPLATSTR("Message size too large. Ensure message length is less than UINT_MAX.")));
236236
}
237237

238238
{
@@ -257,6 +257,47 @@ class winrt_client : public _websocket_client_impl, public std::enable_shared_fr
257257
auto& is_buf = msg._m_impl->streambuf();
258258
auto length = msg._m_impl->length();
259259

260+
if (length == SIZE_MAX)
261+
{
262+
// This indicates we should determine the length automatically.
263+
if (is_buf.has_size())
264+
{
265+
// The user's stream knows how large it is -- there's no need to buffer.
266+
auto buf_sz = is_buf.size();
267+
if (buf_sz >= SIZE_MAX)
268+
{
269+
websocket_exception wx(_XPLATSTR("Cannot send messages larger than SIZE_MAX."));
270+
msg.m_send_tce.set_exception(std::make_exception_ptr(wx));
271+
return;
272+
}
273+
length = static_cast<size_t>(buf_sz);
274+
// We have determined the length and can proceed normally.
275+
}
276+
else
277+
{
278+
// The stream needs to be buffered.
279+
concurrency::streams::container_buffer<std::vector<uint8_t>> stbuf;
280+
auto is_buf_istream = is_buf.create_istream();
281+
msg._m_impl->set_streambuf(stbuf);
282+
is_buf_istream.read_to_end(stbuf).then([this_client, msg](pplx::task<size_t> t)
283+
{
284+
try
285+
{
286+
auto sz = t.get();
287+
msg._m_impl->set_length(sz);
288+
this_client->send_msg(msg);
289+
}
290+
catch (...)
291+
{
292+
auto eptr = std::current_exception();
293+
msg.m_send_tce.set_exception(eptr);
294+
}
295+
});
296+
// We have postponed the call to send_msg() until after the data is buffered.
297+
return;
298+
}
299+
}
300+
260301
// First try to acquire the data (Get a pointer to the next already allocated contiguous block of data)
261302
// If acquire succeeds, send the data over the socket connection, there is no copy of data from stream to temporary buffer.
262303
// If acquire fails, copy the data to a temporary buffer managed by sp_allocated and send it over the socket connection.

Release/tests/Functional/websockets/client/send_msg_tests.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,9 +424,10 @@ TEST_FIXTURE(uri_address, send_stream_binary_msg_no_length)
424424
{
425425
test_websocket_server server;
426426

427-
std::vector<uint8_t> body = { { 0, 1, 2, 0 } };
427+
std::string body = "\x00\x01\x02\x00";
428+
std::vector<uint8_t> msgbuf(body.begin(), body.end());
428429

429-
auto is = streams::container_stream<std::vector<uint8_t>>::open_istream(body);
430+
auto is = streams::container_stream<std::vector<uint8_t>>::open_istream(std::move(msgbuf));
430431

431432
websocket_client client;
432433
{

0 commit comments

Comments
 (0)