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


  • Posts

    • We've added 5% discounts for bulk purchases of Google accounts for orders of 300 or more, and 10% for orders of 500 or more. The discount is applied automatically when you place your order! The discount is indicated in the product title and description for each category.  
    • 🎄 CHRISTMAS EVENT 🎄   ‼️ Information and details: https://forum.l2harbor.com/threads/rozhdestvenskie-xlopoty-christmas-chores.9430/post-171464
    • METATG.ORG Direct Telegram Service Provider A bonus of +7% on every order! *We add 7% more followers than your ordered amount to proactively cover potential drops and guarantee you an honest result." Telegram Followers - Price per 1000 SUBSCRIBERS Subscribers 3 days - $0.10 ~ 8 RUB Subscribers. Daily Completion: 200,000,000 Subscribers 7 days - $0.17 ~ 13.6 RUB Subscribers. Daily Completion: 200,000,000 Subscribers 14 days - $0.20 ~ 16 RUB Subscribers. Daily Completion: 200,000,000 Subscribers 30 days - $0.30 ~ 24 RUB Subscribers. Daily Completion: 200,000,000 Subscribers 60 days - $0.40 ~ 32 RUB Subscribers, 14-day guarantee. Daily Completion: 200,000,000 Subscribers 90 days (Super Fast) - $0.50 ~ 40 RUB Subscribers, 14-day guarantee. Daily Completion: 200,000,000 Subscribers 120 days (Super Fast) - $0.60 ~ 48 RUB Subscribers, 14-day guarantee. Daily Completion: 200,000,000 Subscribers Lifetime (Super Fast) - $0.70 ~ 56 RUB Lifetime Subscribers. 14-day guarantee. Daily Completion: 200,000,000 Telegram Services - Price per 1000 Post Views - $0.06 ~ 5 RUB Reactions - $0.08 ~ 6.5 RUB Bot Starts - $0.10 ~ 8 RUB Bot Starts with referrals - $0.15 ~ 12 RUB DISCOUNTS and CASHBACK for large volumes Direct Supplier. We work from our own accounts with our own software! High execution speed. Multiple payment methods. We work 24/7! Additional discounts are discussed for volumes starting from $1000 per day. SUPPORT 24/7 - TELEGRAM WEBSITE 24/7 - METATG.ORG
    • Added: a brand-new default dashboard template. You can now add multiple game/login server builds. Full support for running both PTS & L2J servers simultaneously, with switching between them. Payment systems: added OmegaPay and Pally (new PayPal-style API). Account history now stores everything: donations, items delivered to characters, referrals, transfers between game accounts, and coin transfers to another master account. Personal Promo Code System: you can create a promo code and assign it to a user or promoter. When donating, a player can enter this promo code to receive bonus coins, and the promo code owner also receives a bonus — all fully configurable in the admin panel.     Look demo site: demo
  • Topics

×
×
  • Create New...

AdBlock Extension Detected!

Our website is made possible by displaying online advertisements to our members.

Please disable AdBlock browser extension first, to be able to use our community.

I've Disabled AdBlock