IO Completion Port Learning Sense

Source: Internet
Author: User
Tags advantage

Time passed really fast, all of a sudden to the May 1, 2014, the national legal holidays, I believe that there must be a lot of colleagues and I, in this country with the celebration of the day, still in ...

Today I looked at the next IO completion port related content, now take advantage of their own brain is still relatively refreshing when the little harvest of their own record, to facilitate their later use can be viewed, if their own book can help to see you, it is also my pleasure, now always feel that there is such a feeling, Sometimes simple to look at the theoretical aspects of the abstract, if you look at some of the theory, can look at the time code examples, that call suddenly enlightened.


1. After you know some basic concepts of port completion, you first need to master the basic framework of the port, the following figure is a separate set of threads used to accept external sockets of the system diagram.


2. As the above separate Kai thread used to receive foreign sockets, when the foreign concurrent access request is very small, the efficiency of the problem is not reflected, when the concurrent request for a lot of, then the problem followed, the following figure is to use AcceptEx to replace the Accept solution



3. The advantage of the second method in the book is that the following three points are better than the first, first socket to create, in our view is only a word can be created, but the system does a lot of things, especially when concurrent creation, the workload is even more amazing, the second way is to user access before the socket to create a good , waiting for the user to come in, this is much better than the first one, followed by the second in the accept at the same time, can receive the user sent over the first message, third, the second method can be thrown at the same time a number of accept, so the speed above can improve a lot.


4. In the completion of the port throughout the framework of the need to use a number of key areas, 1 of the server will first be sure to create the SOCKET used to receive the user's request, where to create a per_socket_contex corresponding, because the IO binding in the process of the need to have this per _socket_contex as one of the parameters, how to connect the socket and this Per_socket_contex, is to assign the socket to the PER_SOCKET_CONTEX variable socket members, The bottom of the work is to bind this per_socket_contex, but this SOCKET is used to listen, then you need to create a sockaddr_in variable, and the sockaddr_in variable and the socket binding, Then listen on this socket.


5. The above task only completes half of the listening work, because we have not yet delivered the accept request, before we post the request, we need to create a Per_io_contex instance by calling the Getnewiocontext function in Per_socket_contex. The rest of the work is to PER_IO_CONTEX member variables in the relevant assignment, first of all said AcceptEx this way is to create a socket, the process is in this place, we create a socket in this place, and assign the socket created here to the AcceptSocket instance in Per_io_contex, and assign the message type we posted to the Msgtype in the Per_io_contex instance, The rest can be delivered by AcceptEx the Per_io_contex assembled above.


6. From the above figure we can see that so far, the main thread has no work, the following work is to the worker thread, in the line function we mainly use the function is the Getqueuedcompletementstatues function to get the two important members inside, One is a Per_socket_contex instance, there is a overlapped instance, in addition to the above two useful information, but also through some variable states to judge different states, if the above function of the Per_socket_contex variable value exit_ CODE, that means the thread can quit, if the return value of the function is false, it indicates that the program has made an error during execution, and if the output parameter in the function indicates the value of the accepted data size is 0 and the message type is send or recv, then the current user is offline. You can delete its Per_socket_contex instance object, except for the above situation, the rest is the normal situation, first through the overlapped instance of the function to obtain the Per_io_contex instance (specifically by what way to obtain, I think it is through the first address of the structure body, through the inside of the Msgtype to determine the current delivery is recv,send,accept in which.


7. If it is accept delivery, we need to recreate a Per_socket_contex instance, first of all, we need to assign the AcceptSocket instance that we just obtained in Per_io_contex to per_socket_ The Localsocket instance in Contex, then binds the Per_socket_contex to the completion port, and then creates a Per_io_contex instance that belongs to this Per_socket_contex instance. You also need to assign the Msgtype value in the instance to the kind of message you want to post, and you also need to assign the AcceptSocket instance in Overlapper to the SOCKET member in the Per_socket_contex instance and the Per_io_contex instance , and deliver the Io_context in recv form.


8. In the process of judging if it is recv delivery, then need to read out the inside of the data, and then the inside of the message cache part of the empty, again put.


We look at this part of the time, first familiar with the framework, and then look inside the specific implementation of it, there are many details of the need to pay attention to, if you cause trouble, please forgive me

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.