4
\$\begingroup\$

Is this a good method to Read\Write packets

using System.Text;

namespace namespace
{
    public unsafe class DataPacket
    {
        public DataPacket(byte[] buffer)
        {
            _buffer = new byte[buffer.Length];
            System.Buffer.BlockCopy(buffer, 0, _buffer, 0, buffer.Length);
        }

        public DataPacket(ushort size, ushort type)
        {
            _buffer = new byte[size];
            WriteUInt16(size, 0);
            WriteUInt16(type, 2);
        }

        private readonly byte[] _buffer;
        protected byte[] Buffer
        {
            get
            {
                return _buffer;
            }
        }

        public byte* Ptr
        {
            get
            {
                fixed (byte* ptr = Buffer)
                    return ptr;
            }
        }

        public ushort Size
        {
            get
            {
                return ReadUInt16(0);
            }
            set
            {
                WriteUInt16(value, 0);
            }
        }

        public ushort Type
        {
            get
            {
                return ReadUInt16(2);
            }
            set
            {
                WriteUInt16(value, 2);
            }
        }

        public void WriteSByte(sbyte value, int offset)
        {
            (*(sbyte*)(Ptr + offset)) = value;
        }
        public void WriteInt16(short value, int offset)
        {
            (*(short*)(Ptr + offset)) = value;
        }
        public void WriteInt32(int value, int offset)
        {
            (*(int*)(Ptr + offset)) = value;
        }
        public void WriteInt64(long value, int offset)
        {
            (*(long*)(Ptr + offset)) = value;
        }
        public void WriteByte(byte value, int offset)
        {
            (*(Ptr + offset)) = value;
        }
        public void WriteUInt16(ushort value, int offset)
        {
            (*(ushort*)(Ptr + offset)) = value;
        }
        public void WriteUInt32(uint value, int offset)
        {
            (*(uint*)(Ptr + offset)) = value;
        }
        public void WriteUInt64(ulong value, int offset)
        {
            (*(ulong*)(Ptr + offset)) = value;
        }
        public void WriteStringWithLength(string value, int offset, out int nextoffset)
        {
            WriteByte((byte)(value.Length > 255 ? 255 : value.Length), offset);
            offset++;

            foreach (var c in value)
            {
                WriteByte((byte)c, offset);
                offset++;
            }
            nextoffset = offset;
        }
        public void WriteString(string value, int offset)
        {
            foreach (var c in value)
            {
                WriteByte((byte)c, offset);
                offset++;
            }
        }

        public sbyte ReadSByte(int offset)
        {
            return (*(sbyte*)(Ptr + offset));
        }
        public short ReadInt16(int offset)
        {
            return (*(short*)(Ptr + offset));
        }
        public int ReadInt32(int offset)
        {
            return (*(int*)(Ptr + offset));
        }
        public long ReadInt64(int offset)
        {
            return (*(long*)(Ptr + offset));
        }
        public byte ReadByte(int offset)
        {
            return (*(Ptr + offset));
        }
        public byte[] ReadBytes(int offset, int length)
        {
            var bytes = new byte[length];
            for (var i = offset; i < length; i++)
                bytes[i] = (*(Ptr + i));
            return bytes;
        }
        public ushort ReadUInt16(int offset)
        {
            return (*(ushort*)(Ptr + offset));
        }
        public uint ReadUInt32(int offset)
        {
            return (*(uint*)(Ptr + offset));
        }
        public ulong ReadUInt64(int offset)
        {
            return (*(ulong*)(Ptr + offset));
        }
        public string ReadString(int offset, int length)
        {
            var sb = new StringBuilder();
            for (var i = 0; i < length; i++)
                sb.Append((char)ReadByte(offset + i));
            return sb.ToString().Replace("\0", "").Replace("\r", "");
        }
        public string ReadString(int offset)
        {
            var size = ReadByte(offset);
            offset++;
            return ReadString(offset, size);
        }

        public byte[] ToArray()
        {
            var newbuffer = new byte[Buffer.Length];
            System.Buffer.BlockCopy(Buffer, 0, newbuffer, 0, Buffer.Length);
            return newbuffer;
        }

        public static implicit operator byte[](DataPacket dPacket)
        {
            return dPacket.ToArray();
        }

        public static implicit operator DataPacket(byte[] packet)
        {
            return new DataPacket(packet);
        }
    }
}
\$\endgroup\$

1 Answer 1

4
\$\begingroup\$

At first glance, I can see several problem with this code (why are you even using pointers? what happens with non-ASCII characters in WriteString()?). But my main issue is that BinaryReader and BinaryWriter do something very similar, so you're kind of reinventing the wheel.

\$\endgroup\$
4
  • \$\begingroup\$ I always prefer using pointers its just a prefer :D even i use memory copy instead of Buffer.BlockCopy. Also could you provide me with an edited code with what you expect it to be ? \$\endgroup\$ Commented Sep 19, 2012 at 14:29
  • \$\begingroup\$ Well, I would expect you to use the two classes instead of DataPacket, so there is no code to show. \$\endgroup\$
    – svick
    Commented Sep 19, 2012 at 20:11
  • \$\begingroup\$ BinaryWriter is working with stream, not with buffer \$\endgroup\$ Commented Sep 19, 2012 at 21:31
  • 1
    \$\begingroup\$ Yeah, but you can use MemoryStream, which is just a Stream wrapper around a byte[]. \$\endgroup\$
    – svick
    Commented Sep 20, 2012 at 1:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.