Skip to content

Commit 812ea7d

Browse files
tcp: Replaced timer structs with Timer class 58d0345
1 parent b8eea3f commit 812ea7d

3 files changed

Lines changed: 27 additions & 64 deletions

File tree

api/net/tcp/connection.hpp

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "tcp_errors.hpp"
2727
#include "write_queue.hpp"
2828
#include <delegate>
29+
#include <util/timer.hpp>
2930

3031
namespace net {
3132
class TCP;
@@ -477,20 +478,11 @@ class Connection : public std::enable_shared_from_this<Connection> {
477478
/** State if connection is in TCP write queue or not. */
478479
bool queued_;
479480

480-
/** When time-wait timer was started. Used in start_time_wait_timeout */
481-
uint64_t time_wait_started;
482-
483481
/** Retransmission timer */
484-
struct {
485-
uint32_t id;
486-
bool active = false;
487-
} rtx_timer;
482+
Timer rtx_timer;
488483

489484
/** Time Wait timeout timer */
490-
struct {
491-
uint32_t id;
492-
bool active = false;
493-
} timewait_timer;
485+
Timer timewait_timer;
494486

495487
/** Number of retransmission attempts on the packet first in RT-queue */
496488
size_t rtx_attempt_ = 0;
@@ -853,20 +845,20 @@ class Connection : public std::enable_shared_from_this<Connection> {
853845
/*
854846
Start retransmission timer.
855847
*/
856-
void rtx_start();
848+
void rtx_start()
849+
{ rtx_timer.start(rttm.rto_ms()); }
857850

858851
/*
859852
Stop retransmission timer.
860853
*/
861-
void rtx_stop();
854+
void rtx_stop()
855+
{ rtx_timer.stop(); }
862856

863857
/*
864858
Restart retransmission timer.
865859
*/
866-
void rtx_reset() {
867-
rtx_stop();
868-
rtx_start();
869-
}
860+
void rtx_reset()
861+
{ rtx_timer.restart(rttm.rto_ms()); }
870862

871863
/*
872864
Retransmission timeout limit reached

src/net/tcp/connection.cpp

Lines changed: 14 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
#include <net/tcp/packet.hpp>
2626
#include <net/tcp/tcp.hpp>
2727
#include <net/tcp/tcp_errors.hpp>
28-
#include <timers>
29-
3028

3129
using namespace net::tcp;
3230
using 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() {
294293
void 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

301299
void 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-
714696
void 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
*/
740722
void 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

812793
void 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

822799
void 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

830804
void 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

836809
void 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() {
853824
void 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

src/net/tcp/connection_states.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,7 @@ State::Result Connection::SynSent::handle(Connection& tcp, Packet_ptr in) {
902902
tcb.IRS = in->seq();
903903
tcb.SND.UNA = in->ack();
904904

905-
if(tcp.rtx_timer.active)
905+
if(tcp.rtx_timer.is_running())
906906
tcp.rtx_stop();
907907

908908
// (our SYN has been ACKed)
@@ -1013,7 +1013,7 @@ State::Result Connection::SynReceived::handle(Connection& tcp, Packet_ptr in) {
10131013

10141014
// Taken from acknowledge (without congestion control)
10151015
tcb.SND.UNA = in->ack();
1016-
if(tcp.rtx_timer.active)
1016+
if(tcp.rtx_timer.is_running())
10171017
tcp.rtx_stop();
10181018

10191019
tcp.signal_connect(); // NOTE: User callback
@@ -1150,7 +1150,7 @@ State::Result Connection::FinWait1::handle(Connection& tcp, Packet_ptr in) {
11501150
if(in->ack() == tcp.tcb().SND.NXT) {
11511151
// TODO: I guess or FIN is ACK'ed..?
11521152
tcp.set_state(TimeWait::instance());
1153-
if(tcp.rtx_timer.active)
1153+
if(tcp.rtx_timer.is_running())
11541154
tcp.rtx_stop();
11551155
tcp.timewait_start();
11561156
} else {
@@ -1197,7 +1197,7 @@ State::Result Connection::FinWait2::handle(Connection& tcp, Packet_ptr in) {
11971197
Start the time-wait timer, turn off the other timers.
11981198
*/
11991199
tcp.set_state(Connection::TimeWait::instance());
1200-
if(tcp.rtx_timer.active)
1200+
if(tcp.rtx_timer.is_running())
12011201
tcp.rtx_stop();
12021202
tcp.timewait_start();
12031203
}

0 commit comments

Comments
 (0)