示例#1
0
        /// <summary>
        /// On UDP socket tasked datagram receive
        /// </summary>
        protected override void OnTaskedDatagramReceive(Datagram datagram)
        {
            if (!players.TryGetValue(datagram.ipEndPoint, out TPlayer player))
            {
                if (datagram.Type == (byte)UdpRequestResponseType.Connect)
                {
                    OnConnectRequest(datagram);
                }
            }
            else
            {
                switch (datagram.Type)
                {
                default:
                    DisconnectPlayer(player.ID, DisconnectUdpResponse.ReasonType.UnknownDatagram);
                    break;

                case (byte)UdpRequestResponseType.Connect:
                    if (logger.Log(LogType.Debug))
                    {
                        logger.Debug($"Receive second UDP room connect request. (id:{player.ID}, remoteEndPoint: {datagram.ipEndPoint}, roomId: {id})");
                    }
                    break;

                case (byte)UdpRequestResponseType.Disconnect:
                    DisconnectPlayer(player.ID, DisconnectUdpResponse.ReasonType.Requested);
                    break;
                }
            }
        }
        /// <summary>
        /// On UDP socket tasked datagram receive
        /// </summary>
        protected override void OnTaskedDatagramReceive(Datagram datagram)
        {
            if (!players.TryGetValue(datagram.IpEndPoint, out TPlayer player))
            {
                if (datagram.Type != (byte)RoomDatagramType.Connect)
                {
                    return;
                }

                try
                {
                    OnConnectRequest(new ConnectUdpRequest(datagram));
                }
                catch
                {
                    if (logger.Log(LogType.Debug))
                    {
                        logger.Debug($"Bad room connect UDP request. (remoteEndPoint: {datagram.IpEndPoint}, roomID: {id})");
                    }
                }
            }
            else
            {
                try
                {
                    switch (datagram.Type)
                    {
                    default:
                        DisconnectPlayer(player.ID, DisconnectUdpResponse.ReasonType.UnknownDatagram);
                        break;

                    case (byte)RoomDatagramType.Ping:
                        OnPingRequest(player);
                        break;

                    case (byte)RoomDatagramType.Connect:
                        if (logger.Log(LogType.Debug))
                        {
                            logger.Debug($"Received second room connect UDP request. (id:{player.ID}, remoteEndPoint: {datagram.IpEndPoint}, roomId: {id})");
                        }
                        break;

                    case (byte)RoomDatagramType.Disconnect:
                        DisconnectPlayer(player.ID, DisconnectUdpResponse.ReasonType.Requested);
                        break;
                    }
                }
                catch (Exception exception)
                {
                    DisconnectPlayer(player.ID, DisconnectUdpResponse.ReasonType.BadDatagram);

                    if (logger.Log(LogType.Warning))
                    {
                        logger.Warning($"Bad room UDP request. (type: {datagram.Type}, accountID:{player.ID}, remoteEndPoint: {datagram.IpEndPoint}, roomID: {id}) {exception}");
                    }
                }
            }
        }