@@ -303,37 +303,14 @@ namespace sio
303
303
}
304
304
}
305
305
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)
330
307
{
331
308
if (ec)
332
309
{
333
310
return ;
334
311
}
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" ));
337
314
}
338
315
339
316
void client_impl::timeout_reconnect (asio::error_code const & ec)
@@ -484,11 +461,6 @@ namespace sio
484
461
485
462
void client_impl::on_message (connection_hdl, client_type::message_ptr msg)
486
463
{
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
- }
492
464
// Parse the incoming message according to socket.IO rules
493
465
m_packet_mgr.put_payload (msg->get_payload ());
494
466
}
@@ -525,6 +497,9 @@ namespace sio
525
497
m_ping_timeout = 60000 ;
526
498
}
527
499
500
+ // Start ping timeout
501
+ update_ping_timeout_timer ();
502
+
528
503
return ;
529
504
}
530
505
failed:
@@ -534,17 +509,15 @@ namespace sio
534
509
535
510
void client_impl::on_ping ()
536
511
{
512
+ // Reply with pong packet.
537
513
packet p (packet::frame_pong);
538
514
m_packet_mgr.encode (p, [&](bool /* isBin*/ ,shared_ptr<const string> payload)
539
515
{
540
516
this ->m_client .send (this ->m_con , *payload, frame::opcode::text);
541
517
});
542
518
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 ();
548
521
}
549
522
550
523
void client_impl::on_decode (packet const & p)
@@ -589,6 +562,16 @@ namespace sio
589
562
m_ping_timeout_timer.reset ();
590
563
}
591
564
}
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
+ }
592
575
593
576
void client_impl::reset_states ()
594
577
{
0 commit comments