Further explore the completion port

Source: Internet
Author: User
a   Quick Start        the concept of port completion and some of the relevant APIs are not discussed, because there are too many articles on the web to introduce these things. The focus here is on how to organize your code. First we look at the example of completion ports in the eighth chapter of Windows network Programming technology. Code can be found on the Internet, there is no paste. This program, which is less than 300 lines, allows us to get started quickly.   Viewer, we can see the general steps of using the server to complete the port: 1.         to create a completion port. The fourth parameter remains 0, specifying that only one worker thread is allowed per processor at a time on the completion port. 2.         determine how many processors are installed in the system. 3.         creates worker threads, and, based on the processor information obtained from step 2, serves completed I/O requests on the completion port. In this simple example, we create only two worker threads for each processor. 4.         prepares a listening socket to listen for incoming connection requests on port 5150. 5.         uses the Accept function to accept incoming connection requests. 6.         creates a data structure, which is often called the completion key, while depositing the accepted socket handle in the structure, and other processing socket IO When you need the information, ready to use 7.         call CreateIoCompletionPort, will be from accept The new socket handle returned is associated with the completion port. By completing the key (Completionkey) parameter, the data structure in 6 is passed to CreateIoCompletionPort. 8.       &nbsP Starts an I/O operation on an accepted connection. Here, we want to post one or more asynchronous WSARecv or WSASend requests on a new socket through overlapping I/O mechanisms. When these I/O requests are complete, a worker thread will service the I/O request, continue processing future I/O requests, and later experience this in the worker routines specified in step 3. 9.         Repeat step 5 ~ 8) until the server aborts.   for the analysis of the program is obvious, 5-8 steps is the key. In worker threads, however, it is also critical to handle IO requests. Two of these data structures are important, one is the completion of the key structure, the other is the overlapping IO structure. Both of these structures are pointers to the pointer, an obscure design designed to make the structure more extensible. The individual feels that understanding how to use these two constructs will understand the core of completion port encoding. Of course, learning to implement the port implementation mechanism still needs to understand how the Windows kernel maintains deeper things such as completion queues, which are not covered in this article.   We can think of the completion key structure as a platform that interacts with worker threads when the main thread of the service accept the connection. Normally we put the client socket handle into this structure when the client connects. In addition, all data structures that cannot be saved in one socket IO can be thrown into the structure so that it can be reused in multiple socket IO. For example, if a client wants to pass a file to the server, you can add a file handle that is open on the server in the completion key, which can be used to read and write data with this handle each socket IO.   We can think of overlapping IO as a buffer type that can be extended. This buffer can be changed according to the specific requirements, it can carry write, write completion, write, write out the completion of the logo, can also carry the specific accepted or sent data, and so on.   In practical applications, we can design the above two structures into Completionkey and buffer two classes, but to ensure that the buffer class representing overlapping IO points to the data structure of the head is an overlapping structure. After   solves the problem of these two structures, another problem is how to program worker threads. The completion port is characterized by a notification of the program once the data has been received. So how to judge the data I received is complete and can be dealt with next. We usually judge whether it is complete according to the data flow characteristics we have received, we can do the next process for the complete data, and we need to wait for the incomplete data.Wait for the next data and splice it with the previous data. This requires us to implement an efficient buffer structure to meet these requirements. Of course, these features are not implemented in the Book of Windows network programming technology.     two   Example AnalysisWith my colleague's advice, I found an example of a completion port that matched the actual requirements on the CodeProject. It provides three types of servers, one is simple echoserver, the other is a server based on a custom package structure, and one is a command based server.   These three servers only have some logical differences in processing data, and the overall architecture is the same. The pros and cons of a package based server and a command based server in the game server, we generally have their own protocol, this Protocol can be based on the package structure, but also based on the command structure, can be flexible according to the needs of the choice. It is generally thought that the package structure is good for the server and the client only need one connection, and if the command-based server needs to pass data, it is best to use another connection, or you will not be able to determine whether the data received or command. But no matter which one is used, a good parser is necessary.   It can help you efficiently parse the contents of a package or the content of a command. Server organization structure The main part of the server is a class library project, the main function of the project is only inherited and Class library project Csocketserver class to implement some logic. The Class Library project wraps some Windows kernel objects, such as events, threads, and so on. The more important part is the Csocketserver class, the socket class, and the implementation of the Ciobuffer class. The collaboration diagram for the Csocketserver class is as follows:

To be continued.

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.