Ejemplo n.º 1
0
        void Update()
        {
            if (doneInitializing) // I want this to start executing if we did the manual start function
            {
                networkJobHandle.Complete();

                if (!connection.IsCreated)
                {
                    Debug.Log("Can't create connection.");
                    if (clientGameManager != null && !disconnectedMessage)
                    {
                        StartCoroutine(clientGameManager.ShowConnectionDisconnected());
                        disconnectedMessage = true;
                    }
                    return;
                }

                DataStreamReader  reader;
                NetworkEvent.Type cmd;
                while ((cmd = connection.PopEvent(networkDriver, out reader)) != NetworkEvent.Type.Empty)
                {
                    if (cmd == NetworkEvent.Type.Connect) // Connecting to the server
                    {
                        Debug.Log("Connected to server");
                    }
                    else if (cmd == NetworkEvent.Type.Data) // Getting in Data
                    {
                        var messageType = (MessageHeader.MessageType)reader.ReadUShort();
                        switch (messageType) // client reads all except setname.
                        {
                            #region Lobby Protocol
                        case MessageHeader.MessageType.None:
                            break;

                        case MessageHeader.MessageType.NewPlayer:
                            var newPlayerMessage = new NewPlayerMessage();
                            newPlayerMessage.DeserializeObject(ref reader);
                            MessageConnection mcNewPlayer = new MessageConnection(connection, newPlayerMessage);
                            messagesQueue.Enqueue(mcNewPlayer);
                            break;

                        case MessageHeader.MessageType.Welcome:
                            var welcomeMessage = new WelcomeMessage();
                            welcomeMessage.DeserializeObject(ref reader);
                            MessageConnection mcWelcome = new MessageConnection(connection, welcomeMessage);
                            messagesQueue.Enqueue(mcWelcome);

                            break;

                        case MessageHeader.MessageType.SetName:
                            break;

                        case MessageHeader.MessageType.RequestDenied:
                            var requestDeniedMessage = new RequestDeniedMessage();
                            requestDeniedMessage.DeserializeObject(ref reader);
                            MessageConnection mcDenied = new MessageConnection(connection, requestDeniedMessage);
                            messagesQueue.Enqueue(mcDenied);
                            break;

                        case MessageHeader.MessageType.PlayerLeft:
                            var playerLeftMessage = new PlayerLeftMessage();
                            playerLeftMessage.DeserializeObject(ref reader);
                            MessageConnection mcPlayerLeft = new MessageConnection(connection, playerLeftMessage);
                            messagesQueue.Enqueue(mcPlayerLeft);
                            break;

                        case MessageHeader.MessageType.StartGame:
                            var startGameMessage = new StartGameMessage();
                            startGameMessage.DeserializeObject(ref reader);
                            MessageConnection mcStartGame = new MessageConnection(connection, startGameMessage);
                            messagesQueue.Enqueue(mcStartGame);
                            break;

                            #endregion
                            #region Game Protocol
                        case MessageHeader.MessageType.PlayerTurn:
                            var playerTurnMessage = new PlayerTurnMessage();
                            playerTurnMessage.DeserializeObject(ref reader);
                            MessageConnection mcPlayerTurn = new MessageConnection(connection, playerTurnMessage);
                            messagesQueue.Enqueue(mcPlayerTurn);
                            break;

                        case MessageHeader.MessageType.RoomInfo:
                            var roomInfoMessage = new RoomInfoMessage();
                            roomInfoMessage.DeserializeObject(ref reader);
                            MessageConnection mcRoomInfo = new MessageConnection(connection, roomInfoMessage);
                            messagesQueue.Enqueue(mcRoomInfo);
                            break;

                        case MessageHeader.MessageType.PlayerEnterRoom:
                            var playerEnterRoomMessage = new PlayerEnterRoomMessage();
                            playerEnterRoomMessage.DeserializeObject(ref reader);
                            MessageConnection mcPlayerEnterRoom = new MessageConnection(connection, playerEnterRoomMessage);
                            messagesQueue.Enqueue(mcPlayerEnterRoom);
                            break;

                        case MessageHeader.MessageType.PlayerLeaveRoom:
                            var playerLeaveRoomMessage = new PlayerLeaveRoomMessage();
                            playerLeaveRoomMessage.DeserializeObject(ref reader);
                            MessageConnection mcPlayerLeaveRoom = new MessageConnection(connection, playerLeaveRoomMessage);
                            messagesQueue.Enqueue(mcPlayerLeaveRoom);
                            break;

                        case MessageHeader.MessageType.ObtainTreasure:
                            var obtainTreasureMessage = new ObtainTreasureMessage();
                            obtainTreasureMessage.DeserializeObject(ref reader);
                            MessageConnection mcObtainTreasure = new MessageConnection(connection, obtainTreasureMessage);
                            messagesQueue.Enqueue(mcObtainTreasure);
                            break;

                        case MessageHeader.MessageType.HitMonster:
                            var hitMonsterMessage = new HitMonsterMessage();
                            hitMonsterMessage.DeserializeObject(ref reader);
                            MessageConnection mcHitMonster = new MessageConnection(connection, hitMonsterMessage);
                            messagesQueue.Enqueue(mcHitMonster);
                            break;

                        case MessageHeader.MessageType.HitByMonster:
                            var hitByMonsterMessage = new HitByMonsterMessage();
                            hitByMonsterMessage.DeserializeObject(ref reader);
                            MessageConnection mcHitByMonster = new MessageConnection(connection, hitByMonsterMessage);
                            messagesQueue.Enqueue(mcHitByMonster);
                            break;

                        case MessageHeader.MessageType.PlayerDefends:
                            var playerDefendsMessage = new PlayerDefendsMessage();
                            playerDefendsMessage.DeserializeObject(ref reader);
                            MessageConnection mcPlayerDefends = new MessageConnection(connection, playerDefendsMessage);
                            messagesQueue.Enqueue(mcPlayerDefends);
                            break;

                        case MessageHeader.MessageType.PlayerLeftDungeon:
                            var playerLeftDungeonMessage = new PlayerLeftDungeonMessage();
                            playerLeftDungeonMessage.DeserializeObject(ref reader);
                            MessageConnection mcPlayerLeftDungeon = new MessageConnection(connection, playerLeftDungeonMessage);
                            messagesQueue.Enqueue(mcPlayerLeftDungeon);
                            break;

                        case MessageHeader.MessageType.PlayerDies:
                            var playerDiesMessage = new PlayerDiesMessage();
                            playerDiesMessage.DeserializeObject(ref reader);
                            MessageConnection mcPlayerDies = new MessageConnection(connection, playerDiesMessage);
                            messagesQueue.Enqueue(mcPlayerDies);
                            break;

                        case MessageHeader.MessageType.EndGame:
                            var endGameMessage = new EndGameMessage();
                            endGameMessage.DeserializeObject(ref reader);
                            MessageConnection mcEndGame = new MessageConnection(connection, endGameMessage);
                            messagesQueue.Enqueue(mcEndGame);
                            break;

                        case MessageHeader.MessageType.MoveRequest:
                            break;

                        case MessageHeader.MessageType.AttackRequest:
                            break;

                        case MessageHeader.MessageType.DefendRequest:
                            break;

                        case MessageHeader.MessageType.ClaimTreasureRequest:
                            break;

                        case MessageHeader.MessageType.LeaveDungeonRequest:
                            break;

                            #endregion
                        case MessageHeader.MessageType.Count:
                            break;

                        default:
                            break;
                        }
                    }
                    else if (cmd == NetworkEvent.Type.Disconnect)
                    {
                        Debug.Log("Disconnected from server");
                        connection = default;
                        ClientGameManager.ShowHostTerminatedRoom();
                    }
                }

                networkJobHandle = networkDriver.ScheduleUpdate();
                networkJobHandle.Complete();
                ProcessMessagesQueue();
            }
        }
Ejemplo n.º 2
0
        void Update()
        {
            if (doneInitializing)
            {
                networkJobHandle.Complete();

                // Clean up connections
                for (int i = 0; i < connections.Length; ++i)
                {
                    if (!connections[i].IsCreated)
                    {
                        connections.RemoveAtSwapBack(i);
                        --i;
                    }
                }

                // Accept connections
                NetworkConnection connection;
                while ((connection = networkDriver.Accept()) != default)
                {
                    // We can only accept 4 connections
                    if (ClientList.Count >= 4)
                    {
                        // Send Request Denied Message
                        SendRequestDeniedMessage(connection);
                        //networkDriver.Disconnect(connection);
                    }
                    else
                    {
                        connections.Add(connection);
                        Debug.Log("Accepted connection");

                        // Log the client & Send the Welcome Message
                        bool host = false;
                        if (connection.InternalId == 0)
                        {
                            host = true;
                        }
                        Client newClient = new Client(connection.InternalId, "", connection, host);
                        newClient.AssignRandomColor();
                        ClientList.Add(newClient);
                        SendWelcomeMessage(newClient);
                    }
                }

                // Read the stream
                DataStreamReader reader;
                for (int i = 0; i < connections.Length; ++i)
                {
                    if (!connections[i].IsCreated)
                    {
                        continue;
                    }

                    NetworkEvent.Type cmd;
                    while ((cmd = networkDriver.PopEventForConnection(connections[i], out reader)) != NetworkEvent.Type.Empty)
                    {
                        if (cmd == NetworkEvent.Type.Data)
                        {
                            var messageType = (MessageHeader.MessageType)reader.ReadUShort();
                            switch (messageType)
                            {
                                #region Lobby Protocol
                            case MessageHeader.MessageType.None:
                                var noneMessage = new NoneMessage();
                                noneMessage.DeserializeObject(ref reader);
                                MessageConnection mcNone = new MessageConnection(connections[i], noneMessage);
                                messagesQueue.Enqueue(mcNone);
                                break;

                            case MessageHeader.MessageType.NewPlayer:
                                break;

                            case MessageHeader.MessageType.Welcome:
                                break;

                            case MessageHeader.MessageType.SetName:
                                var setNameMessage = new SetNameMessage();
                                setNameMessage.DeserializeObject(ref reader);
                                MessageConnection mcSetName = new MessageConnection(connections[i], setNameMessage);
                                messagesQueue.Enqueue(mcSetName);
                                break;

                            case MessageHeader.MessageType.RequestDenied:
                                break;

                            case MessageHeader.MessageType.PlayerLeft:
                                var playerLeftMessage = new PlayerLeftMessage();
                                playerLeftMessage.DeserializeObject(ref reader);
                                MessageConnection mcPlayerLeft = new MessageConnection(connections[i], playerLeftMessage);
                                messagesQueue.Enqueue(mcPlayerLeft);
                                break;

                            case MessageHeader.MessageType.StartGame:
                                var startGameMessage = new StartGameMessage();
                                startGameMessage.DeserializeObject(ref reader);
                                MessageConnection mcStartGame = new MessageConnection(connections[i], startGameMessage);
                                messagesQueue.Enqueue(mcStartGame);
                                break;

                                #endregion
                                #region Game Protocol
                            case MessageHeader.MessageType.PlayerTurn:
                                break;

                            case MessageHeader.MessageType.RoomInfo:
                                break;

                            case MessageHeader.MessageType.PlayerEnterRoom:
                                break;

                            case MessageHeader.MessageType.PlayerLeaveRoom:
                                break;

                            case MessageHeader.MessageType.ObtainTreasure:
                                break;

                            case MessageHeader.MessageType.HitMonster:
                                break;

                            case MessageHeader.MessageType.HitByMonster:
                                break;

                            case MessageHeader.MessageType.PlayerDefends:
                                break;

                            case MessageHeader.MessageType.PlayerLeftDungeon:
                                break;

                            case MessageHeader.MessageType.PlayerDies:
                                break;

                            case MessageHeader.MessageType.EndGame:
                                break;

                            case MessageHeader.MessageType.MoveRequest:
                                var moveRequestMessage = new MoveRequestMessage();
                                moveRequestMessage.DeserializeObject(ref reader);
                                var mcMoveRequest = new MessageConnection(connections[i], moveRequestMessage);
                                messagesQueue.Enqueue(mcMoveRequest);
                                break;

                            case MessageHeader.MessageType.AttackRequest:
                                var attackRequestMessage = new AttackRequestMessage();
                                attackRequestMessage.DeserializeObject(ref reader);
                                var mcAttackRequest = new MessageConnection(connections[i], attackRequestMessage);
                                messagesQueue.Enqueue(mcAttackRequest);
                                break;

                            case MessageHeader.MessageType.DefendRequest:
                                var defendRequestMessage = new DefendRequestMessage();
                                defendRequestMessage.DeserializeObject(ref reader);
                                var mcDefendRequest = new MessageConnection(connections[i], defendRequestMessage);
                                messagesQueue.Enqueue(mcDefendRequest);
                                break;

                            case MessageHeader.MessageType.ClaimTreasureRequest:
                                var claimTreasureRequestMessage = new ClaimTreasureRequestMessage();
                                claimTreasureRequestMessage.DeserializeObject(ref reader);
                                var mcClaimTreasureRequest = new MessageConnection(connections[i], claimTreasureRequestMessage);
                                messagesQueue.Enqueue(mcClaimTreasureRequest);
                                break;

                            case MessageHeader.MessageType.LeaveDungeonRequest:
                                var leaveDungeonRequestMessage = new LeaveDungeonRequestMessage();
                                leaveDungeonRequestMessage.DeserializeObject(ref reader);
                                var mcLeaveDungeonRequest = new MessageConnection(connections[i], leaveDungeonRequestMessage);
                                messagesQueue.Enqueue(mcLeaveDungeonRequest);
                                break;

                                #endregion
                            case MessageHeader.MessageType.Count:
                                break;

                            default:
                                break;
                            }
                        }
                        else if (cmd == NetworkEvent.Type.Disconnect)
                        {
                            Debug.Log("Client disconnected");
                            // send who is leaving
                            var playerLeftMessage = new PlayerLeftMessage {
                                PlayerLeftID = connections[i].InternalId
                            };
                            Client removeClient = null;
                            foreach (Client c in clientList)
                            {
                                if (c.PlayerID == connections[i].InternalId)
                                {
                                    removeClient = c;
                                }
                            }
                            clientList.Remove(removeClient);

                            foreach (Client c in clientList)
                            {
                                if (c.PlayerID != connections[i].InternalId)
                                {
                                    SendMessage(playerLeftMessage, c.Connection);
                                }
                            }
                            HostGameManager.PlayerLeft(removeClient);
                            connections[i] = default;
                        }
                    }
                }

                networkJobHandle = networkDriver.ScheduleUpdate();
                networkJobHandle.Complete();
                ProcessMessagesQueue();
            }
        }