Register for your free account! | Forgot your password?

You last visited: Today at 06:25

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

Advertisement



Packetbuilder

Discussion on Packetbuilder within the CO2 Programming forum part of the Conquer Online 2 category.

Reply
 
Old   #1
 
Arco.'s Avatar
 
elite*gold: 0
Join Date: Feb 2011
Posts: 335
Received Thanks: 170
Packetbuilder

I have a simple packetbuilder right now, but is there any way that this can be optimized for efficiency?
Arco. is offline  
Old 10/18/2011, 21:43   #2
 
elite*gold: 0
Join Date: May 2011
Posts: 1,769
Received Thanks: 756
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.
BaussHacker is offline  
Thanks
1 User
Old 10/18/2011, 22:46   #3
 
Arco.'s Avatar
 
elite*gold: 0
Join Date: Feb 2011
Posts: 335
Received Thanks: 170
Thanks beautiful.
Arco. is offline  
Old 10/18/2011, 23:30   #4
 
elite*gold: 21
Join Date: Jul 2005
Posts: 9,193
Received Thanks: 5,377
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.
pro4never is offline  
Old 10/18/2011, 23:35   #5
 
elite*gold: 20
Join Date: Aug 2007
Posts: 1,749
Received Thanks: 2,198
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
IAmHawtness is offline  
Old 10/19/2011, 00:13   #6


 
Korvacs's Avatar
 
elite*gold: 20
Join Date: Mar 2006
Posts: 6,125
Received Thanks: 2,518
Pointers, a way to write a byte to the buffer would come in handy aswell, i assume you forgot about that..
Korvacs is offline  
Old 10/19/2011, 01:55   #7
 
Arco.'s Avatar
 
elite*gold: 0
Join Date: Feb 2011
Posts: 335
Received Thanks: 170
So how do all of you write/structure packets?
Arco. is offline  
Old 10/19/2011, 02:03   #8


 
Korvacs's Avatar
 
elite*gold: 20
Join Date: Mar 2006
Posts: 6,125
Received Thanks: 2,518
Packet class with buffer and pointer, then a series of methods to write to the buffer using the pointer etc.
Korvacs is offline  
Old 10/19/2011, 02:21   #9
 
Arco.'s Avatar
 
elite*gold: 0
Join Date: Feb 2011
Posts: 335
Received Thanks: 170
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.

Arco. is offline  
Old 10/19/2011, 03:38   #10
 
elite*gold: 21
Join Date: Jul 2005
Posts: 9,193
Received Thanks: 5,377
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.
pro4never is offline  
Old 10/19/2011, 09:53   #11


 
Korvacs's Avatar
 
elite*gold: 20
Join Date: Mar 2006
Posts: 6,125
Received Thanks: 2,518
Mine doesn't require the unsafe or fixed declarations in every method, but yeah something like that.
Korvacs is offline  
Old 10/19/2011, 11:17   #12
 
elite*gold: 0
Join Date: May 2011
Posts: 1,769
Received Thanks: 756
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;
        }
    }
}
BaussHacker is offline  
Thanks
1 User
Old 10/19/2011, 12:33   #13
 
nTL3fTy's Avatar
 
elite*gold: 0
Join Date: Jun 2005
Posts: 692
Received Thanks: 353
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).
nTL3fTy is offline  
Old 10/19/2011, 13:48   #14
 
elite*gold: 0
Join Date: May 2011
Posts: 1,769
Received Thanks: 756
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
BaussHacker is offline  
Old 10/19/2011, 13:48   #15
 
elite*gold: 20
Join Date: Aug 2007
Posts: 1,749
Received Thanks: 2,198
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.
IAmHawtness is offline  
Reply


Similar Threads Similar Threads
Packetbuilder
10/13/2011 - General Coding - 0 Replies
I have a simple packetbuilder right now, but is there any way that this can be optimized for efficiency? class PacketBuilder { private byte buffer; public PacketBuilder(byte Buffer) { buffer = Buffer; } public void ZeroFill(byte Buffer, ushort Offset, ushort Count) {



All times are GMT +2. The time now is 06:26.


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.