예제 #1
0
        async UniTaskVoid ConnectionAcceptedAsync(INetworkPlayer player)
        {
            if (logger.LogEnabled())
            {
                logger.Log("Server accepted client:" + player);
            }

            //Only allow host client to connect when not Listening for new connections
            if (!Listening && player != LocalPlayer)
            {
                return;
            }

            // are more connections allowed? if not, kick
            // (it's easier to handle this in Mirage, so Transports can have
            //  less code and third party transport might not do that anyway)
            // (this way we could also send a custom 'tooFull' message later,
            //  Transport can't do that)
            if (Players.Count >= MaxConnections)
            {
                player.Connection?.Disconnect();
                if (logger.WarnEnabled())
                {
                    logger.LogWarning("Server full, kicked client:" + player);
                }
                return;
            }

            // add connection
            AddConnection(player);

            // let everyone know we just accepted a connection
            Connected?.Invoke(player);

            // now process messages until the connection closes
            try
            {
                await player.ProcessMessagesAsync();
            }
            catch (Exception ex)
            {
                logger.LogException(ex);
            }
            finally
            {
                OnDisconnected(player);
            }
        }