Quote:
Originally Posted by Fаng
Well, I disagree with this method of writing packets. I use structs and pointers to write and handle my packets. If you like this style of writing packets though (I have to admit, it makes the concept a lot easier to understand at first), then there's no problem with that.
Answering your question though, things that can be improved... There's a lot. You could manage the memory instead of using a managed byte array (however, that's too much for something like this). I would just leave it as it is really. You could add a few methods here and there if you'd like. Make better get hash and string methods and such. It's really up to you. Here's a method you can add if you'd like (as an example):
Code:
using System.Drawing;
Code:
/// <summary> This method returns a color from the packet at the specified offset. </summary>
/// <param name="offset">The position at which the reader will read from.</param>
/// <returns>The color read from the packet at the specified offset.</returns>
public Color ReadColor(int offset)
{
// Error check the length:
if (offset + 4 > _array.Length)
throw new IndexOutOfRangeException("Attempted to read out of the bounds of the packet array.");
// Read the value:
fixed (byte* ptr = _array)
return *(Color*)(ptr + offset);
}
Code:
/// <summary> This method writes a color to the packet structure. </summary>
/// <param name="value">The value being written to the packet.</param>
/// <param name="offset">The position where the value will be written to.</param>
public void WriteColor(Color value, int offset)
{
// Error check the length:
if (offset + 4 > _array.Length)
throw new IndexOutOfRangeException("Attempted to write out of the bounds of the packet array.");
// Write the value:
fixed (byte* ptr = _array)
*(Color*)(ptr + offset) = value;
}
|
Well this is very nice. I just like the way the class is written it is very clear, well handled, also i like that style of writing packets.
But can't you use a pointer to non unmanaged types ?! cuz Color is managed System.Drawing.