Exemple #1
0
        // I call this when the Lobby screen is loaded in my game
        public bool ClientRunActiveDiscovery()
        {
            if (!SupportedOnThisPlatform)
            {
                return(false);
            }

            StopDiscovery();

            // Refresh the NIC list on entry to the lobby screen, could refresh on a timer if desired
            cachedIPs = IPAddressUtility.GetBroadcastAdresses();

            try {
                // Setup port
                clientUdpClient = new UdpClient(0);
                clientUdpClient.EnableBroadcast   = true;
                clientUdpClient.MulticastLoopback = false;
            } catch (Exception e) {
                Debug.LogException(e);
                // Free the port if we took it
                if (clientUdpClient != null)
                {
                    NetworkDiscoveryUtility.RunSafe(() => {
                        ShutdownUdpClients();
                    });
                }
                return(false);
            }

            StartCoroutine(ClientListenCoroutine());
            StartCoroutine(ClientBroadcastCoroutine());

            return(true);
        }
Exemple #2
0
        // I call this from my NetworkManage::OnStartServer
        public bool ServerPassiveBroadcastGame(byte[] serverBroadcastPacket)
        {
            if (!SupportedOnThisPlatform)
            {
                return(false);
            }

            StopDiscovery();

            try {
                // Setup port
                serverUdpClient = new UdpClient(m_serverBroadcastListenPort);
                serverUdpClient.EnableBroadcast   = true;
                serverUdpClient.MulticastLoopback = false;
            } catch (Exception e) {
                Debug.LogException(e);
                // Free the port if we took it
                if (serverUdpClient != null)
                {
                    NetworkDiscoveryUtility.RunSafe(() => {
                        ShutdownUdpClients();
                    });
                }
                return(false);
            }

            this.serverBroadcastPacket = serverBroadcastPacket;

            // TODO add coroutine to refresh server stats infrequently/make external update pathway for server to update broadcast packet
            StartCoroutine(ServerListenCoroutine());

            return(true);
        }
Exemple #3
0
        void OnReceivedServerResponse(DiscoveryInfo info)
        {
            // Validation is our capacity to decode the message, if the payload is so different we cant parse it we silently dump it!
            NetworkDiscoveryUtility.RunSafe(() => {
                info.unpackedData = (GameBroadcastPacket)ByteStreamer.StreamFromBytes(info.packetData);
            }, false);

            if (info.unpackedData != null)
            {
                onReceivedServerResponse(info);
            }
        }
Exemple #4
0
        IEnumerator ClientBroadcastCoroutine()
        {
            while (true)
            {
                if (clientUdpClient == null)
                {
                    continue;
                }

                NetworkDiscoveryUtility.RunSafe(() => {
                    SendClientBroadcast();
                });

                yield return(new WaitForSecondsRealtime(m_ActiveDiscoverySecondInterval));
            }
        }
Exemple #5
0
        IEnumerator ClientListenCoroutine()
        {
            while (true)
            {
                yield return(new WaitForSecondsRealtime(0.3f));

                if (clientUdpClient == null)
                {
                    continue;
                }

                NetworkDiscoveryUtility.RunSafe(() => {
                    var info = ReadDataFromUdpClient(clientUdpClient);
                    if (info != null)
                    {
                        OnReceivedServerResponse(info);
                    }
                });
            }
        }
Exemple #6
0
        IEnumerator ServerListenCoroutine()
        {
            while (true)
            {
                yield return(new WaitForSecondsRealtime(0.3f));

                // average time for this (including data receiving and processing): less than 100 us
                Profiler.BeginSample("Receive broadcast");

                NetworkDiscoveryUtility.RunSafe(() => {
                    var info = ReadDataFromUdpClient(serverUdpClient);
                    if (info != null)
                    {
                        ServerOnClientBroadcast(info);
                    }
                });

                Profiler.EndSample();
            }
        }
Exemple #7
0
        public static IPAddress[] GetBroadcastAdresses()
        {
            // try multiple methods - because some of them may fail on some devices, especially if IL2CPP comes into play
            IPAddress[] ips = null;

            NetworkDiscoveryUtility.RunSafe(() => ips = GetBroadcastAdressesFromNetworkInterfaces(), false);

            if (ips == null || ips.Length < 1)
            {
                // try another method
                NetworkDiscoveryUtility.RunSafe(() => ips = GetBroadcastAdressesFromHostEntry(), false);
            }

            if (ips == null || ips.Length < 1)
            {
                // all methods failed, or there is no network interface on this device
                // just use full-broadcast address
                ips = new IPAddress[] { IPAddress.Broadcast };
            }

            return(ips);
        }