Пример #1
0
    /// <summary>
    /// Disconnect from either the lobby or in-game.
    /// </summary>
    public void Disconnect(bool isServer, NetworkType nwType, bool isHostTimeout = false)
    {
        if (NwMgr.IsRunning)
        {
            NwMgr.NetworkHost.Disconnected -= new ConnectionDisconnectedHandler(ClientDisconnectEvent);
        }

        this.isServer = isServer;
        SetNetworkType(nwType);

        // If in the lobby or in-game, and still connected to host
        if ((toggle_InLobbyClientLogic || toggle_InLobbyHostLogic) && !isHostTimeout)
        {
            // Before sending disconnect signal, ensure another player is present to receive it.
            if (TrueLobbyState.NumActivePlayers > 1)
            {
                LobbyPlayerDisconnectPacket sendPkt = new LobbyPlayerDisconnectPacket(isServer);
                NwMgr.SendPacket <LobbyPlayerDisconnectPacket>("info", sendPkt);
            }

            HostLobby_ToggleLobbyHostLogic(false);
            JoinLobby_ToggleLobbyClientLogic(false);

            myIdStatus = PlayerIdStatus.None;
        }

        // Empty lobby state.
        TrueLobbyState = new LobbyStatePacket(true);

        // Trigger nwMgr.Stop() and this.serverLookup.Cancel() to be called in 2 seconds.
        nwMgrShutdownCurStatus = nwMgrShutdownStatus.Triggered;
    }
Пример #2
0
    /// <summary>
    /// As a client of the lobby, handle any received-packet events from the host of the lobby.
    /// </summary>
    protected void InLobbyClientLogic()
    {
        // Need to delay a bit before transferring packets. NwMgr needs to establish connection event after calling Run()
        // before sending packets.
        if (inLobbyClientLogicCurStatus == inLobbyClientLogicStatus.NotStarted)
        {
            inLobbyClientLogicStartDelayFinish = Time.time + inLobbyClientLogicStartDelayTime;
            inLobbyClientLogicCurStatus        = inLobbyClientLogicStatus.DelayingStart;
            return;
        }
        else if (inLobbyClientLogicCurStatus == inLobbyClientLogicStatus.DelayingStart)
        {
            if (Time.time > inLobbyClientLogicStartDelayFinish)
            {
                inLobbyClientLogicCurStatus = inLobbyClientLogicStatus.Started;
            }
            else
            {
                return;
            }
        }



        // If just joined the lobby, request a player id.
        if (myIdStatus == PlayerIdStatus.None)
        {
            if (IsDebug)
            {
                Debug.Log("InLobbyClientLogic: Requesting a player id.");
            }

            NwMgr.SendPacket <LobbyRequestPlayerIdPacket>("info", new LobbyRequestPlayerIdPacket());
            myIdStatus = PlayerIdStatus.Requested;
        }

        // If received my requested player id.
        if (NwMgr.PacketQueue.HasPacket <LobbyGivenPlayerIdPacket>())
        {
            LobbyGivenPlayerIdPacket recvPkt = NwMgr.PacketQueue.GetNextPacket <LobbyGivenPlayerIdPacket>();
            myId       = recvPkt.PlayerId;
            myIdStatus = PlayerIdStatus.Received;
            if (IsDebug)
            {
                Debug.Log("InLobbyClientLogic: Received my player id: " + myId);
            }
        }



        // Retrieved true lobby state from server.
        if (NwMgr.PacketQueue.HasPacket <LobbyStatePacket>())
        {
            if (IsDebug)
            {
                Debug.Log("InLobbyClientLogic: Received lobby state from server.");
            }
            TrueLobbyState = NwMgr.PacketQueue.GetNextPacket <LobbyStatePacket>();

            Menus.RefreshLobby(TrueLobbyState, myId);

            // Go in-game mode if signalled by server.
            if (TrueLobbyState.IsStartGame)
            {
                Debug.Log("Lobby.cs: Got packet to start game. Time: " + Time.time);
                Menus.OnClick_StartGame();
            }
        }

        // Received alert that server is disconnecting. Exit the lobby.
        if (NwMgr.PacketQueue.HasPacket <LobbyPlayerDisconnectPacket>())
        {
            if (IsDebug)
            {
                Debug.Log("InLobbyClientLogic: Received alert that server is disconnecting.");
            }
            NwMgr.PacketQueue.GetNextPacket <LobbyPlayerDisconnectPacket>();

            Menus.OnClick_Disconnect("Server has ended the game.");
            return;
        }

        // Received signal to return to the lobby.
        if (NwMgr.PacketQueue.HasPacket <LobbyReturnPacket>())
        {
            if (IsDebug)
            {
                Debug.Log("InLobbyClientLogic: Received signal to return to lobby.");
            }
            NwMgr.PacketQueue.GetNextPacket <LobbyReturnPacket>();

            Menus.OnClick_ReturnToLobby();
        }
    }
Пример #3
0
    /* These functions below are called by Update(). They handle packet-received events relating to the
     * lobby state. */



    /// <summary>
    /// As the host of the lobby, handle any received packets from my clients.
    /// </summary>
    protected void InLobbyHostLogic()
    {
        int prevNumActivePlayers = TrueLobbyState.NumActivePlayers;

        TrueLobbyState.NumActivePlayers = 1 + NwMgr.NetworkHost.ConnectionCount;

        // If number of active players have changed...
        if (prevNumActivePlayers != TrueLobbyState.NumActivePlayers)
        {
            TrueLobbyState.IsDirty = true;
        }

        // At the start, assign myself the first player Id.
        if (myIdStatus == PlayerIdStatus.None)
        {
            if (IsDebug)
            {
                Debug.Log("InLobbyHostLogic: Assigning myself a player id.");
            }

            myId       = PlayerId.One;
            myIdStatus = PlayerIdStatus.Received;
        }

        // A new client has joined the lobby and is requesting a unique player ID.
        if (NwMgr.PacketQueue.HasPacket <LobbyRequestPlayerIdPacket>())
        {
            if (IsDebug)
            {
                Debug.Log("InLobbyHostLogic: Got new player event.");
            }

            INetworkConnection recvNwConn;
            NwMgr.PacketQueue.GetNextPacket <LobbyRequestPlayerIdPacket>(out recvNwConn);

            // Give client an ID relative to its incoming channel ID.

            LobbyGivenPlayerIdPacket sendPkt = new LobbyGivenPlayerIdPacket();
            sendPkt.PlayerId = DetermineClientPlayerId(recvNwConn);
            NwMgr.SendPacket <LobbyGivenPlayerIdPacket>("info", recvNwConn.Id, sendPkt);

            // NumActivePlayers will be handled at the top of this function.
        }

        // Received update from client (e.g. changed role).
        if (NwMgr.PacketQueue.HasPacket <LobbyUpdateFromClientPacket>())
        {
            if (IsDebug)
            {
                Debug.Log("InLobbyHostLogic: Got lobby update from client.");
            }

            INetworkConnection          recvNwConn;
            LobbyUpdateFromClientPacket recvPkt = NwMgr.PacketQueue.GetNextPacket <LobbyUpdateFromClientPacket>(out recvNwConn);

            PlayerId clientId = DetermineClientPlayerId(recvNwConn);
            TrueLobbyState.MergeUpdateFromClient(recvPkt, clientId);

            TrueLobbyState.IsDirty = true;
        }

        // Received alert that a client is disconnecting from server.
        if (NwMgr.PacketQueue.HasPacket <LobbyPlayerDisconnectPacket>())
        {
            if (IsDebug)
            {
                Debug.Log("InLobbyHostLogic: Client is disconnecting.");
            }

            INetworkConnection recvNwConn;
            NwMgr.PacketQueue.GetNextPacket <LobbyPlayerDisconnectPacket>(out recvNwConn);

            PlayerId lossId = DetermineClientPlayerId(recvNwConn);
            TrueLobbyState.RemoveClient(lossId);

            TrueLobbyState.IsDirty = true;
        }

        if (TrueLobbyState.IsDirty)
        {
            // Broadcast new lobby state.
            NwMgr.SendPacket <LobbyStatePacket>("info", TrueLobbyState);

            // Refresh my lobby GUI.
            Menus.RefreshLobby(TrueLobbyState, myId);

            TrueLobbyState.IsDirty = false;
        }
    }