2525#include < net/tcp/packet.hpp>
2626#include < net/tcp/tcp.hpp>
2727#include < net/tcp/tcp_errors.hpp>
28- #include < timers>
29-
3028
3129using namespace net ::tcp;
3230using namespace std ;
@@ -45,7 +43,8 @@ Connection::Connection(TCP& host, port_t local_port, Socket remote) :
4543 writeq(),
4644 bytes_rx_(0 ), bytes_tx_(0 ),
4745 queued_(false ),
48- time_wait_started(0 )
46+ rtx_timer({this , &Connection::rtx_timeout}),
47+ timewait_timer({this , &Connection::timewait_timeout})
4948{
5049 setup_congestion_control ();
5150 setup_default_callbacks ();
@@ -294,8 +293,7 @@ void Connection::limited_tx() {
294293void Connection::writeq_reset () {
295294 debug2 (" <Connection::writeq_reset> Reseting.\n " );
296295 writeq.reset ();
297- if (rtx_timer.active )
298- rtx_stop ();
296+ rtx_timer.stop ();
299297}
300298
301299void Connection::open (bool active) {
@@ -416,7 +414,7 @@ void Connection::transmit(Packet_ptr packet) {
416414 bytes_tx_ += packet->tcp_data_length ();
417415
418416 host_.transmit (packet);
419- if (packet->should_rtx () and !rtx_timer.active ) {
417+ if (packet->should_rtx () and !rtx_timer.is_running () ) {
420418 rtx_start ();
421419 }
422420}
@@ -475,7 +473,7 @@ bool Connection::handle_ack(Packet_ptr in) {
475473 cb.SND .UNA = in->ack ();
476474
477475 // ack everything in rtx queue
478- if (rtx_timer.active )
476+ if (rtx_timer.is_running () )
479477 rtx_ack (in->ack ());
480478
481479 // update cwnd when congestion avoidance?
@@ -690,29 +688,13 @@ void Connection::retransmit() {
690688 so that it will expire after RTO seconds (for the current value
691689 of RTO).
692690 */
693- if (packet->should_rtx () and !rtx_timer.active ) {
691+ if (packet->should_rtx () and !rtx_timer.is_running () ) {
694692 rtx_start ();
695693 }
696694}
697695
698- void Connection::rtx_start () {
699- Expects (!rtx_timer.active );
700-
701- rtx_timer.id = Timers::oneshot (
702- std::chrono::milliseconds ((int ) (rttm.RTO * 1000.0 )),
703- {this , &Connection::rtx_timeout});
704-
705- rtx_timer.active = true ;
706- }
707-
708- void Connection::rtx_stop () {
709- Expects (rtx_timer.active );
710- Timers::stop (rtx_timer.id );
711- rtx_timer.active = false ;
712- }
713-
714696void Connection::rtx_clear () {
715- if (rtx_timer.active ) {
697+ if (rtx_timer.is_running () ) {
716698 rtx_stop ();
717699 debug2 (" <Connection::rtx_clear> Rtx cleared\n " );
718700 }
@@ -738,7 +720,6 @@ void Connection::rtx_clear() {
738720 begins (i.e., after the three-way handshake completes).
739721*/
740722void Connection::rtx_timeout (uint32_t ) {
741- rtx_timer.active = false ;
742723 debug (" <TCP::Connection::RTX@timeout> %s Timed out (%f). FS: %u\n " ,
743724 to_string ().c_str (), flight_size ());
744725
@@ -762,7 +743,7 @@ void Connection::rtx_timeout(uint32_t) {
762743 rttm.RTO = 3.0 ;
763744 }
764745 // timer need to be restarted
765- if (!rtx_timer.active )
746+ if (!rtx_timer.is_running () )
766747 rtx_start ();
767748
768749 /*
@@ -810,33 +791,23 @@ void Connection::set_state(State& state) {
810791}
811792
812793void Connection::timewait_start () {
813- Expects (!timewait_timer.active );
814- auto timeout = 2 * host ().MSL (); // 60 seconds
815-
816- timewait_timer.id = Timers::oneshot (timeout, {this , &Connection::timewait_timeout});
817- timewait_timer.active = true ;
818-
794+ const auto timeout = 2 * host ().MSL (); // 60 seconds
795+ timewait_timer.start (timeout);
819796 debug2 (" <Connection::timewait_start> TimeWait timer [%u] started.\n " , timewait_timer.id );
820797}
821798
822799void Connection::timewait_stop () {
823- Expects (timewait_timer.active );
824- Timers::stop (timewait_timer.id );
825- timewait_timer.active = false ;
826-
800+ timewait_timer.stop ();
827801 debug2 (" <Connection::timewait_stop> TimeWait timer [%u] stopped.\n " , timewait_timer.id );
828802}
829803
830804void Connection::timewait_restart () {
831- if (timewait_timer.active )
832- timewait_stop ();
833- timewait_start ();
805+ const auto timeout = 2 * host ().MSL (); // 60 seconds
806+ timewait_timer.restart (timeout);
834807}
835808
836809void Connection::timewait_timeout (uint32_t ) {
837810 debug (" <Connection> TimeWait timed out, closing.\n " );
838- timewait_timer.active = false ;
839-
840811 signal_close ();
841812}
842813
@@ -853,7 +824,7 @@ void Connection::signal_close() {
853824void Connection::clean_up () {
854825 // clear timers if active
855826 rtx_clear ();
856- if (timewait_timer.active )
827+ if (timewait_timer.is_running () )
857828 timewait_stop ();
858829
859830 // necessary to keep the shared_ptr alive during the whole function after _on_cleanup_ is called
0 commit comments