private void asyncReceiveCallback(IAsyncResult ar) { if (disposed) // This continues in the background, so shut down if disposed { return; } // Set up the state IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0); EndPoint senderEP = (EndPoint)sender; asyncReceiveState ars = (asyncReceiveState)ar.AsyncState; // Receive the packet try { ars.bufferChunk.Length = ars.sock.EndReceiveFrom(ar, ref senderEP); // We get back a size of 0 when the socket read fails due to timeout if (ars.bufferChunk.Length > 0) { // Send the data back to the app that called AsyncReceiveFrom ars.receivedFromCallback(ars.bufferChunk, senderEP); } } catch (Exception e) { //Pri2: This used to throw a network timeout event into the EventQueue when it was a synchronous receive System.Diagnostics.Debug.WriteLine(e.ToString()); System.Diagnostics.Debugger.Break(); } // Start another pending network read AsyncReceiveFrom(ars.queue, ars.receivedFromCallback); }
public void AsyncReceiveFrom(Queue queueOfBufferChunks, ReceivedFromCallback callback) { if (disposed) // This continues in the background, so shut down if disposed { return; } // Set up the state IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0); EndPoint senderEP = (EndPoint)sender; BufferChunk bc = (BufferChunk)queueOfBufferChunks.Dequeue(); asyncReceiveState ars = new asyncReceiveState(this.sock, bc, queueOfBufferChunks, callback); // Start the ReceiveFrom sock.BeginReceiveFrom(bc, ref senderEP, new AsyncCallback(asyncReceiveCallback), ars); }