当前位置:网站首页>[Stanford Jiwang cs144 project] lab4: tcpconnection

[Stanford Jiwang cs144 project] lab4: tcpconnection

2022-07-07 07:43:00 Altair_ alpha

At the receiving end TCPReceiver Class and sender TCPSender After the class , In this section, the experimental implementation takes charge of these two classes , Provide users with a complete TCP Connect Abstract TCPConnection class . As the title of the handout says , After completing the experiment in this section , We really climbed a peak (summit), It is also the most important essence of this course .

Advance warning , The amount of testing in this section is very large , It will use yours TCP Realization and Linux Of TCP Realize the interaction of various conditions , Previous Sender and Receiver If there are potential problems in the implementation of classes, they will also be exposed . therefore , If you're trying to do it yourself , Please be patient Debug To prepare . Although look directly at others blog It may feel that the logic is very natural , But doing it yourself is totally different . I also spent 2 It took about a whole day to pass all the tests .

 Insert picture description here

TCP It's full duplex communication , That is, both parties can send / Receiving information . As shown in the figure ,TCPConnection Class should be built on TCPSender and TCPReceiver Above . When Sender When you need to send a packet , Send queue from it _segments_out Take out the data packet and actually send it out ; When a packet is received , After necessary treatment, hand it over to Receiver Handle . in addition , We haven't paid attention to it before TCP A very important logo in Baotou RST, This is also TCPConnection Responsible , That is, the handling of abnormal states . Last ,TCPConnection You should also handle the termination of the connection , Including normal termination and abnormal termination . Let's discuss these problems and implementation ideas .

notes : The implementation of sending here is actually to send packets from Sender Take out and transfer to TCPConnection In a send queue of , Later, the higher-level owner will actually be responsible for sending

Receive packets

The corresponding function :void segment_received(const TCPSegment &seg).

  • If RST Identification exists , Immediately set the incoming and outgoing data streams to the error state , And end the connection . You can refine the end connection into a _shutdown(bool) function , Parameter represents normal or abnormal end , Here is the abnormal end . otherwise :
  • Give the packet to Receiver Handle , That is to call Receiver Of segment_received.
  • If ACK Identification exists , notice Sender The information it focuses on (ackno and window size), That is to call Sender Of ack_received.
  • If the packet has a serial number , Ensure that at least one reply packet is replied , That is, you should check Sender Whether the send queue is empty , If it is empty, create an empty bag .
  • “keep-alive” Reply of package : The other party may send a packet containing an invalid serial number to detect whether the connection is still valid , At this time, an empty package should be replied . The implementation method handout has been given .

Send packet

  • Sender The constructed packet to be sent is not complete ,ackno and window size Information should be provided by Receiver give ( If there is ackno, Also set ACK identification ).
  • Any time Sender Send to the queue push I bought a bag ,TCPConnection Should be sent immediately . therefore ,TCPConnection An emptying should be realized Sender queue , fill Receiver Information and send function ( My implementation is named _clear_sendbuf), In any possible way Sender This function is called after the statement that sends the packet .

Perception of time

And Sender identical ,TCPConnection Also through its tick The function is called and passively perceives the passage of time . When tick When called :

  • send Sender tick.
  • If Sender The number of retransmissions exceeds a certain threshold , Terminate the connection and send a tape RST Empty package identified . Terminate the connection and receive the other party above RST Package processing is the same , That is to call _shutdown(false).
  • If the time is right , End the connection normally ( See the next section ).

The termination of the connection

Two cases of abnormal connection termination have been discussed above , That is, active detection of too many retransmissions or passive receipt of the other party RST package , The main discussion here is the normal termination of the connection .

In theory , The two parties communicating through an untrusted connection cannot absolutely ensure a consensus ( Reference resources : The issue of the two armies ), however TCP A scheme with high reliability is designed . On one side (local Fang ) From the point of view of , There are at least four things that make it possible to properly terminate the connection :

  • Inflow (inbound) The data of has been completely received and reorganized .
  • Outward flow (outbound) The data of has been completely sent with FIN The identified package has also been sent .
  • The outgoing flow is fully confirmed by the other party .
  • local Fang believes that remote Can meet the third . This is the most difficult problem to solve , because TCP It won't be right ack Information do ack, Otherwise there will be no end . So there are two options :
    • Delayed closure (lingering). The current three articles are established and after a certain period of time local Fang didn't receive remote We retransmit any data , be local We can make sure that all the data of both sides has been transmitted correctly and close the connection . say concretely , This time is set to Initial retransmission timeout 10 times .
    • Passive closure . If the end of the incoming flow is earlier than that of the outgoing flow FIN Identify the sending of packets , be local Fang closes the connection immediately after the outgoing flow ends . The reasons are explained in the handout , It's not going to unfold here .

test

In addition to running automated tests , After the completion of this section, you can use two of your own TCP End to end communication , And use your own TCP Instead of Lab0 Medium TCPSocket Communicate with real servers , You can also test yourself TCP The performance of the implementation , The operation method is described in the handout .


Full code link :
tcp_connection.hh
tcp_connection.cc

Screenshot of customs clearance
 Insert picture description here
 Insert picture description here
 Insert picture description here

原网站

版权声明
本文为[Altair_ alpha]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207070413448909.html