In other words, what happens to new packets received between the start of execution of the delegate:
public static void Read_Callback(IAsyncResult ar){
StateObject so = (StateObject) ar.AsyncState;
Socket s = so.workSocket;
int read = s.EndReceive(ar);
if (read > 0) {
so.sb.Append(Encoding.ASCII.GetString(so.buffer, 0, read));
and the next the next call of beginReceive on that socket?
s.BeginReceive(so.buffer, 0, StateObject.BUFFER_SIZE, 0,
new AsyncCallback(Async_Send_Receive.Read_Callback), 开发者_如何学Pythonso);
}
Is a second onDataReceived executed in parallel, or does the data queue up in a buffer and the next beginReceive fires the delegate immediately after being called?
The data is queued to a buffer. These callbacks are executed on the thread pool, so as soon as BeginReceive
is called, the callback may be fired.
You have to invoke BeginReceive explicitely, though, you shouldn't do it until the callback completed. See: http://msdn.microsoft.com/en-us/library/bew39x2a.aspx#Y53 for a proper example. As for the more general tcp question, I suppose it depends on the tcp stack implementation, but there is most certainly a buffer where data are accumulated at a lower level. Depending on the sender's setup, it might block when the buffer is full if nobody on the receiver side calls the proper receive method on the socket. Hope this helps.
精彩评论