Exemplo n.º 1
0
 internal SearchReceivedEventArgs(Messages.DiscoveryMessage message, System.Net.IPEndPoint sender)
 {
     ServiceType     = message.Service.ServiceType;
     MaxWaitTime     = message.MaxWaitTime;
     ReceivedContent = message.Content;
     Sender          = sender;
 }
Exemplo n.º 2
0
        public IList <Service> Discover(string serviceType)
        {
            var msg = new Messages.DiscoveryMessage();

            msg.Service             = new Service();
            msg.Service.ServiceType = serviceType;
            msg.MaxWaitTime         = MaxWaitTime;

            var result = new List <Service>();

            using (var client = new UdpClient())
            {
                var buffer = msg.ToArray();
                for (int i = 0; i < MessageCount; i++)
                {
                    client.Send(buffer, buffer.Length, new IPEndPoint(DiscoveryAddress, Port));
                }

                while (true)
                {
                    client.Client.ReceiveTimeout = (int)(DiscoveryTimeout).TotalMilliseconds;
                    var e = new IPEndPoint(IPAddress.Any, 0);
                    try
                    {
                        buffer = client.Receive(ref e);
                    }
                    catch (SocketException ex)
                    {
                        if (ex.SocketErrorCode == SocketError.TimedOut)
                        {
                            if (result.Count < MinDiscoveryCount)
                            {
                                continue;
                            }
                            else
                            {
                                break;
                            }
                        }
                    }

                    var response = Parser.Parse(buffer);
                    if (response != null && response is Messages.DiscoveryResponseMessage)
                    {
                        HandleMessage(response, e);

                        if (!result.Contains(response.Service))
                        {
                            result.Add(response.Service);
                        }
                    }
                }
            }

            return(result);
        }
Exemplo n.º 3
0
        public IList<Service> Discover(string serviceType)
        {
            var msg = new Messages.DiscoveryMessage();
            msg.Service = new Service();
            msg.Service.ServiceType = serviceType;
            msg.MaxWaitTime = MaxWaitTime;

            var result = new List<Service>();

            using (var client = new UdpClient())
            {
                var buffer = msg.ToArray();
                for (int i = 0; i < MessageCount; i++)
                {
                    client.Send(buffer, buffer.Length, new IPEndPoint(DiscoveryAddress, Port));
                }

                while (true)
                {
                    client.Client.ReceiveTimeout = (int)(DiscoveryTimeout).TotalMilliseconds;
                    var e = new IPEndPoint(IPAddress.Any, 0);
                    try
                    {
                        buffer = client.Receive(ref e);
                    }
                    catch (SocketException ex)
                    {
                        if (ex.SocketErrorCode == SocketError.TimedOut)
                        {
                            if (result.Count < MinDiscoveryCount)
                                continue;
                            else
                                break;
                        }
                    }

                    var response = Parser.Parse(buffer);
                    if (response != null && response is Messages.DiscoveryResponseMessage)
                    {
                        HandleMessage(response, e);

                        if (!result.Contains(response.Service))
                            result.Add(response.Service);
                    }
                }
            }

            return result;
        }