Packetbuilder

10/18/2011 21:21 Arco.#1
I have a simple packetbuilder right now, but is there any way that this can be optimized for efficiency?
10/18/2011 21:43 BaussHacker#2
Why you making Offset ushort? Just make it an int instead from ushort -> int, because index of an array is int.

buffer[int], even it doesn't say anything, it still uses time converting from ushort to int. I could be wrong, but it's how I look at it.

And this:
Code:
        public void WriteStringWithLength(string Arg, ushort Offset)
        {
            buffer[Offset] = (byte)Arg.Length;
            Offset++;
            ushort i = 0;
            while (i < Arg.Length)
            {
                buffer[(ushort)(i + Offset)] = (byte)Arg[i];
                i = (ushort)(i + 1);
            }
        }
Just use a for loop.
Code:
        public void WriteStringWithLength(string Arg, ushort Offset)
        {
            buffer[Offset] = (byte)Arg.Length;
            for (int i = 1; i < Arg.Length; i++)
                buffer[(ushort)(i + Offset)] = (byte)Arg[i];
        }
Same goes for:
Code:
public void WriteString(string Arg, ushort Offset)
        {
            ushort i = 0;
            while (i < Arg.Length)
            {
                buffer[(ushort)(i + Offset)] = (byte)Arg[i];
                i = (ushort)(i + 1);
            }
        }
Code:
        public void WriteString(string Arg, ushort Offset)
        {
            for (int i = 0; i < Arg.Length; i++)
                buffer[(ushort)(i + Offset)] = (byte)Arg[i];
        }
Can't really see much more, not even sure if it will do lot difference.
10/18/2011 22:46 Arco.#3
Thanks beautiful.
10/18/2011 23:30 pro4never#4
Few things..

Pointers. I don't see much reason not to use them here
Structs. Maybe its just me who's fond if it but I'd rather just use a memory structure that's organized the same as my packet then marshal it to send.
10/18/2011 23:35 IAmHawtness#5
Quote:
Originally Posted by pro4never View Post
Few things..

Pointers. I don't see much reason not to use them here
Structs. Maybe its just me who's fond if it but I'd rather just use a memory structure that's organized the same as my packet then marshal it to send.
I agree with the pointers part, unsafe code would optimize this code a lot. All these bitwise shifts are pretty CPU intensive (as in they require a lot more cycles) compared to using pointers.

I've never been a fan of using structs though, mainly because I'm often too lazy to completely structure a packet
10/19/2011 00:13 Korvacs#6
Pointers, a way to write a byte to the buffer would come in handy aswell, i assume you forgot about that..
10/19/2011 01:55 Arco.#7
So how do all of you write/structure packets?
10/19/2011 02:03 Korvacs#8
Packet class with buffer and pointer, then a series of methods to write to the buffer using the pointer etc.
10/19/2011 02:21 Arco.#9
Sorta long the lines of this?
Code:
        public static void WriteUInt32(uint arg, int offset, byte[] buffer)
        {
            unsafe
            {
                fixed (byte* Buffer = buffer)
                {
                    *((uint*)(Buffer + offset)) = arg;
                }
            }
        }
Alright in case that's what you mean by it, this is what I recoded my packetbuilder to.

10/19/2011 03:38 pro4never#10
I'd strongly recommend looking into tq's net string packer...

It's sexy as hell imo. You can reverse it from the eudemons binary leaked source. It's what we use in my source instead of a method like your WriteStringList method there.
10/19/2011 09:53 Korvacs#11
Mine doesn't require the unsafe or fixed declarations in every method, but yeah something like that.
10/19/2011 11:17 BaussHacker#12
Quote:
Originally Posted by Arco. View Post
Sorta long the lines of this?
Code:
        public static void WriteUInt32(uint arg, int offset, byte[] buffer)
        {
            unsafe
            {
                fixed (byte* Buffer = buffer)
                {
                    *((uint*)(Buffer + offset)) = arg;
                }
            }
        }
Alright in case that's what you mean by it, this is what I recoded my packetbuilder to.

Make the class unsafe, then make a pointer within the class, instead declaring it everytime you're writing an offset.
Code:
byte* m_ptr
{
get
{
fixed (byte* ptr = buffer)
return ptr;
}
}
Now you don't need to declare an unsafe/fixed everytime. I think that's what Korvacs meant.

I don't know if it could help anything, but this is my packet-builder.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConquerServerProject.Core;

namespace ConquerServerProject.Packets
{
    public unsafe class ConquerPacket
    {
        public ConquerPacket(int Length, bool AddTQ)
        {
            if (Length > Kernel.MaxPacketSize)
                throw new Exception("Invalid Packet Length.");

            m_packet = new byte[Length + 8];

            if (AddTQ)
                Write(Kernel.ServerPrefix);
        }
        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;

        public void Write(byte Value)
        {
            *((byte*)(m_ptr + m_offset)) = (byte)Value;
            m_offset++;
        }
        public void Write(byte[] Value)
        {
            foreach (byte b in Value)
                Write(b);
        }
        public void Write(ushort Value)
        {
            *((ushort*)(m_ptr + m_offset)) = (ushort)Value;
            m_offset += 2;
        }
        public void Write(uint Value)
        {
            *((uint*)(m_ptr + m_offset)) = (uint)Value;
            m_offset += 4;
        }
        public void Write(ulong Value)
        {
            *((uint*)(m_ptr + m_offset)) = (uint)Value;
            m_offset += 8;
        }
        public void Write(string Value)
        {
            for (int i = 0; i < Value.Length; i++)
            {
                *((byte*)(m_ptr + m_offset)) = Convert.ToByte(Value[i]);
                m_offset++;
            }
        }
        public void WriteString(string Value)
        {
            Write((byte)Value.Length);
            m_offset++;
            for (int i = 0; i < Length; i++)
            {
                 *((byte*)(m_ptr + m_offset)) = Convert.ToByte(Value[i]);
                 m_offset++;
            }
        }
        public void Move(int AddOffset)
        {
            m_offset += AddOffset;
        }

        public static implicit operator byte[](ConquerPacket Packet)
        {
            return Packet.m_packet;
        }
    }
}
10/19/2011 12:33 nTL3fTy#13
Quote:
Originally Posted by BaussHacker View Post
...
You could also declare the pointer, and set some allocated space to it (like in COAI):
Code:
ptr = (byte*)(void*)Marshal.AllocHGlobal(size);
where size could be variable or a max packet length (1024 for example).
10/19/2011 13:48 BaussHacker#14
Quote:
Originally Posted by nTL3fTy View Post
You could also declare the pointer, and set some allocated space to it (like in COAI):
Code:
ptr = (byte*)(void*)Marshal.AllocHGlobal(size);
where size could be variable or a max packet length (1024 for example).
Never knew of that. I will look up on that ;O
10/19/2011 13:48 IAmHawtness#15
Quote:
Originally Posted by nTL3fTy View Post
You could also declare the pointer, and set some allocated space to it (like in COAI):
Code:
ptr = (byte*)(void*)Marshal.AllocHGlobal(size);
where size could be variable or a max packet length (1024 for example).
What would the benefit be using AllocHGlobal instead of just "new byte[1024]"? They both allocate memory on the heap, but with AllocHGlobal you have to manually deallocate the memory when you're done using it.