コード例 #1
0
    /// <summary>
    /// Call this method along with your game loop, if you don't do this, you won't be able to receive messages
    /// </summary>
    public void ClientUpdate()
    {
        if (IsActive())
        {
            while (peer.HasReceived(out BitStream bitStream, out ulong guid, out int packet_size))
            {
                DefaultMessageIDTypes packet_id = (DefaultMessageIDTypes)bitStream.ReadByte();

                if (packet_id < DefaultMessageIDTypes.ID_USER_PACKET_ENUM)
                {
                    switch (packet_id)
                    {
                    case DefaultMessageIDTypes.ID_CONNECTION_REQUEST_ACCEPTED:
                        IsConnecting = false;
                        IsConnected  = true;
                        ClientGUID   = peer.GetMyGUID();
                        ServerGUID   = guid;
                        OnConnected();
                        break;

                    case DefaultMessageIDTypes.ID_DISCONNECTION_NOTIFICATION:
                        IsConnecting = IsConnected = false;
                        peer.Shutdown();
                        OnDisconnected(DisconnectReason.ConnectionClosed);
                        break;

                    case DefaultMessageIDTypes.ID_CONNECTION_LOST:
                        IsConnecting = IsConnected = false;
                        peer.Shutdown();
                        OnDisconnected(DisconnectReason.ConnectionLost);
                        break;

                    case DefaultMessageIDTypes.ID_CONNECTION_BANNED:
                        IsConnecting = IsConnected = false;
                        peer.Shutdown();
                        OnDisconnected(DisconnectReason.IsBanned);
                        break;

                    case DefaultMessageIDTypes.ID_INCOMPATIBLE_PROTOCOL_VERSION:
                        IsConnecting = IsConnected = false;
                        peer.Shutdown();
                        OnDisconnected(DisconnectReason.IncompatibleProtocol);
                        break;

                    case DefaultMessageIDTypes.ID_INVALID_PASSWORD:
                        IsConnecting = IsConnected = false;
                        peer.Shutdown();
                        OnDisconnected(DisconnectReason.InvalidPassword);
                        break;

                    case DefaultMessageIDTypes.ID_PUBLIC_KEY_MISMATCH:
                        IsConnecting = IsConnected = false;
                        peer.Shutdown();
                        OnDisconnected(DisconnectReason.SecurityError);
                        break;

                    case DefaultMessageIDTypes.ID_REMOTE_SYSTEM_REQUIRES_PUBLIC_KEY:
                        IsConnecting = IsConnected = false;
                        peer.Shutdown();
                        OnDisconnected(DisconnectReason.SecurityError);
                        break;

                    case DefaultMessageIDTypes.ID_OUR_SYSTEM_REQUIRES_SECURITY:
                        IsConnecting = IsConnected = false;
                        peer.Shutdown();
                        OnDisconnected(DisconnectReason.SecurityError);
                        break;

                    case DefaultMessageIDTypes.ID_CONNECTION_ATTEMPT_FAILED:
                        IsConnecting = IsConnected = false;
                        peer.Shutdown();
                        OnDisconnected(DisconnectReason.AttemptFailed);
                        break;

                    case DefaultMessageIDTypes.ID_NO_FREE_INCOMING_CONNECTIONS:
                        IsConnecting = IsConnected = false;
                        peer.Shutdown();
                        OnDisconnected(DisconnectReason.ServerIsFull);
                        break;

                    case DefaultMessageIDTypes.ID_IP_RECENTLY_CONNECTED:
                        IsConnecting = IsConnected = false;
                        peer.Shutdown();
                        OnDisconnected(DisconnectReason.ConnectionRecently);
                        break;

                    case DefaultMessageIDTypes.ID_UNCONNECTED_PONG:
                        bitStream.ResetReadPointer();
                        OnSerializeData(bitStream, packet_size);
                        break;
                    }
                }
                else if (packet_id >= DefaultMessageIDTypes.ID_USER_PACKET_ENUM)
                {
                    bitStream.ResetReadPointer();
                    OnSerializeData(bitStream, packet_size);
                }
            }
        }
    }