Muduo Source Analysis--The journey of data in Muduo
Developer on Alibaba Coud: Build your first app with APIs, SDKs, and tutorials on the Alibaba Cloud. Read more ＞
After instantiating a evenloop, there will be two socketfd (these two sockets belong to two channel respectively, one is the wakefd of asynchronous Awakening, A timequeue initialization fd is added to the listening queue data sending process: data sent mainly by asynchronous wake, When the primary IO thread accepts a new connection, instantiates a tcpconnection in TCPServer, and the new connection is mounted to a eventloop in a thread pool (eventloopthreappool). Then it will drop the user's connection callback (that is, after the new connection, the user wants to do the action on this connection, this is defined by the user, the Lord but through the TCPServer registration, infiltration into the tcpconnection execution, of course, the user's this program can have, can not, In this function can send data, you can accept data, this is a protocol between the server and the client issue, where if the connection immediately after sending the data, that is, the user's own connection callback function to send data in the function call, Reflected in the tcpconnection is Connectioncallback_). When instantiating a new connection, call the Runinloop function in the eventloop that the main IO thread belongs to, and the Runinloop function needs to know what function to run, and when it accepts a new connection, this function is Connectestablish, This function is defined in the channel in the Connectestablish function has inherited the connection from the user layer to establish the corresponding function processing, the function is connectioncallback. When sending the data, the user layer invokes the Send ()-related function, and in the tcpconnection, the Sendinloop function that is called in the end, in this function, if the current socket is not writing data, and the output buffer in the tcpconnection outputbuffer no data, then the direct send data to see how much data can be sent, if the data sent received over, Then the function that calls the Queueinloop function to send the data that is defined in the user's layer to the end (this function is Writecompletecallback_, which is assumed to be defined in the user's own Writecompltecallback_ function is to continue sending data), queueThe Inloop function first stores the function that needs to be executed (here is the user-defined Writecompletecallback_ function) in the function loud, and if the function in this vector can be executed at the moment (the flag bit is Callingpendfunctors_), Then call Wakeup () asynchronous wakeup. The sending of this data is over. But one of the wakes is to let poll return and perform a callback in the function vector in EventLoop. Then the current registration callback is writecompletcallback_. This callback is still sending data. That is, each time the data is sent, the callback is registered, then the thread wakes up and the poll returns to perform the callback that was just registered. Instructions in the data sent to receive, not only this event to do the safe, then continue to do what in the function Sendinloop through the Queueinloop function also registered, Queueinloop not only registered the user's function, but also to inform the thread immediately to deal with the function just registered. So far, once the data is sent out, but to understand that this time the data is done in the case where the socket can be written and the output buffer has no data, and all the data is sent directly. So what if the current socket is being written, or the output buffer has data, or if the data is written without writing to the socket all at once. If the current socket is not writable, or the output buffer has data, copy the data to the output buffer and begin to focus on the Pollout event on the socket. If the current socket is writable and the output buffer has no data, but writes without writing all the data, the remaining data is stored in the output buffer and then the Pollout event is concerned. Then how to deal with the Pollout event on the socket. in the Handlewrite () function There is about how to handle when the Pollout event arrives (written to reasonable, remember that when an event arrives on a socketfd, Just perform the Handleevent function on the channel that is associated with this socket, and perform the Handleread,hadlewrite handleclose function in this function), in the Handlewrite function, Read the data from the output buffer and write directly to the channel associated SOCKETFD, if finished, will close the socketfd on the writable thing, at the same time register after the operation, from the user layer of the writecompletecallback_. The above section is about sending data to users. So how do users read data? User read data is read from the InputBuffer in this tcpconnection. When a Pollin event occurs on a socket, the Handleread function is called in tcpconnection and the READFD function in the buffer class is called in the Handread function. This function is responsible for reading the data on the socket to the InputBuffer in the tcpconnection. If you read the data and call the user layer's callback Messagecallback_ in the Handleread function, this callback from the user layer is in contact with InputBuffer.
This article is an English version of an article which is originally in the Chinese language on aliyun.com and is provided for information purposes only. This website makes no representation or warranty of any kind, either expressed or implied, as to the accuracy, completeness ownership or
reliability of the article or any translations thereof. If you have any concerns or complaints relating to the article, please send an email, providing a detailed description of the concern or
complaint, to email@example.com. A staff member will contact you within 5 working days. Once verified, infringing content will be removed immediately.
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:
and provide relevant evidence. A staff member will contact you within 5 working days.