Alrighty, well I made this packetbuilder, I feel as if it can be improved though. Anyone have any ideas, any suggestions etc?[...]
Seems really similar to the one I did for you. But, you need to free the memory and everything... Take a look at what I did. You'll be able to improve it. Oh, and as I said when I did the builder for you, you should use structures and pointers to them, it's faster.
Seems really similar to the one I did for you. But, you need to free the memory and everything... Take a look at what I did. You'll be able to improve it. Oh, and as I said when I did the builder for you, you should use structures and pointers to them, it's faster.
using System;
using System.Text;
using System.Runtime.InteropServices;
namespace COServer
{
public unsafe class PacketBuilder
{
public enum SeekMode
{
Set = 0,
Cur = 1,
End = 2,
};
private Encoding Encoding;
private Byte* pBuffer;
private Int32 Length;
private Int32 Position;
public PacketBuilder(Int32 Length)
{
this.Encoding = Encoding.GetEncoding("iso-8859-1");
this.pBuffer = (Byte*)Marshal.AllocHGlobal(Length).ToPointer();
this.Length = Length;
this.Position = 0;
//AllocHGlobal doesn't alloc zero-filled memory.
ZeroFill(Length);
this.Position = 0;
}
public PacketBuilder(Int32 Length, Encoding Encoding)
{
this.Encoding = Encoding;
this.pBuffer = (Byte*)Marshal.AllocHGlobal(Length).ToPointer();
this.Length = Length;
this.Position = 0;
//AllocHGlobal doesn't alloc zero-filled memory.
ZeroFill(Length);
this.Position = 0;
}
~PacketBuilder()
{
if (pBuffer != null)
Marshal.FreeHGlobal((IntPtr)pBuffer);
pBuffer = null;
}
public void Seek(Int32 Position, SeekMode Mode)
{
if (Mode == SeekMode.Set)
this.Position = Position;
else if (Mode == SeekMode.Cur)
this.Position += Position;
else if (Mode == SeekMode.End)
this.Position = Length - Position;
}
public Int32 Tell() { return Position; }
public void ZeroFill(Int32 Length)
{
for (Int32 i = 0; i < Length / 4; i++) //Should be faster for large block...
Write((Int32)0x00);
for (Int32 i = 0; i < Length % 4; i++)
Write((Byte)0x00);
}
public void Write(SByte Param) { *(pBuffer + Position) = (Byte)Param; Position += sizeof(SByte); }
public void Write(Byte Param) { *(pBuffer + Position) = Param; Position += sizeof(Byte); }
public void Write(Int16 Param) { *((Int16*)(pBuffer + Position)) = Param; Position += sizeof(Int16); }
public void Write(UInt16 Param) { *((UInt16*)(pBuffer + Position)) = Param; Position += sizeof(UInt16); }
public void Write(Int32 Param) { *((Int32*)(pBuffer + Position)) = Param; Position += sizeof(Int32); }
public void Write(UInt32 Param) { *((UInt32*)(pBuffer + Position)) = Param; Position += sizeof(UInt32); }
public void Write(Int64 Param) { *((Int64*)(pBuffer + Position)) = Param; Position += sizeof(Int64); }
public void Write(UInt64 Param) { *((UInt64*)(pBuffer + Position)) = Param; Position += sizeof(UInt64); }
public void Write(Byte[] Param) { Marshal.Copy(Param, 0, (IntPtr)(pBuffer + Position), Param.Length); Position += Param.Length * sizeof(Byte); }
public void Write(String Param)
{
Byte[] Buffer = Encoding.GetBytes(Param);
Marshal.Copy(Buffer, 0, (IntPtr)(pBuffer + Position), Buffer.Length);
Position += Buffer.Length;
}
public void Write(String[] Params)
{
foreach (String Param in Params)
{
Byte[] Buffer = Encoding.GetBytes(Param);
Marshal.Copy(Buffer, 0, (IntPtr)(pBuffer + Position), Buffer.Length);
Position += Buffer.Length;
}
}
public void Write(String Param, Boolean WithLength)
{
Byte[] Buffer = Encoding.GetBytes(Param);
if (WithLength)
Write((Byte)Buffer.Length);
Marshal.Copy(Buffer, 0, (IntPtr)(pBuffer + Position), Buffer.Length);
Position += Buffer.Length;
}
public void Write(String[] Params, Boolean WithLength)
{
foreach (String Param in Params)
{
Byte[] Buffer = Encoding.GetBytes(Param);
if (WithLength)
Write((Byte)Buffer.Length);
Marshal.Copy(Buffer, 0, (IntPtr)(pBuffer + Position), Buffer.Length);
Position += Buffer.Length;
}
}
public static implicit operator Byte[](PacketBuilder Packet)
{
Byte[] Buffer = new Byte[Packet.Length];
Marshal.Copy((IntPtr)Packet.pBuffer, Buffer, 0, Packet.Length);
return Buffer;
}
}
}
Yeah, the one you made, I took that and read it over, learned about pointers, and used it as a reference to make a builder. Simplified it a good bit, also explicitly declare offsets, return a ByteArray. Thanks to your builder, I learned a good bit about pointers <3
Even better - pin the byte pointer so it's not swiped by the GC, and assign the internal pointer in the "packet" base class to the pointer to the byte array in a constructor *for packets to process* with the finalizer always freeing the unmanaged memory. Write a bunch of functions in the packet class to split up the fields by pointer arithmetic, derive all packets off of the packet class, and in these derived classes, use accessors to use the base class read and write functions. Tada, you're using just a tiny bit more memory and have a very slight performance degrade for 1028739x neater and more flexible packet handling than pointers to structures.
I've done all this except pinning the byte pointer and assigning it for processing packets (right now, we're copying the data over with memcpy... but hey, I'm never satisfied so what the hell), but there's probably something stupidly wrong with doing this... Now I'm going to go implement that. I'll let you know if what I said works or not. If someone knows it does/doesn't work, please tell me before I start wasting time. ;d
I don't use a packet builder. I make them using structs and send them to the client in one line of code. I never have to copy it... or process it... or waste processor time doing comparisons in the wrapper.
I don't use a packet builder. I make them using structs and send them to the client in one line of code. I never have to copy it... or process it... or waste processor time doing comparisons in the wrapper.
Doing it that way is ugly, time consuming, and not very object oriented.
Doing it that way is ugly, time consuming, and not very object oriented.
Lol, if you say so o.o
I personally think it's much more organized than anything else I've seen. In addition to that, it's much faster than traditional packet building. I've tested it. =p
Lol, if you say so o.o
I personally think it's much more organized than anything else I've seen. In addition to that, it's much faster than traditional packet building. I've tested it. =p
Cause you've used every single method of packetbuilding known to man right?
Forgot, Fang is pro coder, my bad.
Lol, if you say so o.o
I personally think it's much more organized than anything else I've seen. In addition to that, it's much faster than traditional packet building. I've tested it. =p
How is your method "much faster" than mine? Using pointers to structures has exactly the same function as my packet class, except mine is much more flexible and object oriented. I'm pretty sure small accessors are inlined in my case, so what I have is unsafe code only in the packet class and beautiful code in the derived classes and everywhere that uses them that has the same performance as, if not faster than, your method, is much easier to work with, and follows proper OOP guidelines - not an opinion.
How is your method "much faster" than mine? Using pointers to structures has exactly the same function as my packet class, except mine is much more flexible and object oriented. I'm pretty sure small accessors are inlined in my case, so what I have is unsafe code only in the packet class and beautiful code in the derived classes and everywhere that uses them that has the same performance as, if not faster than, your method, is much easier to work with, and follows proper OOP guidelines - not an opinion.
I was comparing it to traditional packet building. You don't have any stupid comparisons like Impulse's wrapper does. It should be fine. And as far as a difference between structs and pointer wrappers like yours... there is no difference (performance wise). I honestly like structs better. It's just a preference of mine.
Edit: Jack says there probably is a difference between structs and wrappers like yours - but it's so small that it wouldn't make a big difference.
Edit: Jack says there probably is a difference between structs and wrappers like yours - but it's so small that it wouldn't make a big difference.
If the compiler inlines small functions and accessors like mine, then there isn't a difference. As of right now, because I copy packets with memcpy into the internal pointer, yeah, there's a difference.
Packetbuilder 10/21/2011 - CO2 Programming - 18 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)
{
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)
{