private void ResponseReceived(UdpReceiveResult ar) { var bytes = ar.Buffer; var remote = ar.RemoteEndPoint; Debug.WriteLine($"P: Received response from {remote.Address}"); var typeBytes = Beacon.Encode(BeaconType).ToList(); Debug.WriteLine(string.Join(", ", typeBytes.Select(_ => (char)_))); if (Beacon.HasPrefix(bytes, typeBytes)) { try { var portBytes = bytes.Skip(typeBytes.Count()).Take(2).ToArray(); var port = (ushort)IPAddress.NetworkToHostOrder((short)BitConverter.ToUInt16(portBytes, 0)); var payload = Beacon.Decode(bytes.Skip(typeBytes.Count() + 2)); Debug.WriteLine($"P: New beacon {remote.Address},{port}"); NewBeacon(new BeaconLocation(new IPEndPoint(remote.Address, port), payload, DateTime.Now)); } catch (Exception ex) { Debug.WriteLine(ex); } } }
private async Task BroadcastProbeAsync() { Debug.WriteLine($"P: Broadcasting probe with discovery port {Beacon.DiscoveryPort}"); var probe = Beacon.Encode(BeaconType).ToArray(); await udp.SendAsync(probe, probe.Length, new IPEndPoint(IPAddress.Broadcast, Beacon.DiscoveryPort)); }