示例#1
0
    private void Update()
    {
        if (doAbort)
        {
            doAbort = false;
            AbortGame();
        }
        if (didConnectSuccessfully)
        {
            didConnectSuccessfully = false;
            OnConnected();
        }
        if (doAttemptConnect)
        {
            doAttemptConnect = false;
            OnAttemptingReconnect();
        }

        if (!client.IsConnected && tickCount > 0 && !UserData.Instance.IsGameOver)
        {
            matchInfoDisplay.GameState = "Disconnected";
        }

        // Process all packets that have been received by the server thus far
        byte[] data;
        while (client.GetNextPacket(out data))
        {
            if (UserData.Instance.IsGameOver)
            {
                return;
            }

            var packet     = PacketFactory.BytesToPacket(data);
            var byteBuffer = new ByteBuffer(data);

            // Uncomment for logs containing which packet type you receive
            // Debug.Log("SyncGameController: Received packet: " + (Opcode)packet.Opcode);
            switch ((Opcode)packet.Opcode)
            {
            case Opcode.MatchSuccess:
                client.ResetReadTimer();
                on(MatchSuccess.GetRootAsMatchSuccess(byteBuffer));

                client.SetReadTimeout(2000);
                break;

            case Opcode.GameState:
                on(GameState.GetRootAsGameState(byteBuffer));
                break;

            case Opcode.MatchOver:
                on(MatchOver.GetRootAsMatchOver(byteBuffer));
                break;

            case Opcode.OpponentConnectionStatus:
                on(OpponentConnectionStatus.GetRootAsOpponentConnectionStatus(byteBuffer));
                break;

            case Opcode.PlayerReconnected:
                on(PlayerReconnected.GetRootAsPlayerReconnected(byteBuffer));
                break;

            case Opcode.OpponentPaused:
                on(OpponentPaused.GetRootAsOpponentPaused(byteBuffer));
                break;

            case Opcode.OpponentResumed:
                on(OpponentResumed.GetRootAsOpponentResumed(byteBuffer));
                break;

            case Opcode.Chat:
                on(Chat.GetRootAsChat(byteBuffer));
                break;

            case Opcode.KeepAlive:
                break;

            default:
                Debug.Log("SyncGameController: Received packet with unimplemented/unsupported authcode: " + packet.Opcode);
                break;
            }
        }
    }