Пример #1
0
        /// <summary>
        /// This object will request a server id.
        /// </summary>
        /// <param name="type">The unit type as defined in [type]Headers.cs</param>
        public void RequestServerID()
        {
            Packet p = new Packet(Headers.GAME_REQUEST_OBJECT_ID);
            p.AddInt(localID);

            GameServerConnectionManager.GetInstance().SendPacket(p);
        }
Пример #2
0
 /// <summary>
 /// Called when the packet is sent.
 /// </summary>
 /// <param name="p">The packet that was sent.</param>
 public void SentPacket(Packet p, SocketClient client)
 {
     lock (syncSentPackets)
     {
         sentPackets.AddLast(new PacketSenderPair(p, client));
     }
 }
Пример #3
0
 /// <summary>
 /// Decodes a packet to a string.
 /// </summary>
 /// <param name="p">The packet to decode.</param>
 /// <returns>The string containing the information that was contained in the packet. </returns>
 public static String DecodePacketString(Packet p, int index)
 {
     String result = "";
     byte[] data = p.GetData();
     for( int i = index; i < data.Length; i++){
         result += ((char)data[i]) + "";
     }
     return result;
 }
        public void DataReceived(Packet p)
        {
            switch (p.GetHeader())
            {
                case TestHeaders.STEADY_TEST:
                    {
                        ParentComponent menu = MenuManager.GetInstance().GetCurrentlyDisplayedMenu();
                        if (menu is TestConnectionMenu)
                        {
                            TestConnectionMenu testMenu = ((TestConnectionMenu)menu);

                            testMenu.steadyPacketTestBar.currentValue++;
                        }
                        break;
                    }
                case TestHeaders.BURST_TEST:
                    {
                        ParentComponent menu = MenuManager.GetInstance().GetCurrentlyDisplayedMenu();
                        if (menu is TestConnectionMenu)
                        {
                            TestConnectionMenu testMenu = ((TestConnectionMenu)menu);

                            testMenu.burstPacketTestBar.currentValue++;
                        }
                        break;
                    }
                case TestHeaders.MALFORM_TEST:
                    {
                        ParentComponent menu = MenuManager.GetInstance().GetCurrentlyDisplayedMenu();
                        if (menu is TestConnectionMenu)
                        {
                            TestConnectionMenu testMenu = ((TestConnectionMenu)menu);

                            String hash = PacketUtil.DecodePacketString(p, 0);
                            testMenu.hashesReceived[testMenu.malformPacketsReceivedCount] = hash;
                            testMenu.malformPacketsReceivedCount++;
                        }
                        break;
                    }
            }
        }
Пример #5
0
 /// <summary>
 /// Decodes a packet that should contain an int.
 /// </summary>
 /// <param name="p">The packet</param>
 /// <param name="index">The byte index to start searching for the int</param>
 /// <returns>The integer</returns>
 public static int DecodePacketInt(Packet p, int index)
 {
     return BitConverter.ToInt32(p.GetData(), index);
 }
Пример #6
0
        private void ProcessQueue()
        {
            Console.Out.WriteLine("Starting processing thread!");
            while (isRunning)
            {
                if (buggedPackets.Count != 0 && buggedPackets.Count % 100 == 0)
                {
                    Console.Out.WriteLine("Bugged packets is now " + buggedPackets.Count + ". Enjoy your exception");
                    int crash = Int32.Parse("LOL");
                }
                while (receivedPackets.Count > 0 && receivedPackets.First != null)
                {
                    if (onProcessPacket == null)
                    {
                        Console.Out.WriteLine("There are packets in the queue, but noone is listening to them!!");
                    }
                    else
                    {
                        //if (receivedPackets.First == null) continue;
                        try
                        {
                            // Process the packets
                            Packet[] receivedPacketArray = this.ConstructPacket(receivedPackets.First.Value.packet);
                            for (int i = 0; i < receivedPacketArray.Length; i++)
                            {
                                Packet receivedPacket = receivedPacketArray[i];
                                // Console.Out.WriteLine("Processing packet " + receivedPacket.GetHeader());
                                onProcessPacket(receivedPacket);

                                if (receivedPackets.First.Value.client.confirmPackets)
                                {
                                    if (receivedPacket.GetHeader() != Headers.PACKET_RECEIVED)
                                    {
                                        //Console.Out.WriteLine("Confirming packet with header " + receivedPacket.GetHeader() +
                                        //    " ID " + receivedPacket.GetPacketID());
                                        // Send a confirmation packet, that the packet is processed and received
                                        Packet confirmPacket = new Packet(Headers.PACKET_RECEIVED);
                                        confirmPacket.SetPacketID(receivedPacket.GetPacketID());
                                        receivedPackets.First.Value.client.SendPacket(confirmPacket);
                                    }
                                    else
                                    {
                                        ConfirmPacket(receivedPacket.GetPacketID());
                                    }
                                }
                            }
                            receivedPackets.RemoveFirst();
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine("Nullpointer in PacketProcessor D=. Removing first to buggedPackets");
                            if (receivedPackets.First == null)
                            {
                                Console.WriteLine("First is null");
                            }
                            else
                            {
                                buggedPackets.AddLast(receivedPackets.First.Value);
                                receivedPackets.RemoveFirst();
                            }
                            // Stop for now, try again later
                            break;
                        }
                    }
                }
                lock (syncSentPackets)
                {
                    for (int i = 0; i < this.sentPackets.Count; i++)
                    {
                        PacketSenderPair pair = this.sentPackets.ElementAt(i);
                        if (pair.client.confirmPackets)
                        {
                            double now = (new TimeSpan(DateTime.UtcNow.Ticks).TotalMilliseconds);
                            if (now - pair.packet.timeSent > packetResendTimeoutMS)
                            {
                                Console.Out.WriteLine("Resending packet with ID " + pair.packet.GetPacketID() + " and header " +
                                    ((int)pair.packet.GetHeader()).ToString("x") /* +
                            //   ".. ( " + now + " - " + pair.packet.timeSent + " )"*/);
                                this.sentPackets.Remove(pair);
                                pair.client.SendPacket(pair.packet);
                                i--;
                            }
                        }
                    }
                }
                Thread.Sleep(10);
            }
            Console.Out.WriteLine("Ending processing thread!");
            isRunning = false;
        }
Пример #7
0
 /// <summary>
 /// When a packet is sent
 /// </summary>
 /// <param name="p"></param>
 public void OnPacketSend(Packet p)
 {
     TestDelegate bla = delegate()
          {
              if (this.user == null) return;
              if (ServerUI.GetInstance().lastSelectedClientName == this.user.username)
                  ServerUI.GetInstance().RefillMessageLogs(this.client.log.messageLog);
          };
     ((Control)ServerUI.GetInstance()).BeginInvoke(bla);
 }
Пример #8
0
        /// <summary>
        /// Manage all units and buildings etc to be in sync with the rest of the players in the
        /// multiplayer game.
        /// </summary>
        public void Synchronize()
        {
            int objectsSynced = 0;

            while (objectsSynced < maxObjectsPerFrame && unitList.Count > 0)
            {
                // Sync this unit.
                Unit unit = unitList.First.Value;
                if (!unit.multiplayerData.isCreated)
                {
                    // Notify the rest of the world of the creation of this unit.
                    Packet newUnitPacket = new Packet(UnitHeaders.GAME_NEW_UNIT);

                    // Get this packet going before the other one
                    newUnitPacket.AddInt(unit.player.multiplayerID);
                    newUnitPacket.AddInt(unit.multiplayerData.serverID);
                    newUnitPacket.AddInt(unit.multiplayerData.GetObjectType());
                    // Notify everyone else that we have created a unit
                    GameServerConnectionManager.GetInstance().SendPacket(newUnitPacket);
                    unit.multiplayerData.isCreated = true;
                }

                Packet p = new Packet(UnitHeaders.GAME_UNIT_LOCATION);
                p.AddInt(unit.multiplayerData.serverID);
                p.AddInt(unit.multiplayerData.moveTarget.X);
                p.AddInt(unit.multiplayerData.moveTarget.Y);
                p.AddInt((int)unit.x);
                p.AddInt((int)unit.y);

                unit.multiplayerData.lastPulse = new TimeSpan(DateTime.UtcNow.Ticks).TotalMilliseconds;
                GameServerConnectionManager.GetInstance().SendPacket(p);

                Console.Out.WriteLine("Synchronised " + unit);

                unitList.RemoveFirst();
                objectsSynced++;
            }

            while (objectsSynced < maxObjectsPerFrame && buildingList.Count > 0)
            {
                Building building = buildingList.First.Value;

                if (!building.multiplayerData.isCreated)
                {
                    // Notify the rest of the world of the creation of this unit.
                    Packet newBuildingPacket = new Packet(BuildingHeaders.GAME_NEW_BUILDING);

                    // Get this packet going before the other one
                    newBuildingPacket.AddInt(building.p.multiplayerID);
                    newBuildingPacket.AddInt(building.multiplayerData.serverID);
                    newBuildingPacket.AddInt(building.multiplayerData.GetObjectType());
                    newBuildingPacket.AddInt(building.constructedBy.multiplayerData.serverID);

                    // Notify everyone else that we have created a unit
                    GameServerConnectionManager.GetInstance().SendPacket(newBuildingPacket);
                    building.multiplayerData.isCreated = true;
                }

                Packet movePacket = new Packet(BuildingHeaders.GAME_BUILDING_LOCATION);
                movePacket.AddInt(building.multiplayerData.serverID);
                movePacket.AddInt((int)building.x);
                movePacket.AddInt((int)building.y);
                GameServerConnectionManager.GetInstance().SendPacket(movePacket);

                buildingList.RemoveFirst();
                objectsSynced++;
            }

            while (objectsSynced < maxObjectsPerFrame && eventList.Count > 0)
            {
                DamageEvent e = eventList.First.Value;

                Packet damagePacket = new Packet();
                if (e.by is Arrow)
                {
                    Arrow arrow = (Arrow)e.by;
                    damagePacket.SetHeader(UnitHeaders.GAME_UNIT_RANGED_DAMAGE);
                    damagePacket.AddInt(arrow.multiplayerData.serverID);
                    damagePacket.AddInt(e.source.multiplayerData.serverID);
                    damagePacket.AddInt(e.target.multiplayerData.serverID);
                }
                else if (e.by is MeleeSwing)
                {
                    MeleeSwing swing = (MeleeSwing)e.by;
                    damagePacket.SetHeader(UnitHeaders.GAME_UNIT_MELEE_DAMAGE);
                    damagePacket.AddInt(EncodeMeleeSwing(swing.type));
                    damagePacket.AddInt(e.source.multiplayerData.serverID);
                    damagePacket.AddInt(e.target.multiplayerData.serverID);
                }

                GameServerConnectionManager.GetInstance().SendPacket(damagePacket);

                eventList.RemoveFirst();
                objectsSynced++;
            }

            while (objectsSynced < maxObjectsPerFrame && projectileList.Count > 0)
            {
                Projectile toSync = projectileList.First.Value;

                if (toSync is Arrow && !toSync.multiplayerData.isCreated)
                {
                    Packet newArrowPacket = new Packet(UnitHeaders.GAME_UNIT_RANGED_SHOT);
                    newArrowPacket.AddInt(toSync.multiplayerData.serverID);
                    newArrowPacket.AddInt(toSync.parent.multiplayerData.serverID);
                    newArrowPacket.AddInt(toSync.target.multiplayerData.serverID);

                    GameServerConnectionManager.GetInstance().SendPacket(newArrowPacket);
                    toSync.multiplayerData.isCreated = true;
                }

                projectileList.RemoveFirst();
                objectsSynced++;
            }
        }
Пример #9
0
 /// <summary>
 /// When the ready state changed.
 /// </summary>
 /// <param name="source">The source, bla</param>
 public void OnReadyChanged(XNACheckBox source)
 {
     Packet p = new Packet(Headers.GAME_READY_CHANGED);
     p.AddInt(user.channelID);
     p.AddInt(user.id);
     // 1 for true, 0 for false.
     p.AddInt(this.readyCheckBox.selected ? 1 : 0);
     ChatServerConnectionManager.GetInstance().SendPacket(p);
 }
Пример #10
0
        public void DataReceived(Packet p)
        {
            switch (p.GetHeader())
            {
                case Headers.KEEP_ALIVE:
                    {
                        ChatServerConnectionManager.GetInstance().SendPacket(new Packet(Headers.KEEP_ALIVE));
                        break;
                    }
                case Headers.HANDSHAKE_2:
                    {
                        ChatServerConnectionManager.GetInstance().SetLoginStatus("Connected");
                        // Finish handshake
                        ChatServerConnectionManager.GetInstance().SendPacket(new Packet(Headers.HANDSHAKE_3));

                        // Request for a user ID
                        Packet newPacket = new Packet(Headers.CLIENT_USERNAME);

                            newPacket.AddString(ChatServerConnectionManager.GetInstance().user.username);
                        // Random user names are for testing only, so we don't want to show the menu
                        if (!ChatServerConnectionManager.GetInstance().useRandomUsername)
                        {
                            MenuManager.GetInstance().ShowMenu(MenuManager.Menu.MultiplayerLobby);
                        }
                        ChatServerConnectionManager.GetInstance().SendPacket(newPacket);

                        break;
                    }
                case Headers.SERVER_DISCONNECT:
                    {
                        ChatServerConnectionManager.GetInstance().DisconnectFromServer();
                        // Create a dialog, and add a listener to the OK button.
                        ChatServerConnectionManager.GetInstance().OnDisconnect();
                        break;
                    }
                case Headers.CLIENT_USER_ID:
                    {
                        // Set the received user ID.
                        ChatServerConnectionManager.GetInstance().user.id = PacketUtil.DecodePacketInt(p, 0);
                        // Console.Out.WriteLine("Received user ID from the server: " + ChatServerConnectionManager.GetInstance().user.id);
                        break;
                    }
                case Headers.GAME_MAP_CHANGED:
                    {
                        int gameID = PacketUtil.DecodePacketInt(p, 0);
                        String mapName = PacketUtil.DecodePacketString(p, 4);

                        ParentComponent menu = MenuManager.GetInstance().GetCurrentlyDisplayedMenu();
                        if (menu is MultiplayerLobby)
                        {
                            MultiplayerLobby lobby = ((MultiplayerLobby)menu);
                            MultiplayerGame game = lobby.GetGameByID(gameID);
                            game.mapname = mapName;
                            for (int i = 0; i < lobby.gameList.Count(); i++)
                            {
                                GameDisplayPanel panel = lobby.GetGameDisplayPanelByIndex(i);
                                if (panel.multiplayerGame.id == gameID)
                                {
                                    panel.ChangeMap(mapName);
                                    break;
                                }
                            }
                        }
                        else if (menu is GameLobby)
                        {
                            GameLobby lobby = ((GameLobby)menu);
                            lobby.mapPreviewPanel.ChangeMap(mapName);
                        }
                        Console.Out.WriteLine("Received GAME_MAP_CHANGED packet");
                        break;
                    }
                case Headers.CLIENT_CHANNEL:
                    {
                        UserManager.GetInstance().users.Clear();
                        // UserManager.GetInstance().users.Clear();
                        ChatServerConnectionManager.GetInstance().user.channelID = PacketUtil.DecodePacketInt(p, 0);
                        // Console.Out.WriteLine("Switched channel to: " + ChatServerConnectionManager.GetInstance().user.channelID);
                        break;
                    }
                case Headers.CHAT_MESSAGE:
                    {
                        // Get the channel
                        int channel = PacketUtil.DecodePacketInt(p, 0);
                        // Get the message
                        String message = PacketUtil.DecodePacketString(p, 4);
                        ParentComponent menu = MenuManager.GetInstance().GetCurrentlyDisplayedMenu();
                        if (menu is MultiplayerLobby)
                        {
                            MultiplayerLobby lobby = ((MultiplayerLobby)menu);
                            lobby.AddMessageToLog(message);
                        }
                        else if (menu is GameLobby)
                        {
                            GameLobby lobby = ((GameLobby)menu);
                            lobby.AddMessageToLog(message);
                        }
                        break;
                    }
                case Headers.NEW_USER:
                    {
                        int userID = PacketUtil.DecodePacketInt(p, 0);
                        String username = PacketUtil.DecodePacketString(p, 4);
                        User user = new User(username);
                        user.id = userID;
                        user.channelID = ChatServerConnectionManager.GetInstance().user.channelID;
                        ParentComponent menu = MenuManager.GetInstance().GetCurrentlyDisplayedMenu();
                        if (UserManager.GetInstance().GetUserByID(user.id) == null) UserManager.GetInstance().users.AddLast(user);

                        if (menu is MultiplayerLobby)
                        {
                            MultiplayerLobby lobby = ((MultiplayerLobby)menu);
                            lobby.AddUser(user);
                        }

                        break;
                    }
                case Headers.USER_LEFT:
                    {
                        int userID = PacketUtil.DecodePacketInt(p, 0);
                        User user = UserManager.GetInstance().GetUserByID(userID);
                        if (user != null)
                        {
                            ParentComponent menu = MenuManager.GetInstance().GetCurrentlyDisplayedMenu();
                            if (menu is MultiplayerLobby)
                            {
                                MultiplayerLobby lobby = ((MultiplayerLobby)menu);
                                lobby.RemoveUser(user);
                            }
                        }

                        break;
                    }
            }
        }
Пример #11
0
        /// <summary>
        /// User pressed a key in the message textfield.
        /// </summary>
        /// <param name="e">The event</param>
        public void OnKeyPressed(KeyEvent e)
        {
            if (e.key.ToString() == "Enter")
            {
                ChatServerConnectionManager chat = ChatServerConnectionManager.GetInstance();
                String message = chat.user.username + ": " + messageTextField.text;
                messageTextField.text = "";
                // AddMessageToLog(message);
                Packet packet = new Packet();

                packet.SetHeader(Headers.CHAT_MESSAGE);
                packet.AddInt(chat.user.channelID);
                packet.AddString(message);
                chat.SendPacket(packet);
            }
        }
Пример #12
0
 /// <summary>
 /// User entered a game name and pressed OK
 /// </summary>
 /// <param name="source">The source</param>
 public void CreateGame(XNAButton source)
 {
     if (gameNameInput.textfield.text.Length < 4)
     {
         XNAMessageDialog.CreateDialog("Please enter a game name that is 4 characters or longer.", XNAMessageDialog.DialogType.OK);
         return;
     }
     Packet p = new Packet(Headers.CLIENT_CREATE_GAME);
     p.AddInt(ChatServerConnectionManager.GetInstance().user.id);
     p.AddString(gameNameInput.textfield.text);
     ChatServerConnectionManager.GetInstance().SendPacket(p);
 }
Пример #13
0
        public void DataReceived(Packet p)
        {
            ChatServerConnectionManager manager = ChatServerConnectionManager.GetInstance();
            switch (p.GetHeader())
            {
                case Headers.NEW_USER:
                    {
                        int userID = PacketUtil.DecodePacketInt(p, 0);
                        String username = PacketUtil.DecodePacketString(p, 4);
                        User user = new User(username);
                        user.id = userID;
                        user.channelID = manager.user.channelID;
                        ParentComponent menu = MenuManager.GetInstance().GetCurrentlyDisplayedMenu();

                        if (menu is GameLobby)
                        {
                            GameLobby lobby = ((GameLobby)menu);
                            lobby.UserJoined(user);
                        }

                        break;
                    }
                case Headers.USER_LEFT:
                    {
                        int userID = PacketUtil.DecodePacketInt(p, 0);
                        User user = UserManager.GetInstance().GetUserByID(userID);
                        if (user != null)
                        {
                            ParentComponent menu = MenuManager.GetInstance().GetCurrentlyDisplayedMenu();
                            if (menu is GameLobby)
                            {
                                GameLobby lobby = ((GameLobby)menu);
                                lobby.UserLeft(user);
                            }
                        }

                        break;
                    }
                // Client received an ID for creating a game.
                case Headers.GAME_ID:
                    {
                        int gameID = PacketUtil.DecodePacketInt(p, 0);

                        MultiplayerLobby lobby = ((MultiplayerLobby)MenuManager.GetInstance().GetCurrentlyDisplayedMenu());
                        String gameName = lobby.gameNameInput.textfield.text;

                        MenuManager.GetInstance().ShowMenu(MenuManager.Menu.GameLobby);
                        GameLobby gameLobby = (GameLobby)MenuManager.GetInstance().GetCurrentlyDisplayedMenu();

                        gameLobby.multiplayerGame = new MultiplayerGame(gameID,
                            gameName, "");
                        gameLobby.multiplayerGame.host = manager.user;
                        break;
                    }
                case Headers.SERVER_CREATE_GAME:
                    {
                        ParentComponent menu = MenuManager.GetInstance().GetCurrentlyDisplayedMenu();
                        if (menu is GameLobby)
                        {
                            // Confirmation that the game was created? idk
                        }
                        else if (menu is MultiplayerLobby)
                        {
                            MultiplayerGame game = new MultiplayerGame(
                                PacketUtil.DecodePacketInt(p, 0),
                                PacketUtil.DecodePacketString(p, 4),
                                "<No map selected yet>");
                            MultiplayerLobby lobby = (MultiplayerLobby)menu;
                            lobby.AddGame(game);
                        }
                        break;
                    }
                case Headers.SERVER_DESTROY_GAME:
                    {
                        int gameID = PacketUtil.DecodePacketInt(p, 0);

                        ParentComponent menu = MenuManager.GetInstance().GetCurrentlyDisplayedMenu();
                        if (menu is GameLobby)
                        {
                            MenuManager.GetInstance().ShowMenu(MenuManager.Menu.MultiplayerLobby);
                            XNAMessageDialog dialog =
                                XNAMessageDialog.CreateDialog("The host has disconnected.", XNAMessageDialog.DialogType.OK);
                            // When OK is pressed .. get back to the lobby.
                            dialog.button1.onClickListeners +=
                                delegate(XNAButton source)
                                {
                                    // Change channel
                                    Packet leftGamePacket = new Packet(Headers.CLIENT_LEFT_GAME);
                                    ChatServerConnectionManager.GetInstance().SendPacket(leftGamePacket);
                                };

                        }
                        else if (menu is MultiplayerLobby)
                        {
                            MultiplayerLobby lobby = (MultiplayerLobby)menu;
                            lobby.RemoveGameByID(gameID);
                        }
                        break;
                    }
                case Headers.GAME_MAP_CHANGED:
                    {
                        ParentComponent menu = MenuManager.GetInstance().GetCurrentlyDisplayedMenu();
                        if (menu is MultiplayerLobby)
                        {
                            MultiplayerLobby lobby = (MultiplayerLobby)menu;
                            MultiplayerGame game = lobby.GetGameByID(PacketUtil.DecodePacketInt(p, 0));
                            if (game != null)
                            {
                                game.mapname = PacketUtil.DecodePacketString(p, 4);
                            }
                        }
                        break;
                    }
                case Headers.SERVER_REQUEST_JOIN:
                    {
                        ParentComponent menu = MenuManager.GetInstance().GetCurrentlyDisplayedMenu();
                        if (menu is GameLobby)
                        {
                            GameLobby lobby = (GameLobby)menu;
                            Packet newPacket = new Packet();
                            if (lobby.IsFull()) newPacket.SetHeader(Headers.CLIENT_GAME_FULL);
                            else newPacket.SetHeader(Headers.CLIENT_OK_JOIN);
                            newPacket.AddInt(PacketUtil.DecodePacketInt(p, 0));
                            newPacket.AddInt(PacketUtil.DecodePacketInt(p, 4));
                            manager.SendPacket(newPacket);
                        }
                        break;
                    }
                case Headers.CLIENT_OK_JOIN:
                    {
                        // Packet newPacket = new Packet(Headers.CLIENT_CHANNEL);
                        // newPacket.AddInt(PacketUtil.DecodePacketInt(p, 0));
                        MenuManager.GetInstance().ShowMenu(MenuManager.Menu.GameLobby);
                        // manager.SendPacket(newPacket);

                        break;
                    }
                case Headers.CLIENT_GAME_FULL:
                    {
                        XNAMessageDialog.CreateDialog("The game is full.", XNAMessageDialog.DialogType.OK);
                        break;
                    }

                case Headers.GAME_COLOR_CHANGED:
                    {
                        int userID = PacketUtil.DecodePacketInt(p, 4);
                        int colorID = PacketUtil.DecodePacketInt(p, 8);

                        ParentComponent menu = MenuManager.GetInstance().GetCurrentlyDisplayedMenu();
                        if (menu is GameLobby)
                        {
                            GameLobby lobby = (GameLobby)menu;
                            UserDisplayPanel panel = lobby.GetDisplayPanelByUserId(userID);
                            if (panel != null)
                            {
                                panel.SelectColor(colorID);
                            }
                            else
                            {
                                Console.Out.WriteLine("Tried to change the color of a user that doesn't exist!");
                            }
                        }

                        break;
                    }

                case Headers.GAME_TEAM_CHANGED:
                    {
                        int userID = PacketUtil.DecodePacketInt(p, 4);
                        int teamID = PacketUtil.DecodePacketInt(p, 8);

                        ParentComponent menu = MenuManager.GetInstance().GetCurrentlyDisplayedMenu();
                        if (menu is GameLobby)
                        {
                            GameLobby lobby = (GameLobby)menu;
                            UserDisplayPanel panel = lobby.GetDisplayPanelByUserId(userID);
                            if (panel != null)
                            {
                                panel.teamDropdown.SelectItem(teamID - 1);
                            }
                            else
                            {
                                Console.Out.WriteLine("Tried to change the team of a user that doesn't exist!");
                            }
                        }
                        break;
                    }

                case Headers.GAME_READY_CHANGED:
                    {
                        int userID = PacketUtil.DecodePacketInt(p, 4);
                        int readyState = PacketUtil.DecodePacketInt(p, 8);

                        ParentComponent menu = MenuManager.GetInstance().GetCurrentlyDisplayedMenu();
                        if (menu is GameLobby)
                        {
                            GameLobby lobby = (GameLobby)menu;
                            UserDisplayPanel panel = lobby.GetDisplayPanelByUserId(userID);
                            if (panel != null)
                            {
                                panel.readyCheckBox.selected = readyState != 0;
                            }
                            else
                            {
                                Console.Out.WriteLine("Tried to change the ready state of a user that doesn't exist!");
                            }
                        }
                        break;
                    }
                case Headers.GAME_KICK_CLIENT:
                    {
                        MenuManager.GetInstance().ShowMenu(MenuManager.Menu.MultiplayerLobby);
                        XNAMessageDialog dialog =
                            XNAMessageDialog.CreateDialog("You have been kicked by the host.", XNAMessageDialog.DialogType.OK);

                        break;
                    }
                case Headers.SERVER_GAME_START:
                    {
                        ParentComponent menu = MenuManager.GetInstance().GetCurrentlyDisplayedMenu();
                        int seconds = PacketUtil.DecodePacketInt(p, 0);
                        if (seconds != 0)
                        {
                            if (menu is GameLobby)
                            {
                                GameLobby lobby = (GameLobby)menu;
                                lobby.AddMessageToLog("Game will start in " + seconds);
                                lobby.leaveGameButton.visible = false;
                            }
                        }
                        else if (menu is GameLobby)
                        {
                            GameLobby lobby = ((GameLobby)menu);
                            Game1.GetInstance().multiplayerGame = new MultiplayerGame(ChatServerConnectionManager.GetInstance().user.channelID,
                                "<Gamename>", "<Mapname>");

                            StateManager.GetInstance().gameState = StateManager.State.GameInit;
                            MenuManager.GetInstance().ShowMenu(MenuManager.Menu.MultiPlayerLoadMenu);
                            StateManager.GetInstance().gameState = StateManager.State.GameLoading;
                            // Loading will start now
                            // See Game1.cs for sending the packet that the game is done loading (update loop)
                        }

                        break;
                    }
                case Headers.DONE_LOADING:
                    {
                        int playerID = PacketUtil.DecodePacketInt(p, 0);
                        Console.Out.WriteLine("Done loading packet received! -> " + playerID);
                        Game1.GetInstance().GetPlayerByMultiplayerID(playerID).doneLoading = true;
                        for (int i = 0; i < Game1.GetInstance().players.Count(); i++)
                        {
                            if (!Game1.GetInstance().players.ElementAt(i).doneLoading) return;
                        }

                        StateManager.GetInstance().gameState = StateManager.State.GameRunning;
                        //  int count = 0;
                        Game1.CURRENT_PLAYER.SpawnStartUnits();
                        /*foreach (Player player in Game1.GetInstance().players)
                        {
                            player.SpawnStartUnits();
                            count++;
                        }*/
                        break;
                    }

                case Headers.LOADING_PROGRESS:
                    {
                        int userID = PacketUtil.DecodePacketInt(p, 0);
                        int percentage = PacketUtil.DecodePacketInt(p, 4);

                        User user = Game1.GetInstance().multiplayerGame.GetUserByID(userID);

                        MPLoadScreen loadScreen = ((MPLoadScreen)MenuManager.GetInstance().GetCurrentlyDisplayedMenu());
                        // It may be null for a split second after the loading finishes
                        if( loadScreen != null ) loadScreen.SetPercentageDone(user, percentage);
                        break;
                    }
                case Headers.LOADING_WHAT:
                    {
                        int userID = PacketUtil.DecodePacketInt(p, 0);
                        String what = PacketUtil.DecodePacketString(p, 4);

                        User user = Game1.GetInstance().multiplayerGame.GetUserByID(userID);

                        MPLoadScreen loadScreen = ((MPLoadScreen)MenuManager.GetInstance().GetCurrentlyDisplayedMenu());
                        // It may be null for a split second after the loading finishes
                        if (loadScreen != null) loadScreen.SetLoadingWhat(user, what);
                        break;
                    }
                case Headers.MAP_POSITION_CHANGED:
                    {
                        int userID = PacketUtil.DecodePacketInt(p, 0);
                        int mapIndex = PacketUtil.DecodePacketInt(p, 4);

                        GameLobby lobby = (GameLobby)MenuManager.GetInstance().GetCurrentlyDisplayedMenu();
                        lobby.mapPreviewPanel.playerLocationGroup.OnPlayerIndexChangedMP(
                            userID, mapIndex);

                        break;
                    }
            }
        }
Пример #14
0
        /// <summary>
        /// Pressed the start game button!
        /// </summary>
        /// <param name="source"></param>
        public void StartGame(XNAButton source)
        {
            Boolean isReady = true;
            foreach (UserDisplayPanel panel in userDisplayPanels)
            {
                if (!panel.readyCheckBox.selected)
                {
                    this.AddMessageToLog("Not all players are ready.");
                    isReady = false;
                    break;
                }
            }

            if (isReady)
            {
                Packet p = new Packet(Headers.CLIENT_GAME_START);
                ChatServerConnectionManager.GetInstance().SendPacket(p);

                this.startGameButton.visible = false;
            }
        }
Пример #15
0
        /// <summary>
        /// Leave the game.
        /// </summary>
        /// <param name="source">The button source</param>
        public void LeaveGame(XNAButton source)
        {
            // If I was the host..
            if (IsCurrentUserHost())
            {
                // Destroy the game
                Packet destroyGame = new Packet(Headers.CLIENT_DESTROY_GAME);
                ChatServerConnectionManager.GetInstance().SendPacket(destroyGame);
            }

            // Change channel
            Packet leftGamePacket = new Packet(Headers.CLIENT_LEFT_GAME);
            ChatServerConnectionManager.GetInstance().SendPacket(leftGamePacket);

            // Show the menu
            MenuManager.GetInstance().ShowMenu(MenuManager.Menu.MultiplayerLobby);
        }
Пример #16
0
 /// <summary>
 /// Notify the rest that this user's color has changed.
 /// </summary>
 public void OnColorChanged()
 {
     Packet p = new Packet(Headers.GAME_COLOR_CHANGED);
     p.AddInt(user.channelID);
     p.AddInt(user.id);
     for (int i = 0; i < this.colors.Length; i++)
     {
         if (colors[i] == colorDropdown.GetSelectedButton().backgroundColor)
         {
             p.AddInt(i);
             break;
         }
     }
     ChatServerConnectionManager.GetInstance().SendPacket(p);
 }
Пример #17
0
 /// <summary>
 /// When the kick button has been pressed.
 /// </summary>
 public void OnKickButtonPressed(XNAButton source)
 {
     Packet p = new Packet(Headers.GAME_KICK_CLIENT);
     p.AddInt(user.channelID);
     p.AddInt(user.id);
     ChatServerConnectionManager.GetInstance().SendPacket(p);
 }
Пример #18
0
        public void DataReceived(Packet p)
        {
            switch (p.GetHeader())
            {
                case Headers.KEEP_ALIVE:
                    {
                        ChatServerConnectionManager.GetInstance().SendPacket(new Packet(Headers.KEEP_ALIVE));
                        break;
                    }
                case Headers.HANDSHAKE_2:
                    {
                        ChatServerConnectionManager.GetInstance().SetLoginStatus("Connected");
                        // Finish handshake
                        ChatServerConnectionManager.GetInstance().SendPacket(new Packet(Headers.HANDSHAKE_3));

                        // Request for a user ID
                        Packet newPacket = new Packet(Headers.CLIENT_USERNAME);
                        newPacket.AddString(ChatServerConnectionManager.GetInstance().user.username);
                        ChatServerConnectionManager.GetInstance().SendPacket(newPacket);

                        MenuManager.GetInstance().ShowMenu(MenuManager.Menu.MultiplayerLobby);
                        break;
                    }
                case Headers.SERVER_DISCONNECT:
                    {
                        ChatServerConnectionManager.GetInstance().DisconnectFromServer();
                        // Create a dialog, and add a listener to the OK button.
                        ChatServerConnectionManager.GetInstance().OnDisconnect();
                        break;
                    }
                case Headers.CLIENT_USER_ID:
                    {
                        // Set the received user ID.
                        ChatServerConnectionManager.GetInstance().user.id = PacketUtil.DecodePacketInt(p, 0);
                        // Console.Out.WriteLine("Received user ID from the server: " + ChatServerConnectionManager.GetInstance().user.id);
                        break;
                    }
                case Headers.CLIENT_CHANNEL:
                    {
                        UserManager.GetInstance().users.Clear();
                        // UserManager.GetInstance().users.Clear();
                        ChatServerConnectionManager.GetInstance().user.channelID = PacketUtil.DecodePacketInt(p, 0);
                        // Console.Out.WriteLine("Switched channel to: " + ChatServerConnectionManager.GetInstance().user.channelID);
                        break;
                    }
                case Headers.CHAT_MESSAGE:
                    {
                        // Get the channel
                        int channel = PacketUtil.DecodePacketInt(p, 0);
                        // Get the message
                        String message = PacketUtil.DecodePacketString(p, 4);
                        ParentComponent menu = MenuManager.GetInstance().GetCurrentlyDisplayedMenu();
                        if (menu is MultiplayerLobby)
                        {
                            MultiplayerLobby lobby = ((MultiplayerLobby)menu);
                            lobby.AddMessageToLog(message);
                        }
                        else if (menu is GameLobby)
                        {
                            GameLobby lobby = ((GameLobby)menu);
                            lobby.AddMessageToLog(message);
                        }
                        break;
                    }
                case Headers.NEW_USER:
                    {
                        int userID = PacketUtil.DecodePacketInt(p, 0);
                        String username = PacketUtil.DecodePacketString(p, 4);
                        User user = new User(username);
                        user.id = userID;
                        user.channelID = ChatServerConnectionManager.GetInstance().user.channelID;
                        ParentComponent menu = MenuManager.GetInstance().GetCurrentlyDisplayedMenu();
                        if (UserManager.GetInstance().GetUserByID(user.id) == null) UserManager.GetInstance().users.AddLast(user);

                        if (menu is MultiplayerLobby)
                        {
                            MultiplayerLobby lobby = ((MultiplayerLobby)menu);
                            lobby.AddUser(user);
                        }

                        break;
                    }
                case Headers.USER_LEFT:
                    {
                        int userID = PacketUtil.DecodePacketInt(p, 0);
                        User user = UserManager.GetInstance().GetUserByID(userID);
                        if (user != null)
                        {
                            ParentComponent menu = MenuManager.GetInstance().GetCurrentlyDisplayedMenu();
                            if (menu is MultiplayerLobby)
                            {
                                MultiplayerLobby lobby = ((MultiplayerLobby)menu);
                                lobby.RemoveUser(user);
                            }
                        }

                        break;
                    }
            }
        }
Пример #19
0
 /// <summary>
 /// Notify the rest that this user's team has changed.
 /// </summary>
 public void OnTeamChanged()
 {
     Packet p = new Packet(Headers.GAME_TEAM_CHANGED);
     p.AddInt(user.channelID);
     p.AddInt(user.id);
     p.AddInt(Int32.Parse(this.teamDropdown.GetSelectedOption()));
     ChatServerConnectionManager.GetInstance().SendPacket(p);
 }
Пример #20
0
 /// <summary>
 /// User wants to join this game.
 /// </summary>
 /// <param name="button">The button that was pressed.</param>
 public void JoinGame(XNAButton button)
 {
     Packet joinPacket = new Packet(Headers.CLIENT_REQUEST_JOIN);
     joinPacket.AddInt(this.multiplayerGame.id);
     joinPacket.AddInt(ChatServerConnectionManager.GetInstance().user.id);
     ChatServerConnectionManager.GetInstance().SendPacket(joinPacket);
 }
Пример #21
0
        /// <summary>
        /// Called when the server recieved data from the client.
        /// </summary>
        /// <param name="data">The data that was sent</param>
        public void OnPacketReceived(Packet p)
        {
            switch (p.GetHeader())
            {
                case Headers.KEEP_ALIVE:
                    {
                        lastAliveTicks = System.DateTime.UtcNow.Ticks;
                        break;
                    }
                case Headers.HANDSHAKE_1:
                    {
                        client.SendPacket(new Packet(Headers.HANDSHAKE_2));
                        break;
                    }
                case Headers.HANDSHAKE_3:
                    {
                        // Nothing
                        break;
                    }
                case Headers.CLIENT_DISCONNECT:
                    {
                        this.OnDisconnect();
                        break;
                    }
                case Headers.CLIENT_USERNAME:
                    {
                        this.user = new ServerUser(PacketUtil.DecodePacketString(p, 0), this);

                        Packet newPacket = new Packet(Headers.CLIENT_USER_ID);
                        newPacket.AddInt(this.user.id);
                        this.client.SendPacket(newPacket);

                        // Put the user in channel 1 (the main lobby)
                        ChannelManager.GetInstance().GetChannelByID(1).AddUser(user);

                        break;
                    }
                case Headers.CLIENT_CHANNEL:
                    {
                        Console.Error.WriteLine(this.user + " tried to change his channel! That's not allowed.");
                        //int newChannel = PacketUtil.DecodePacketInt(p, 0);
                        //this.user.ChangeChannel(newChannel);
                        break;
                    }
                case Headers.CHAT_MESSAGE:
                    {
                        // Get the channel
                        int channel = PacketUtil.DecodePacketInt(p, 0);
                        // Get the message
                        String message = PacketUtil.DecodePacketString(p, 4);
                        // Send it to the users!
                        ChannelManager.GetInstance().GetChannelByID(channel).SendMessageToUsers(message);
                        break;
                    }
                case Headers.CLIENT_CREATE_GAME:
                    {
                        if (this.user.channelID != 1)
                        {
                            Console.Error.WriteLine("Received a request to create a channel that is NOT from a user in the lobby!");
                            return;
                        }
                        // User that requested this
                        int userID = PacketUtil.DecodePacketInt(p, 0);
                        // Name of the game
                        String gameName = PacketUtil.DecodePacketString(p, 4);
                        // Create game
                        MultiplayerGame mg = new MultiplayerGame(
                            MultiplayerGameManager.GetInstance().RequestGameID(),
                            gameName, "<No map selected yet>");
                        mg.host = (ServerUser)ServerUserManager.GetInstance().GetUserByID(userID);
                        // Add game to list
                        MultiplayerGameManager.GetInstance().games.AddLast(mg);
                        Console.Out.WriteLine("Created a game with ID = " + mg.id);

                        // Return the host its channel and game ID
                        Packet gameIDPacket = new Packet(Headers.GAME_ID);
                        gameIDPacket.AddInt(mg.id);
                        this.client.SendPacket(gameIDPacket);

                        this.user.ChangeChannel(mg.id);

                        // Notify all others in the channel that the game was created.
                        ChannelManager.GetInstance().GetChannelByID(1).CreatedGame(mg);
                        break;
                    }
                case Headers.GAME_MAP_CHANGED:
                    {

                        break;
                    }
                case Headers.CLIENT_DESTROY_GAME:
                    {
                        MultiplayerGame game = MultiplayerGameManager.GetInstance().GetGameByHost(this.user);
                        if (game == null)
                        {
                            Console.Error.WriteLine("Client " + user.id + " tried to destroy a game that is not his!");
                        }
                        else
                        {
                            // Tell everyone in the lobby that the game was destroyed.
                            ChannelManager.GetInstance().GetChannelByID(1).DestroyGame(game);
                            // Tell everyone in the game itsself that the game was destroyed.
                            ChannelManager.GetInstance().GetChannelByID(game.id).DestroyGame(game);
                            // Remove it completely
                            MultiplayerGameManager.GetInstance().games.Remove(game);
                        }
                        break;
                    }
                case Headers.CLIENT_REQUEST_JOIN:
                    {
                        int gameID = PacketUtil.DecodePacketInt(p, 0);
                        int userID = PacketUtil.DecodePacketInt(p, 4);
                        MultiplayerGame game = MultiplayerGameManager.GetInstance().GetGameByID(gameID);
                        Packet joinPacket = new Packet(Headers.SERVER_REQUEST_JOIN);
                        joinPacket.AddInt(gameID);
                        joinPacket.AddInt(userID);
                        ((ServerUser)ServerUserManager.GetInstance().GetUserByID(game.host.id)).chatListener.client.SendPacket(
                           joinPacket);
                        break;
                    }
                case Headers.CLIENT_OK_JOIN:
                    {
                        // Notify the client of the response by host.
                        int gameID = PacketUtil.DecodePacketInt(p, 0);
                        int userID = PacketUtil.DecodePacketInt(p, 4);
                        MultiplayerGame game = MultiplayerGameManager.GetInstance().GetGameByID(gameID);
                        ((ServerUser)ServerUserManager.GetInstance().GetUserByID(userID)).chatListener.client.SendPacket(p);
                        // Change this user's channel to the game channel.
                        ((ServerUser)ServerUserManager.GetInstance().GetUserByID(userID)).chatListener.user.ChangeChannel(gameID);
                        break;
                    }
                case Headers.CLIENT_GAME_FULL:
                    {
                        int gameID = PacketUtil.DecodePacketInt(p, 0);
                        int userID = PacketUtil.DecodePacketInt(p, 4);
                        MultiplayerGame game = MultiplayerGameManager.GetInstance().GetGameByID(gameID);
                        ((ServerUser)ServerUserManager.GetInstance().GetUserByID(userID)).chatListener.client.SendPacket(p);
                        break;
                    }
                case Headers.CLIENT_LEFT_GAME:
                    {
                        // Put user back in lobby.
                        this.user.ChangeChannel(1);
                        break;
                    }
                case Headers.GAME_COLOR_CHANGED:
                    {
                        int channel = PacketUtil.DecodePacketInt(p, 0);
                        int userID = PacketUtil.DecodePacketInt(p, 4);
                        int color = PacketUtil.DecodePacketInt(p, 8);
                        ((ServerUser)ServerUserManager.GetInstance().GetUserByID(userID)).color = color;
                        Channel c = ChannelManager.GetInstance().GetChannelByID(channel);
                        c.ChangeColor(userID, color);
                        break;
                    }
                case Headers.GAME_TEAM_CHANGED:
                    {
                        int channel = PacketUtil.DecodePacketInt(p, 0);
                        int userID = PacketUtil.DecodePacketInt(p, 4);
                        int team = PacketUtil.DecodePacketInt(p, 8);
                        ((ServerUser)ServerUserManager.GetInstance().GetUserByID(userID)).team = team;
                        Channel c = ChannelManager.GetInstance().GetChannelByID(channel);
                        c.ChangeTeam(userID, team);
                        break;
                    }
                case Headers.GAME_READY_CHANGED:
                    {
                        int channel = PacketUtil.DecodePacketInt(p, 0);
                        int userID = PacketUtil.DecodePacketInt(p, 4);
                        int readyState = PacketUtil.DecodePacketInt(p, 8);
                        ((ServerUser)ServerUserManager.GetInstance().GetUserByID(userID)).readyState = readyState;
                        Channel c = ChannelManager.GetInstance().GetChannelByID(channel);
                        c.ChangeReadyState(userID, readyState);
                        break;
                    }
                case Headers.GAME_KICK_CLIENT:
                    {
                        int userID = PacketUtil.DecodePacketInt(p, 4);
                        Packet kickPacket = new Packet(Headers.GAME_KICK_CLIENT);
                        kickPacket.AddInt(userID);
                        ((ServerUser)ServerUserManager.GetInstance().GetUserByID(userID)).
                            chatListener.client.SendPacket(kickPacket);

                        ((ServerUser)ServerUserManager.GetInstance().GetUserByID(userID)).ChangeChannel(1);
                        break;
                    }
                case Headers.CLIENT_GAME_START:
                    {
                        System.Timers.Timer gameStartTimer = new System.Timers.Timer();
                        gameStartTimer.Elapsed += new ElapsedEventHandler(GameStart);
                        gameStartTimer.Interval = 1000;
                        gameStartTimer.Start();
                        this.gameStartSeconds = 5;
                        break;
                    }
                default:
                    {
                        String currTime = System.DateTime.Now.ToLongTimeString() + "," + System.DateTime.Now.Millisecond + " ";
                        this.client.log.messageLog.AddLast(new Logger.LogMessage(currTime + "UNKNOWN Header switcher reached default. " +
                            "(" + p.GetHeader() + ")", true));
                        break;
                    }
            }
            TestDelegate bla = delegate()
            {
                if (this.user == null) return;
                if (ServerUI.GetInstance().lastSelectedClientName == this.user.username)
                    ServerUI.GetInstance().RefillMessageLogs(this.client.log.messageLog);
            };
            ((Control)ServerUI.GetInstance()).BeginInvoke(bla);
        }
 /// <summary>
 /// Attempts to send a packet to the server.
 /// </summary>
 /// <returns>If the packet was sent or not.</returns>
 /// <param name="packet">The packet to send</param>
 public Boolean SendPacket(Packet packet)
 {
     if (packet.GetFullData().Length == 0)
     {
         Console.Error.WriteLine("Cannot send a packet with length = 0");
         return false;
     }
     this.connection.SendPacket(packet);
     return true;
 }
Пример #23
0
        /// <summary>
        /// Constructs a packet from a full byte array.
        /// </summary>
        /// <param name="fullData">The data as received by the socket.</param>
        /// <returns>The packet, including header, packet ID, and the data.</returns>
        private Packet[] ConstructPacket(byte[] fullData)
        {
            String stringData = this.BytesToString(fullData);
            String[] split = stringData.Split((char)SocketClient.END_OF_PACKET);
            Packet[] packets = new Packet[split.Length - 1];

            if (split.Length > 2)
                Console.Out.WriteLine("Found a double packet! (full data: \n'" + stringData + "')");

            for (int i = 0; i < split.Length - 1; i++)
            {
                if (split[i].Contains((char)SocketClient.END_OF_PACKET))
                {
                    Console.Out.WriteLine("Packet contained end of packet char!");
                }
                // Extra catch for empty packets
                if (split[i].Length < 2) continue;
                byte[] currentPacketData = this.StringToBytes(split[i]);
                byte[] packetID = new byte[4];
                for (int j = 1; j < 5; j++)
                {
                    packetID[j - 1] = currentPacketData[j];
                }

                byte[] headerlessData = new byte[currentPacketData.Length - 5];
                for (int j = 5; j < currentPacketData.Length; j++)
                {
                    headerlessData[j - 5] = currentPacketData[j];
                }

                Packet p = new Packet(currentPacketData[0], headerlessData);
                p.SetPacketID(packetID);
                packets[i] = p;
            }
            return packets;
        }
Пример #24
0
 /// <summary>
 /// Packet processor has determined that the packet needed processing.
 /// </summary>
 /// <param name="p">The packet that needed processing</param>
 public void OnProcessPacket(Packet p)
 {
     log.Log(p, true);
 }
Пример #25
0
 public PacketSenderPair(Packet packet, SocketClient client)
 {
     this.packet = packet;
     this.client = client;
 }
Пример #26
0
 /// <summary>
 /// Sends a packet to the Socket we're connected with.
 /// </summary>
 /// <param name="packet">The packet to send</param>
 public void SendPacket(Packet packet)
 {
     lock (SyncSend)
     {
         if (Sock.Connected)
         {
             try
             {
                 byte[] data = packet.GetFullData();
                 Sock.Send(data, data.Length, SocketFlags.None);
                 log.Log(packet, false);
                 if (onPacketSendListeners != null) onPacketSendListeners(packet);
             }
             catch (Exception ex)
             {
                 Show("Unable to sent a packet.");
                 Show(ex);
             }
         }
         else
             Show("Fail. You shouldn't be able to make it send a packet, without having a connection.");
     }
 }
Пример #27
0
        /// <summary>
        /// Logs the data to the logger.
        /// </summary>
        /// <param name="data"></param>
        public void Log(Packet p, Boolean isReceived)
        {
            String currTime = System.DateTime.Now.ToLongTimeString() + "," + System.DateTime.Now.Millisecond + " ";
            switch (p.GetHeader())
            {
                case Headers.PACKET_RECEIVED:
                    {
                        // Ignore these packets, there will be a lot of them ..
                        break;
                    }
                case Headers.HANDSHAKE_1:
                    {
                        if (isReceived) this.messageLog.AddLast(new LogMessage(currTime + "HANDSHAKE_1 Received handshake request (1)", isReceived));
                        else this.messageLog.AddLast(new LogMessage(currTime + "HANDSHAKE_1 Sent handshake request (1)", isReceived));
                        break;
                    }
                case Headers.HANDSHAKE_2:
                    {
                        if (isReceived) this.messageLog.AddLast(new LogMessage(currTime + "HANDSHAKE_2 Received handshake acknowledge (2)", isReceived));
                        else this.messageLog.AddLast(new LogMessage(currTime + "HANDSHAKE_2 Sent handshake acknowledge (2)", isReceived));
                        break;
                    }
                case Headers.HANDSHAKE_3:
                    {
                        if (isReceived) this.messageLog.AddLast(new LogMessage(currTime + "HANDSHAKE_3 Received handshake confirmation (3)", isReceived));
                        else this.messageLog.AddLast(new LogMessage(currTime + "HANDSHAKE_3 Sent handshake confirmation (3)", isReceived));
                        break;
                    }
                case Headers.KEEP_ALIVE:
                    {
                        if (isReceived) this.messageLog.AddLast(new LogMessage(currTime + "KEEP_ALIVE User is still connected (still alive)", isReceived));
                        else this.messageLog.AddLast(new LogMessage(currTime + "KEEP_ALIVE Asking client if he's still alive ", isReceived));
                        break;
                    }
                case Headers.CHAT_MESSAGE:
                    {
                        if (isReceived)
                            this.messageLog.AddLast(new LogMessage(currTime + "CHAT_MESSAGE Received sent a chat message in channel " + PacketUtil.DecodePacketInt(p, 0) +
                                ": " + PacketUtil.DecodePacketString(p, 4), isReceived));
                        else this.messageLog.AddLast(new LogMessage(currTime + "CHAT_MESSAGE Sent chat message to all in channel " + PacketUtil.DecodePacketInt(p, 0) +
                                ": " + PacketUtil.DecodePacketString(p, 4), isReceived));
                        break;
                    }
                case Headers.CLIENT_DISCONNECT:
                    {
                        if (isReceived) this.messageLog.AddLast(new LogMessage(currTime + "CLIENT_DISCONNECT Client disconnected", isReceived));
                        else this.messageLog.AddLast(new LogMessage(currTime + "CLIENT_DISCONNECT Disconnected this client", isReceived));
                        break;
                    }
                case Headers.SERVER_DISCONNECT:
                    {
                        if (isReceived) this.messageLog.AddLast(new LogMessage(currTime + "SERVER_DISCONNECT Server disconnected", isReceived));
                        else this.messageLog.AddLast(new LogMessage(currTime + "SERVER_DISCONNECT Disconnected from server", isReceived));
                        break;
                    }
                case Headers.CLIENT_USERNAME:
                    {
                        if (isReceived) this.messageLog.AddLast(new LogMessage(currTime + "CLIENT_USERNAME Requested a username: "******"CLIENT_USERNAME Confirming requested username: "******"CLIENT_USER_ID Requested a userid: " +
                            PacketUtil.DecodePacketInt(p, 0), isReceived));
                        else this.messageLog.AddLast(new LogMessage(currTime + "CLIENT_USER_ID Confirming requested userid: " + PacketUtil.DecodePacketInt(p, 0), isReceived));
                        break;
                    }
                case Headers.CLIENT_CHANNEL:
                    {
                        if (isReceived) this.messageLog.AddLast(new LogMessage(currTime + "CLIENT_CHANNEL Received change channel request to " +
                            PacketUtil.DecodePacketInt(p, 0), isReceived));
                        else this.messageLog.AddLast(new LogMessage(currTime + "CLIENT_CHANNEL Sent change channel to " +
                            PacketUtil.DecodePacketInt(p, 0), isReceived));
                        break;
                    }
                case Headers.NEW_USER:
                    {
                        if (isReceived) this.messageLog.AddLast(new LogMessage(currTime + "NEW_USER Received new user: "******"NEW_USER Sent new user: "******"USER_LEFT Received user has left: " +
                            PacketUtil.DecodePacketString(p, 4), isReceived));
                        else this.messageLog.AddLast(new LogMessage(currTime + "USER_LEFT Sent user has left: " +
                            PacketUtil.DecodePacketString(p, 4), isReceived));
                        break;
                    }
                case Headers.CLIENT_CREATE_GAME:
                    {
                        if (isReceived) this.messageLog.AddLast(new LogMessage(currTime + "CLIENT_CREATE_GAME Received game creation request: userid = " +
                            PacketUtil.DecodePacketInt(p, 0) + ", gamename = " +
                            PacketUtil.DecodePacketString(p, 4), isReceived));
                        else this.messageLog.AddLast(new LogMessage(currTime + "CLIENT_CREATE_GAME Sent game creation request: userid = " +
                            PacketUtil.DecodePacketInt(p, 0) + ", gamename = " +
                            PacketUtil.DecodePacketString(p, 4), isReceived));
                        break;
                    }
                case Headers.SERVER_CREATE_GAME:
                    {
                        if (isReceived) this.messageLog.AddLast(new LogMessage(currTime + "SERVER_CREATE_GAME Received server game creation request: " +
                            "gameid = " + PacketUtil.DecodePacketInt(p, 0) +
                            ", gamename = " + PacketUtil.DecodePacketString(p, 4), isReceived));
                        else this.messageLog.AddLast(new LogMessage(currTime + "SERVER_CREATE_GAME Sent server game creation request: " +
                            "gameid = " + PacketUtil.DecodePacketInt(p, 0) +
                            ", gamename = " + PacketUtil.DecodePacketString(p, 4), isReceived));
                        break;
                    }
                case Headers.GAME_ID:
                    {
                        if (isReceived) this.messageLog.AddLast(new LogMessage(currTime + "GAME_ID Received a game ID: " +
                            PacketUtil.DecodePacketInt(p, 0), isReceived));
                        else this.messageLog.AddLast(new LogMessage(currTime + "GAME_ID Sent a game ID to use: " +
                           PacketUtil.DecodePacketInt(p, 0), isReceived));
                        break;
                    }
                case Headers.GAME_MAP_CHANGED:
                    {
                        if (isReceived) this.messageLog.AddLast(new LogMessage(currTime + "GAME_MAP_CHANGED Received game map of game id = " +
                            PacketUtil.DecodePacketInt(p, 0) + " has changed to -> " +
                            PacketUtil.DecodePacketString(p, 4), isReceived));
                        else this.messageLog.AddLast(new LogMessage(currTime + "GAME_MAP_CHANGED Sent that game map of game id = " +
                           PacketUtil.DecodePacketInt(p, 0) + " has changed to -> " +
                           PacketUtil.DecodePacketString(p, 4), isReceived));
                        break;
                    }
                case Headers.CLIENT_DESTROY_GAME:
                    {
                        if (isReceived) this.messageLog.AddLast(new LogMessage(currTime + "CLIENT_DESTROY_GAME Received destroy game request by client!", isReceived));
                        else this.messageLog.AddLast(new LogMessage(currTime + "CLIENT_DESTROY_GAME Sent destroy game request.", isReceived));
                        break;
                    }
                case Headers.SERVER_DESTROY_GAME:
                    {
                        if (isReceived) this.messageLog.AddLast(new LogMessage(currTime + "SERVER_DESTROY_GAME Received destroy game request of game " +
                           PacketUtil.DecodePacketInt(p, 0), isReceived));
                        else this.messageLog.AddLast(new LogMessage(currTime + "SERVER_DESTROY_GAME Sent destroy game request of game " +
                           PacketUtil.DecodePacketInt(p, 0) + " to all clients.", isReceived));
                        break;
                    }
                case Headers.CLIENT_REQUEST_JOIN:
                    {
                        if (isReceived) this.messageLog.AddLast(new LogMessage(currTime + "CLIENT_REQUEST_JOIN Received request to join game " +
                            PacketUtil.DecodePacketInt(p, 0), isReceived));
                        else this.messageLog.AddLast(new LogMessage(currTime + "CLIENT_REQUEST_JOIN Sent request to join game " +
                            PacketUtil.DecodePacketInt(p, 0), isReceived));
                        break;
                    }
                case Headers.SERVER_REQUEST_JOIN:
                    {
                        if (isReceived) this.messageLog.AddLast(new LogMessage(currTime + "SERVER_REQUEST_JOIN Received request if user " +
                            PacketUtil.DecodePacketInt(p, 4) + " can join my game " +
                            PacketUtil.DecodePacketInt(p, 0), isReceived));
                        else this.messageLog.AddLast(new LogMessage(currTime + "SERVER_REQUEST_JOIN Sent request if user " +
                            PacketUtil.DecodePacketInt(p, 4) + " can join my game " +
                            PacketUtil.DecodePacketInt(p, 0), isReceived));
                        break;
                    }
                case Headers.CLIENT_OK_JOIN:
                    {
                        if (isReceived) this.messageLog.AddLast(new LogMessage(currTime + "CLIENT_OK_JOIN Received OK to join game. ", isReceived));
                        else this.messageLog.AddLast(new LogMessage(currTime + "CLIENT_OK_JOIN Sent OK to join game. ", isReceived));
                        break;
                    }
                case Headers.CLIENT_GAME_FULL:
                    {
                        if (isReceived) this.messageLog.AddLast(new LogMessage(currTime + "CLIENT_GAME_FULL Received game full message. ", isReceived));
                        else this.messageLog.AddLast(new LogMessage(currTime + "CLIENT_GAME_FULL Sent game full message.", isReceived));
                        break;
                    }
                case Headers.CLIENT_LEFT_GAME:
                    {
                        if (isReceived) this.messageLog.AddLast(new LogMessage(currTime + "CLIENT_LEFT_GAME Received client has left game message. ", isReceived));
                        else this.messageLog.AddLast(new LogMessage(currTime + "CLIENT_LEFT_GAME Sent client has left message.", isReceived));
                        break;
                    }
                case Headers.GAME_COLOR_CHANGED:
                    {
                        if (isReceived) this.messageLog.AddLast(new LogMessage(currTime + "GAME_COLOR_CHANGED Received client has changed color message: color = " +
                            PacketUtil.DecodePacketInt(p, 8), isReceived));
                        else this.messageLog.AddLast(new LogMessage(currTime + "GAME_COLOR_CHANGED Sent client has changed color message: color = " +
                            PacketUtil.DecodePacketInt(p, 8), isReceived));
                        break;
                    }
                case Headers.GAME_TEAM_CHANGED:
                    {
                        if (isReceived) this.messageLog.AddLast(new LogMessage(currTime + "GAME_TEAM_CHANGED Received client has changed team message: team = " +
                            PacketUtil.DecodePacketInt(p, 8), isReceived));
                        else this.messageLog.AddLast(new LogMessage(currTime + "GAME_TEAM_CHANGED Sent client has changed team message: team = " +
                            PacketUtil.DecodePacketInt(p, 8), isReceived));
                        break;
                    }
                case Headers.GAME_READY_CHANGED:
                    {
                        if (isReceived) this.messageLog.AddLast(new LogMessage(currTime + "GAME_READY_CHANGED Received client has changed ready state: " +
                            PacketUtil.DecodePacketInt(p, 8), isReceived));
                        else this.messageLog.AddLast(new LogMessage(currTime + "GAME_READY_CHANGED Sent client has changed ready state: " +
                            PacketUtil.DecodePacketInt(p, 8), isReceived));
                        break;
                    }
                case Headers.GAME_KICK_CLIENT:
                    {
                        if (isReceived) this.messageLog.AddLast(new LogMessage(currTime + "GAME_KICK_CLIENT Received client kick message: " +
                            PacketUtil.DecodePacketInt(p, 0), isReceived));
                        else this.messageLog.AddLast(new LogMessage(currTime + "GAME_KICK_CLIENT Sent client kick message: " +
                            PacketUtil.DecodePacketInt(p, 0), isReceived));
                        break;
                    }
                case Headers.CLIENT_GAME_START:
                    {
                        if (isReceived) this.messageLog.AddLast(new LogMessage(currTime + "CLIENT_GAME_START Received client game start message: ", isReceived));
                        else this.messageLog.AddLast(new LogMessage(currTime + "CLIENT_GAME_START Sent client game start message: ", isReceived));
                        break;
                    }
                case Headers.SERVER_GAME_START:
                    {
                        if (isReceived) this.messageLog.AddLast(new LogMessage(currTime + "SERVER_GAME_START Received server game start message: " +
                            PacketUtil.DecodePacketInt(p, 0), isReceived));
                        else this.messageLog.AddLast(new LogMessage(currTime + "SERVER_GAME_START Sent server game start message: " +
                            PacketUtil.DecodePacketInt(p, 0), isReceived));
                        break;
                    }
                case Headers.DONE_LOADING:
                    {
                        if (isReceived) this.messageLog.AddLast(new LogMessage(currTime + "DONE_LOADING Received client finished loading message: " +
                            PacketUtil.DecodePacketInt(p, 0), isReceived));
                        else this.messageLog.AddLast(new LogMessage(currTime + "DONE_LOADING Sent finished loading message: " +
                            PacketUtil.DecodePacketInt(p, 0), isReceived));
                        break;
                    }
                case Headers.GAME_REQUEST_OBJECT_ID:
                    {
                        if (isReceived) this.messageLog.AddLast(new LogMessage(currTime + "GAME_REQUEST_OBJECT_ID Received client wants object ID message: local = " +
                            PacketUtil.DecodePacketInt(p, 0), isReceived));
                        else this.messageLog.AddLast(new LogMessage(currTime + "GAME_REQUEST_OBJECT_ID Sent server ID package: local = " +
                            PacketUtil.DecodePacketInt(p, 0), isReceived));
                        break;
                    }
                case Headers.GAME_OBJECT_ID:
                    {
                        if (isReceived) this.messageLog.AddLast(new LogMessage(currTime + "GAME_OBJECT_ID Received client wants object ID message: local = " +
                            PacketUtil.DecodePacketInt(p, 0) + ", serverID = " +
                            PacketUtil.DecodePacketInt(p, 4), isReceived));
                        else this.messageLog.AddLast(new LogMessage(currTime + "GAME_OBJECT_ID Sent server ID package: local = " +
                            PacketUtil.DecodePacketInt(p, 0) + ", serverID = " +
                            PacketUtil.DecodePacketInt(p, 4), isReceived));
                        break;
                    }
                case UnitHeaders.GAME_UNIT_LOCATION:
                    {
                        if (isReceived) this.messageLog.AddLast(new LogMessage(currTime + "GAME_UNIT_LOCATION Received client sync unit request (" +
                        PacketUtil.DecodePacketInt(p, 0) + ") target(" +
                        PacketUtil.DecodePacketInt(p, 4) + ", " +
                        PacketUtil.DecodePacketInt(p, 8) + "), current("+
                        PacketUtil.DecodePacketInt(p, 12) + ", " +
                        PacketUtil.DecodePacketInt(p, 16) + ")", isReceived));
                        else this.messageLog.AddLast(new LogMessage(currTime + "GAME_UNIT_LOCATION Sent client sync unit request (" +
                        PacketUtil.DecodePacketInt(p, 0) + ") target(" +
                        PacketUtil.DecodePacketInt(p, 4) + ", " +
                        PacketUtil.DecodePacketInt(p, 8) + "), current("+
                        PacketUtil.DecodePacketInt(p, 12) + ", " +
                        PacketUtil.DecodePacketInt(p, 16) + ")", isReceived));
                        break;
                    }
                case UnitHeaders.GAME_NEW_UNIT:
                    {
                        if (isReceived) this.messageLog.AddLast(new LogMessage(currTime + "GAME_NEW_UNIT Received client wants to create new unit: player = " +
                            PacketUtil.DecodePacketInt(p, 0) + ", serverID = " + +
                            PacketUtil.DecodePacketInt(p, 4) + ", type = " + +
                            PacketUtil.DecodePacketInt(p, 8), isReceived));
                        else this.messageLog.AddLast(new LogMessage(currTime + "GAME_NEW_UNIT Sent create new unit: player = " +
                            PacketUtil.DecodePacketInt(p, 0) + ", serverID = " +
                            PacketUtil.DecodePacketInt(p, 4) + ", type = " +
                            PacketUtil.DecodePacketInt(p, 8), isReceived));
                        break;
                    }
                case Headers.GAME_REQUEST_OBJECT_DATA:
                    {
                        if (isReceived) this.messageLog.AddLast(new LogMessage(currTime + "GAME_REQUEST_UNIT_DATA Received client wants info about a unit: player = " +
                            PacketUtil.DecodePacketInt(p, 0) + ", serverID = " + +
                            PacketUtil.DecodePacketInt(p, 4), isReceived));
                        else this.messageLog.AddLast(new LogMessage(currTime + "GAME_REQUEST_UNIT_DATA Sent info about a unit: player = " +
                            PacketUtil.DecodePacketInt(p, 0) + ", serverID = " +
                            PacketUtil.DecodePacketInt(p, 4), isReceived));
                        break;
                    }
                case Headers.GAME_SEND_OBJECT_DATA:
                    {
                        if (isReceived) this.messageLog.AddLast(new LogMessage(currTime + "GAME_SEND_UNIT_DATA Received info about a unit: targetPlayer = " +
                            PacketUtil.DecodePacketInt(p, 0) + ", ownerID = " +
                            PacketUtil.DecodePacketInt(p, 4) + ", serverID = " +
                            PacketUtil.DecodePacketInt(p, 8) + ", type = " +
                            PacketUtil.DecodePacketInt(p, 12), isReceived));
                        else this.messageLog.AddLast(new LogMessage(currTime + "GAME_SEND_UNIT_DATA Sent data about a unit: targetPlayer = " +
                            PacketUtil.DecodePacketInt(p, 0) + ", ownerID = " +
                            PacketUtil.DecodePacketInt(p, 4) + ", serverID = " +
                            PacketUtil.DecodePacketInt(p, 8) + ", type = " +
                            PacketUtil.DecodePacketInt(p, 12), isReceived));
                        break;
                    }
                case BuildingHeaders.GAME_NEW_BUILDING:
                    {
                        if (isReceived) this.messageLog.AddLast(new LogMessage(currTime + "GAME_NEW_BUILDING Received client wants to create a new building: ownerID = " +
                            PacketUtil.DecodePacketInt(p, 0) + ", serverID = " +
                            PacketUtil.DecodePacketInt(p, 4) + ", type = " +
                            PacketUtil.DecodePacketInt(p, 8) + ", by = " +
                            PacketUtil.DecodePacketInt(p, 12), isReceived));
                        else this.messageLog.AddLast(new LogMessage(currTime + "GAME_NEW_BUILDING Sent create building request: ownerID = " +
                            PacketUtil.DecodePacketInt(p, 0) + ", serverID = " +
                            PacketUtil.DecodePacketInt(p, 4) + ", type = " +
                            PacketUtil.DecodePacketInt(p, 8) + ", by = " +
                            PacketUtil.DecodePacketInt(p, 12), isReceived));
                        break;
                    }
                case BuildingHeaders.GAME_BUILDING_LOCATION:
                    {
                        if (isReceived) this.messageLog.AddLast(new LogMessage(currTime + "GAME_BUILDING_LOCATION Received building location update request: serverID = " +
                            PacketUtil.DecodePacketInt(p, 0) + ", (" +
                            PacketUtil.DecodePacketInt(p, 4) + ", " +
                            PacketUtil.DecodePacketInt(p, 8) + ")", isReceived));
                        else this.messageLog.AddLast(new LogMessage(currTime + "GAME_BUILDING_LOCATION Sent building location request: serverID = " +
                            PacketUtil.DecodePacketInt(p, 0) + ", (" +
                            PacketUtil.DecodePacketInt(p, 4) + ", " +
                            PacketUtil.DecodePacketInt(p, 8) + ")", isReceived));
                        break;
                    }
                case UnitHeaders.GAME_UNIT_MELEE_DAMAGE:
                    {
                        if (isReceived) this.messageLog.AddLast(new LogMessage(currTime + "GAME_UNIT_MELEE_DAMAGE Received unit damage done request: typeSource = " +
                            PacketUtil.DecodePacketInt(p, 0) + ", targetID = " +
                            PacketUtil.DecodePacketInt(p, 4) + ", fromID =  " +
                            PacketUtil.DecodePacketInt(p, 8), isReceived));
                        else this.messageLog.AddLast(new LogMessage(currTime + "GAME_UNIT_MELEE_DAMAGE Sent unit damage done request: typeSource = " +
                            PacketUtil.DecodePacketInt(p, 0) + ", targetID = " +
                            PacketUtil.DecodePacketInt(p, 4) + ", fromID =  " +
                            PacketUtil.DecodePacketInt(p, 8), isReceived));
                        break;
                    }
                case UnitHeaders.GAME_UNIT_RANGED_SHOT:
                    {
                        if (isReceived) this.messageLog.AddLast(new LogMessage(currTime + "GAME_UNIT_RANGED_SHOT Received unit ranged damage done request: arrowID = " +
                            PacketUtil.DecodePacketInt(p, 0) + ", sourceID = " +
                            PacketUtil.DecodePacketInt(p, 4) + ", targetID = " +
                            PacketUtil.DecodePacketInt(p, 8), isReceived));
                        else this.messageLog.AddLast(new LogMessage(currTime + "GAME_UNIT_RANGED_SHOT Sent unit ranged damage done request: arrowID = " +
                            PacketUtil.DecodePacketInt(p, 0) + ", sourceID = " +
                            PacketUtil.DecodePacketInt(p, 4) + ", targetID = " +
                            PacketUtil.DecodePacketInt(p, 8), isReceived));
                        break;
                    }

                /*
                 *
                    if (isReceived) this.messageLog.AddLast(new LogMessage( currTime + " ",isReceived ));
                    else this.messageLog.AddLast(new LogMessage( currTime + " ",isReceived ));
                 *
                 */
                default:
                    {
                        if (isReceived) this.messageLog.AddLast(new LogMessage(currTime + " Received an unknown request (" + p.GetHeader() + ") "
                            + "(or have you forgotten to add the header to the log?)", isReceived));
                        else this.messageLog.AddLast(new LogMessage(currTime + " Sent unknown request (" + p.GetHeader() + ") "
                            + "(or have you forgotten to add the header to the log?)", isReceived));
                        break;
                    }
            }
        }
Пример #28
0
        /// <summary>
        /// Enables the socket to accept packets
        /// </summary>
        public void Enable()
        {
            if (this.packetProcessor == null) packetProcessor = new PacketProcessor();
            packetProcessor.onProcessPacket += this.OnProcessPacket;
            packetProcessor.StartProcessing();

            lock (SyncRecv)
            {
                Console.Out.WriteLine("Starting receiving packets..");
                while (Receiving)
                {
                    if (Sock.Connected)
                    {
                        int size = 0;
                        try
                        {
                            size = Sock.Receive(buff);
                        }
                        catch (Exception e)
                        {
                            this.Disable();
                            break;
                        }
                        if (size == 0)
                        {
                            Disable();
                            Show("Will not accept 'byte[i <= 0]'");
                            Receiving = false;
                            break;
                        }
                        if (size < 1000)
                        {
                            byte[] data = new byte[size];
                            Array.Copy(buff, data, size);
                            //TODO: Should go to a SocketProcessor here.

                            byte[] headerlessData = new byte[data.Length - 1];
                            for (int i = 1; i < data.Length; i++)
                            {
                                headerlessData[i - 1] = data[i];
                            }
                            Packet p = new Packet(data[0], headerlessData);
                            this.packetProcessor.QueuePacket(p);
                        }
                        else
                        {
                            Disable();
                            Show("Cannot accept 'bytes[i > 1000]'");
                            Receiving = false;
                            break;
                        }
                    }
                }
                if (onDisconnectListeners != null) onDisconnectListeners();
                Console.Out.WriteLine("Ending receiving packets..");
            }
            packetProcessor.StopProcessing();
        }
Пример #29
0
        public void DataReceived(Packet p)
        {
            switch (p.GetHeader())
            {
                case BuildingHeaders.GAME_NEW_BUILDING:
                    {
                        /*
                        /// <summary>
                        /// Types to be used to request object IDs
                        /// </summary>
                        public const int TYPE_BARRACKS = 0, TYPE_FACTORY = 1, TYPE_FORTRESS = 2, TYPE_RESOURCES_GATHER = 3;

                        /// <summary>
                        /// Client synchronizes a building
                        /// [Header] [Int32 serverID] [Int32 locationX] [Int32 locationY]
                        /// </summary>
                        public const byte GAME_BUILDING_LOCATION = 0x40;
                        */

                        int playerID = PacketUtil.DecodePacketInt(p, 0);
                        int serverID = PacketUtil.DecodePacketInt(p, 4);
                        int buildingType = PacketUtil.DecodePacketInt(p, 8);
                        int by = PacketUtil.DecodePacketInt(p, 12);

                        if (MultiplayerDataManager.GetInstance().GetDataByServerID(serverID, false) == null)
                        {
                            ObjectCreator.GetInstance().CreateBuilding(playerID, serverID, buildingType, by);
                        }

                        break;
                    }
                case BuildingHeaders.GAME_BUILDING_LOCATION:
                    {
                        int serverID = PacketUtil.DecodePacketInt(p, 0);
                        int locationX = PacketUtil.DecodePacketInt(p, 4);
                        int locationY = PacketUtil.DecodePacketInt(p, 8);

                        MultiplayerData data;
                        int count = 0;
                        do
                        {
                            data = MultiplayerDataManager.GetInstance().GetDataByServerID(serverID, true);
                            count++;
                            if (count > 5)
                            {
                                Console.Out.WriteLine("Unable to fetch data, requesting..");
                                Packet packet = new Packet(Headers.GAME_REQUEST_OBJECT_DATA);
                                packet.AddInt(Game1.CURRENT_PLAYER.multiplayerID);
                                packet.AddInt(serverID);
                                GameServerConnectionManager.GetInstance().SendPacket(packet);

                                return;
                            }
                        }
                        while (data == null);

                        Building building = ((BuildingMultiplayerData)data).building;

                        building.x = locationX;
                        building.y = locationY;

                        try
                        {
                            if (building.state == Building.State.MultiplayerWaitingForLocation) building.PlaceBuilding(building.constructedBy);
                        }
                        catch (Exception e) { }

                        break;
                    }
            }
        }
Пример #30
0
        /// <summary>
        /// Called when the OK button was pressed.
        /// </summary>
        /// <param name="source">The XNA Button</param>
        public virtual void OnOKClick(XNAButton source)
        {
            if (!ChatServerConnectionManager.GetInstance().connection.Receiving) return;
            String map = this.GetSelectedMap();
            if (map != null)
            {
                // this.previewPanel.selectedMapLbl.text = map;

                Packet p = new Packet(Headers.GAME_MAP_CHANGED);
                p.AddInt(ChatServerConnectionManager.GetInstance().user.channelID);
                p.AddString(map);
                ChatServerConnectionManager.GetInstance().SendPacket(p);
                Console.Out.WriteLine("Sent GAME_MAP_CHANGED packet");
            }
            this.Dispose();
        }