예제 #1
0
        public static void Send(IPAddress source, IPAddress destination, string verb, int fingerprint)
        {
            using (UdpClient client = new UdpClient(new IPEndPoint(source, 0)))
            {
                Data.Packet packet = new Data.Packet();
                packet.MachineName = Environment.MachineName;
                packet.Fingerprint = fingerprint;
                packet.Verb = verb;

                JavaScriptSerializer serializer = new JavaScriptSerializer();
                string jsonData = serializer.Serialize(packet);
                byte[] bytes = Encoding.ASCII.GetBytes(jsonData);

                IPEndPoint ip = new IPEndPoint(destination, Config.Port);
                try
                {
                    client.Send(bytes, bytes.Length, ip);
                }
                catch (SocketException ex)
                {
                    if (ex.SocketErrorCode == SocketError.HostUnreachable)
                        //reasoning: we broadcast on all interfaces
                        //on OS X this may result in No route to host
                        Console.WriteLine(String.Format("{0}: {1}", source, ex.Message));
                    else
                        throw;
                }
                finally
                {
                    client.Close();
                }
            }
        }
예제 #2
0
        public static void Send(IPAddress ipAddress, string verb, int fingerprint)
        {
            using (UdpClient client = new UdpClient())
            {
                Data.Packet packet = new Data.Packet();
                packet.MachineName = Environment.MachineName;
                packet.Fingerprint = fingerprint;
                packet.Verb        = verb;

                JavaScriptSerializer serializer = new JavaScriptSerializer();
                string jsonData = serializer.Serialize(packet);
                byte[] bytes    = Encoding.ASCII.GetBytes(jsonData);

                IPEndPoint ip = new IPEndPoint(ipAddress, Config.Port);
                client.Send(bytes, bytes.Length, ip);
                client.Close();
            }
        }
예제 #3
0
        public static void Send(IPAddress ipAddress, string verb, int fingerprint)
        {
            using (UdpClient client = new UdpClient())
            {
                Data.Packet packet = new Data.Packet();
                packet.MachineName = Environment.MachineName;
                packet.Fingerprint = fingerprint;
                packet.Verb = verb;

                JavaScriptSerializer serializer = new JavaScriptSerializer();
                string jsonData = serializer.Serialize(packet);
                byte[] bytes = Encoding.ASCII.GetBytes(jsonData);

                IPEndPoint ip = new IPEndPoint(ipAddress, Config.Port);
                client.Send(bytes, bytes.Length, ip);
                client.Close();
            }
        }
예제 #4
0
        private static void Send(IPAddress source, IPAddress destination, string verb, int fingerprint)
        {
            using (UdpClient client = new UdpClient(new IPEndPoint(source, 0)))
            {
                Data.Packet packet = new Data.Packet();
                packet.MachineName = Environment.MachineName;
                packet.Fingerprint = fingerprint;
                packet.Verb        = verb;

                JavaScriptSerializer serializer = new JavaScriptSerializer();
                string jsonData = serializer.Serialize(packet);
                byte[] bytes    = Encoding.ASCII.GetBytes(jsonData);

                IPEndPoint ip = new IPEndPoint(destination, Config.Port);
                try
                {
                    client.Send(bytes, bytes.Length, ip);
                }
                catch (SocketException ex)
                {
                    if (ex.SocketErrorCode == SocketError.HostUnreachable)
                    {
                        //reasoning: we broadcast on all interfaces
                        //on OS X this may result in No route to host
                        Console.WriteLine(String.Format("{0}: {1}", source, ex.Message));
                    }
                    else
                    {
                        throw;
                    }
                }
                finally
                {
                    client.Close();
                }
            }
        }
예제 #5
0
        private void ProcessReceived(IPEndPoint source, byte[] bytes)
        {
            //only response to discovery on the local network since we transfer fingerprints
            if ((source.AddressFamily != AddressFamily.InterNetwork) &&
                (source.AddressFamily != AddressFamily.InterNetworkV6))
            {
                return;
            }

            string jsonData = Encoding.ASCII.GetString(bytes);

            JavaScriptSerializer serializer = new JavaScriptSerializer();

            Data.Packet packet = serializer.Deserialize <Data.Packet>(jsonData);

            if (packet.Verb.Equals(Verbs.Online))
            {
                string ipAddress = source.Address.ToString();
                if (!instances.Any(i => i.IpAddress.Equals(ipAddress)))
                {
                    Data.Instance instance = new Data.Instance
                    {
                        IpAddress   = ipAddress,
                        MachineName = packet.MachineName,
                        Fingerprint = packet.Fingerprint
                    };

                    //lock for thread-safety - collection may be modified
                    lock (instances)
                    {
                        instances.Add(instance);
                    }

                    Sender.Send(source.Address, Verbs.Online, fingerprint);

                    if (InstanceOnline != null)
                    {
                        InstanceOnline(this, new InstanceChangedArgs {
                            Instance = instance
                        });
                    }
                }
            }
            else if (packet.Verb.Equals(Verbs.Offline))
            {
                string ipAddress = source.Address.ToString();

                //lock for thread-safety - collection may be modified
                lock (instances)
                {
                    Data.Instance instance = instances.SingleOrDefault(i => i.IpAddress.Equals(ipAddress));
                    if (instance != null)
                    {
                        instances.Remove(instance);

                        if (InstanceOffline != null)
                        {
                            InstanceOffline(this, new InstanceChangedArgs {
                                Instance = instance
                            });
                        }
                    }
                }
            }
        }
예제 #6
0
        private void ProcessReceived(IPEndPoint source, byte[] bytes)
        {
            //only response to discovery on the local network since we transfer fingerprints
            if ((source.AddressFamily != AddressFamily.InterNetwork) &&
                (source.AddressFamily != AddressFamily.InterNetworkV6))
            {
                return;
            }

            string jsonData = Encoding.ASCII.GetString(bytes);

            JavaScriptSerializer serializer = new JavaScriptSerializer();

            Data.Packet packet = serializer.Deserialize <Data.Packet>(jsonData);

            if (packet.Verb.Equals(Verbs.Online))
            {
                //lock for thread-safety, otherwise the dupe check below may not be sound
                lock (instances)
                {
                    //search by MachineName and Fingerprint - these are unique while IP address may not be
                    //reasoning - the same machine may have multiple IP addresses as discovery supports multiple interfaces
                    if (!instances.Any(i => i.MachineName.Equals(packet.MachineName) && (i.Fingerprint == packet.Fingerprint)))
                    {
                        Data.Instance instance = new Data.Instance
                        {
                            IpAddress   = source.Address.ToString(),
                            MachineName = packet.MachineName,
                            Fingerprint = packet.Fingerprint
                        };

                        instances.Add(instance);

                        Sender.Send(source.Address, Verbs.Online, fingerprint);

                        if (InstanceOnline != null)
                        {
                            InstanceOnline(this, new InstanceChangedArgs {
                                Instance = instance
                            });
                        }
                    }
                }
            }
            else if (packet.Verb.Equals(Verbs.Offline))
            {
                string ipAddress = source.Address.ToString();

                //lock for thread-safety - collection may be modified
                lock (instances)
                {
                    //search by MachineName and Fingerprint - these are unique while IP address may not be
                    //reasoning - the same machine may have multiple IP addresses as discovery supports multiple interfaces
                    Data.Instance instance = instances.SingleOrDefault(i => i.MachineName.Equals(packet.MachineName) && (i.Fingerprint == packet.Fingerprint));
                    if (instance != null)
                    {
                        instances.Remove(instance);

                        if (InstanceOffline != null)
                        {
                            InstanceOffline(this, new InstanceChangedArgs {
                                Instance = instance
                            });
                        }
                    }
                }
            }
        }