IOCP Documentation Interpretation Question - Buffer Ambiguity

Since I am not a native English speaker, I might miss something, so maybe someone here knows me better.

Adapted from WSASend to add to MSDN:

lpBuffers [in]

Pointer to an array of WSABUF structures. Each WSABUF structure contains a pointer to a buffer and the length, in bytes, of the buffer. For a Winsock application after WSASend, a function is called which system buffers belong to and the application may not access them . This array must remain valid during the send operation.

Ok, can you see the bold text? This is an obscure place!

I can think of two translations for this line (maybe something else, you name it):
Translation 1 - "buffers" refers to the OVERLAPPED structure that I pass to this function when it is called. I can reuse the object again only when I receive a completion notification. Translation 2 - β€œbuffers” refer to actual buffers , to those from whom data is transmitted. If the WSABUF object points to a single buffer, I cannot touch this buffer until the operation is complete.

Can anyone say what is the correct interpretation of this line?

And ..... If the answer is second - how do you resolve it?
Because for me, this implies that for every data / buffer I send, I have to keep a copy of it on the sender side - thus having many "pending" buffers (in different sizes) in a high-traffic application that really goes to corrupt " scalability. "

Statement 1:
In addition to the paragraph above ("AND ...."), I thought IOCP copies the data that will be sent to its own buffer, and sends from there if you do not set SO_SNDBUF to zero.

Statement 2:
I use buffered stacks (you know, something like char cBuff[1024]; in the body of the function - if the translation to the main question is the second option (i.e., buffers should remain as they are until the transfer will not be completed), then ... what really makes you think about the big time! Can you come up with a way to solve it? (I know, I asked him in other words).

+2
source share
1 answer

The answer is that the overlapping structure and the data buffer itself cannot be reused or freed until the operation is completed.

This is because the operation is performed asynchronously, even if the data is eventually copied to operating systems that belong to buffers in the TCP / IP stack, which may not happen until some time in the future, and you are informed when completion occurs during recording. Please note that with replenishment of the record, they can be delayed for an amazing time if you send without explicit flow control and rely on the TCP stack to control the flow for you (see here. Some OVERLAPS using WSASend are not returned in a timely manner using GetQueuedCompletionStatus? ) .. .

You cannot use stacked distributed buffers unless you put an event in an overlapping structure and block it until the async operation completes; this is not so much because you add complexity to a normal blocking call and you do not get much by issuing an asynchronous call and then waiting for it.

In my IOCP server infrastructure (which you can get for free here ) I use dynamically allocated buffers that include the OVERLAPPED structure, and which links are counted. This means that the cleanup (in my case they are returned to the pool for reuse) occurs when the completion occurs and the link is released. It also means that you can continue to use the buffer after the operation, and cleaning is still simple.

See also here: I / O completion port, how to free context on socket and I / O context?

+3
source

Source: https://habr.com/ru/post/1312704/


All Articles