Packetbuilder

10/19/2011 19:20 InfamousNoone#16
Quote:
Originally Posted by IAmHawtness View Post
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.
Using the 'new' operator in C# means you're allocating managed memory. Which means, everytime you want to write to it using pointer logic, you have to fix it down. Fixing it down means your forcibly preventing the GC from moving the data to some other location in the memory.

Moral of the story, constantly/repetitively fixing down an array is expensive in contrast to doing it once (or not at all).
10/19/2011 21:36 _DreadNought_#17
Infamous, How do you structure your packets and whats your fav way of doing it?(curious)
10/20/2011 07:09 InfamousNoone#18
I generally stick to the C/C++ way of doing it first demonstrated by bone-you.
i.e.
Code:
typedef packet0x420 struct {
	PACKETHEADER Header;
	DWORD Argument;
} MSG_TRADE;
So in C#, my code would look like,
Code:
struct MsgTrade
{
	public short Size;
	public short Type;
	public int Argument;
}
Code:
var msg = new MsgTrade();
msg.Size = sizeof(MsgTrade);
msg.Type = PacketTypes.Trade;
msg.Argument = ivar;
Client.Send(&msg);
And then for any packets that have non-static sizes (for instance, the synchronization, message, etc packet) I implement an interface that a GetBytes() function, and a FromBytes() function.

ConquerAI however uses a packet builder for everything, this system was already set up when I joined the project so I decided to just follow suite instead of changing it to work to my liking.
10/21/2011 01:14 Korvacs#19
Quote:
Originally Posted by Korvacs View Post
Mine doesn't require the unsafe or fixed declarations in every method, but yeah something like that.
Ironically just dropped this method all together today on my current project lol.