示例#1
0
        //---------------------------------------------------------------------------------------------//
        //Private functions:
        //---------------------------------------------------------------------------------------------//
        private void NET_MainThread()
        {
            if (isHost == true)
            {
                while (state != NET_State.NET_Shutdown)
                {
                    //Broadcast server infos
                    NET_Messages.NET_Message_ServerBroadcast message = new NET_Messages.NET_Message_ServerBroadcast(DateTime.Now);
                    NET_DataPack pack = new NET_DataPack();
                    pack.SetData("PlayerSlotsUsed", playerCount);
                    pack.SetData("ServerIPAddress", localIP.ToString());
                    pack.SetData("GameState", gameState);
                    pack.SetData("ServerName", name);
                    message.SetData(pack.SerializeData());
                    udpSystem.UDP_EnqueueMessage(message);

                    Thread.Sleep(200);
                }
            }
        }
示例#2
0
 public void Disconnect(int playerID)
 {
     if (state == TCP_Connection_State.TCPC_Connected)
     {
         NET_Debug.InjectMessage("[NET_TCP_CN]", "[OK] Disconnected");
         NET_Messages.NET_Message_Disconnect disconnectMessage = new NET_Messages.NET_Message_Disconnect(DateTime.Now);
         NET_DataPack pack = new NET_DataPack();
         pack.SetData("PlayerID", playerID);
         disconnectMessage.SetData(pack.SerializeData());
         lock (tcpOUT_MessageQueue)
         {
             tcpOUT_MessageQueue.Enqueue(disconnectMessage);
         }
         TCP_Sending();
         this.state = TCP_Connection_State.TCPC_Disconnected;
     }
 }
示例#3
0
        /// <summary>
        /// Function to call on every update cycle
        /// </summary>
        public void NET_Update()
        {
            //Record current time
            thisTime = DateTime.Now;

            //Process all enqueued udp messages
            NET_Messages.IMessage[] udpMessages = udpSystem.UDP_DequeueMessages();
            if (udpMessages != null)
            {
                foreach (NET_Messages.IMessage MS in udpMessages)
                {
                    NET_ProcessMessage(MS);
                }
            }

            //Process all enqueued tcp messages
            if (isHost == true)
            {
                this.playerCount = tcpServer.TCP_Server_GetPlayerCount();

                NET_Messages.IMessage[] playerSlot2Messages = tcpServer.TCP_DequeueMessages(2);
                if (playerSlot2Messages != null)
                {
                    foreach (NET_Messages.IMessage MS in playerSlot2Messages)
                    {
                        NET_ProcessMessage(MS);
                    }
                }
                NET_Messages.IMessage[] playerSlot3Messages = tcpServer.TCP_DequeueMessages(3);
                if (playerSlot3Messages != null)
                {
                    foreach (NET_Messages.IMessage MS in playerSlot3Messages)
                    {
                        NET_ProcessMessage(MS);
                    }
                }
                NET_Messages.IMessage[] playerSlot4Messages = tcpServer.TCP_DequeueMessages(4);
                if (playerSlot4Messages != null)
                {
                    foreach (NET_Messages.IMessage MS in playerSlot4Messages)
                    {
                        NET_ProcessMessage(MS);
                    }
                }
            }
            else
            {
                NET_Messages.IMessage[] playersMessages = tcpClient.TCP_DequeueMessages();
                if (playersMessages != null)
                {
                    foreach (NET_Messages.IMessage MS in playersMessages)
                    {
                        NET_ProcessMessage(MS);
                    }
                }
            }

            //TimeStepping V1
            float delta = thisTime.Subtract(lastTime).Milliseconds;

            if ((float)Math.Abs(delta) > timeStep)
            {
                this.lastTime = DateTime.Now;
                //Create update message V2
                //Old system:
                //3 messages in / 12 messages out
                //New system:
                //3 messages in / 3 messages out
                if (playerID != 0 && playerCount > 1)
                {
                    NET_Messages.NET_Message_Update update = new NET_Messages.NET_Message_Update(DateTime.Now);

                    if (isHost == true)
                    {
                        NET_DataPack packP1 = null;
                        NET_DataPack packP2 = null;
                        NET_DataPack packP3 = null;
                        NET_DataPack packP4 = null;

                        if (playersGameData[0] != null)
                        {
                            packP1 = playersGameData[0];
                            packP1.SetData("PlayerID", 1);
                        }
                        if (playersGameData[1] != null)
                        {
                            packP2 = playersGameData[1];
                            packP2.SetData("PlayerID", 2);
                        }
                        if (playersGameData[2] != null)
                        {
                            packP3 = playersGameData[2];
                            packP3.SetData("PlayerID", 3);
                        }
                        if (playersGameData[3] != null)
                        {
                            packP4 = playersGameData[3];
                            packP4.SetData("PlayerID", 4);
                        }

                        //Create uber-pack containing packs from all players (System 1 to reduce message count by factor 3 --> but rise message size by 4)
                        NET_UberPack uberPack = new NET_UberPack(packP1, packP2, packP3, packP4);
                        update.SetData(uberPack.SerializePacks());

                        //Enqueue update message
                        tcpServer.TCP_EnqueueMessage(update, 2);
                        tcpServer.TCP_EnqueueMessage(update, 3);
                        tcpServer.TCP_EnqueueMessage(update, 4);
                    }
                    else
                    {
                        if (playersGameData[playerID - 1] != null)
                        {
                            NET_DataPack pack = playersGameData[playerID - 1];
                            pack.SetData("PlayerID", playerID);
                            update.SetData(pack.SerializeData());

                            tcpClient.TCP_EnqueueMessage(update);
                        }
                    }
                }
            }
        }
示例#4
0
        //---------------------------------------------------------------------------------------------//
        //Private functions:
        //---------------------------------------------------------------------------------------------//
        private void TCP_MainThread()
        {
            while (serverStatus != TCP_Status.TCP_Shutdown)
            {
                while (serverStatus == TCP_Status.TCP_Running)
                {
                    //Connect to pending connections
                    if (clientCount < 3)
                    {
                        int emptySlot = -1;
                        if (connections[0] == null)
                        {
                            emptySlot = 0;
                        }
                        else if (connections[1] == null)
                        {
                            emptySlot = 1;
                        }
                        else if (connections[2] == null)
                        {
                            emptySlot = 2;
                        }
                        if (this.server.Pending() && blockNewClients == false)
                        {
                            //Build welcome message for new player
                            NET_Messages.NET_Message_Welcome welcomeMessage = new NET_Messages.NET_Message_Welcome(DateTime.Now);
                            NET_DataPack pack = new NET_DataPack();

                            pack.SetData("YourPlayerID", emptySlot + 2);
                            welcomeMessage.SetData(pack.SerializeData());

                            //Build new connection to player
                            TcpClient client = server.AcceptTcpClient();
                            connections[emptySlot] = new NET_TcpConnection(client);
                            connections[emptySlot].Connected();
                            //Enqueue welcome message
                            connections[emptySlot].TCP_EnqueueMessage(welcomeMessage);
                            clientCount++;
                        }
                    }
                    //Delete disconnected clients
                    for (int x = 0; x < 3; x++)
                    {
                        if (connections[x] != null)
                        {
                            if (connections[x].GetState() == "TCPC_DeleteMe")
                            {
                                connections[x] = null;
                                clientCount--;
                            }
                        }
                    }
                    Thread.Sleep(20);
                }
                //Delete disconnected clients
                for (int x = 0; x < 3; x++)
                {
                    if (connections[x] != null)
                    {
                        if (connections[x].GetState() == "TCPC_DeleteMe")
                        {
                            connections[x] = null;
                            clientCount--;
                        }
                    }
                }
                Thread.Sleep(10);
            }
            server.Stop();
            //Game is closing! --> Exit thread
        }