Пример #1
0
        private void ThreadPingSetup()
        {
            pingConnection = new Thread(() =>
            {
                Ping pingSender = new Ping();


                while (true)
                {
                    List <Person> listP = PersonOne.ToList();
                    Thread.Sleep(1500);
                    foreach (Person p in listP)
                    {
                        PingReply reply = pingSender.Send(IPAddress.Parse(p.IP));
                        bool b          = true;
                        if (reply.Status != IPStatus.Success)
                        {
                            Dispatcher.Invoke(new Action(() =>
                            {
                                PersonOne.Remove(p);
                            }));
                        }
                    }
                }
            });
            pingConnection.Start();
        }
Пример #2
0
        private Person searchIp(string ipforSearch)
        {
            List <Person> listTemp = PersonOne.ToList();

            foreach (Person p in listTemp)
            {
                if (p.IP.Equals(ipforSearch))
                {
                    return(p);
                }
            }
            return(null);
        }
Пример #3
0
        private void ThreadReceivingAnnSetup()
        {
            receivingAnnouncement = new Thread(() =>

            {
                //Creates a UdpClient for reading incoming data.
                UdpClient receivingUdpClient = new UdpClient(port);
                receivingUdpClient.JoinMulticastGroup(IPAddress.Parse("224.0.0.2"));
                //Creates an IPEndPoint to record the IP Address and port number of the sender.
                // The IPEndPoint will allow you to read datagrams sent from any source.
                IPEndPoint RemoteIpEndPoint        = new IPEndPoint(IPAddress.Any, port);
                receivingUdpClient.EnableBroadcast = true;
                receivingUdpClient.Client.SetIPProtectionLevel(IPProtectionLevel.Unrestricted);
                //receivingUdpClient.MulticastLoopback = false;
                try
                {
                    while (true)
                    {
                        int i        = 0;
                        byte[] name1 = null;
                        byte[] image = null;
                        int length   = 0;


                        // Blocks until a message returns on this socket from a remote host.

                        Byte[] receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint);
                        string ipforSearch  = RemoteIpEndPoint.Address.ToString();
                        length = BitConverter.ToInt32(receiveBytes, i);
                        if (ipforSearch.Equals(IPAddress.Loopback.ToString()))
                        {
                            ipforSearch = App.app.broadcastCalculation().ToString();
                        }

                        bool found = false;
                        if (ipforSearch.Equals(IPAddress.Loopback.ToString()))
                        {
                            length = 0;
                        }
                        IPAddress myIp = App.app.broadcastCalculation();
                        foreach (IPAddress ip in App.app.myIp)
                        {
                            if (ip.Equals(RemoteIpEndPoint.Address))
                            {
                                found = true;
                            }
                        }

                        if (!Properties.Settings.Default.DiscoverySelf && found)
                        {
                            length = 0;
                        }
                        //if (!myIp.Equals(IPAddress.Parse(ipforSearch)) && found) length=0;
                        if (length == 0)
                        {
                            Ping pingSender   = new Ping();
                            IPAddress address = RemoteIpEndPoint.Address;
                            PingReply reply   = pingSender.Send(address);
                            bool b            = true;

                            if (reply.Status == IPStatus.Success)
                            {
                                b = true;
                            }
                            else
                            {
                                b = false;
                            }

                            Dispatcher.Invoke(new Action(() =>
                            {
                                List <Person> tmp = PersonOne.ToList();
                                foreach (Person p in tmp)
                                {
                                    if (p.IP.Equals(ipforSearch))
                                    {
                                        if (!p.tick || !b)
                                        {
                                            foreach (ToSend o in send_list.ToList())
                                            {
                                                if (o.ip.Equals(ipforSearch))
                                                {
                                                    insertDeleteSendList(o, false);
                                                }
                                            }
                                            PersonOne.Remove(p);
                                        }
                                        else
                                        {
                                            foreach (ToSend o in send_list.ToList())
                                            {
                                                if (o.ip.Equals(ipforSearch))
                                                {
                                                    if (o.bkgWorker == null)
                                                    {
                                                        insertDeleteSendList(o, false);
                                                        PersonOne.Remove(p);
                                                        break;
                                                    }

                                                    o.o     = PersonOne;
                                                    o.index = p;
                                                }
                                            }
                                        }
                                    }
                                }
                            }));
                            continue;
                        }
                        i    += 4;
                        name1 = new byte[length];


                        for (int j = 0; j < length; j++)
                        {
                            name1[j] = receiveBytes[i + j];
                        }
                        i     += length;
                        length = BitConverter.ToInt32(receiveBytes, i);

                        //  MemoryStream stream = receiveImage(RemoteIpEndPoint.Address);
                        person = searchIp(ipforSearch);



                        if (person != null)
                        {
                            if (person.tick)
                            {
                                continue;
                            }
                        }
                        if (person == null || person.change != length)
                        {
                            image = receiveImage(RemoteIpEndPoint.Address);

                            Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
                            {
                                if (person != null)
                                {
                                    int index = PersonOne.IndexOf(person);
                                    bool tick = person.tick;
                                    PersonOne.RemoveAt(index);
                                    person        = new Person();
                                    person.Name   = Encoding.ASCII.GetString(name1);
                                    person.IP     = RemoteIpEndPoint.Address.ToString();
                                    person.Image  = ByteArraytoBitmap(image);//StreamtoBitmap(stream);// new BitmapImage(
                                    person.change = length;
                                    person.tick   = tick;
                                    PersonOne.Insert(index, person);
                                    return;
                                }
                                person        = new Person();
                                person.Name   = Encoding.ASCII.GetString(name1);
                                person.change = length;
                                person.tick   = false;
                                person.IP     = RemoteIpEndPoint.Address.ToString();
                                person.Image  = ByteArraytoBitmap(image);//StreamtoBitmap(stream);// new BitmapImage(Uri);
                                PersonOne.Add(person);
                            }));
                        }
                        else
                        {
                            if (Encoding.ASCII.GetString(name1).Equals(person.Name))
                            {
                                continue;
                            }
                            Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Send, new Action(() =>
                            {
                                int index = PersonOne.IndexOf(person);
                                PersonOne.RemoveAt(index);
                                person.Name = Encoding.ASCII.GetString(name1);
                                PersonOne.Insert(index, person);
                            }));
                        }
                    }
                }
                catch (Exception)
                { }
                finally {
                    receivingUdpClient.Close();
                }
            });
        }