Packet Info.

01/19/2010 23:05 InfamousNoone#16
Quote:
Originally Posted by Kiyono View Post
So what would be the correct way of doing it since it looks pretty much the same as your example.

//edit I think that I see what the problem is, uint uses 4 bytes making it 22 bytes total while it was stated before that 20 was the max causing the 2 leftover bytes to be dumped into invalid memory.
This means that ushort had to be used cause that uses 2 bytes which would end up at 20, correct?
Here's an example of how it -should- be done, though this adds another layer ontop of what I do seeing I never do the conversion to a byte[]

Code:
/* extra function which I -don't- use, I use the pointer */
byte[] SafeArray(void* lpMemory, int Size)
{
	byte[] Safe = new byte[Size];
#ifdef _MEMCPY_SUPPORTED_
	fixed (byte* lpSafe = Safe)
		memcpy(lpSafe, lpMemory, Size);
#else
	byte* lpbMemory = (byte*)lpMemory;
	for (int i = 0; i < Size; i++)
		Safe[i] = lpbMemory[i];
#endif
        return Safe;
}

// ....

struct SimplePacket
{
	public ushort Size;
	public ushort Type
	public int Value;
}

// ....
SimplePacket simple = new SimplePacket();
simple.Size = (ushort)sizeof(SimplePacket);
simple.Type = 0x666;
simple.Value = 69;
return SafeArray(&simple, simple.Size);
01/20/2010 10:07 Korvacs#17
Quote:
Originally Posted by Kiyono View Post
So what would be the correct way of doing it since it looks pretty much the same as your example.

//edit I think that I see what the problem is, uint uses 4 bytes making it 22 bytes total while it was stated before that 20 was the max causing the 2 leftover bytes to be dumped into invalid memory.
This means that ushort had to be used cause that uses 2 bytes which would end up at 20, correct?
Close, in atual fact nothing from the uint would be put into the byte array, since the byte array is of length 20, only values from 0 -> 19 are valid entries, so if you started at offset 20 you would already be outside the array, however your correct in that it would be dumped into invalid memory.

If we started at Offset 18 then only a ushort or smaller would fit because it would populate byte[18] & byte[19], which would be the last 2 values in the array.
01/20/2010 12:03 ~Yuki~#18
Thanks that helped me understand pointers a bit more.
01/20/2010 18:49 Kiyono#19
Quote:
Originally Posted by InfamousNoone View Post
Here's an example of how it -should- be done, though this adds another layer ontop of what I do seeing I never do the conversion to a byte[]

Code:
/* extra function which I -don't- use, I use the pointer */
byte[] SafeArray(void* lpMemory, int Size)
{
	byte[] Safe = new byte[Size];
#ifdef _MEMCPY_SUPPORTED_
	fixed (byte* lpSafe = Safe)
		memcpy(lpSafe, lpMemory, Size);
#else
	byte* lpbMemory = (byte*)lpMemory;
	for (int i = 0; i < Size; i++)
		Safe[i] = lpbMemory[i];
#endif
        return Safe;
}

// ....

struct SimplePacket
{
	public ushort Size;
	public ushort Type
	public int Value;
}

// ....
SimplePacket simple = new SimplePacket();
simple.Size = (ushort)sizeof(SimplePacket);
simple.Type = 0x666;
simple.Value = 69;
return SafeArray(&simple, simple.Size);
Well thanks for the example.
Quote:
Originally Posted by Korvacs View Post
Close, in atual fact nothing from the uint would be put into the byte array, since the byte array is of length 20, only values from 0 -> 19 are valid entries, so if you started at offset 20 you would already be outside the array, however your correct in that it would be dumped into invalid memory.

If we started at Offset 18 then only a ushort or smaller would fit because it would populate byte[18] & byte[19], which would be the last 2 values in the array.
Well I was close enough but here's another question, your example (previous page) looks really different from Hybrid's example, it there a reason for that or is your example "simplified"?
01/20/2010 19:43 Korvacs#20
Quote:
Originally Posted by Kiyono View Post
Well thanks for the example.

Well I was close enough but here's another question, your example (previous page) looks really different from Hybrid's example, it there a reason for that or is your example "simplified"?
My example is a straight up pointer to a byte array, hybrid places values into a structure, thats the main difference.
01/20/2010 22:31 _tao4229_#21
LOTF does packet building fine - all the packets are allocated correctly, if it was wrong you wouldn't get spontaneous exceptions, your program would most likely crash the first time you built in invalid packet.
You wouldn't get a memory leak, it's an access violation.

LOTF's instability is in it's socket system and MySQL connection handling.