예제 #1
0
    void Update()
    {
        CaptureFrame();

        int incomingSocketID     = -1;
        int incomingConnectionID = -1;
        int incomingChannelID    = -1;

        byte[] incomingMessageBuffer = new byte[_incomingBufferSize];
        int    dataSize = 0;
        byte   error;

        NetworkEventType incomingNetworkEvent = NetworkTransport.Receive(out incomingSocketID, out incomingConnectionID,
                                                                         out incomingChannelID, incomingMessageBuffer, _incomingBufferSize, out dataSize, out error);

        switch (incomingNetworkEvent)
        {
        case NetworkEventType.Nothing:
            break;

        case NetworkEventType.ConnectEvent:
            Debug.Log("GameServer: ConnectEvent");
            // Master Server Connection
            if (incomingConnectionID == 1)
            {
                // Tell the Master Server to Inform the Client of the connection info
                string jsonToBeSent = "2";

                jsonToBeSent += JsonUtility.ToJson(new PortID(GS_Port));
                byte[] messageBuffer = Encoding.UTF8.GetBytes(jsonToBeSent);
                NetworkTransport.Send(_socketID, MS_ConnectionID, TCP_ChannelID, messageBuffer, messageBuffer.Length, out error);
            }
            // Client Connections
            else if (incomingConnectionID > 1)
            {
                ClientInfo clientInfo = new ClientInfo();
                clientInfo.socketID     = incomingSocketID;
                clientInfo.ConnectionID = incomingConnectionID;
                clientInfo.ChannelID    = incomingChannelID;
                _clients.Add(incomingConnectionID, clientInfo);

                _inGamePlayers = _clients.Count;

                // Need to inform master server of current connections every time a new client connects
                string jsonToBeSend = "6";
                jsonToBeSend += JsonUtility.ToJson(new GameServerInfo(_inGamePlayers, _serverName));
                SendJSONMessageToMaster(jsonToBeSend);
            }

            break;

        case NetworkEventType.DataEvent:
            Debug.Log("Game Server: Data Event");

            string message = Encoding.UTF8.GetString(incomingMessageBuffer);
            int    prefix  = 0;
            int    index;
            string newMessage = "";

            // New parser now allows client commands to be > 1 digit
            for (index = 0; index < message.Length; ++index)
            {
                if (message[index] == '{')
                {
                    break;
                }
            }

            prefix     = Convert.ToInt32(message.Substring(0, index));
            newMessage = message.Substring(index);

            //process user game input
            if (prefix == (int)GameServerCommands.PlayerInput)
            {
                PlayerIO input = JsonUtility.FromJson <PlayerIO>(newMessage);
                GameManager.Instance.PlayerActions(incomingConnectionID, input);
            }

            else if (prefix == (int)GameServerCommands.SetUsername)
            {
                _clients[incomingConnectionID].username  = JsonUtility.FromJson <LoginInfo>(newMessage).username;
                _clients[incomingConnectionID].playerNum = incomingConnectionID - 2;     // decremented so the range starts with 0 and not 1

                _notifArea.playerEntered(JsonUtility.FromJson <LoginInfo>(newMessage).username);

                string jsonToBeSent = "0";
                SendJSONMessage(jsonToBeSent, _clients[incomingConnectionID], QosType.Reliable);

                // IF the lobby is not loaded, load it.
                if (WindowManager.Instance.currentWindow == WindowIDs.None)
                {
                    WindowManager.Instance.ToggleWindows(WindowIDs.None, WindowIDs.Lobby);
                }

                if (_lobby.gameObject.activeInHierarchy == true)
                {
                    _lobby.AddPlayerToLobby(_clients[incomingConnectionID].username, _clients[incomingConnectionID].playerNum);
                }
            }
            else if (prefix == (int)GameServerCommands.LeaveLobby)
            {
                Debug.Log("Leave Lobby Command");

                _inGamePlayers = --_inGamePlayers < 0 ? 0 : _inGamePlayers;

                if (_lobby.gameObject.activeInHierarchy == true)
                {
                    _lobby.RemovePlayerFromLobby(_clients[incomingConnectionID].playerNum);
                }
            }
            else if (prefix == (int)GameServerCommands.LeaveGame)
            {
                _inGamePlayers--;
                if (_inGamePlayers < 0)
                {
                    _inGamePlayers = 0;
                }

                if (_inGamePlayers < 1)
                {
                    GameManager.Instance.ResetGameManager();
                    SceneManager.LoadScene("Server Game Version");

                    string toBeSent = "8" /*MasterServerCommands.GS_openToConnections.ToString()*/;    /*UNTESTED - Should be 8*/
                    //Debug.Log("Testing line 257 of GameServerManager: " + Assert.Equals("8", MasterServerCommands.GS_openToConnections.ToString()));
                    GameInstanceStats gs = new GameInstanceStats(_serverName);
                    toBeSent += JsonUtility.ToJson(gs);
                    SendJSONMessageToMaster(toBeSent);
                }
            }
            else if (prefix == (int)GameServerCommands.AssignName)
            {
                GameInstanceStats gs = JsonUtility.FromJson <GameInstanceStats>(newMessage);
                _serverName = gs.serverName;
            }

            break;

        case NetworkEventType.DisconnectEvent:
            if (incomingConnectionID > 1)
            {
                _inGamePlayers = --_inGamePlayers < 0 ? 0 : _inGamePlayers;

                if (_lobby.gameObject.activeInHierarchy == true)
                {
                    _lobby.RemovePlayerFromLobby(_clients[incomingConnectionID].playerNum);
                }

                GameManager.Instance.LeaveGame(incomingConnectionID);
                _clients.Remove(incomingConnectionID);

                // Need to inform master server of current connections
                string jsonToBeSent1 = "6";
                jsonToBeSent1 += JsonUtility.ToJson(new GameServerInfo(_inGamePlayers, _serverName));
                SendJSONMessageToMaster(jsonToBeSent1);

                if (_inGamePlayers < 1)
                {
                    GameManager.Instance.ResetGameManager();
                    WindowManager.Instance.ToggleWindows(WindowIDs.PlayerInfo, WindowIDs.None);
                    SceneManager.LoadScene("Server Game Version");
                }
            }
            break;
        }
    }