Socket for Chrome source code analysis (6)

Source: Internet
Author: User

In tcpclientsocketwin, I/O bytes or error code, because the write and read functions return two types of values, because the errors defined in winerr and wsaerr are integers, the returned values of Read and Write include the error code and normal I/O bytes, therefore, the system error code must be converted to a private negative error code. These are implemented by calling the mapwinsockerror function. In addition, you also need to define a private error code to replace the system error code.

Here we can summarize the process of tcpclientsocketwin processing the URL request at the upper layer.

The first call is the connect (completioncallback * callback) function, which has only one parameter, that is, the callback function specified by the caller. The non-key part is omitted, the function first sets next_connect_state _ to connect_state_connect and then calls doconnectloop. Obviously, this function processes the entire TCP connection process in a loop. Because the initial status is connect_state_connect, the code will jump to doconnect. If doconnect is completed immediately, it will set the status to connect_state_connect_complete. In this way, the next loop code will jump to doconnectcomplete to process some necessary processes. If doconnect returns err_io_pending, the connection request enters the asynchronous wait state. At this time, the loop ends and the status is returned to connect. In connect, if the connection request enters the err_io_pending state, the passed callback function is assigned to read_callback _, and then the entire function is returned to the caller. Through layer-by-layer return, the caller will receive the asynchronous wait state.

 


Review the entire tcpclientsocketwin class. When the caller wants to establish a connection, it first calls the connect function, creates a socket in doconnect, and calls the system API: connect. When the connection is established successfully, didcompleteconnect is executed. This function notifies the upper-layer caller through the callback function.

When Upper-layer callers need to send or receive I/O data from the network, they call write or read, and they have their own implementation functions.

In this mode, the upper-layer callers and communication modules are separated, and the communication modules and threads are separated.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.