public async void AwaitMulticastRequest() { try { //Listen on port 4568 since its Unicast and cant be bound to port 4567 (in use for multicast) using (var client = new UdpClient(MulticastEndPoint.Port, AddressFamily.InterNetwork)) { client.Ttl = 2; client.JoinMulticastGroup(MulticastEndPoint.Address); client.Client.ReceiveBufferSize = 18; var ipEndPoint = new IPEndPoint(IPAddress.Loopback, MulticastEndPoint.Port); while (true) { Debug.WriteLine("Awaiting multicast request"); var result = await client.ReceiveAsync(); var stateProtocol = StateProtocol.FromBytes(result.Buffer); //Check if reply is an actual discovery reply if (stateProtocol.ProtocolType == ProtocolRequest.DeviceDiscovery) { if (ipEndPoint.Address.Equals(IPAddress.Parse("127.0.0.1"))) { return; } var robeatsDevice = new RobeatsDevice { Id = stateProtocol.DeviceId, Name = stateProtocol.DeviceName, EndPoint = new IPEndPoint(ipEndPoint.Address, 4568), StateProtocol = stateProtocol }; OnDeviceDetect(new DeviceDiscoveryEventArgs { RobeatsDevice = robeatsDevice }); } else { Debug.WriteLine("Not a reply. ignoring"); } } } } catch (Exception e) { Debug.WriteLine(e); } }
public async void AwaitDiscoveryReply() { try { using (var client = new UdpClient(4568, AddressFamily.InterNetwork)) { while (true) { Debug.WriteLine("Awaiting DR"); var result = await client.ReceiveAsync(); Debug.WriteLine("Received DR"); var stateProtocol = StateProtocol.FromBytes(result.Buffer); if (stateProtocol.ProtocolType == ProtocolRequest.DeviceDiscoveryReply) { var robeatsDevice = new RobeatsDevice { Id = stateProtocol.DeviceId, Name = stateProtocol.DeviceName, /*EndPoint = client.Client.RemoteEndPoint,*/ StateProtocol = stateProtocol }; OnDiscoveryReply(new DeviceDiscoveryEventArgs { RobeatsDevice = robeatsDevice }); } } } } catch (Exception ex) { Debug.WriteLine(ex); } }