Skip to content

Commit 9857cb9

Browse files
committed
Merge branch 'development' of https://git01.codeplex.com/casablanca into ws_winrt_connection
Conflicts: Release/src/websockets/client/ws_winrt.cpp
2 parents 3d16311 + 7356763 commit 9857cb9

File tree

5 files changed

+77
-45
lines changed

5 files changed

+77
-45
lines changed

Release/include/cpprest/ws_msg.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,11 +256,21 @@ class websocket_incoming_message
256256
/// <summary>
257257
/// Returns the type of the received message.
258258
/// </summary>
259+
CASABLANCA_DEPRECATED("Incorrectly spelled API, use message_type() instead.")
259260
websocket_message_type messge_type() const
260261
{
261262
return _m_impl->message_type();
262263
}
263264

265+
/// <summary>
266+
/// Returns the type of the received message, either string or binary.
267+
/// </summary>
268+
/// <returns>websocket_message_type</returns>
269+
websocket_message_type message_type() const
270+
{
271+
return _m_impl->message_type();
272+
}
273+
264274
private:
265275
friend class details::winrt_client;
266276
friend class details::wspp_client;

Release/src/websockets/client/ws_winrt.cpp

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ class winrt_client : public _websocket_client_impl, public std::enable_shared_fr
209209
result.get();
210210
m_messageWriter = ref new DataWriter(m_msg_websocket->OutputStream);
211211
}
212-
catch(Platform::Exception^ e)
212+
catch (Platform::Exception^ e)
213213
{
214214
websocket_exception exc(e->HResult, build_error_msg(e, "ConnectAsync"));
215215
close_pending_tasks_with_error(exc);
@@ -248,18 +248,16 @@ class winrt_client : public _websocket_client_impl, public std::enable_shared_fr
248248
return pplx::task_from_exception<void>(websocket_exception("Message size too large. Ensure message length is less than UINT_MAX."));
249249
}
250250

251+
if (++m_num_sends == 1) // No sends in progress
251252
{
252-
if (++m_num_sends == 1) // No sends in progress
253-
{
254-
// Start sending the message
255-
send_msg(msg);
256-
}
257-
else
258-
{
259-
// Only actually have to take the lock if touching the queue.
260-
std::lock_guard<std::mutex> lock(m_send_lock);
261-
m_outgoing_msg_queue.push(msg);
262-
}
253+
// Start sending the message
254+
send_msg(msg);
255+
}
256+
else
257+
{
258+
// Only actually have to take the lock if touching the queue.
259+
std::lock_guard<std::mutex> lock(m_send_lock);
260+
m_outgoing_msg_queue.push(msg);
263261
}
264262
return pplx::create_task(msg.body_sent());
265263
}
@@ -362,13 +360,19 @@ class winrt_client : public _websocket_client_impl, public std::enable_shared_fr
362360
eptr = std::make_exception_ptr(websocket_exception("Failed to send all the bytes."));
363361
}
364362
}
365-
catch (const websocket_exception& ex)
363+
catch (Platform::Exception^ e)
366364
{
367-
eptr = std::make_exception_ptr(ex);
365+
// Convert to websocket_exception.
366+
eptr = std::make_exception_ptr(websocket_exception(e->HResult, build_error_msg(e, "send_msg")));
367+
}
368+
catch (const websocket_exception &e)
369+
{
370+
// Catch to avoid slicing and losing the type if falling through to catch (...).
371+
eptr = std::make_exception_ptr(e);
368372
}
369373
catch (...)
370374
{
371-
eptr = std::make_exception_ptr(websocket_exception("Failed to send data over the websocket connection."));
375+
eptr = std::make_exception_ptr(std::current_exception());
372376
}
373377

374378
if (acquired)
@@ -381,21 +385,22 @@ class winrt_client : public _websocket_client_impl, public std::enable_shared_fr
381385
{
382386
msg.signal_body_sent(eptr);
383387
}
388+
else
389+
{
390+
msg.signal_body_sent();
391+
}
384392

393+
if (--this_client->m_num_sends > 0)
385394
{
386-
if (--this_client->m_num_sends > 0)
395+
// Only hold the lock when actually touching the queue.
396+
websocket_outgoing_message next_msg;
387397
{
388-
// Only hold the lock when actually touching the queue.
389-
websocket_outgoing_message next_msg;
390-
{
391-
std::lock_guard<std::mutex> lock(this_client->m_send_lock);
392-
next_msg = this_client->m_outgoing_msg_queue.front();
393-
this_client->m_outgoing_msg_queue.pop();
394-
}
395-
this_client->send_msg(next_msg);
398+
std::lock_guard<std::mutex> lock(this_client->m_send_lock);
399+
next_msg = this_client->m_outgoing_msg_queue.front();
400+
this_client->m_outgoing_msg_queue.pop();
396401
}
402+
this_client->send_msg(next_msg);
397403
}
398-
msg.signal_body_sent();
399404
});
400405
}
401406

Release/tests/functional/websockets/client/authentication_tests.cpp

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -89,24 +89,41 @@ TEST_FIXTURE(uri_address, auth_with_credentials, "Ignore", "245")
8989
TEST(ssl_test)
9090
{
9191
websocket_client client;
92-
client.connect(U("wss://echo.websocket.org/")).wait();
9392
std::string body_str("hello");
9493

95-
auto receive_task = client.receive().then([body_str](websocket_incoming_message ret_msg)
94+
try
9695
{
97-
auto ret_str = ret_msg.extract_string().get();
96+
client.connect(U("wss://echo.websocket.org/")).wait();
97+
auto receive_task = client.receive().then([body_str](websocket_incoming_message ret_msg)
98+
{
99+
auto ret_str = ret_msg.extract_string().get();
98100

99-
VERIFY_ARE_EQUAL(ret_msg.length(), body_str.length());
100-
VERIFY_ARE_EQUAL(body_str.compare(ret_str), 0);
101-
VERIFY_ARE_EQUAL(ret_msg.messge_type(), websocket_message_type::text_message);
102-
});
101+
VERIFY_ARE_EQUAL(ret_msg.length(), body_str.length());
102+
VERIFY_ARE_EQUAL(body_str.compare(ret_str), 0);
103+
VERIFY_ARE_EQUAL(ret_msg.message_type(), websocket_message_type::text_message);
104+
});
103105

104-
websocket_outgoing_message msg;
105-
msg.set_utf8_message(body_str);
106-
client.send(msg).wait();
106+
websocket_outgoing_message msg;
107+
msg.set_utf8_message(body_str);
108+
client.send(msg).wait();
107109

108-
receive_task.wait();
109-
client.close().wait();
110+
receive_task.wait();
111+
client.close().wait();
112+
}
113+
catch (const websocket_exception &e)
114+
{
115+
const auto msg = std::string(e.what());
116+
if (msg.find("set_fail_handler") != std::string::npos)
117+
{
118+
if (msg.find("TLS handshake timed out") != std::string::npos || msg.find("Timer Expired") != std::string::npos)
119+
{
120+
// Since this test depends on an outside server sometimes it sporadically can fail due to timeouts
121+
// especially on our build machines.
122+
return;
123+
}
124+
}
125+
throw;
126+
}
110127
}
111128

112129
// These tests are specific to our websocketpp based implementation.

Release/tests/functional/websockets/client/client_construction.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ TEST_FIXTURE(uri_address, move_operations)
128128

129129
VERIFY_ARE_EQUAL(ret_msg.length(), body.length());
130130
VERIFY_ARE_EQUAL(body.compare(ret_str), 0);
131-
VERIFY_ARE_EQUAL(ret_msg.messge_type(), websocket_message_type::text_message);
131+
VERIFY_ARE_EQUAL(ret_msg.message_type(), websocket_message_type::text_message);
132132
});
133133

134134
test_websocket_msg rmsg;
@@ -158,7 +158,7 @@ TEST_FIXTURE(uri_address, move_operations)
158158

159159
VERIFY_ARE_EQUAL(ret_msg.length(), body.length());
160160
VERIFY_ARE_EQUAL(body.compare(ret_str), 0);
161-
VERIFY_ARE_EQUAL(ret_msg.messge_type(), websocket_message_type::text_message);
161+
VERIFY_ARE_EQUAL(ret_msg.message_type(), websocket_message_type::text_message);
162162
});
163163
t1.wait();
164164
client.close().wait();

Release/tests/functional/websockets/client/receive_msg_tests.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pplx::task<void> receive_text_msg_helper(websocket_client& client,
5757

5858
VERIFY_ARE_EQUAL(ret_msg.length(), body_str.length());
5959
VERIFY_ARE_EQUAL(body_str.compare(ret_str), 0);
60-
VERIFY_ARE_EQUAL(ret_msg.messge_type(), websocket_message_type::text_message);
60+
VERIFY_ARE_EQUAL(ret_msg.message_type(), websocket_message_type::text_message);
6161
});
6262

6363
test_websocket_msg msg;
@@ -87,9 +87,9 @@ pplx::task<void> receive_msg_stream_helper(websocket_client& client,
8787
VERIFY_ARE_EQUAL(ret_msg.length(), body.size());
8888
VERIFY_ARE_EQUAL(body, ret_data.collection());
8989
if (type == test_websocket_message_type::WEB_SOCKET_BINARY_MESSAGE_TYPE)
90-
VERIFY_ARE_EQUAL(ret_msg.messge_type(), websocket_message_type::binary_message);
90+
VERIFY_ARE_EQUAL(ret_msg.message_type(), websocket_message_type::binary_message);
9191
else if (type == test_websocket_message_type::WEB_SOCKET_UTF8_MESSAGE_TYPE)
92-
VERIFY_ARE_EQUAL(ret_msg.messge_type(), websocket_message_type::text_message);
92+
VERIFY_ARE_EQUAL(ret_msg.message_type(), websocket_message_type::text_message);
9393
});
9494

9595
test_websocket_msg msg;
@@ -154,7 +154,7 @@ TEST_FIXTURE(uri_address, receive_text_msg_fragments, "Ignore", "898451")
154154
auto ret_str = ret_msg.extract_string().get();
155155

156156
VERIFY_ARE_EQUAL(body_str.compare(ret_str), 0);
157-
VERIFY_ARE_EQUAL(ret_msg.messge_type(), websocket_message_type::text_message);
157+
VERIFY_ARE_EQUAL(ret_msg.message_type(), websocket_message_type::text_message);
158158
});
159159

160160
test_websocket_msg msg1;
@@ -235,7 +235,7 @@ TEST_FIXTURE(uri_address, receive_after_server_send)
235235
{
236236
auto ret_str = ret_msg.extract_string().get();
237237
VERIFY_ARE_EQUAL(body_str.compare(ret_str), 0);
238-
VERIFY_ARE_EQUAL(ret_msg.messge_type(), websocket_message_type::text_message);
238+
VERIFY_ARE_EQUAL(ret_msg.message_type(), websocket_message_type::text_message);
239239
}).wait();
240240

241241
client.close().wait();
@@ -256,7 +256,7 @@ TEST_FIXTURE(uri_address, receive_before_connect)
256256

257257
VERIFY_ARE_EQUAL(ret_msg.length(), body_str.length());
258258
VERIFY_ARE_EQUAL(body_str.compare(ret_str), 0);
259-
VERIFY_ARE_EQUAL(ret_msg.messge_type(), websocket_message_type::text_message);
259+
VERIFY_ARE_EQUAL(ret_msg.message_type(), websocket_message_type::text_message);
260260
});
261261

262262
// Connect after the client is waiting on a receive task.

0 commit comments

Comments
 (0)