コード例 #1
0
        private void OnReceiveData(List <Packet.Packet> packets)
        {
            // We received packets from the server, which means the server is still alive
            OnHeartBeat?.Invoke();

            foreach (var packet in packets)
            {
                // Create a ClientUpdatePacket from the raw packet instance,
                // and read the values into it
                var clientUpdatePacket = new ClientUpdatePacket(packet);
                clientUpdatePacket.ReadPacket();

                UpdateManager.OnReceivePacket(clientUpdatePacket);

                _packetManager.HandleClientPacket(clientUpdatePacket);
            }
        }
コード例 #2
0
        /// <summary>
        /// Callback method for when the net client receives data.
        /// </summary>
        /// <param name="packets">A list of raw received packets.</param>
        private void OnReceiveData(List <Packet.Packet> packets)
        {
            foreach (var packet in packets)
            {
                // Create a ClientUpdatePacket from the raw packet instance,
                // and read the values into it
                var clientUpdatePacket = new ClientUpdatePacket(packet);
                if (!clientUpdatePacket.ReadPacket())
                {
                    // If ReadPacket returns false, we received a malformed packet, which we simply ignore for now
                    continue;
                }

                UpdateManager.OnReceivePacket <ClientUpdatePacket, ClientPacketId>(clientUpdatePacket);

                // If we are not yet connected we check whether this packet contains a login response,
                // so we can finish connecting
                if (!IsConnected)
                {
                    if (clientUpdatePacket.GetPacketData().TryGetValue(
                            ClientPacketId.LoginResponse,
                            out var packetData)
                        )
                    {
                        var loginResponse = (LoginResponse)packetData;

                        switch (loginResponse.LoginResponseStatus)
                        {
                        case LoginResponseStatus.Success:
                            OnConnect(loginResponse);
                            break;

                        case LoginResponseStatus.NotWhiteListed:
                            OnConnectFailed(new ConnectFailedResult {
                                Type = ConnectFailedResult.FailType.NotWhiteListed
                            });
                            return;

                        case LoginResponseStatus.Banned:
                            OnConnectFailed(new ConnectFailedResult {
                                Type = ConnectFailedResult.FailType.Banned
                            });
                            return;

                        case LoginResponseStatus.InvalidAddons:
                            OnConnectFailed(new ConnectFailedResult {
                                Type      = ConnectFailedResult.FailType.InvalidAddons,
                                AddonData = loginResponse.AddonData
                            });
                            return;

                        case LoginResponseStatus.InvalidUsername:
                            OnConnectFailed(new ConnectFailedResult {
                                Type = ConnectFailedResult.FailType.InvalidUsername
                            });
                            return;

                        default:
                            OnConnectFailed(new ConnectFailedResult {
                                Type = ConnectFailedResult.FailType.Unknown
                            });
                            return;
                        }

                        break;
                    }
                }

                _packetManager.HandleClientPacket(clientUpdatePacket);
            }
        }