#pragma once
#include <cstddef>
#include <cstring>
#include <vector>


namespace tinyrpc {
    class TcpBuffer {
    public:
        TcpBuffer(std::size_t size = 128) : m_buffer(size) {

        }

        ~TcpBuffer()  {
            
        }

        void dilatation() {
            resize(m_buffer.size() * 2);
        }

        std::size_t getReadable() const {
            return m_write_index - m_read_index;
        }

        std::size_t getWriteable() const {
            return m_buffer.size() - m_write_index;
        }

        void* getWriteAddress() {
            return m_buffer.data() + m_write_index;
        }

        void* getReadAddress() {
            return m_buffer.data() + m_read_index;
        } 

        void adjustBuffer();

        void readOffset(std::size_t offset) ;
        void writeOffset(std::size_t offset) ;

        void clear();

        int readFromBuffer(void* buf, std::size_t size);
        int writeToBuffer(const void* buf, std::size_t size);

        void resize(std::size_t size);

    private:
        std::size_t m_read_index{0};  
        std::size_t m_write_index{0}; 

        std::vector<char> m_buffer{};
    };

}