Jump to content

Recommended Posts

Posted

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
    }
}

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

This community uses essential cookies to function properly. Non-essential cookies and third-party services are used only with your consent. Read our Privacy Policy and We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue..