GetQueuedCompletionStatus the order in which the return value is processed

Source: Internet
Author: User



IOCP is the most complex of the 5 socket programming models, using only a few APIs, enabling the program to support hundreds or thousands of connections, while the GetQueuedCompletionStatus function is the most important API in IOCP, a function that returns a value that contains a variety of information.
The general processing order is: The return value of the function GetQueuedCompletionStatus is judged first, if it fails, look at the value of WSAGetLastError (). Otherwise, check whether the Overlapp struct is null, NOT NULL, determine overlapped related things and bytes transmitted, and so on.


First look at the full declaration of the GetQueuedCompletionStatus function:
BOOL GetQueuedCompletionStatus (
HANDLE Completionport,
Lpdword Lpnumberofbytes,
Pulong_ptr lpCompletionKey,
lpoverlapped *lpoverlapped,
DWORD dwmilliseconds
);

Then look at the description of the return value on MSDN:

If the function dequeues a completion packet for a successful I/O operation from the completion port, the return value is Nonzero. The function stores information in the variables pointed to by the lpnumberofbytestransferred, lpCompletionKey, and Lpover Lapped parameters.
If the function takes a completed package from the completion port for a successful I/O operation, the return value is not 0. The function stores relevant information in parameters that point to lpnumberofbytestransferred, lpCompletionKey, and lpoverlapped.

------------------1th situation


If *lpoverlapped is NULL and the function does not dequeue a completion packet from the completion port, the return value is zero. The function does not store information on the variables pointed to by the lpnumberofbytestransferred and lpCompletionKey Parameters. To get extended error information, call GetLastError. If the function did not dequeue a completion packet because the wait timed out, GetLastError returns WAIT_TIMEOUT.
The return value is 0 if *lpoverlapped is empty and the function is not removed from the completion port to complete the package. The function does not store information in the parameters pointed to by Lpnumberofbytes and lpCompletionKey. Call GetLastError to get an extended error message. If the function fails to dequeue the package due to a wait timeout, GetLastError returns WAIT_TIMEOUT.

------------------2nd situation


If *lpoverlapped is not NULL and the function dequeues a completion packet for a failed I/O operation from the completion Port, the return value is zero. The function stores information in the variables pointed to by lpnumberofbytestransferred, lpCompletionKey, and Lpoverlapp Ed. To get extended error information, call GetLastError
If *lpoverlapped is not empty and the function is out of the completion port to dequeue a failed I/O operation completion Package, the return value is 0. The function stores relevant information in a parameter pointer to lpnumberofbytestransferred, lpCompletionKey, and lpoverlapped. Call GetLastError to get extended error information.

------------------3rd Situation


IF a socket handle associated with a completion port was closed, GetQueuedCompletionStatus returns ERROR_SUCCESS, with LPNU Mberofbytes equal Zero.
If a socket handle associated to a completion port is closed, GetQueuedCompletionStatus returns ERROR_SUCCESS (also 0), and lpnumberofbytes equals 0

-----------------4th situation

In combination with various situations, my own implementation of the IOCP processing sequence is as follows:

DWORD WINAPI ciocpmodel::workerthread (lpvoid lpparam) {threadparams_worker* Pparam = (threadparams_worker*) LpParam; ciocpmodel* Piocpmodel = (ciocpmodel*) pparam->piocpmodel;int nthreadno = (int) pparam->nthreadno; Write_log ("Work thread Start,id:%d.", Nthreadno); OVERLAPPED *poverlapped = NULL; Per_socket_context *psocketcontext = Null;dword dwbytestransfered = 0;//loops processing the request, knowing that the shutdown message is received while (W Ait_object_0! = WaitForSingleObject (piocpmodel->m_hshutdownevent, 0)) {BOOL Breturn = GetQueuedCompletionStatus ( Piocpmodel->m_hiocompletionport,&dwbytestransfered, (pulong_ptr) &psocketcontext,&poverlapped,            INFINITE);//Determine if there is an error if (Breturn = = FALSE) {if (NULL = = poverlapped)//handle 2nd case continue; poverlapped!=nulldword Dwerr = GetLastError (); if (Psocketcontext = = NULL) {continue;}  Else{piocpmodel->handleerror (Psocketcontext,dwerr);//Handling of 3rd and 4th cases continue;}} else {//Read incoming parameters per_io_context* Piocontext = Containing_record (poverlapped, Per_io_context, m_overlapped); Determine if a client has disconnected if ((0 = = dwbytestransfered) && (recv_postedhead = = Piocontext->m_optype | | send_posted = = piocontext->m_optype| | Recv_postedbody = = Piocontext->m_optype)) {//release the corresponding resource Piocpmodel->removesocketcontext (Psocketcontext);  Continue } else{//handles the 1th case of switch (piocontext->m_optype) {accept_posted:{}break;case recv_postedhead:{piocpmodel->d Orecvhead (psocketcontext,piocontext,dwbytestransfered);} Break;case Recv_postedbody:piocpmodel->dorecvdata (psocketcontext,piocontext,dwbytestransfered); break;case Send_posted:{piocpmodel->dosenddata (psocketcontext,piocontext,dwbytestransfered);} break;default://should not be executed here Writesinglelog ("_workthread's Piocontext->m_optype Param invalid.\n"); Switch}//if}//if}//whilewrite_log ("Work thread%d exit.\n", nthreadno);//Release thread parameter release (lpparam); return 0;} Displays and handles errors on the completion port bool Ciocpmodel::handleerror (per_socket_context *pcontext,const dword& dwerr) {if (NULL = = PContext) return false;//If it is timed out, then continue to wait if (wait_timeout = = Dwerr) {//Confirm that the client is still alive ... if (! Issocketalive (Pcontext->getsocket ())) {Writesinglelog ("Client disconnect!"); This->removesocketcontext (PContext); return true;} Else{writesinglelog ("Network Operate time out!");  return true;}} Else{write_log ("Shutdown client,error Code is:%d! ", Dwerr); This->removesocketcontext (PContext); return true;}} Determine if the client socket has been disconnected, Otherwise, posting the WSARECV operation on an invalid socket will result in an exception//using the method of trying to send data to this socket to determine the return value of the socket call//Because if the client network is abnormally disconnected (such as a client crashing or unplugging a network cable, etc.), The server side is unable to receive notification of client disconnect bool Ciocpmodel::issocketalive (SOCKET s) {int nbytesent=send (S, "", 0,0); if ( -1 = = nbytesent) return False;return true;}


GetQueuedCompletionStatus the order in which the return value is processed

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.