question

Andreyka-0382 avatar image
0 Votes"
Andreyka-0382 asked GaryNebbett answered

Winsock: WSASend и GetQueuedCompletionStatus

Hello,

Let me please ask you a question:

There are two functions, WSASend and GetQueuedCompletionStatus. Both functions take the WSAOVERLAPPED structure as one of the parameters, into which they[function] write "something" there.

In the case of WSASend and GetQueuedCompletionStatus, in structure WSAOVERLAPPED is written to the parameter WSAOVERLAPPED.InternalHigh - is the number of bytes that are supposedly sent at the moment by the WSASend function.

And here's the question:

for the GetQueuedCompletionStatus function - the second parameter is:

from MSDN:
lpNumberOfBytesTransferred

A pointer to a variable that receives the number of bytes transferred in a completed I/O operation.


and the 4th parameter, as I already wrote above, is the WSAOVERLAPPED structure:

from MSDN:
A pointer to a variable that receives the address of the OVERLAPPED structure that was specified when the completed I/O operation was started.

Function GetQueuedCompletionStatus fills in the structure field WSAOVERLAPPED.InternalHigh - "the number of bytes transferred in a completed I/O operation."

BUT, why is the second parameter needed then?
Or why then transfer WSAOVERLAPPED in function GetQueuedCompletionStatus , if there is a second parameter, where GetQueuedCompletionStatus will write the - ""the number of bytes transferred in a completed I/O operation.""?

I'm confused.




   int main()
     {
        
        
     ///...
        
        
     HANDLE My_handle_Create_IOCP = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, NULL, Number_Threads);  //Create IOCP
        
        
     my_WSASocketA = WSASocketA(ver_IP, my_type, my_protocol, NULL, 0, WSA_FLAG_OVERLAPPED);                                  //Create Socket
        
        
     CreateIoCompletionPort((HANDLE)my_SOCKET, My_handle_Create_IOCP, int_key, 0);                                                          //Bind Socket to IOCP
        
        
     //.....
     }



And processing function queues CompletionPort:





  void My_func_CompletionPort(HANDLE iocp, std::vector<SOCKET>& my_SOCKET_vec_1D)
     {
        
     DWORD my_DWORD;                
     ULONG_PTR   my_CompletionKey;
        
     WSAOVERLAPPED* my_WSAOVERLAPPED_1;
        
        
     while (1)
     {
        
     BOOL my_BOOL_GetQueuedCompletionStatus = GetQueuedCompletionStatus(iocp, &my_DWORD, &my_CompletionKey, &my_WSAOVERLAPPED_1, INFINITE);
        
        
        
     if (my_BOOL_GetQueuedCompletionStatus == TRUE)
     {
        
               std::cout<<my_DWORD<<std::endl;
               std::cout<<my_WSAOVERLAPPED_1->InternalHigh <<std::endl;
        
        
               //my_DWORD and my_WSAOVERLAPPED_1->InternalHigh ------> the same values!!!!!!!!
        
             }
        
         }












windows-apic++windows-10-network
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

GaryNebbett avatar image
0 Votes"
GaryNebbett answered

Hello Andreyka,

The documentation for the OVERLAPPED structure says:

InternalHigh

The number of bytes transferred for the I/O request. The system sets this member if the request is completed without errors.

The InternalHigh member was originally reserved for system use and its behavior may change.

It was once intended that this field was "opaque" (its contents should have no meaning to correctly written applications), but as APIs have developed and been added, it was necessary to partially document its use. The APIs try to provide more specific and controlled access via other mechanisms (e.g. additional parameters).

In the case of WSASend, the other mechanism (the lpNumberOfBytesSent parameter) only contains a valid value if the WSASend completes synchronously; lpNumberOfBytesSent is not written to after WSASend returns, even if it returns WSA_IO_PENDING. If WSASend does return WSA_IO_PENDING, then the OVERLAPPED structure may be written to after WSASend returns, and then knowing the use of InternalHigh might be the only way to see how many bytes were actually sent.

In the case of GetQueuedCompletionStatus, it is probably intended that the caller need not know what interpretation to place on the internal/private members of the OVERLAPPED structure.

Gary

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.