Exemple #1
0
        /// <summary>Enumerates the auto discoverable servers on the network using the specified data on the specified port.</summary>
        /// <param name="port">The port the server will use to listen for auto discover requests.</param>
        /// <param name="data">The data the server will use to identify auto discover requests.</param>
        /// <returns>An <see cref="IEnumerable{ServerInfo}"/> that includes information about the server.</returns>
        public static IEnumerable <ServerInfo> Discover(int port, byte[] data)
        {
            IPEndPoint broadcast  = new IPEndPoint(IPAddress.Broadcast, port);
            UdpClient  connection = new UdpClient()
            {
                EnableBroadcast = true
            };

            try
            {
                connection.SafeSend(data, broadcast);

                byte[] responseData = connection.SafeReceive(ref broadcast);

                while (responseData != null)
                {
                    var info = Create(responseData);
                    if (info != null)
                    {
                        yield return(info);
                    }

                    broadcast    = new IPEndPoint(IPAddress.Broadcast, port);
                    responseData = connection.SafeReceive(ref broadcast);
                }
            }
            finally
            {
                try { connection.Close(); }
                catch (Exception ex)
                {
                    Logger.Write(LogLevel.Error, "An error occurred closing the auto-discovery connection. Error: {0}", JsonConvert.SerializeObject(ex));
                }
            }
        }