Beispiel #1
0
 public static byte[] Encode(BeaconMessage beaconMessage)
 {
     return(InternalEncode(BeaconMessageTag)
            .Concat(InternalEncode(beaconMessage.ServiceId))
            .Concat(BitConverter.GetBytes((ushort)IPAddress.HostToNetworkOrder((short)beaconMessage.Port)))
            .Concat(InternalEncode(beaconMessage.ServiceName))
            .ToArray());
 }
Beispiel #2
0
        public Beacon(IPAddress localIp, string serviceId, ushort port, string serviceName)
        {
            Message = new BeaconMessage(serviceId, port, serviceName);

            _udp = new UdpClient(new IPEndPoint(localIp, Protocol.DiscoveryPort));
            _udp.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
            _udp.AllowNatTraversal(true);

            _udp.BeginReceive(Receive, null);
        }
Beispiel #3
0
        private void AddLocation(BeaconMessage message, IPEndPoint address)
        {
            // Modifications to the collection are done in the thread that created the Probe
            // To ensure handler to the CollectionChanged event are not surprised
            var location = new BeaconLocation(message, address);

            _callerThread.Send(s =>
            {
                var existingLocation = _locations.FirstOrDefault(bl => bl.Equals(location));
                if (existingLocation != null)
                {
                    existingLocation.RefreshTime();
                }
                else
                {
                    _locations.Add(location);
                }
            }, null);
        }
Beispiel #4
0
        public static bool TryDecodeBeaconMessage(byte[] data, out BeaconMessage beaconMessage)
        {
            string tag = InternalDecode(data, out int index);

            if (!BeaconMessageTag.Equals(tag))
            {
                beaconMessage = new BeaconMessage();
                return(false);
            }

            string serviceId = InternalDecode(data, index, out index);

            ushort port = (ushort)IPAddress.NetworkToHostOrder((short)BitConverter.ToUInt16(data, index));

            index += sizeof(ushort);

            string serviceName = InternalDecode(data, index);

            beaconMessage = new BeaconMessage(serviceId, port, serviceName);
            return(true);
        }
Beispiel #5
0
 public BeaconLocation(BeaconMessage message, IPEndPoint remote)
 {
     ServiceName = message.ServiceName;
     Address     = new IPEndPoint(remote.Address, message.Port);
     RefreshTime();
 }