[Release] Simple Packet Writer

11/17/2012 00:20 Danial Eugen#16
Quote:
Originally Posted by Fаng View Post
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.
11/17/2012 02:23 Spirited#17
Quote:
Originally Posted by Danial Eugen View Post
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.
Any struct that doesn't contain managed classes can be a pointer. Color is just a struct (containing four bytes for the ARGB color code) with a list of constants and a few methods.