// UDP Consumer static async Task <BufferBlockWrapperUDP> ConsumeAsyncUDP(ISourceBlock <BufferBlockWrapperUDP> source) { // Read from the source buffer while (await source.OutputAvailableAsync()) { BufferBlockWrapperUDP wrapper = await source.ReceiveAsync(); UDPMessage msg = wrapper.message; int netID = wrapper.netID; ConsumeMessageUDP(msg, netID); } return(null); }
public static void UDPReceiveCallback(IAsyncResult ar) { UdpClient u = (UdpClient)((UDPState)(ar.AsyncState)).u; IPEndPoint e = (IPEndPoint)((UDPState)(ar.AsyncState)).e; Byte[] receiveBytes = u.EndReceive(ar, ref e); //Console.WriteLine("Bytes received: {0}", receiveBytes.Length); messageReceived = true; UDPState s = (UDPState)ar.AsyncState; UDPMessage msg = UDPMessage.Parser.ParseFrom(receiveBytes); BufferBlockWrapperUDP wrap = new BufferBlockWrapperUDP(); wrap.message = msg; wrap.netID = clients.Find(c => ((IPEndPoint)c.workSocket.RemoteEndPoint).Address.Equals(e.Address)).netID; asyncQueueUDP.SendAsync(wrap); u.BeginReceive(new AsyncCallback(UDPReceiveCallback), s); }