Skip to content

Commit 50f61b3

Browse files
committed
Removing managing of asio io_service in websocket_client since websocketpp now can manage for us.
1 parent 7ee120d commit 50f61b3

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

Release/src/websockets/client/ws_client.cpp

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ class wspp_client : public _websocket_client_impl, public std::enable_shared_fro
117117
public:
118118
wspp_client(websocket_client_config config) :
119119
_websocket_client_impl(std::move(config)),
120-
m_work(utility::details::make_unique<boost::asio::io_service::work>(m_service)),
121120
m_state(CREATED),
122121
m_num_sends(0)
123122
#if defined(__APPLE__) || defined(ANDROID) || defined(_MS_WINDOWS)
@@ -130,9 +129,6 @@ class wspp_client : public _websocket_client_impl, public std::enable_shared_fro
130129
_ASSERTE(m_state < DESTROYED);
131130
std::unique_lock<std::mutex> lock(m_receive_queue_lock);
132131

133-
// First, trigger the boost::io_service to spin down
134-
m_work.reset();
135-
136132
// Now, what states could we be in?
137133
switch (m_state) {
138134
case DESTROYED:
@@ -164,14 +160,33 @@ class wspp_client : public _websocket_client_impl, public std::enable_shared_fro
164160
}
165161

166162
// We have released the lock on all paths here
167-
m_service.stop();
168-
if (m_thread.joinable())
169-
m_thread.join();
163+
if (m_client)
164+
{
165+
if (m_client->is_tls_client())
166+
{
167+
stop_client_impl<websocketpp::config::asio_tls_client>();
168+
}
169+
else
170+
{
171+
stop_client_impl<websocketpp::config::asio_client>();
172+
}
173+
if (m_thread.joinable())
174+
{
175+
m_thread.join();
176+
}
177+
}
170178

171179
// At this point, there should be no more references to me.
172180
m_state = DESTROYED;
173181
}
174182

183+
template <typename WebsocketConfigType>
184+
void stop_client_impl()
185+
{
186+
auto &client = m_client->client<WebsocketConfigType>();
187+
client.stop_perpetual();
188+
}
189+
175190
pplx::task<void> connect()
176191
{
177192
if (m_uri.scheme() == U("wss"))
@@ -227,7 +242,8 @@ class wspp_client : public _websocket_client_impl, public std::enable_shared_fro
227242

228243
client.clear_access_channels(websocketpp::log::alevel::all);
229244
client.clear_error_channels(websocketpp::log::alevel::all);
230-
client.init_asio(&m_service);
245+
client.init_asio();
246+
client.start_perpetual();
231247

232248
_ASSERTE(m_state == CREATED);
233249
client.set_open_handler([this](websocketpp::connection_hdl)
@@ -343,13 +359,7 @@ class wspp_client : public _websocket_client_impl, public std::enable_shared_fro
343359

344360
m_state = CONNECTING;
345361
client.connect(con);
346-
347-
m_thread = std::thread([this]()
348-
{
349-
m_service.run();
350-
_ASSERTE(m_state == CLOSED);
351-
});
352-
362+
m_thread = std::thread(&websocketpp::client<WebsocketConfigType>::run, &client);
353363
return pplx::create_task(m_connect_tce);
354364
}
355365

@@ -638,9 +648,6 @@ class wspp_client : public _websocket_client_impl, public std::enable_shared_fro
638648
}
639649

640650
private:
641-
// The m_service should be the first member (and therefore the last to be destroyed)
642-
boost::asio::io_service m_service;
643-
std::unique_ptr<boost::asio::io_service::work> m_work;
644651
std::thread m_thread;
645652

646653
// Perform type erasure to set the websocketpp client in use at runtime

0 commit comments

Comments
 (0)