last day (14 days later) » 

1:01 AM
0
Q: Is there any way to use IOCP to notify when a socket is readable / writeable?

Nathaniel J. SmithI'm looking for some way to get a signal on an I/O completion port when a socket becomes readable/writeable (i.e. the next send/recv will complete immediately). Basically I want an overlapped version of WSASelect. (Yes, I know that for many applications, this is unnecessary, and you can just kee...

 
you not need all this. after socket connected he all time "writeable" and "readable". you can have multiple overlapped send at time. however only one recv request at time exist sense have. you need make recv just after connect, and then after previous recv finished. until disconnect. "will complete immediately" - when using asynchronous io this no sense
when send is finished - you got notify about this. so for example when you need send big data - you can send only chunk. when send of this chunk is finished - you got notify in IOCP about this, and inside this notify - send another chunk. and so on.. i already many times do this
 
There doesn't actually seem to be a function named WSASelect. Do you just mean select()? Have you looked at WSAAsyncSelect?
 
Select not need at all when we using IOCP. just our callback called when any operation finished.
 
I think @RbMm is right, except that you might have to set the buffer size to zero first, see, e.g., support.microsoft.com/en-us/kb/214397.
 
some pseudo code : /*callback when Send Finished*/ void OnSend() { if (m_cbLeft) { cb=max(maxchunk,m_cbLeft); Send(cb); m_cbLeft -= cb; } }
 
1:01 AM
... the important question is what exactly it means when the previous asynchronous send() call is reported to be complete. I suspect that you'll find that this happens at the same time that select() reports the socket to be writable, at least if you've disabled buffering. But I don't know that for a fact. One of the experts might be able to answer.
 
i mean next - we can easy control send data size, which currently buffering in driver; let be _size. when we call Send with cb data - _size += cb, when Send with cb finished - _size -= cb. if we view that _size become too large - we stop send. and in onsend callback, when we decrement _size and view that it become small enough - again call size. all sense here call additional send from onsend callback
@HarryJohnston - what exactly it means when the previous asynchronous send() call is reported to be complete. - until tcp driver process send he used our send buffer - the send buffer must be valid and not changed until the send complete. (this because kernel here MDL used for direct map our buffer). when send complete - this mean that data really send by tcp driver
@HarryJohnston - dont know are this documented, but from my experience - tcp driver not copy data to kernel when we call send, but direct map our buffer to kernel space and used it all time until transmit data over net. only when send really finished or fail - the send operation will be completed and OVERLAPPED (IO_STATUS_BLOCK) queued to IOCP
 
@RbMm: please ignore my previous (now deleted) comment.
 
sorry. just for info if it interesting. if not ignore too ) this not mean that data directly from buffer goes onto wire as is. but when we do send driver must somehow maintain our send data. this can be done or by copy our buffer to kernel or by mapping (describe it with MDL and map). when i beginning work with this - i make mistake - i free (modify) data in send buffer just after WSASend ( OVERLAPPED of course was valid all time) and i notice strange errors.
or send finished with errors (like checksum error) or other end received bad data. after some experiments i understand that modification to send buffer just after send affected send. this can be only if data not just copied by mapped.
 
I hadn't read what you were saying carefully enough. I would expect the send() to complete once the TCP driver was finished with the data, which is exactly what you said, I just misunderstood.
 
1:21 AM
@RbMm, but it's all more complicated than that - the TCP driver has to be able to retransmit data if any of the packets get dropped. Presumably it doesn't wait until the other end has confirmed receipt of all the data before completing the I/O operation, so it must be internally buffering at least some of it. But how much? I don't really understand how TCP windows work, so I'm at a loss to guess just how the driver behaves.
(It isn't clear to me what exactly it means for a socket to be writable in the first place, so I can't really say whether that's exactly the same concept as the previous I/O having completed, or if not, what the difference is, and which one would be the best choice. Without more input from the OP or a subject-matter expert, I think I'd better leave it at that.)
 

last day (14 days later) »