private void ResponseReceived(IAsyncResult ar) { _log.Information("Incomming Reponse"); var remote = new IPEndPoint(IPAddress.Any, 0); var bytes = _udp.EndReceive(ar, ref remote); var typeBytes = Beacon.Encode(BeaconType); if (Beacon.HasPrefix(bytes, typeBytes)) { try { _log.Information("Processing Response"); var portBytes = bytes.Skip(typeBytes.Length).Take(2).ToArray(); var port = (ushort)IPAddress.NetworkToHostOrder((short)BitConverter.ToUInt16(portBytes, 0)); var payload = Beacon.Decode(bytes.Skip(typeBytes.Length + 2)); NewBeacon(new BeaconLocation(new IPEndPoint(remote.Address, port), payload, DateTime.Now)); } catch (Exception ex) { _log.Error(ex, "Error on Decode Recived Beacon"); } } else { _log.Information("Incompatiple Data"); } _udp.BeginReceive(ResponseReceived, null); }
private void ResponseReceived(IAsyncResult ar) { var remote = new IPEndPoint(IPAddress.Any, 0); var bytes = udp.EndReceive(ar, ref remote); 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)); NewBeacon(new BeaconLocation(new IPEndPoint(remote.Address, port), payload, DateTime.Now)); } catch (Exception ex) { Debug.WriteLine(ex); } } udp.BeginReceive(ResponseReceived, null); }
private void ResponseReceived(IAsyncResult ar) { if (ar.AsyncState == null) { throw new InvalidOperationException("The ResponseReceived callback should have contained a UdpClient in AsyncState but it was null."); } var udp = (UdpClient)ar.AsyncState; var remote = new IPEndPoint(IPAddress.Any, 0); var bytes = udp.EndReceive(ar, ref remote); 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)); NewBeacon(new BeaconLocation(new IPEndPoint(remote.Address, port), payload, DateTime.Now)); } catch (Exception ex) { Debug.WriteLine(ex); } } udp.BeginReceive(ResponseReceived, udp); }