Register for your free account! | Forgot your password?

You last visited: Today at 12:03

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



Packet structuring

Discussion on Packet structuring within the CO2 Private Server forum part of the Conquer Online 2 category.

Reply
 
Old   #1
 
elite*gold: 0
Join Date: Dec 2011
Posts: 1,537
Received Thanks: 785
Packet structuring

Example on CharacterCreation.

If you store the structures values like this:
Code:
        public Data<ushort> Size = new Data<ushort>() { Value = 60, Offset = 0 };
        public Data<ushort> Type = new Data<ushort>() { Value = 0x3e9, Offset = 2 };
        public Data<string> Account = new Data<string>() { Offset = 4 };
        public Data<string> CharName = new Data<string>() { Offset = 20 };
        public Data<string> Password = new Data<string>() { Offset = 36 };
        public Data<ushort> Model = new Data<ushort>() { Offset = 52 };
        public Data<ushort> Job = new Data<ushort>() { Offset = 54 };
        public Data<uint> UID = new Data<uint>() { Offset = 56 };
Data struct:
Code:
    public struct Data<T>
    {
        public int Offset;
        public T Value;

        public static implicit operator T(Data<T> data)
        {
            return data.Value;
        }
    }
Then at reading the packet:
Code:
        public static implicit operator CharacterCreationPacket(byte[] Packet)
        {
            CharacterCreationPacket packet = new CharacterCreationPacket(Packet);

            packet.Size.Value = packet.ReadUInt16(packet.Size.Offset);
            packet.Type.Value = packet.ReadUInt16(packet.Type.Offset);
            for (int i = 0; i < 16; i++)
            {
                byte BYTE = packet.ReadByte(packet.Account.Offset + i);
                if (BYTE > 0)
                {
                    packet.Account.Value += Convert.ToChar(BYTE);
                }
            }
            for (int i = 0; i < 16; i++)
            {
                byte BYTE = packet.ReadByte(packet.CharName.Offset + i);
                if (BYTE > 0)
                {
                    packet.CharName.Value += Convert.ToChar(BYTE);
                }
            }
            for (int i = 0; i < 16; i++)
            {
                byte BYTE = packet.ReadByte(packet.Password.Offset + i);
                if (BYTE > 0)
                {
                    packet.Password.Value += Convert.ToChar(BYTE);
                }
            }
            packet.Model.Value = packet.ReadUInt16(packet.Model.Offset);
            packet.Job.Value = packet.ReadUInt16(packet.Job.Offset);
            packet.UID.Value = packet.ReadUInt16(packet.UID.Offset);

            return packet;
        }
Would it have any benefit or it would just slow down everything?
I don't have a username is offline  
Old 12/28/2011, 04:29   #2
 
Spirited's Avatar
 
elite*gold: 12
Join Date: Jul 2011
Posts: 8,211
Received Thanks: 4,114
Slow down things.
Edit: Use real structs. It's faster when you convert it to a byte array.
Spirited is offline  
Old 12/28/2011, 08:01   #3
 
Lateralus's Avatar
 
elite*gold: 0
Join Date: May 2005
Posts: 1,892
Received Thanks: 918
This isn't too bad, but it's slower than *most* methods using pointers and it looks like the code for your types can get ugly. Use a packet wrapper, stuff reading and writing functions you'll need inside it, then inherit that wrapper in each of the type packets. Any other way (aka, using structs) is not object oriented and "permanent", meaning if you change your packet structuring method, you're going to need to navigate through your entire code changing every single reference.
Lateralus is offline  
Reply




All times are GMT +2. The time now is 12:03.


Powered by vBulletin®
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2024 elitepvpers All Rights Reserved.