Jump to content

C# Byte Buffer


Recommended Posts

Note sure where to share it, so im sharing it here.

About the code:
Its a byte buffer (it can be used for networking, server-client packet reader/sender)

using System;
using System.Collections.Generic;
using System.Text;

namespace Server
{
    class Buffer : IDisposable
    {
       
        #region "MAIN"

        List<byte> _bufferlist;
        byte[] _readbuffer;
        int _readpos;
        bool _buffupdate = false;

        public Buffer()
        {
            _bufferlist = new List<byte>();
            _readpos = 0;
        }

        public int GetReadPos()
        {
            return _readpos;
        }

        public byte[] ToArray()
        {
            return _bufferlist.ToArray();
        }

        public int Count()
        {
            return _bufferlist.Count;
        }

        public int Lenght()
        {
            return Count() - _readpos;
        }

        public void Clear()
        {
            _bufferlist.Clear();
            _readpos = 0;
        }

        #endregion

        #region"WRITE DATA"
        public void WriteBytes(byte[] input)
        {
            _bufferlist.AddRange(input);
            _buffupdate = true;
        }

        public void WriteByte(byte input)
        {
            _bufferlist.Add(input);
            _buffupdate = true;
        }

        public void WriteInteger(int input)
        {
            _bufferlist.AddRange(BitConverter.GetBytes(input));
            _buffupdate = true;
        }

        public void WriteFloat(float input)
        {
            _bufferlist.AddRange(BitConverter.GetBytes(input));
            _buffupdate = true;
        }

        public void WriteString(string input)
        {
            _bufferlist.AddRange(BitConverter.GetBytes(input.Length));
            _bufferlist.AddRange(Encoding.ASCII.GetBytes(input));
            _buffupdate = true;
        }

        public static byte[] Decrypt(byte[] data)
        {
            byte[] _data = data;
           // _data = Crypto.DecryptData(data, data.Length);
           // _data = Crypto.CryptBody(data, data.Length);
            return _data;
        }
        #endregion

        #region"READ DATA"

        public int ReadInteger(bool peek = true)
        {
            if(_bufferlist.Count > _readpos)
            {
                if(_buffupdate)
                {
                    _readbuffer = _bufferlist.ToArray();
                    _buffupdate = false;
                }

                int value = BitConverter.ToInt32(_readbuffer, _readpos);
                if (peek & _bufferlist.Count > _readpos)
                {
                    _readpos += 4;
                }
                return value;
            }
            else
            {
                throw new Exception("Buffer is past his Limit");
            }
        }

        public float ReadFloat(bool peek = true)
        {
            if (_bufferlist.Count > _readpos)
            {
                if (_buffupdate)
                {
                    _readbuffer = _bufferlist.ToArray();
                    _buffupdate = false;
                }

                float value = BitConverter.ToSingle(_readbuffer, _readpos);
                if (peek & _bufferlist.Count > _readpos)
                {
                    _readpos += 4;
                }
                return value;
            }
            else
            {
                throw new Exception("Buffer is past his Limit");
            }
        }

        public byte ReadByte(bool peek = true)
        {
            if (_bufferlist.Count > _readpos)
            {
                if (_buffupdate)
                {
                    _readbuffer = _bufferlist.ToArray();
                    _buffupdate = false;
                }

                byte value = _readbuffer[_readpos];
                if (peek & _bufferlist.Count > _readpos)
                {
                    _readpos += 1;
                }
                return value;
            }
            else
            {
                throw new Exception("Buffer is past his Limit");
            }
        }

        public byte[] ReadBytes(int lenght, bool peek = true)
        {
                if (_buffupdate)
                {
                    _readbuffer = _bufferlist.ToArray();
                    _buffupdate = false;
                }

            byte[] value = _bufferlist.GetRange(_readpos, lenght).ToArray();
                if (peek & _bufferlist.Count > _readpos)
                {
                    _readpos += lenght;
                }
                return value;   
        }

        public string ReadString(bool peek = true)
        {
            int length = ReadInteger(true);
            if (_buffupdate)
                {
                    _readbuffer = _bufferlist.ToArray();
                    _buffupdate = false;
                }

            string value = Encoding.ASCII.GetString(_readbuffer, _readpos, length);
                if (peek & _bufferlist.Count > _readpos)
                {
                    _readpos += length;
                }
                return value;
            }

        public static byte[] Encrypt(byte[] data)
        {
            byte[] _data = data;
          //  _data = Crypto.EncryptData(_data, _data.Length, 5);
            return _data;
        }
        #endregion

        #region"IDisposable"

        private bool disposedValue = false;

        protected virtual void Dispose(bool disposing)
        {
            if(!disposedValue)
            {
                if(disposing)
                {
                    _bufferlist.Clear();
                }
                _readpos = 0;
            }
            disposedValue = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        #endregion
    }
}

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...