Sender需要负责写入所有Lab2中receiver需要读取的数据:the sequence number, the SYN flag, the payload, 和 the FIN flag. 需要读取receiver写入的数据:the ackno and the window size 即下图中,写入蓝色部分,读取红色部分。
//! \param[in] capacity the capacity of the outgoing byte stream //! \param[in] retx_timeout the initial amount of time to wait before retransmitting the oldest outstanding segment //! \param[in] fixed_isn the Initial Sequence Number to use, if set (otherwise uses a random ISN) TCPSender::TCPSender(constsize_t capacity, constuint16_t retx_timeout, const std::optional<WrappingInt32> fixed_isn) : _isn(fixed_isn.value_or(WrappingInt32{random_device()()})) , _initial_retransmission_timeout{retx_timeout} , _stream(capacity) { _timer.set_retrans_timeout(_initial_retransmission_timeout); }
if (abs_ackno > _next_seqno) { // Impossible ack. return; }
_window_size = window_size; bool ack = false;
// Try to remove acked outstanding segments. while (!_outstanding_segments.empty()) { auto &p = _outstanding_segments.front(); if (p.first + p.second.length_in_sequence_space() - 1 < abs_ackno) { _bytes_in_flight -= _outstanding_segments.front().second.length_in_sequence_space(); _outstanding_segments.pop(); ack = true; } else { break; } }
if (ack) { _timer.reset(_initial_retransmission_timeout); } fill_window(); }
//! \param[in] ms_since_last_tick the number of milliseconds since the last call to this method voidTCPSender::tick(constsize_t ms_since_last_tick){ if (_timer.check_timeout(ms_since_last_tick, _window_size != 0) && !_outstanding_segments.empty()) { _segments_out.push(_outstanding_segments.front().second); } }