Hmm packet writer question.

09/21/2011 01:00 BaussHacker#1
Why do everybody uses something like.
Code:
WriteByte(byte value);
WriteUInt32(uint value);
//....
When you could just do something like.
Code:
    public unsafe class ConquerPacket
    {
        public ConquerPacket(int Length)
        {
            if (Length > Kernel.MaxPacketSize)
                throw new Exception("Invalid Packet Length.");

            m_packet = new byte[Length];
        }
        public int Length { get { return m_packet.Length; } }
        private byte* m_ptr
        {
            get
            {
                fixed (byte* ptr = m_packet)
                    return ptr;
            }
        }
        private byte[] m_packet;
        private int m_offset;

        /// <summary>
        /// Writing a value to the packet.
        /// </summary>
        /// <param name="Value">The value to write.</param>
        /// <param name="DataType">The type of the value.</param>
        /// <param name="StringLength">The length of the string, should be 0, if it's not a string.</param>
        public void Write(object Value, Enums.DataTypes DataType, int StringLength)
        {
            switch (DataType)
            {
                #region Byte
                case Enums.DataTypes.Byte:
                    {
                        *((byte*)(m_ptr + m_offset)) = (byte)Value;
                        m_offset++;
                        break;
                    }
                #endregion
                #region Byte
                case Enums.DataTypes.Bytes:
                    {
                        byte[] value = (byte[])Value;
                        foreach (byte b in value)
                            Write(b, Enums.DataTypes.Byte, 0);

                        break;
                    }
                #endregion
                #region UInt16
                case Enums.DataTypes.UInt16:
                    {
                        *((ushort*)(m_ptr + m_offset)) = (ushort)Value;
                        m_offset += 2;
                        break;
                    }
                #endregion
                #region UInt32
                case Enums.DataTypes.UInt32:
                    {
                        *((uint*)(m_ptr + m_offset)) = (uint)Value;
                        m_offset += 4;
                        break;
                    }
                #endregion
                #region UInt64
                case Enums.DataTypes.UInt64:
                    {
                        *((uint*)(m_ptr + m_offset)) = (uint)Value;
                        m_offset += 8;
                        break;
                    }
                #endregion
                #region String
                case Enums.DataTypes.String:
                    {
                        string value = Value.ToString();
                        if (value.Length > Length)
                        {
                            for (int i = 0; i < Length; i++)
                            {
                                *((byte*)(m_ptr + m_offset)) = Convert.ToByte(value[i]);
                                m_offset++;
                            }
                        }
                        else
                        {
                            for (int i = 0; i < value.Length; i++)
                            {
                                *((byte*)(m_ptr + m_offset)) = Convert.ToByte(value[i]);
                                m_offset++;
                            }
                        }
                        break;
                    }
                #endregion
            }
        }
        public void Move(int AddOffset)
        {
            m_offset += AddOffset;
        }

        public static implicit operator byte[](ConquerPacket Packet)
        {
            return Packet.m_packet;
        }
    }
Example:
Code:
                        ConquerPacket Packet = new ConquerPacket(32);
                        Packet.Write(Packet.Length, Enums.DataTypes.UInt16, 0);
                        Packet.Write(0x41f, Enums.DataTypes.UInt16, 0);
                        Packet.Write(packet.IV, Enums.DataTypes.Bytes, 0);
                        Packet.Write(Kernel.ServerIP, Enums.DataTypes.String, 0);
                        Packet.Move(16 - Kernel.ServerIP.Length);
                        Packet.Write(Kernel.GamePort, Enums.DataTypes.UInt16, 0);
:D
09/21/2011 01:09 Mr_PoP#2
is not it the same-thing but instead of separate methods u made one method which ur still separating "the types " inside it using the enum lol?
09/21/2011 01:09 BaussHacker#3
Quote:
Originally Posted by Mr_PoP View Post
is not it the same-thing but instead of separate methods u made one method which ur still separating inside it using the enum lol?
Yup. Just why make seperate methods, when you can do it all in one?

Perfomance-wise it will be the same basically.
09/21/2011 01:14 Mr_PoP#4
Quote:
Originally Posted by BaussHacker View Post
Yup. Just why make seperate methods, when you can do it all in one?

Perfomance-wise it will be the same basically.
well am against the idea of enuming methods , you should have separate methods based on it's functionality!
09/21/2011 01:17 BaussHacker#5
Quote:
Originally Posted by Mr_PoP View Post
well am against the idea of enuming methods , you should have separate methods based on it's functionality!
In the end it might just be personal. I like this way tho, but maybe it's because I'm used to do things like this working with parameters in MSSQL etc.? :rolleyes:
09/21/2011 01:20 Mr_PoP#6
Quote:
Originally Posted by BaussHacker View Post
In the end it might just be personal. I like this way tho, but maybe it's because I'm used to do things like this working with parameters in MSSQL etc.? :rolleyes:
yeah :P agreed :rolleyes:
09/21/2011 01:24 Korvacs#7
Yours will be a tiny bit slower than just using individual methods, not just because of the switch but because you need to check the type of the value, it doesnt actually improve readability because you dont know if your writing ints, shorts or bytes, so that actually makes the code harder to read.

Perhaps those are some of the reasons why this isnt done...
09/21/2011 01:29 BaussHacker#8
Sooooo, I should switch to methods again?
09/21/2011 01:36 .Kinshi#9
Code:
void Write(Byte b) { }
void Write(UInt16 b) { }
void Write(UInt32 b) { }
... etc
Then you just call one function name, clean and sexy imo.
09/21/2011 01:44 BaussHacker#10
Quote:
Originally Posted by .Kinshi View Post
Code:
void Write(Byte b) { }
void Write(UInt16 b) { }
void Write(UInt32 b) { }
... etc
Then you just call one function name, clean and sexy imo.
I feel stupid now. Spank me.
09/21/2011 02:08 .Kinshi#11
Quote:
Originally Posted by BaussHacker View Post
I feel stupid now. Spank me.
Gladly ;)
09/21/2011 09:51 BaussHacker#12
Quote:
Originally Posted by .Kinshi View Post
Gladly ;)
;)

____
Alright, so I have switched it back to methods again.