/// <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 ReceiveFromFindLocalNetworkBroadcast(CachedUdpClient localListingsClient)
        {
            IPEndPoint groupEp  = default;
            string     endpoint = string.Empty;
            var        data     = localListingsClient.Receive(ref groupEp, ref endpoint);

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

            ParseFindLocalNetworkBroadcastResponse(endpoint, data);
        }
Пример #3
0
        public static void SetupLocalUdpListings()
        {
            if (LocalConnections == null)
            {
                LocalConnections = new List <BroadcastEndpoints>();
            }

            lock (LocalConnections)
            {
                LocalConnections.Clear();
            }

            localListingsClient = new CachedUdpClient(19375);
            localListingsClient.EnableBroadcast = true;
            Task.Queue(() => { CloseLocalListingsClient(); }, 1000);

            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"), DEFAULT_PORT));

                try
                {
                    while (localListingsClient != null && !ExitingApplication)
                    {
                        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)
                        {
                            LocalConnections.Add(new BroadcastEndpoints(address, port, true));
                        }
                        else if (data[0] == CLIENT_BROADCAST_CODE)
                        {
                            LocalConnections.Add(new BroadcastEndpoints(address, port, false));
                        }
                    }
                }
                catch { }
            });
        }