Skip to content
This repository was archived by the owner on Mar 12, 2021. It is now read-only.

Commit 0715d7a

Browse files
author
moozzyk
committed
HomebrewEvent - replacing pplx::event with a cross platform counterpart
1 parent 7ae3d93 commit 0715d7a

12 files changed

Lines changed: 128 additions & 63 deletions

src/signalrclient/Build/VS/signalrclient.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
<ClInclude Include="..\..\connection_impl.h" />
4141
<ClInclude Include="..\..\constants.h" />
4242
<ClInclude Include="..\..\default_websocket_client.h" />
43+
<ClInclude Include="..\..\event.h" />
4344
<ClInclude Include="..\..\http_sender.h" />
4445
<ClInclude Include="..\..\hub_connection_impl.h" />
4546
<ClInclude Include="..\..\internal_hub_proxy.h" />

src/signalrclient/Build/VS/signalrclient.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@
108108
<ClInclude Include="..\..\case_insensitive_comparison_utils.h">
109109
<Filter>Header Files</Filter>
110110
</ClInclude>
111+
<ClInclude Include="..\..\event.h">
112+
<Filter>Header Files</Filter>
113+
</ClInclude>
111114
</ItemGroup>
112115
<ItemGroup>
113116
<ClCompile Include="..\..\stdafx.cpp">

src/signalrclient/connection_impl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "transport_factory.h"
1313
#include "logger.h"
1414
#include "negotiation_response.h"
15+
#include "event.h"
1516

1617
namespace signalr
1718
{
@@ -68,7 +69,7 @@ namespace signalr
6869

6970
pplx::cancellation_token_source m_disconnect_cts;
7071
std::mutex m_stop_lock;
71-
pplx::event m_start_completed_event;
72+
event m_start_completed_event;
7273
utility::string_t m_connection_token;
7374
utility::string_t m_connection_data;
7475
int m_reconnect_window; // in milliseconds

src/signalrclient/event.h

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
#pragma once
5+
6+
#include <mutex>
7+
8+
namespace signalr
9+
{
10+
class event
11+
{
12+
private:
13+
std::mutex m_lock;
14+
std::condition_variable m_condition;
15+
bool m_signaled;
16+
public:
17+
18+
static const unsigned int timeout_infinite = 0xFFFFFFFF;
19+
20+
event()
21+
: m_signaled(false)
22+
{
23+
}
24+
25+
void set()
26+
{
27+
std::lock_guard<std::mutex> lock(m_lock);
28+
m_signaled = true;
29+
m_condition.notify_all();
30+
}
31+
32+
void reset()
33+
{
34+
std::lock_guard<std::mutex> lock(m_lock);
35+
m_signaled = false;
36+
}
37+
38+
unsigned int wait(unsigned int timeout)
39+
{
40+
std::unique_lock<std::mutex> lock(m_lock);
41+
if (timeout == event::timeout_infinite)
42+
{
43+
m_condition.wait(lock, [this]() { return m_signaled; });
44+
return 0;
45+
}
46+
else
47+
{
48+
std::chrono::milliseconds period(timeout);
49+
auto status = m_condition.wait_for(lock, period, [this]() { return m_signaled; });
50+
_ASSERTE(status == m_signaled);
51+
// Return 0 if the wait completed as a result of signaling the event. Otherwise, return timeout_infinite
52+
return status ? 0 : event::timeout_infinite;
53+
}
54+
}
55+
56+
unsigned int wait()
57+
{
58+
return wait(event::timeout_infinite);
59+
}
60+
};
61+
}

src/signalrclient/transport.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
#pragma once;
4+
#pragma once
55

66
#include "pplx/pplxtasks.h"
77
#include "cpprest/base_uri.h"

test/signalrclient-e2e-tests/connection_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ TEST(connection_tests, send_message)
3030
{
3131
auto conn = std::make_shared<signalr::connection>(url + U("raw-connection"));
3232
auto message = std::make_shared<utility::string_t>();
33-
auto received_event = std::make_shared<pplx::event>();
33+
auto received_event = std::make_shared<signalr::event>();
3434

3535
conn->set_message_received([message, received_event](const utility::string_t& payload)
3636
{
@@ -56,7 +56,7 @@ TEST(connection_tests, send_message_after_connection_restart)
5656
{
5757
auto conn = std::make_shared<signalr::connection>(url + U("raw-connection"));
5858
auto message = std::make_shared<utility::string_t>();
59-
auto received_event = std::make_shared<pplx::event>();
59+
auto received_event = std::make_shared<signalr::event>();
6060

6161
conn->set_message_received([message, received_event](const utility::string_t& payload)
6262
{

test/signalrclient-e2e-tests/hub_connection_tests.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ TEST(hub_connection_tests, connection_status_start_stop_start_reconnect)
1616
{
1717
auto hub_conn = std::make_shared<signalr::hub_connection>(url);
1818
auto weak_hub_conn = std::weak_ptr<signalr::hub_connection>(hub_conn);
19-
auto reconnecting_event = std::make_shared<pplx::event>();
20-
auto reconnected_event = std::make_shared<pplx::event>();
19+
auto reconnecting_event = std::make_shared<signalr::event>();
20+
auto reconnected_event = std::make_shared<signalr::event>();
2121

2222
auto hub_proxy = hub_conn->create_hub_proxy(U("hubConnection"));
2323

@@ -66,7 +66,7 @@ TEST(hub_connection_tests, send_message)
6666
{
6767
auto hub_conn = std::make_shared<signalr::hub_connection>(url + U("custom"), U(""), signalr::trace_level::all, nullptr, false);
6868
auto message = std::make_shared<utility::string_t>();
69-
auto received_event = std::make_shared<pplx::event>();
69+
auto received_event = std::make_shared<signalr::event>();
7070

7171
auto hub_proxy = hub_conn->create_hub_proxy(U("hubConnection"));
7272

@@ -112,7 +112,7 @@ TEST(hub_connection_tests, send_message_after_connection_restart)
112112
{
113113
auto hub_conn = std::make_shared<signalr::hub_connection>(url);
114114
auto message = std::make_shared<utility::string_t>();
115-
auto received_event = std::make_shared<pplx::event>();
115+
auto received_event = std::make_shared<signalr::event>();
116116

117117
auto hub_proxy = hub_conn->create_hub_proxy(U("hubConnection"));
118118

@@ -144,8 +144,8 @@ TEST(hub_connection_tests, send_message_after_reconnect)
144144
{
145145
auto hub_conn = std::make_shared<signalr::hub_connection>(url);
146146
auto message = std::make_shared<utility::string_t>();
147-
auto reconnected_event = std::make_shared<pplx::event>();
148-
auto received_event = std::make_shared<pplx::event>();
147+
auto reconnected_event = std::make_shared<signalr::event>();
148+
auto received_event = std::make_shared<signalr::event>();
149149

150150
auto hub_proxy = hub_conn->create_hub_proxy(U("hubConnection"));
151151

@@ -186,7 +186,7 @@ TEST(hub_connection_tests, send_message_empty_param)
186186
{
187187
auto hub_conn = std::make_shared<signalr::hub_connection>(url);
188188
auto message = std::make_shared<utility::string_t>();
189-
auto received_event = std::make_shared<pplx::event>();
189+
auto received_event = std::make_shared<signalr::event>();
190190

191191
auto hub_proxy = hub_conn->create_hub_proxy(U("hubConnection"));
192192

@@ -211,7 +211,7 @@ TEST(hub_connection_tests, send_message_primitive_params)
211211
{
212212
auto hub_conn = std::make_shared<signalr::hub_connection>(url);
213213
auto message = std::make_shared<utility::string_t>();
214-
auto received_event = std::make_shared<pplx::event>();
214+
auto received_event = std::make_shared<signalr::event>();
215215

216216
auto hub_proxy = hub_conn->create_hub_proxy(U("hubConnection"));
217217

@@ -249,7 +249,7 @@ TEST(hub_connection_tests, send_message_complex_type)
249249
{
250250
auto hub_conn = std::make_shared<signalr::hub_connection>(url);
251251
auto message = std::make_shared<utility::string_t>();
252-
auto received_event = std::make_shared<pplx::event>();
252+
auto received_event = std::make_shared<signalr::event>();
253253

254254
auto hub_proxy = hub_conn->create_hub_proxy(U("hubConnection"));
255255

test/signalrclient-e2e-tests/stdafx.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@
88
#define NOMINMAX
99
#endif
1010

11-
#include "gtest/gtest.h"
11+
#include "gtest/gtest.h"
12+
#include "../../src/signalrclient/event.h"

test/signalrclienttests/connection_impl_tests.cpp

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ TEST(connection_impl_set_message_received, callback_invoked_when_message_receive
463463

464464
auto message = std::make_shared<utility::string_t>();
465465

466-
auto message_received_event = std::make_shared<pplx::event>();
466+
auto message_received_event = std::make_shared<event>();
467467
connection->set_message_received_string([message, message_received_event](const utility::string_t &m){
468468
if (m == _XPLATSTR("\"Test\""))
469469
{
@@ -505,7 +505,7 @@ TEST(connection_impl_set_message_received, exception_from_callback_caught_and_lo
505505
std::shared_ptr<log_writer> writer(std::make_shared<memory_log_writer>());
506506
auto connection = create_connection(websocket_client, writer, trace_level::errors);
507507

508-
auto message_received_event = std::make_shared<pplx::event>();
508+
auto message_received_event = std::make_shared<event>();
509509
connection->set_message_received_string([message_received_event](const utility::string_t &m){
510510
if (m == _XPLATSTR("\"throw\""))
511511
{
@@ -551,7 +551,7 @@ TEST(connection_impl_set_message_received, non_std_exception_from_callback_caugh
551551
std::shared_ptr<log_writer> writer(std::make_shared<memory_log_writer>());
552552
auto connection = create_connection(websocket_client, writer, trace_level::errors);
553553

554-
auto message_received_event = std::make_shared<pplx::event>();
554+
auto message_received_event = std::make_shared<event>();
555555
connection->set_message_received_string([message_received_event](const utility::string_t &m)
556556
{
557557
if (m == _XPLATSTR("\"throw\""))
@@ -598,7 +598,7 @@ TEST(connection_impl_set_message_received, error_logged_for_malformed_payload)
598598
std::shared_ptr<log_writer> writer(std::make_shared<memory_log_writer>());
599599
auto connection = create_connection(websocket_client, writer, trace_level::errors);
600600

601-
auto message_received_event = std::make_shared<pplx::event>();
601+
auto message_received_event = std::make_shared<event>();
602602
connection->set_message_received_string([message_received_event](const utility::string_t&)
603603
{
604604
// this is called only once because we have just one response with a message
@@ -638,7 +638,7 @@ TEST(connection_impl_set_message_received, unexpected_responses_logged)
638638
std::shared_ptr<log_writer> writer(std::make_shared<memory_log_writer>());
639639
auto connection = create_connection(websocket_client, writer, trace_level::info);
640640

641-
auto message_received_event = std::make_shared<pplx::event>();
641+
auto message_received_event = std::make_shared<event>();
642642
connection->set_message_received_string([message_received_event](const utility::string_t&)
643643
{
644644
// this is called only once because we have just one response with a message
@@ -733,7 +733,7 @@ TEST(connection_impl_stop, stopping_disconnected_connection_is_no_op)
733733

734734
TEST(connection_impl_stop, stopping_disconnecting_connection_returns_cancelled_task)
735735
{
736-
pplx::event close_event;
736+
event close_event;
737737
auto writer = std::shared_ptr<log_writer>{std::make_shared<memory_log_writer>()};
738738

739739
auto websocket_client = create_test_websocket_client(
@@ -870,7 +870,7 @@ TEST(connection_impl_stop, dtor_stops_the_connection)
870870

871871
TEST(connection_impl_stop, stop_cancels_ongoing_start_request)
872872
{
873-
auto disconnect_completed_event = std::make_shared<pplx::event>();
873+
auto disconnect_completed_event = std::make_shared<event>();
874874

875875
auto websocket_client = create_test_websocket_client(
876876
/* receive function */ [disconnect_completed_event]()
@@ -1146,7 +1146,7 @@ TEST(connection_impl_reconnect, can_reconnect)
11461146

11471147
auto connection = create_connection(websocket_client);
11481148
connection->set_reconnect_delay(100);
1149-
auto reconnected_event = std::make_shared<pplx::event>();
1149+
auto reconnected_event = std::make_shared<event>();
11501150
connection->set_reconnected([reconnected_event](){ reconnected_event->set(); });
11511151
connection->start();
11521152

@@ -1178,7 +1178,7 @@ TEST(connection_impl_reconnect, successful_reconnect_state_changes)
11781178
std::shared_ptr<log_writer> writer(std::make_shared<memory_log_writer>());
11791179
auto connection = create_connection(websocket_client, writer, trace_level::state_changes);
11801180
connection->set_reconnect_delay(100);
1181-
auto reconnected_event = std::make_shared<pplx::event>();
1181+
auto reconnected_event = std::make_shared<event>();
11821182
connection->set_reconnected([reconnected_event](){ reconnected_event->set(); });
11831183
connection->start();
11841184

@@ -1244,7 +1244,7 @@ TEST(connection_impl_reconnect, connection_stopped_if_reconnecting_failed)
12441244
connection_impl::create(create_uri(), _XPLATSTR(""), trace_level::state_changes,
12451245
writer, std::move(web_request_factory), std::make_unique<test_transport_factory>(websocket_client));
12461246

1247-
auto disconnected_event = std::make_shared<pplx::event>();
1247+
auto disconnected_event = std::make_shared<event>();
12481248
connection->set_disconnected([disconnected_event](){ disconnected_event->set(); });
12491249
connection->set_reconnect_delay(100);
12501250
connection->start();
@@ -1264,7 +1264,7 @@ TEST(connection_impl_reconnect, connection_stopped_if_reconnecting_failed)
12641264

12651265
TEST(connection_impl_reconnect, reconnect_works_if_connection_dropped_during_after_init_and_before_start_successfully_completed)
12661266
{
1267-
auto connection_dropped_event = std::make_shared<pplx::event>();
1267+
auto connection_dropped_event = std::make_shared<event>();
12681268

12691269
int call_number = -1;
12701270
auto websocket_client = create_test_websocket_client(
@@ -1291,7 +1291,7 @@ TEST(connection_impl_reconnect, reconnect_works_if_connection_dropped_during_aft
12911291
std::shared_ptr<log_writer> writer(std::make_shared<memory_log_writer>());
12921292
auto connection = create_connection(websocket_client, writer, trace_level::state_changes);
12931293
connection->set_reconnect_delay(100);
1294-
auto reconnected_event = std::make_shared<pplx::event>();
1294+
auto reconnected_event = std::make_shared<event>();
12951295
connection->set_reconnected([reconnected_event](){ reconnected_event->set(); });
12961296

12971297
connection->start();
@@ -1308,7 +1308,7 @@ TEST(connection_impl_reconnect, reconnect_works_if_connection_dropped_during_aft
13081308

13091309
TEST(connection_impl_reconnect, reconnect_cancelled_if_connection_dropped_during_start_and_start_failed)
13101310
{
1311-
auto connection_dropped_event = std::make_shared<pplx::event>();
1311+
auto connection_dropped_event = std::make_shared<event>();
13121312

13131313
auto web_request_factory = std::make_unique<test_web_request_factory>([&connection_dropped_event](const web::uri& url)
13141314
{
@@ -1428,7 +1428,7 @@ TEST(connection_impl_reconnect, reconnect_cancelled_when_connection_being_stoppe
14281428
std::shared_ptr<log_writer> writer(std::make_shared<memory_log_writer>());
14291429
auto connection = create_connection(websocket_client, writer, trace_level::all);
14301430
connection->set_reconnect_delay(100);
1431-
pplx::event reconnecting_event{};
1431+
event reconnecting_event{};
14321432
connection->set_reconnecting([&reconnecting_event](){ reconnecting_event.set(); });
14331433

14341434
connection->start().then([&connection_started](){ connection_started = true; });
@@ -1502,7 +1502,7 @@ TEST(connection_impl_reconnect, reconnect_cancelled_if_connection_goes_out_of_sc
15021502
{
15031503
auto connection = create_connection(websocket_client, writer, trace_level::state_changes);
15041504
connection->set_reconnect_delay(100);
1505-
pplx::event reconnecting_event{};
1505+
event reconnecting_event{};
15061506
connection->set_reconnecting([&reconnecting_event](){ reconnecting_event.set(); });
15071507

15081508
connection->start().then([&connection_started](){ connection_started = true; });
@@ -1552,7 +1552,7 @@ TEST(connection_impl_reconnect, std_exception_for_reconnected_reconnecting_callb
15521552
auto connection = create_connection(websocket_client, writer, trace_level::errors);
15531553
connection->set_reconnect_delay(100);
15541554
connection->set_reconnecting([](){ throw std::runtime_error("exception from reconnecting"); });
1555-
auto reconnected_event = std::make_shared<pplx::event>();
1555+
auto reconnected_event = std::make_shared<event>();
15561556
connection->set_reconnected([reconnected_event]()
15571557
{
15581558
reconnected_event->set();
@@ -1599,7 +1599,7 @@ TEST(connection_impl_reconnect, exception_for_reconnected_reconnecting_callback_
15991599
auto connection = create_connection(websocket_client, writer, trace_level::errors);
16001600
connection->set_reconnect_delay(100);
16011601
connection->set_reconnecting([](){ throw 42; });
1602-
auto reconnected_event = std::make_shared<pplx::event>();
1602+
auto reconnected_event = std::make_shared<event>();
16031603
connection->set_reconnected([reconnected_event]()
16041604
{
16051605
reconnected_event->set();
@@ -1672,7 +1672,7 @@ TEST(connection_impl_reconnect, can_stop_connection_from_reconnecting_event)
16721672
connection_impl::create(create_uri(), _XPLATSTR(""), trace_level::state_changes,
16731673
writer, std::move(web_request_factory), std::make_unique<test_transport_factory>(websocket_client));
16741674

1675-
auto stop_event = std::make_shared<pplx::event>();
1675+
auto stop_event = std::make_shared<event>();
16761676
connection->set_reconnecting([&connection, stop_event]()
16771677
{
16781678
connection->stop()
@@ -1756,7 +1756,7 @@ TEST(connection_impl_reconnect, current_reconnect_cancelled_if_another_reconnect
17561756
}
17571757
});
17581758

1759-
pplx::event reconnected_event;
1759+
event reconnected_event;
17601760
connection->set_reconnected([&reconnected_event]()
17611761
{
17621762
reconnected_event.set();

0 commit comments

Comments
 (0)