Thread: C# BufferSharp

Results 1 to 2 of 2
  1. #1 C# BufferSharp 
    Registered Member General Pony's Avatar
    Join Date
    Aug 2012
    Age
    27
    Posts
    20
    Thanks given
    4
    Thanks received
    2
    Rep Power
    11
    I wrote this up in C# in around 15 minutes or so, it basically serves the purpose of packing my packets into small byte arrays, and shipping them where ever need be, at which point I'll unpack 'em.

    I'm still a novice to C#, I guess I have around two months experience or so, give or take a week. I just wanted a bit of advice on what I could improve, if anything.

    Also, on a side note. I had the idea to increase the byte array size by a power of two when need be, to minimize the amount of times I need to re size byte array. I believe this is called exponential growth rather than the current linear. Anyway, some opinions on whether I should go with this implementation or not would be great!

    Code:
    using System;
    using System.Text;
    namespace BufferSharp
    {
            public class BufferData
            {
                    private byte[] _buffer; //Holds our total amount of bytes.
                    private int _offset; // Holds our current offset in the byte array.
                    public BufferData()
                    {
                            _buffer = new byte[0];
                    }
                    /// <summary>
                    /// Writes A Byte Into The Buffer
                    /// </summary>
                    /// <param name="value">Byte Value To Write Into The Buffer.</param>
                    public void WriteByte(byte value)
                    {
                            byte[] src = BitConverter.GetBytes(value);
                            byte[] tmparray = new byte[_buffer.Length + src.Length];
                            Buffer.BlockCopy(_buffer, 0, tmparray, 0, _buffer.Length);
                            Buffer.BlockCopy(src, 0, tmparray, tmparray.Length - src.Length, src.Length);
                            _buffer = tmparray;
                    }
                    /// <summary>
                    /// Writes A 64 Bit Integer Into The Buffer.
                    /// </summary>
                    /// <param name="value">Long Value To Write Into The Buffer.</param>
                    public void WriteLong(long value)
                    {
                            byte[] src = BitConverter.GetBytes(value);
                            byte[] tmparray = new byte[_buffer.Length + src.Length];
                            Buffer.BlockCopy(_buffer, 0, tmparray, 0, _buffer.Length);
                            Buffer.BlockCopy(src, 0, tmparray, tmparray.Length - src.Length, src.Length);
                            _buffer = tmparray;
                    }
                    /// <summary>
                    /// Writes A 16 Bit Integer Into The Buffer.
                    /// </summary>
                    /// <param name="value">Int Value To Write Into The Buffer.</param>
                    public void WriteInt16(Int16 value)
                    {
                            byte[] src = BitConverter.GetBytes(value);
                            byte[] tmparray = new byte[_buffer.Length + src.Length];
                            Buffer.BlockCopy(_buffer, 0, tmparray, 0, _buffer.Length);
                            Buffer.BlockCopy(src, 0, tmparray, tmparray.Length - src.Length, src.Length);
                            _buffer = tmparray;
                    }
                    /// <summary>
                    /// Writes A String Into The Buffer.
                    /// </summary>
                    /// <param name="value">String Value To Write Into The Buffer.</param>
                    public void WriteString(string value)
                    {
                            byte[] src = ASCIIEncoding.ASCII.GetBytes(value);
                            WriteByte((byte)src.Length);
                            byte[] tmpdata = new byte[_buffer.Length + src.Length];
                            Buffer.BlockCopy(_buffer, 0, tmpdata, 0, _buffer.Length);
                            Buffer.BlockCopy(src, 0, tmpdata, tmpdata.Length - src.Length, src.Length);
                            _buffer = tmpdata;
                    }
                    /// <summary>
                    /// Writes A 32 Bit Integer Into The Buffer.
                    /// </summary>
                    /// <param name="value">Int Value To Write Into The Buffer</param>
                    public void WriteInt32(Int32 value)
                    {
                            byte[] src = BitConverter.GetBytes(value);
                            byte[] tmparray = new byte[_buffer.Length + src.Length];
                            Buffer.BlockCopy(_buffer, 0, tmparray, 0, _buffer.Length);
                            Buffer.BlockCopy(src, 0, tmparray, tmparray.Length - src.Length, src.Length);
                            _buffer = tmparray;
                    }
                    /// <summary>
                    /// Grabs The Buffer For Usage.
                    /// </summary>
                    /// <returns></returns>
                    public byte[] GrabBuffer()
                    {
                            return _buffer;
                    }
                    /// <summary>
                    /// Prepares The Buffer For Reading.
                    /// </summary>
                    /// <param name="buffer">Byte Array To Begin Reading From.</param>
                    public void BeginRead(byte[] buffer)
                    {
                            _buffer = buffer;
                            _offset = 0;
                    }
                    /// <summary>
                    /// Reads An Int From The Buffer.
                    /// </summary>
                    /// <returns>A 16 Bit Int Value.</returns>
                    public Int16 ReadInt16()
                    {
                            byte[] tmpdata = new byte[3];
                            System.Buffer.BlockCopy(_buffer, _offset, tmpdata, 0, 2);
                            _offset = _offset + 2;
                            return BitConverter.ToInt16(tmpdata, 0);
                    }
                    /// <summary>
                    /// Reads An Int From The Buffer.
                    /// </summary>
                    /// <returns>A 32 Bit Int Value</returns>
                    public Int32 ReadInt32()
                    {
                            byte[] tmpdata = new byte[5];
                            System.Buffer.BlockCopy(_buffer, _offset, tmpdata, 0, 4);
                            _offset = _offset + 4;
                            return BitConverter.ToInt32(tmpdata, 0);
                    }
                    /// <summary>
                    /// Reads A Byte From The Buffer.
                    /// </summary>
                    /// <returns>A 8 Bit Byte Value.</returns>
                    public byte ReadByte()
                    {
                            byte[] tmpdata = new byte[2];
                            System.Buffer.BlockCopy(_buffer, _offset, tmpdata, 0, 1);
                            _offset = _offset + 2;
                            return Convert.ToByte(BitConverter.ToInt16(tmpdata, 0));
                    }
                    /// <summary>
                    /// Reads A String From The Buffer.
                    /// </summary>
                    /// <returns>String Read From The Buffer.</returns>
                    public string ReadString()
                    {
                            byte size = ReadByte();
                            byte[] tmpdata = new byte[size + 1];
                            System.Buffer.BlockCopy(_buffer, _offset, tmpdata, 0, size);
                            _offset = _offset + size;
                            return System.Text.ASCIIEncoding.ASCII.GetString(tmpdata);
                    }
                    /// <summary>
                    /// Reads A Long Value From The Buffer
                    /// </summary>
                    /// <returns>A 64 Bit Int Value Read From The Buffer.</returns>
                    public long ReadLong()
                    {
                            byte[] tmpdata = new byte[9];
                            System.Buffer.BlockCopy(_buffer, _offset, tmpdata, 0, 8);
                            _offset = _offset + 8;
                            return BitConverter.ToInt64(tmpdata, 0);
                    }
            }
    }
    Regards,
    General Pony
    Reply With Quote  
     

  2. #2  
    ???

    funkE's Avatar
    Join Date
    Feb 2008
    Posts
    2,612
    Thanks given
    255
    Thanks received
    989
    Rep Power
    1366
    slow because of the use of some buffer class instead of direct access to an array of bytes and use of conversion methods. c# already has this kind of functionality built in and it's fast.

    MemoryStream Class (System.IO)
    BinaryReader Class (System.IO)

    also when documenting code don't capitalize the first letter of every word. if you're going to use the long names for data types (int16 instead of short, etc.) then be consistent. byte = Byte, short = Int16, int = Int32, long = Int64, bool = Boolean and so on.

    let's say you have a short:
    Code:
    public short ReadShort()
    {
    	return buffer[index++] + (buffer[index++] << 8);
    	// return (buffer[index++] << 8) + buffer[index]; // depending on endianness and such.
    }
    .
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •