当前位置:网站首页>[Stanford Jiwang cs144 project] lab3: tcpsender
[Stanford Jiwang cs144 project] lab3: tcpsender
2022-07-07 07:43:00 【Altair_ alpha】
In this section TCP The sender in the protocol TCPSender The implementation of the .Sender Have an input ByteStream, On behalf of the data provided by the user to be sent , and Sender Be responsible for assembling it into TCP Packet and send it out . So called sending , That is to put the data package push To a queue _segments_out Then you can , The next section realizes TCPConnection class ( That is to say Sender and Receiver Owner ) It will be responsible for taking packets from the queue and actually sending them .
Except for the guarantee TCPSegment Data in the 、 Serial number and SYN,FIN Wait until the logo is set correctly ,Sender There are two main issues to consider . First, the receiving window ,Receiver The amount of data that can be received at one time is limited , Will pass Header in Window Size Field to Sender Dynamic feedback of this information .Sender It should be ensured that ( That is, it has been sent and has not been confirmed to receive ) The amount of data is less than or equal to this window .
Second, data retransmission , Because the data packet may be lost during transmission ,Sender You cannot discard a packet immediately after sending it , Instead, it is placed in the temporary storage area ( be called outstanding data), If you haven't received it within a period of time Receiver Confirmation signal of ( adopt ackno) You need to retransmit . The handout stipulates that it will be true TCP A series of rules after the agreement is slightly simplified are as follows :
- Perception of time . In order to ensure the certainty of program testing ,Sender The implementation does not actively call any actual time API, Instead, it passively invokes its
tickThe function perceives the passage of time , The parameter of this function is from the last calltickThe number of milliseconds elapsed after . - The concept of timer . There needs to be a Timer, It can be activated , Triggered after a set time interval , It can also be stopped manually . Because the perception of time is passive , therefore Timer It can only be in
tickFunction . - Sender Accept an initial Retransmission timeout (retransmission timeout, RTO) Parameters , This initial parameter is fixed , At the same time, there is a current RTO Parameters . Every time a packet is sent , If Timer Not activated , Start and make it current RTO Triggered in milliseconds . When all data in the staging area is ack when , stop it Timer.
- When Timer stay
tickWhen triggered in , Will not be completely ack Of One of the earliest ( That is, the one with the smallest serial number ) Temporary packet retransmission . meanwhile , If the window size is not 0, Record the number of consecutive retransmissions , This information will be implemented in the next section TCPConnection Used to determine whether to end the connection , and Make current RTO Double ( Index retreat ). Then according to the current RTO( Probably just updated ) start-up Timer. - When receiving a request for newly sent data ack when , Will the current RTO Reset to initial RTO, And set the number of consecutive retransmissions to zero . If the staging area is not empty , start-up Timer.
Implementation , Mainly Timer + Three main functions . Refer to the code of many students on the Internet Timer The function is put in Sender In the class implementation , I still have a separate class here as follows :
// Helper class to determine whether a given timeout has reached (i.e., expired) since started.
// It won't emit any signal but provide accessor for caller to check its state
// Also the class won't call any system time function but rely on its update() called to
// know time elapsed and whether timeout is reached.
class Timer {
public:
// start the timer with given timeout.
// call start() on a started timer acts like reset() called first
void start(unsigned int timeout);
// update the timer with info of time elapsed since start/last update
void update(unsigned int time_elapsed);
// reset(stop) the timer
void reset();
// check whether the timer is active
bool active() const {
return _active; }
// check whether the timer has timed out (if the timer is inactive, return value is false)
bool expired() const {
return _active && _expired; }
private:
bool _active{
false};
bool _expired{
false};
unsigned int _current_time{
0};
unsigned int _timeout{
0};
};
One of the main functions : void ack_received(const WrappingInt32 ackno, const uint16_t window_size). The window size is determined by the members of a class _window_size Record updates , This parameter is used in the following sending function .ackno the unwrap Then compare with the packet number in the temporary storage area one by one , Remove the completely confirmed packets ( Note that the packet number in the staging area can be used to order from small to large ). If you can remove , Reset by rule RTO And retransmission count . If the staging area is empty after removal , take Timer stop it . Because the temporary storage area may be vacated , Last call fill_window( See below ) Try sending a new packet . The code is as follows :
void TCPSender::ack_received(const WrappingInt32 ackno, const uint16_t window_size) {
_window_size = window_size;
// use next seqno as checkpoint
uint64_t ack_seqno = unwrap(ackno, _isn, _next_seqno);
// it's impossible that ackno > _next_seqno, because that byte hasn't been sent yet!
if (ack_seqno > _next_seqno) {
return;
}
// remove completely ack-ed segments from the retransmission buffer
// because the segment in retrans buffer is ordered by seqno,
// it's ok to break once current seg can't be erased (subsequent seg has larger seqno)
for (auto it = _retrans_buf.begin(); it != _retrans_buf.end();) {
if (unwrap((*it).header().seqno, _isn, _next_seqno) + (*it).length_in_sequence_space() <= ack_seqno) {
it = _retrans_buf.erase(it);
_retrans_timeout = _initial_retransmission_timeout;
_timer.start(_retrans_timeout);
_consec_retrans_count = 0;
} else
break;
}
// stop the timer if retransmission buffer is clear
if (_retrans_buf.empty())
_timer.reset();
// refill the window
fill_window();
}
The second of the main functions :void tick(const size_t ms_since_last_tick). If Timer Started , Then update the time according to the parameters , Then check whether it triggers . If the trigger , Then retransmit according to the rules , Increase retransmission count , send RTO Double , And restart Timer. The code is as follows :
void TCPSender::tick(const size_t ms_since_last_tick) {
if (_timer.active())
_timer.update(ms_since_last_tick);
if (_timer.expired()) {
_segments_out.emplace(_retrans_buf.front());
if (_window_size > 0) {
_consec_retrans_count++;
_retrans_timeout *= 2;
}
_timer.start(_retrans_timeout);
}
}
The third of the main functions :void fill_window(). That is, the one mentioned at the beginning “ send data 、 Serial number and SYN,FIN Wait for the packet with the correct identification setting ”. Generally speaking, the process is : Determine whether the window is not filled → If it is not filled , Make one TCPSegment, Specifically, it is divided into judgment and release SIN identification , Put the data , Judge release FIN Identify three steps → take TCPSegment Add to _segments_out And staging area , Update the serial number and remaining space . Cycle until the window is filled . See the following source file for details , Don't paste it .
Full code link :
tcp_sender.hh
tcp_sender.cc
Screenshot of customs clearance 
边栏推荐
- 【leetcode】1020. Number of enclaves
- pytorch 参数初始化
- My ideal software tester development status
- 外包幹了三年,廢了...
- 2022-07-06: will the following go language codes be panic? A: Meeting; B: No. package main import “C“ func main() { var ch chan struct
- Flutter riverpod is comprehensively and deeply analyzed. Why is it officially recommended?
- 解决could not find or load the Qt platform plugin “xcb“in ““.
- 海思芯片(hi3516dv300)uboot镜像生成过程详解
- 今日现货白银操作建议
- Kbu1510-asemi power supply special 15A rectifier bridge kbu1510
猜你喜欢

外包干了三年,废了...

Jenkins remote build project timeout problem

外包幹了三年,廢了...

95后CV工程师晒出工资单,狠补了这个,真香...

Music | cat and mouse -- classic not only plot

海思芯片(hi3516dv300)uboot镜像生成过程详解

深度学习花书+机器学习西瓜书电子版我找到了

leetcode:105. Constructing binary trees from preorder and inorder traversal sequences

Write CPU yourself -- Chapter 9 -- learning notes

二、并发、测试笔记 青训营笔记
随机推荐
【数学笔记】弧度
Wechat applet full stack development practice Chapter 3 Introduction and use of APIs commonly used in wechat applet development -- 3.10 tabbar component (I) how to open and use the default tabbar comp
Mysql高低版本切换需要修改的配置5-8(此处以aicode为例)
今日现货白银操作建议
【webrtc】m98 screen和window采集
idea添加类注释模板和方法模板
Live online system source code, using valueanimator to achieve view zoom in and out animation effect
MobaXterm
1141_ SiCp learning notes_ Functions abstracted as black boxes
直播平台源码,可折叠式菜单栏
基于Flask搭建个人网站
【性能压测】如何做好性能压测?
Gslx680 touch screen driver source code analysis (gslx680. C)
Route jump in wechat applet
@component(““)
JSON introduction and JS parsing JSON
面试结束后,被面试官在朋友圈吐槽了......
外包干了三年,废了...
三、高质量编程与性能调优实战 青训营笔记
ASEMI整流桥RS210参数,RS210规格,RS210封装