Exemple #1
0
        private void ResponseReceived(UdpReceiveResult ar)                       //IAsyncResult ar)
        {
            var    remote    = ar.RemoteEndPoint;                                //new IPEndPoint(IPAddress.Any, 0);
            var    bytes     = ar.Buffer;                                        // udp.EndReceive(ar, ref remote);
            string value     = new DatagramPacket(bytes).Decode();
            var    typeBytes = new DatagramPacket(BeaconType).Encode().ToList(); //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   = new DatagramPacket(bytes.Skip(typeBytes.Count() + 2)).Decode(); //Beacon.Decode(bytes.Skip(typeBytes.Count() + 2));
                    NewBeacon(new Core.BeaconLocation(new IPEndPoint(remote.Address, port), payload, DateTime.Now));
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex);
                }
            }

            //udp.BeginReceive(ResponseReceived, null);
            Listen();
        }
Exemple #2
0
        private void BroadcastProbe()
        {
            var probe = new DatagramPacket(BeaconType).Encode().ToArray(); //Beacon.Encode(BeaconType).ToArray();

            udp.SendAsync(probe, probe.Length, new IPEndPoint(IPAddress.Broadcast, Beacon.DiscoveryPort));
            //udp.Send(probe, probe.Length, new IPEndPoint(IPAddress.Broadcast, Beacon.DiscoveryPort));
        }
Exemple #3
0
        private void ProbeReceived(UdpReceiveResult ar) //IAsyncResult ar)
        {
            var remote = ar.RemoteEndPoint;             //new IPEndPoint(IPAddress.Any, 0);
            var bytes  = ar.Buffer;

            // Compare beacon type to probe type
            var typeBytes = new DatagramPacket(this._config.BeaconType).Encode();

            if (HasPrefix(bytes, typeBytes))
            {
                // If true, respond again with our type, port and payload
                var responseData = new DatagramPacket(this._config.BeaconType).Encode() //Encode(BeaconType)
                                   .Concat(BitConverter.GetBytes((ushort)IPAddress.HostToNetworkOrder((short)this._config.AdvertisedPort)))
                                   .Concat(new DatagramPacket(this._config.BeaconData).Encode()).ToArray();
                //udp.Send(responseData, responseData.Length, remote);
                var data = new DatagramPacket(responseData).Decode();
                udp.SendAsync(responseData, responseData.Length, remote);
            }

            if (!Stopped)
            {
                Listen();          // udp.BeginReceive(ProbeReceived, null);
            }
        }