示例#1
0
        // Before main looping starts, we loop here and wait for approval message
        private static void WaitForStartingInfo()
        {
            // When this is set to true, we are approved and ready to go
            bool CanStart = false;

            // New incomgin message
            NetIncomingMessage inc;

            // Loop untill we are approved
            while (!CanStart)
            {

                // If new messages arrived
                if ((inc = Client.ReadMessage()) != null)
                {
                    // Switch based on the message types
                    switch (inc.MessageType)
                    {

                        // All manually sent messages are type of "Data"
                        case NetIncomingMessageType.Data:

                            // Read the first byte
                            // This way we can separate packets from each others
                            if (inc.ReadByte() == (byte)PacketTypes.WORLDSTATE)
                            {
                                // Worldstate packet structure
                                //
                                // int = count of players
                                // character obj * count

                                //Console.WriteLine("WorldState Update");

                                // Empty the gamestatelist
                                // new data is coming, so everything we knew on last frame, does not count here
                                // Even if client would manipulate this list ( hack ), it wont matter, becouse server handles the real list
                                GameStateList.Clear();

                                // Declare count
                                int count = 0;

                                // Read int
                                count = inc.ReadInt32();

                                // Iterate all players
                                for (int i = 0; i < count; i++)
                                {

                                    // Create new character to hold the data
                                    Character ch = new Character();

                                    // Read all properties ( Server writes characters all props, so now we can read em here. Easy )
                                    inc.ReadAllProperties(ch);

                                    // Add it to list
                                    GameStateList.Add(ch);
                                }

                                // When all players are added to list, start the game
                                CanStart = true;
                            }
                            break;

                        default:
                            // Should not happen and if happens, don't care
                            Console.WriteLine(inc.ReadString() + " Strange message");
                            break;
                    }
                }
            }
        }
示例#2
0
        /// <summary>
        /// Check for new incoming messages from server
        /// </summary>
        private static void CheckServerMessages()
        {
            // Create new incoming message holder
            NetIncomingMessage inc;

            // While theres new messages
            //
            // THIS is exactly the same as in WaitForStartingInfo() function
            // Check if its Data message
            // If its WorldState, read all the characters to list
            while ((inc = Client.ReadMessage()) != null)
            {
                if (inc.MessageType == NetIncomingMessageType.Data)
                {
                    if (inc.ReadByte() == (byte)PacketTypes.WORLDSTATE)
                    {
                        Console.WriteLine("World State uppaus");
                        GameStateList.Clear();
                        int jii = 0;
                        jii = inc.ReadInt32();
                        for (int i = 0; i < jii; i++)
                        {
                            Character ch = new Character();
                            inc.ReadAllProperties(ch);
                            GameStateList.Add(ch);
                        }
                    }
                }
            }
        }