Пример #1
0
 /// <summary>
 /// Function to call an event on server/client
 /// If machine works as server:
 /// The event will be sent to all clients
 /// If machine works as client:
 /// The event will be sent to server only
 /// </summary>
 /// <param name="eventCall">
 /// Param (eventCall) the event to call
 /// </param>
 public void NET_CallEvent(NET_EventCall eventCall)
 {
     if (isHost == true)
     {
         NET_Messages.NET_Message_EventCall callMessage = new NET_Messages.NET_Message_EventCall(DateTime.Now);
         callMessage.SetData(eventCall.GetEventData().SerializeData());
         this.tcpServer.TCP_EnqueueMessage(callMessage, 2);
         this.tcpServer.TCP_EnqueueMessage(callMessage, 3);
         this.tcpServer.TCP_EnqueueMessage(callMessage, 4);
     }
     else
     {
         NET_Messages.NET_Message_EventCall callMessage = new NET_Messages.NET_Message_EventCall(DateTime.Now);
         callMessage.SetData(eventCall.GetEventData().SerializeData());
         this.tcpClient.TCP_EnqueueMessage(callMessage);
     }
 }
Пример #2
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;
                        }
                    }
                }
            }
        }