/// <summary>
        /// A method to find all of the local UDP servers and clients on the network
        /// </summary>
        public static void RefreshLocalUdpListings(ushort portNumber = DEFAULT_PORT, int responseBuffer = 1000)
        {
            // Initialize the list to hold all of the local network endpoints that respond to the request
            if (LocalEndpoints == null)
            {
                LocalEndpoints = new List <BroadcastEndpoints>();
            }

            // Make sure to clear out the existing endpoints
            lock (LocalEndpoints)
            {
                LocalEndpoints.Clear();
            }

            // Create a client to write on the network and discover other clients and servers
            localListingsClient = new CachedUdpClient(19375);
            localListingsClient.EnableBroadcast = true;
            Task.Queue(() => { CloseLocalListingsClient(); }, responseBuffer);

            Task.Queue(() =>
            {
                IPEndPoint groupEp = default(IPEndPoint);
                string endpoint    = string.Empty;

                localListingsClient.Send(new byte[] { BROADCAST_LISTING_REQUEST_1, BROADCAST_LISTING_REQUEST_2, BROADCAST_LISTING_REQUEST_3 }, 3, new IPEndPoint(IPAddress.Parse("255.255.255.255"), portNumber));

                try
                {
                    while (localListingsClient != null && !EndingSession)
                    {
                        var data = localListingsClient.Receive(ref groupEp, ref endpoint);

                        if (data.Size != 1)
                        {
                            continue;
                        }

                        string[] parts = endpoint.Split('+');
                        string address = parts[0];
                        ushort port    = ushort.Parse(parts[1]);
                        if (data[0] == SERVER_BROADCAST_CODE)
                        {
                            var ep = new BroadcastEndpoints(address, port, true);
                            LocalEndpoints.Add(ep);

                            if (localServerLocated != null)
                            {
                                localServerLocated(ep);
                            }
                        }
                        else if (data[0] == CLIENT_BROADCAST_CODE)
                        {
                            LocalEndpoints.Add(new BroadcastEndpoints(address, port, false));
                        }
                    }
                }
                catch { }
            });
        }
예제 #2
0
        private static void AddFoundLocalNetworkEndpoint(string address, ushort port)
        {
            var ep = new BroadcastEndpoints(address, port, true);

            LocalEndpoints.Add(ep);

            if (localServerLocated != null)
            {
                localServerLocated(ep, null);
            }
        }