Socket I/O model completion port

Source: Internet
Author: User

Http://tangfeng.javaeye.com/blog/518148

The "complete port" model is the most complex I/O model so far. However, if an application needs to manage a large number of sockets at the same time, this model can often achieve the best system performance! Unfortunately, this model only applies to Windows NT and Windows 2000 operating systems. Because of the complexity of the design, you only need to manage hundreds or even thousands of sockets at the same time for your application, and want to increase the number of CPUs installed in the system, the "complete port" model should be considered only when the application performance can be linearly improved. A basic principle to remember is to develop high-performance server applications for Windows NT or Windows 2000, in addition, if you want to provide services for a large number of socket I/O requests (Web servers are a typical example), the I/O complete port model is the best choice!

// Write by Larry <br/> // 2009-8-20 <br/> // This is a server using completion port. <br/> # include "stdafx. H "<br/> # include <winsock2.h> <br/> # include <stdio. h> <br/> # pragma comment (Lib, "ws2_32.lib ") <br/> # Define Port 5150 <br/> # define msgsize 1024 <br/> typedef Enum <br/>{< br/> recv_posted <br/>} operation_type; <br/> typedef struct <br/> {<br/> wsaoverlapped overlap; <br/> wsabuf buffer; <br/> char szmessage [msgsize]; <br/> DWORD numberofbytesrecvd; <br/> DWORD flags; <br/> operation_type operationtype; <br/>} per_io_operation_data, * lpper_io_operation_data; <br/> DWORD winapi workerthread (lpvoid completionportid); </P> <p> int main (INT argc, char * argv []) <br/>{< br/> wsadata; <br/> socket slisten, sclient; <br/> sockaddr_in local, client; <br/> dword I, dwthreadid; <br/> int iaddrsize = sizeof (sockaddr_in); <br/> handle completionport = invalid_handle_value; <br/> system_info sysinfo; <br/> define lpperiodata = NULL; <br/> // initialize Windows Socket library <br/> wsastartup (0x0202, & wsadata ); <br/> // create completion port <br/> completionport = createiocompletionport (invalid_handle_value, null, 0, 0 ); <br/> // create worker thread <br/> getsysteminfo (& sysinfo); <br/> for (I = 0; I <sysinfo. dwnumberofprocessors; I ++) <br/>{< br/> createthread (null, 0, workerthread, completionport, 0, & dwthreadid ); <br/>}< br/> // create listening socket <br/> slisten = socket (af_inet, sock_stream, ipproto_tcp ); <br/> // bind <br/> Local. sin_family = af_inet; <br/> Local. sin_addr.s_un.s_addr = htonl (inaddr_any); <br/> Local. sin_port = htons (port); <br/> BIND (slisten, (sockaddr *) & Local, sizeof (sockaddr_in )); <br/> // listen <br/> listen (slisten, 3); <br/> while (true) <br/>{< br/> // accept a connection <br/> sclient = accept (slisten, (sockaddr *) & client, & iaddrsize ); <br/> printf ("accepted client: % s: % d/N", inet_ntoa (client. sin_addr), ntohs (client. sin_port); <br/> // associate the newly arrived client socket with completion port <br/> createiocompletionport (handle) sclient, completionport, (DWORD) sclient, 0 ); <br/> // launch an asynchronous operation for new arrived connection <br/> lpperiodata = (bytes) heapalloc (<br/> getprocessheap (), <br/> heap_zero_memory, <br/> sizeof (per_io_operation_data); <br/> lpperiodata-> buffer. len = msgsize; <br/> lpperiodata-> buffer. buf = lpperiodata-> szmessage; <br/> lpperiodata-> operationtype = recv_posted; <br/> wsarecv (sclient, <br/> & lpperiodata-> buffer, <br/> 1, <br/> & lpperiodata-> numberofbytesrecvd, <br/> & lpperiodata-> flags, <br/> & lpperiodata-> overlap, <br/> null); <br/>}< br/> postqueuedcompletionstatus (completionport, 0 xffffffff, 0, null); <br/> closehandle (completionport ); <br/> closesocket (slisten); <br/> wsacleanup (); <br/> return 0; <br/>}< br/> DWORD winapi workerthread (lpvoid completionportid) <br/>{ <br/> handle completionport = (handle) completionportid; <br/> DWORD dwbytestransferred; <br/> socket sclient; <br/> lpper_io_operation_data lpperiodata = NULL; <br/> while (true) <br/> {<br/> getqueuedcompletionstatus (<br/> completionport, <br/> & dwbytestransferred, <br/> (DWORD *) & sclient, <br/> (lpoverlapped *) & lpperiodata, <br/> infinite); <br/> If (dwbytestransferred = 0 xffffffffff) <br/>{< br/> return 0; <br/>}< br/> If (lpperiodata-> operationtype = recv_posted) <br/>{< br/> If (dwbytestransferred = 0) <br/>{< br/> // connection was closed by client <br/> closesocket (sclient); <br/> heapfree (getprocessheap (), 0, lpperiodata ); <br/>}< br/> else <br/> {<br/> lpperiodata-> szmessage [dwbytestransferred] = '/0'; <br/> send (sclient, lpperiodata-> szmessage, dwbytestransferred, 0); </P> <p> // launch another Asynchronous Operation for sclient <br/> memset (lpperiodata, 0, sizeof (per_io_operation_data); <br/> lpperiodata-> buffer. len = msgsize; <br/> lpperiodata-> buffer. buf = lpperiodata-> szmessage; <br/> lpperiodata-> operationtype = recv_posted; <br/> wsarecv (sclient, <br/> & lpperiodata-> buffer, <br/> 1, <br/> & lpperiodata-> numberofbytesrecvd, <br/> & lpperiodata-> flags, <br/> & lpperiodata-> overlap, <br/> null); <br/>}< br/> return 0; <br/>}< br/>

The main process on the server side:
1. Create a port object
2. Create a worker thread (the number of worker threads is determined by the number of CPUs, so as to achieve the best performance)
3. Create a listening socket, bind and listen, and then the program enters the loop
4. in the loop, I did the following:
(1) accept a client connection
(2). Bind the client socket with the completion port (or call createiocompletionport, but this time the function is different ),Note: In principle, the third parameter passed to createiocompletionport should be a completion key. Generally, the program transmits a single handle data structure address, the Single Handle data contains information related to the client connection. Because we only care about the socket handle, the socket handle is directly transferred as the completion key; 
(3) trigger a wsarecv asynchronous call. This time, "trailing data" is used to keep the buffer used to receive data after the wsaoverlapped object. In addition, there are important information such as the operation type.

In the worker thread loop, we
1. Call getqueuedcompletionstatus to obtain information about this I/O (for example, socket handle, number of transmitted bytes, address of a single I/O data structure, etc)
2. Find the receiving data buffer through a single I/O data structure, and then send the data to the client intact
3. trigger a wsarecv asynchronous operation again

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.