コード例 #1
0
 //---------------------------------------------------------------------------------------------//
 //Constructor:
 //---------------------------------------------------------------------------------------------//
 public NET_ServerInfo(string serverIPAddress, int playerCount, GAME_State gameState, string serverName)
 {
     this.serverName = serverName;
     this.serverAddress = IPAddress.Parse(serverIPAddress);
     this.playerCount = playerCount;
     this.gameState = gameState;
 }
コード例 #2
0
 public void INFO_SetState(GAME_State gameState)
 {
     this.gameState = gameState;
 }
コード例 #3
0
        private void NET_ProcessMessage(NET_Messages.IMessage message)
        {
            //---------------------------------------------------------------------------------------------//
            //Message handling:
            //---------------------------------------------------------------------------------------------//
            if (message != null)
            {
                //Host message handling
                if (isHost == true)
                {
                    //Update message
                    if (message as NET_Messages.NET_Message_Update != null)
                    {
                        object objectBuffer = 0;
                        byte[] byteBuffer   = new byte[1];
                        message.GetData(ref byteBuffer, ref objectBuffer, false);
                        NET_DataPack pack = new NET_DataPack(byteBuffer);

                        //Set update data to internal slots
                        int messagePID = (int)pack.GetData("PlayerID");
                        playersGameData[messagePID - 1] = pack;
                    }
                    //Event call message
                    else if (message as NET_Messages.NET_Message_EventCall != null)
                    {
                        object objectBuffer = 0;
                        byte[] byteBuffer   = new byte[1];
                        message.GetData(ref byteBuffer, ref objectBuffer, false);
                        NET_DataPack pack = new NET_DataPack(byteBuffer);

                        //Add event to list
                        NET_EventCall call = new NET_EventCall(pack);
                        eventCalls.Add(call);
                    }
                    //Disconnect message
                    else if (message as NET_Messages.NET_Message_Disconnect != null)
                    {
                        object objectBuffer = 0;
                        byte[] byteBuffer   = new byte[1];
                        message.GetData(ref byteBuffer, ref objectBuffer, false);
                        NET_DataPack pack = new NET_DataPack(byteBuffer);

                        int messagePID = (int)pack.GetData("PlayerID");
                        tcpServer.TCP_Server_ClientDisconnected(messagePID);
                    }
                }
                //Client message handling
                else
                {
                    //Server broadcast
                    if (message as NET_Messages.NET_Message_ServerBroadcast != null)
                    {
                        object objectBuffer = 0;
                        byte[] byteBuffer   = new byte[1];
                        message.GetData(ref byteBuffer, ref objectBuffer, false);
                        NET_DataPack pack = new NET_DataPack(byteBuffer);

                        string     ipAddress = (string)pack.GetData("ServerIPAddress");
                        int        playerC   = (int)pack.GetData("PlayerSlotsUsed");
                        string     serName   = (string)pack.GetData("ServerName");
                        GAME_State state     = (GAME_State)pack.GetData("GameState");
                        if (serverInfos.ContainsKey(ipAddress) == false)
                        {
                            NET_ServerInfo info = new NET_ServerInfo(ipAddress, playerC, gameState, serName);
                            serverInfos.Add(ipAddress, info);
                        }
                        else
                        {
                            NET_ServerInfo info = serverInfos[ipAddress];
                            info.INFO_SetPlayerCount(playerC);
                            info.INFO_SetState(gameState);
                            serverInfos[ipAddress] = info;
                        }
                        //If server broadcast is from connected server --> set player count
                        if (ipAddress == this.serverIP.ToString())
                        {
                            this.playerCount = playerC;
                        }
                    }
                    //Update message
                    else if (message as NET_Messages.NET_Message_Update != null)
                    {
                        object objectBuffer = 0;
                        byte[] byteBuffer   = new byte[1];
                        message.GetData(ref byteBuffer, ref objectBuffer, false);
                        NET_UberPack uberPack = new NET_UberPack(byteBuffer);

                        //Unpack uber pack
                        NET_DataPack packP1 = uberPack.GetPack(1);
                        NET_DataPack packP2 = uberPack.GetPack(2);
                        NET_DataPack packP3 = uberPack.GetPack(3);
                        NET_DataPack packP4 = uberPack.GetPack(4);

                        //Set update data to internal slots
                        if (packP1 != null && playerID != 1)
                        {
                            playersGameData[0] = packP1;
                        }
                        if (packP2 != null && playerID != 2)
                        {
                            playersGameData[1] = packP2;
                        }
                        if (packP3 != null && playerID != 3)
                        {
                            playersGameData[2] = packP3;
                        }
                        if (packP4 != null && playerID != 4)
                        {
                            playersGameData[3] = packP4;
                        }
                    }
                    //Event call message
                    else if (message as NET_Messages.NET_Message_EventCall != null)
                    {
                        object objectBuffer = 0;
                        byte[] byteBuffer   = new byte[1];
                        message.GetData(ref byteBuffer, ref objectBuffer, false);
                        NET_DataPack pack = new NET_DataPack(byteBuffer);

                        NET_EventCall call = new NET_EventCall(pack);
                        eventCalls.Add(call);
                    }
                    //Welcome message --> Set playerID for clients
                    else if (message as NET_Messages.NET_Message_Welcome != null)
                    {
                        object objectBuffer = 0;
                        byte[] byteBuffer   = new byte[1];
                        message.GetData(ref byteBuffer, ref objectBuffer, false);
                        NET_DataPack pack = new NET_DataPack(byteBuffer);

                        this.playerID = (int)pack.GetData("YourPlayerID");
                    }
                    //Disconnect message
                    else if (message as NET_Messages.NET_Message_Disconnect != null)
                    {
                        object objectBuffer = 0;
                        byte[] byteBuffer   = new byte[1];
                        message.GetData(ref byteBuffer, ref objectBuffer, false);
                        NET_DataPack pack = new NET_DataPack(byteBuffer);

                        int messagePID = (int)pack.GetData("PlayerID");
                        if (messagePID == playerID)
                        {
                            tcpClient.TCP_Disconnected();
                            playerID = 0;
                        }
                    }
                }
            }
        }
コード例 #4
0
 /// <summary>
 /// Function to set current gamestate, this gamestate will be broadcasted
 /// </summary>
 /// <param name="newState">
 /// Param (newState) the gamestate to set
 /// </param>
 public void NET_SetGameState(GAME_State newState)
 {
     this.gameState = newState;
 }