Packet structuring

12/28/2011 02:44 I don't have a username#1
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?
12/28/2011 04:29 Spirited#2
Slow down things.
Edit: Use real structs. It's faster when you convert it to a byte array.
12/28/2011 08:01 Lateralus#3
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.