Ejemplo n.º 1
0
 public void Discover()
 {
     lock (lockObject)
     {
         DiscoveryMessage response = new DiscoveryMessage {
             Command = DiscoveryCommand.HelloRequest
         };
         foreach (var seed in _fixSeedIPs)
         {
             var ip = new IPEndPoint(IPAddress.Parse(seed), 15000);
             response.Command = DiscoveryCommand.HelloRequest;
             udp.Send(response.ToByteArray(), response.Length, ip);
         }
         foreach (var point in endPoints.ToArray())
         {
             response.Command = DiscoveryCommand.HelloRequest;
             udp.Send(response.ToByteArray(), response.Length, point);
         }
         endPoints.Clear();
     }
 }
Ejemplo n.º 2
0
        private void Receive(IAsyncResult ar)
        {
            IPEndPoint ip = new IPEndPoint(IPAddress.Any, 15000);

            byte[]           bytes    = udp.EndReceive(ar, ref ip);
            DiscoveryMessage message  = new DiscoveryMessage(bytes);
            DiscoveryMessage response = new DiscoveryMessage();

            if (message.Command == DiscoveryCommand.HelloRequest)
            {
                // byte[] send = Encoding.ASCII.GetBytes("server");
                response.Command = DiscoveryCommand.NodeListRequest;
                response.Payload = null;
                udp.Send(response.ToByteArray(), response.Length, ip);
            }
            if (message.Command == DiscoveryCommand.HelloResponse)
            {
                response.Command = DiscoveryCommand.NodeListRequest;
                response.Payload = null;
                udp.Send(response.ToByteArray(), response.Length, ip);
            }
            if (message.Command == DiscoveryCommand.NodeListRequest)
            {
                using (MemoryStream m = new MemoryStream())
                {
                    //SslStream stream = new SslStream(m);
                    StreamWriter sw = new StreamWriter(m);
                    foreach (var p in GetEndPoints().ToArray())
                    {
                        sw.Write(p.Address.ToString());
                        sw.Write(':');
                        sw.Write(p.Port);
                        sw.Write(';');
                    }
                    sw.Flush();
                    response.Command = DiscoveryCommand.NodeListResponse;
                    response.Payload = m.ToArray();
                    udp.Send(response.ToByteArray(), response.Length, ip);
                    sw.Dispose();
                }
            }
            else if (message.Command == DiscoveryCommand.NodeListResponse)
            {
                lock (lockObject)
                {
                    string[] ips = message.ToString().Split(';');
                    foreach (string p in ips)
                    {
                        if (p == "")
                        {
                            continue;
                        }
                        string[] ps = p.Split(':');
                        if (endPoints.Count(p1 => p1.Address.ToString() == ps[0] && p1.Port == Convert.ToInt32(ps[1])) == 0)
                        {
                            endPoints.Add(new IPEndPoint(IPAddress.Parse(ps[0]), Convert.ToInt32(ps[1])));
                        }
                    }
                }
            }

            lock (lockObject)
            {
                if (endPoints.Count(p => p.Address.ToString() == ip.Address.ToString() && p.Port == ip.Port) == 0)
                {
                    endPoints.Add(ip);
                }
            }

            StartListening();
        }