tags:

views:

40

answers:

1

Since I'm not a native English speaker I might be missing something so maybe someone here knows better than me.

Taken from WSASend's doumentation at MSDN:

lpBuffers [in]

A 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, once the WSASend function is called, the system owns these buffers and the application may not access them. This array must remain valid for the duration of the send operation.

Ok, can you see the bold text? That's the unclear spot!

I can think of two translations for this line (might be something else, you name it):
Translation 1 - "buffers" refers to the OVERLAPPED structure that I pass this function when calling it. I may reuse the object again only when getting a completion notification about it.
Translation 2 - "buffers" refer to the actual buffers, those with the data I'm sending. If the WSABUF object points to one buffer, then I cannot touch this buffer until the operation is complete.

Can anyone tell what's the right interpretation to that line?

And..... If the answer is the second one - how would you resolve it?
Because to me it implies that for each and every data/buffer I'm sending I must retain a copy of it at the sender side - thus having MANY "pending" buffers (in different sizes) on an high traffic application, which really going to hurt "scalability".

Statement 1:
In addition to the above paragraph (the "And...."), I thought that IOCP copies the data to-be-sent to it's own buffer and sends from there, unless you set SO_SNDBUF to zero.

Statement 2:
I use stack-allocated buffers (you know, something like char cBuff[1024]; at the function body - if the translation to the main question is the second option (i.e buffers must stay as they are until the send is complete), then... that really screws things up big-time! Can you think of a way to resolve it? (I know, I asked it in other words above).

A: 

The answer is that the overlapped structure and the data buffer itself cannot be reused or released until the completion for the operation occurs.

This is because the operation is completed asynchronously so even if the data is eventually copied into operating system owned buffers in the TCP/IP stack that may not occur until some time in the future and you're notified of when by the write completion occurring. Note that with write completions these may be delayed for a surprising amount of time if you're sending without explicit flow control and relying on the the TCP stack to do flow control for you (see here: http://stackoverflow.com/questions/1145220/some-overlaps-using-wsasend-not-returning-in-a-timely-manner-using-getqueuedcompl) ...

You can't use stack allocated buffers unless you place an event in the overlapped structure and block on it until the async operation completes; there's not a lot of point in doing that as you add complexity over a normal blocking call and you don't gain a great deal by issuing the call async and then waiting on it.

In my IOCP server framework (which you can get for free from here) I use dynamically allocated buffers which include the OVERLAPPED structure and which are reference counted. This means that the cleanup (in my case they're returned to a pool for reuse) happens when the completion occurs and the reference is released. It also means that you can choose to continue to use the buffer after the operation and the cleanup is still simple.

See also here: http://stackoverflow.com/questions/677155/i-o-completion-port-how-to-free-per-socket-context-and-per-i-o-context

Len Holgate
Thank you so much for a reply Len, getting it from you means a lot to me! I get the overall idea yet I'm not able to get down to specific issues, described at http://stackoverflow.com/questions/3034047/limiting-tcp-sends-with-a-to-be-sent-queue-and-other-design-issues .
Poni
Marked as answer since your answer is the answer at the end of the day. Thank you again!
Poni