Esempio n. 1
0
        /// <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)
        {
            CloseLocalListingsClient();

            if (LocalEndpoints == null)
            {
                LocalEndpoints = new List <BroadcastEndpoints>();
            }
            else
            {
                lock (LocalEndpoints)
                {
                    LocalEndpoints.Clear();
                }
            }

            foreach (IPAddress ipAddress in GetLocalIPs())
            {
                // Create a client to write on the network and discover other clients and servers
                CachedUdpClient localListingsClient = new CachedUdpClient(new IPEndPoint(ipAddress, 19375));
                localListingsClient.EnableBroadcast = true;
                AddNewLocalListingClient(localListingsClient);

                Task.Queue(CloseLocalListingsClient, responseBuffer);

                Task.Queue(() =>
                {
                    SendFindLocalNetworkBroadcast(localListingsClient, portNumber);
                });
            }
        }
        /// <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 { }
            });
        }
Esempio n. 3
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);
            }
        }
Esempio n. 4
0
        private static void ParseFindLocalNetworkBroadcastResponse(string endpoint, BMSByte data)
        {
            string[] parts   = endpoint.Split('+');
            string   address = parts[0];

            if (ushort.TryParse(parts[1], out var port))
            {
                if (data[0] == SERVER_BROADCAST_CODE)
                {
                    AddFoundLocalNetworkEndpoint(address, port);
                }
                else if (data[0] == CLIENT_BROADCAST_CODE)
                {
                    LocalEndpoints.Add(new BroadcastEndpoints(address, port, false));
                }
            }
        }
Esempio n. 5
0
        /// <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)
        {
            lock (localListingsClientList)
            {
                foreach (CachedUdpClient cachedUdpClient in localListingsClientList)
                {
                    cachedUdpClient.Client.Close();
                }
                localListingsClientList.Clear();
            }

            // 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();
            }

            foreach (IPAddress ipAddress in GetLocalIPs())
            {
                // Create a client to write on the network and discover other clients and servers
                CachedUdpClient localListingsClient = new CachedUdpClient(new IPEndPoint(ipAddress, portNumber));
                localListingsClient.createTime = DateTime.Now;

                localListingsClient.EnableBroadcast = true;
                lock (localListingsClientList)
                {
                    localListingsClientList.Add(localListingsClient);
                }
                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)
                        {
                            localListingsClient.ReceiveLanDiscovery(ref groupEp, ref endpoint, ipAddress.ToString(), Convert.ToInt32(portNumber));
                        }
                    }
                    catch (ObjectDisposedException disposedEx)
                    {
                        //BeardedManStudios.Forge.Logging.BMSLog.Log("Socket is disposed already.");
                        //lastDebugMessage = "Socket is disposed";
                    }

                    catch (Exception e)
                    {
                        //BeardedManStudios.Forge.Logging.BMSLog.Log("Exception Caught message: " + e.ToString());
                        //lastDebugMessage = e.ToString();
                    }
                });
            }
        }