private static void ReceiveCallback(IAsyncResult result) { if (result == null) { return; } EbReceiveState state = (EbReceiveState)result.AsyncState; byte[] receivedBytes = null; try { receivedBytes = state.UdpClient.EndReceive(result, ref state.EndPoint); } catch (ObjectDisposedException) { } if (receivedBytes?.Length > 0 && state.Receive != null) { Task.Factory.StartNew(() => state.Receive(receivedBytes)); } if (!state.Sender.Closing) { state.UdpClient.BeginReceive(state.AsyncReceiveCallback, state); } }
private static void ReceiveCallback(IAsyncResult result) { if (result == null) { return; } byte[] receivedBytes = null; EbReceiveState state = (EbReceiveState)result.AsyncState; try { receivedBytes = state.UdpClient.EndReceive(result, ref state.EndPoint); } catch (ObjectDisposedException) { } if (receivedBytes?.Length > 0) { ReceiveEventArgs eventArgs = new ReceiveEventArgs(receivedBytes, state.EndPoint); if (state.Receive != null) { Task.Factory.StartNew(() => state.Receive(eventArgs)).ContinueWith((task => { byte[] toWrite = new ByteBuffer().Write((int)eventArgs.ResponseCode).ToArray(); state.UdpClient.Send(toWrite, toWrite.Length, state.EndPoint); })); } } if (!state.Sender.Closing) { state.UdpClient.BeginReceive(state.AsyncReceiveCallback, state); } }