Skip to content

Commit 5ab0135

Browse files
committed
wip
1 parent 58d10dd commit 5ab0135

File tree

4 files changed

+153
-144
lines changed

4 files changed

+153
-144
lines changed

include/signalrclient/hub_connection_builder.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
namespace signalr
1414
{
15-
class hub_connection_builder
15+
class hub_connection_builder final
1616
{
1717
public:
1818
SIGNALRCLIENT_API static hub_connection_builder create(const std::string& url);
@@ -27,19 +27,17 @@ namespace signalr
2727

2828
SIGNALRCLIENT_API hub_connection_builder& operator=(const hub_connection_builder&);
2929

30-
SIGNALRCLIENT_API hub_connection_builder& with_logging(std::shared_ptr<log_writer> logger, trace_level logLevel);
30+
SIGNALRCLIENT_API hub_connection_builder& with_logging(std::shared_ptr<log_writer> logger, trace_level log_level);
3131

32-
SIGNALRCLIENT_API hub_connection_builder& with_websocket_factory(std::function<std::shared_ptr<websocket_client>(const signalr_client_config&)> factory);
32+
SIGNALRCLIENT_API hub_connection_builder& with_websocket_factory(std::function<std::shared_ptr<websocket_client>(const signalr_client_config&)> websocket_factory);
3333

3434
SIGNALRCLIENT_API hub_connection_builder& with_http_client_factory(std::function<std::shared_ptr<http_client>(const signalr_client_config&)> http_client_factory);
3535

3636
SIGNALRCLIENT_API hub_connection_builder& skip_negotiation(bool skip = true);
3737

38-
#ifdef USE_MSGPACK
3938
SIGNALRCLIENT_API hub_connection_builder& with_messagepack_hub_protocol();
40-
#endif
4139

42-
SIGNALRCLIENT_API hub_connection build();
40+
SIGNALRCLIENT_API std::unique_ptr<hub_connection> build();
4341
private:
4442
hub_connection_builder(const std::string& url);
4543

@@ -50,5 +48,7 @@ namespace signalr
5048
std::function<std::shared_ptr<http_client>(const signalr_client_config&)> m_http_client_factory;
5149
bool m_skip_negotiation = false;
5250
bool m_use_messagepack = false;
51+
52+
bool m_built = false;
5353
};
5454
}

src/signalrclient/hub_connection_builder.cpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,17 @@ namespace signalr
5151
hub_connection_builder::~hub_connection_builder()
5252
{}
5353

54-
hub_connection_builder& hub_connection_builder::with_logging(std::shared_ptr<log_writer> logger, trace_level logLevel)
54+
hub_connection_builder& hub_connection_builder::with_logging(std::shared_ptr<log_writer> logger, trace_level log_level)
5555
{
5656
m_logger = logger;
57-
m_log_level = logLevel;
57+
m_log_level = log_level;
5858

5959
return *this;
6060
}
6161

62-
hub_connection_builder& hub_connection_builder::with_websocket_factory(std::function<std::shared_ptr<websocket_client>(const signalr_client_config&)> factory)
62+
hub_connection_builder& hub_connection_builder::with_websocket_factory(std::function<std::shared_ptr<websocket_client>(const signalr_client_config&)> websocket_factory)
6363
{
64-
m_websocket_factory = factory;
64+
m_websocket_factory = websocket_factory;
6565

6666
return *this;
6767
}
@@ -80,15 +80,17 @@ namespace signalr
8080
return *this;
8181
}
8282

83-
#ifdef USE_MSGPACK
8483
hub_connection_builder& hub_connection_builder::with_messagepack_hub_protocol()
8584
{
85+
#ifdef USE_MSGPACK
8686
m_use_messagepack = true;
8787
return *this;
88-
}
88+
#else
89+
throw std::runtime_error("library was not built with messagepack support. Define USE_MSGPACK when building.");
8990
#endif
91+
}
9092

91-
hub_connection hub_connection_builder::build()
93+
std::unique_ptr<hub_connection> hub_connection_builder::build()
9294
{
9395
#ifndef USE_CPPRESTSDK
9496
if (m_http_client_factory == nullptr)
@@ -115,6 +117,13 @@ namespace signalr
115117
hub_protocol = std::unique_ptr<json_hub_protocol>(new json_hub_protocol());
116118
}
117119

118-
return hub_connection(m_url, std::move(hub_protocol), m_log_level, m_logger, m_http_client_factory, m_websocket_factory, m_skip_negotiation);
120+
if (m_built)
121+
{
122+
throw std::runtime_error("calling build() more than once is not supported.");
123+
}
124+
125+
m_built = true;
126+
127+
return std::unique_ptr<hub_connection>(new hub_connection(m_url, std::move(hub_protocol), m_log_level, m_logger, m_http_client_factory, m_websocket_factory, m_skip_negotiation));
119128
}
120129
}

test/signalrclienttests/connection_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1519,7 +1519,7 @@ TEST(connection_impl_stop, dtor_stops_the_connection)
15191519
// The connection_impl will be destroyed when the last reference to shared_ptr holding is released. This can happen
15201520
// on a different thread in which case the dtor will be invoked on a different thread so we need to wait for this
15211521
// to happen and if it does not the test will fail
1522-
for (int wait_time_ms = 5; wait_time_ms < 6000 && memory_writer->get_log_entries().size() < 6; wait_time_ms <<= 1)
1522+
for (int wait_time_ms = 5; wait_time_ms < 6000 && memory_writer->get_log_entries().size() < 12; wait_time_ms <<= 1)
15231523
{
15241524
std::this_thread::sleep_for(std::chrono::milliseconds(wait_time_ms));
15251525
}

0 commit comments

Comments
 (0)