Skip to content

Commit 5a0c1ec

Browse files
authored
core: re-enable ping timeout behaviour for 3x branch (#351)
Heartbeat mechanism was not properly working as intended. For more info see: https://socket.io/docs/v4/how-it-works/#disconnection-detection
1 parent c733c3e commit 5a0c1ec

File tree

2 files changed

+22
-37
lines changed

2 files changed

+22
-37
lines changed

src/internal/sio_client_impl.cpp

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -303,37 +303,14 @@ namespace sio
303303
}
304304
}
305305

306-
void client_impl::ping(const asio::error_code& ec)
307-
{
308-
if(ec || m_con.expired())
309-
{
310-
if (ec != asio::error::operation_aborted)
311-
LOG("ping exit,con is expired?"<<m_con.expired()<<",ec:"<<ec.message()<<endl);
312-
return;
313-
}
314-
packet p(packet::frame_ping);
315-
m_packet_mgr.encode(p, [&](bool /*isBin*/,shared_ptr<const string> payload)
316-
{
317-
lib::error_code ec;
318-
this->m_client.send(this->m_con, *payload, frame::opcode::text, ec);
319-
});
320-
if(!m_ping_timeout_timer)
321-
{
322-
m_ping_timeout_timer.reset(new asio::steady_timer(m_client.get_io_service()));
323-
std::error_code timeout_ec;
324-
m_ping_timeout_timer->expires_from_now(milliseconds(m_ping_timeout), timeout_ec);
325-
m_ping_timeout_timer->async_wait(std::bind(&client_impl::timeout_pong, this, std::placeholders::_1));
326-
}
327-
}
328-
329-
void client_impl::timeout_pong(const asio::error_code &ec)
306+
void client_impl::timeout_ping(const asio::error_code &ec)
330307
{
331308
if(ec)
332309
{
333310
return;
334311
}
335-
LOG("Pong timeout"<<endl);
336-
m_client.get_io_service().dispatch(std::bind(&client_impl::close_impl, this,close::status::policy_violation,"Pong timeout"));
312+
LOG("Ping timeout"<<endl);
313+
m_client.get_io_service().dispatch(std::bind(&client_impl::close_impl, this,close::status::policy_violation,"Ping timeout"));
337314
}
338315

339316
void client_impl::timeout_reconnect(asio::error_code const& ec)
@@ -484,11 +461,6 @@ namespace sio
484461

485462
void client_impl::on_message(connection_hdl, client_type::message_ptr msg)
486463
{
487-
if (m_ping_timeout_timer) {
488-
asio::error_code ec;
489-
m_ping_timeout_timer->expires_from_now(milliseconds(m_ping_timeout),ec);
490-
m_ping_timeout_timer->async_wait(std::bind(&client_impl::timeout_pong, this, std::placeholders::_1));
491-
}
492464
// Parse the incoming message according to socket.IO rules
493465
m_packet_mgr.put_payload(msg->get_payload());
494466
}
@@ -525,6 +497,9 @@ namespace sio
525497
m_ping_timeout = 60000;
526498
}
527499

500+
// Start ping timeout
501+
update_ping_timeout_timer();
502+
528503
return;
529504
}
530505
failed:
@@ -534,17 +509,15 @@ namespace sio
534509

535510
void client_impl::on_ping()
536511
{
512+
// Reply with pong packet.
537513
packet p(packet::frame_pong);
538514
m_packet_mgr.encode(p, [&](bool /*isBin*/,shared_ptr<const string> payload)
539515
{
540516
this->m_client.send(this->m_con, *payload, frame::opcode::text);
541517
});
542518

543-
if(m_ping_timeout_timer)
544-
{
545-
m_ping_timeout_timer->cancel();
546-
m_ping_timeout_timer.reset();
547-
}
519+
// Reset the ping timeout.
520+
update_ping_timeout_timer();
548521
}
549522

550523
void client_impl::on_decode(packet const& p)
@@ -589,6 +562,16 @@ namespace sio
589562
m_ping_timeout_timer.reset();
590563
}
591564
}
565+
566+
void client_impl::update_ping_timeout_timer() {
567+
if (!m_ping_timeout_timer) {
568+
m_ping_timeout_timer = std::unique_ptr<asio::steady_timer>(new asio::steady_timer(get_io_service()));
569+
}
570+
571+
asio::error_code ec;
572+
m_ping_timeout_timer->expires_from_now(milliseconds(m_ping_interval + m_ping_timeout), ec);
573+
m_ping_timeout_timer->async_wait(std::bind(&client_impl::timeout_ping, this, std::placeholders::_1));
574+
}
592575

593576
void client_impl::reset_states()
594577
{

src/internal/sio_client_impl.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ namespace sio
151151

152152
void ping(const asio::error_code& ec);
153153

154-
void timeout_pong(const asio::error_code& ec);
154+
void timeout_ping(const asio::error_code& ec);
155155

156156
void timeout_reconnect(asio::error_code const& ec);
157157

@@ -181,6 +181,8 @@ namespace sio
181181
void reset_states();
182182

183183
void clear_timers();
184+
185+
void update_ping_timeout_timer();
184186

185187
#if SIO_TLS
186188
typedef websocketpp::lib::shared_ptr<asio::ssl::context> context_ptr;

0 commit comments

Comments
 (0)