C++ 在库中使用std向量作为存储的缓冲区以及在另一个库中使用此缓冲区的困难

C++ 在库中使用std向量作为存储的缓冲区以及在另一个库中使用此缓冲区的困难,c++,visual-c++,vector,stdvector,C++,Visual C++,Vector,Stdvector,我有一个名为IBuffer的类,它使用std::vector作为无符号字符数据的存储。 这个类是我的基础库CUR.LIB的一部分。 此类的职责是将数据写入缓冲区并从中读取。 好啊 现在,我在另一个库中使用这个类来序列化和反序列化从网络接收的消息 当我试图将新数据推回std::vector时,我总是遇到内存访问冲突读取位置0Xxxxxxx的问题。 因为这里很难写上千行代码,所以我把堆栈跟踪放在这里,希望有人能帮我解决这个问题 ntdll.dll!@RtlpLowFragHeapFr

我有一个名为IBuffer的类,它使用std::vector作为无符号字符数据的存储。 这个类是我的基础库CUR.LIB的一部分。 此类的职责是将数据写入缓冲区并从中读取。 好啊 现在,我在另一个库中使用这个类来序列化和反序列化从网络接收的消息

当我试图将新数据推回std::vector时,我总是遇到内存访问冲突读取位置0Xxxxxxx的问题。 因为这里很难写上千行代码,所以我把堆栈跟踪放在这里,希望有人能帮我解决这个问题

        ntdll.dll!@RtlpLowFragHeapFree@8()  + 0x2c bytes    
    ntdll.dll!_RtlFreeHeap@12()  + 0x7e bytes   
    kernel32.dll!_HeapFree@12()  + 0x14 bytes   
    msvcr100d.dll!__free_base()  + 0x29 bytes   
    msvcr100d.dll!__free_dbg_nolock()  + 0x4ae bytes    
    msvcr100d.dll!__free_dbg()  + 0x50 bytes    
    msvcr100d.dll!operator delete()  + 0xb9 bytes   
>   ICore.dll!std::allocator<unsigned char>::deallocate(unsigned char * _Ptr=0x002c7810, unsigned int __formal=1)  Line 182 + 0x9 bytes C++
    ICore.dll!std::vector<unsigned char,std::allocator<unsigned char> >::_Insert_n(std::_Vector_const_iterator<std::_Vector_val<unsigned char,std::allocator<unsigned char> > > _Where=221 'Ý', unsigned int _Count=2, const unsigned char & _Val='')  Line 1375    C++
    ICore.dll!std::vector<unsigned char,std::allocator<unsigned char> >::insert(std::_Vector_const_iterator<std::_Vector_val<unsigned char,std::allocator<unsigned char> > > _Where=221 'Ý', unsigned int _Count=1, const unsigned char & _Val='')  Line 1060   C++
    ICore.dll!Utils::IBuffer::WriteShort(unsigned short data=256)  Line 63 + 0x56 bytes C++
    ICore.dll!Serialization::IBufferWriter::writeUShort(unsigned short data=256)  Line 82   C++
    IRNet.dll!IRNetwork::IStunMessage::AcceptWriter(Serialization::IIWriter * writer=0x0040f080)  Line 340 + 0x2e bytes C++
    IRNet.dll!Serialization::IBufferWriter::WriteObject<IRNetwork::IStunMessage>(const Object::IObject * object=0x002cb5f0)  Line 105 + 0x17 bytes  C++
    IRNet.dll!IRNetwork::IStunMessage::Serialize(Object::SharedPtr<Utils::IBuffer> buffer={...})  Line 769  C++
    IRNet.dll!IRNetwork::IStun::SendUnReliably(IRNetwork::IStunMessage * message=0x002cb5f0)  Line 157  C++
    IRNet.dll!IRNetwork::IStun::SendBind()  Line 61 + 0xc bytes C++
    IRNet.dll!IRNetwork::IStun::Bind(unsigned int localfd=184)  Line 48 C++
    IRNet.dll!IRNetwork::INatContext::StunBind(unsigned int fd=184)  Line 52 + 0xc bytes    C++
    testDhcpv4.exe!TestStunAttribute::Start()  Line 48 + 0x24 bytes C++
    testDhcpv4.exe!wmain(int argc=1, wchar_t * * argv=0x002c71d8)  Line 69  C++
    testDhcpv4.exe!__tmainCRTStartup()  Line 552 + 0x19 bytes   C
    testDhcpv4.exe!wmainCRTStartup()  Line 371  C
    kernel32.dll!@BaseThreadInitThunk@12()  + 0x12 bytes    
    ntdll.dll!___RtlUserThreadStart@8()  + 0x27 bytes   
    ntdll.dll!__RtlUserThreadStart@8()  + 0x1b bytes    
ntdll.dll@RtlpLowFragHeapFree@8()+0x2c字节
ntdll.dll_RtlFreeHeap@12()+0x7e字节
内核32.dll_HeapFree@12()+0x14字节
msvcr100d.dll__空闲_base()+0x29字节
msvcr100d.dll__free_dbg_nolock()+0x4ae字节
msvcr100d.dll__空闲_dbg()+0x50字节
msvcr100d.dll!运算符delete()+0xb9字节
>ICore.dll!STD::分配器::DealPoint(未签名的char *ptR=0x02c7810,未签名int×y正规=1)行182 +0x9字节C++
ICore.dll!STD::vector::插入插件(STD::x向量,const迭代器,其中=221′′,无符号int y计数=2,const unchar char和yVal=‘’’)行1375 c++
ICore.dll!STD::vector:插入(STD:OxVector,SconixIdector,其中=221′′,无符号int y计数=1,const unchar char和yVal=‘’’)行1060 c++
ICore.dll!IBuffer::WriteShort(无符号短数据=256)行63 +0x56字节C++
ICore.dll!序列化::IbffeReWrr::Read EuthAuthor(无符号短数据=256)82行C++
IRNet.dll!IRNetwork::ISTENMIST::接受机(序列化:IIOrth**作者= 0x000 40F080)340 +0x2e字节C++
IRNet.dll!序列化::BuffffReWr::WreObject(const object:iObjultObject=0x02CB5F0)行105 +0x17字节C++
IRNet.dll!IRNetwork::ISunNeMe::Serialize(对象:SyrdpTrpRead={…})769 C++
IRNet.dll!IRNetwork::ISunun::SunDeal可靠(IrNoto:ItSunMeult*Mease= 0x02CB5F0)第157行C++
IRNet.dll!IRNetwork::ISTUN::SeNbDud()行61 +0xC字节C++
IRNet.dll!IRNetwork::ISUNN::Bind(未签名int Loalfd=184)48行C++
IRNet.dll!StunBind:(IRNetwork)(无符号INTFD=184)行52 +0xC字节C++
testDhcpv4.exe!TestStistAt::Stand()行48 +0x24字节C++
testDhcpv4.exe!WMIN(int ARCC=1,WCHARGET***ARGV= 0x02C71D8)69行C++
testDhcpv4.exe__tmainCRTStartup()行552+0x19字节C
testDhcpv4.exe!wmainCRTStartup()行371 C
内核32.dll@BaseThreadInitThunk@12()+0x12字节
ntdll.dll___RtlUserThreadStart@8()+0x27字节
ntdll.dll__RtlUserThreadStart@8()+0x1b字节
IBUffer.h

#include "stdafx.h"
#include "ICoreConfig.h"
#include "ILMutex.hxx"
#include "ILock.hxx"
#include <vector>
using namespace IThreading;
using namespace std;
namespace Utils
{
/*
#ifdef EXP_STL
#    define DECLSPECIFIER __declspec(dllexport)
#    define EXPIMP_TEMPLATE
#else
#    define DECLSPECIFIER __declspec(dllimport)
#    define EXPIMP_TEMPLATE extern
#endif

    EXPIMP_TEMPLATE template class DECLSPECIFIER std::vector<unsigned char>;*/

    template class ICORE_API std::allocator<unsigned char>;
    template class ICORE_API std::vector<unsigned char,std::allocator<unsigned char> >;
    /// <summary>
    /// 
    /// </summary>
    typedef enum 
    {
        /*Begin Of Buffer*/
        BOB = 0,
        /*End Of Buffer*/
        EOB = 1,
        /*Unknown*/
        UNK = 2
    } BufferState_t;
    class ICORE_API IBuffer
    {
    public:
        IBuffer(void);
        IBuffer(IBuffer* newbuff);
        ~IBuffer(void);
        //Methods
        int Write(const void* data,size_t size);
        int Write(const void** data, size_t sz);
        int Write(size_t position , const void* data,size_t sz);
        int WriteShort(unsigned short data);
        int WriteShort(uint32_t position,unsigned short data);
        int WriteByte(unsigned char data);
        int WriteUInt(unsigned int data);
        int WriteUlong(unsigned long data);



        //************************************
        // Method:    ReadByte
        // FullName:  BufferPool::ReadByte
        // Access:    public 
        // Returns:   unsigned char
        // Qualifier:
        //Description: Read and return one byte of data by starting from begining of buffer
        //************************************
        unsigned char ReadByte();
        unsigned short ReadShort();
        unsigned int ReadUint32();
        unsigned long ReadUlong();
        unsigned char* Read(size_t sz);

        /** Converts a 16-bit value from host to TCP/IP network byte order (big-endian).
        * @param x The 16-bit (in host byte order) value to convert.
        * @retval @a x in TCP/IP network byte order.
        */
        unsigned short HostToNetworkByteOrder(unsigned short x);

        /* Memory alignment hack */
        //************************************
        // Method:    HostToNetworkByteOrder
        // FullName:  BufferPool::HostToNetworkByteOrder
        // Access:    public 
        // Returns:   unsigned short
        // Qualifier:
        // Parameter: const void * px
        // Description: This method used when we want to Pars existing buffer
        //************************************
        unsigned short HostToNetworkByteOrder(const void* px);

        unsigned short NetworkToHostByteOrder(const unsigned short x);

        /** Converts a 32-bit value from host to TCP/IP network byte order (big-endian).
        * @param x The 32-bit (in host byte order) value to convert.
        * @retval @a x in TCP/IP network byte order.
        */
        unsigned long HostToNetworkByteOrder(unsigned long x);

        /* Memory alignment hack */
        //************************************
        // Method:    HostToNetworkByteOrderUL
        // FullName:  BufferPool::HostToNetworkByteOrderUL
        // Access:    public 
        // Returns:   unsigned long
        // Qualifier:
        // Parameter: const void * px
        // Description: this method used when we want to pars existing buffer
        //************************************
        unsigned long HostToNetworkByteOrderUL(const void* px);
        //* parse unsigned int type to byte array as is
        vector<unsigned char> Uint32ToByteArray(unsigned int x);

        uint8_t* ToUInt8ptr();

        unsigned long NetworkToHostByteOrder(const unsigned long x);


/*      IBuffer operator =(const IBuffer &buf);*/
        IBuffer& operator =(IBuffer &Rhs);
        IBuffer& operator =(char* rhs);

        operator char*();
        operator void*();
        operator uint8_t*();

        //************************************
        // Method:    operator++
        // FullName:  BufferPool::operator++
        // Access:    public 
        // Returns:   BufferPool
        // Qualifier:
        // Description: This method increment dataPtr
        //              read one byte , increment dataPtr by one
        //              read short , increment dataPtr by two
        //              read unsigned int increment dataPtr by 4
        //              note : each read function set currPosion in suitable way
        //              we do this because of continuous buffer reading
        //************************************
        void operator ++();
        bool operator<(const IBuffer& buff)
        {
            return this->Size() < buff.Size() ? true : false;
        }

        bool operator==(const IBuffer& buff)
        {
            return this->buffer == buff.buffer;
        }
        //Properties
        bool IsEmpty();
        unsigned char* Value();

        /** Indicates whether we are on a Big Endian host or not.<br>
        * IMPORTANT: Before calling this function, you should initialize the network stack by using
        * @ref tnet_startup().
        * @retval @a true if the program is running on a Big Endian host and @a false otherwise.
        */
        bool IsBigEndian();
        inline unsigned char HI_Uint16(unsigned short x)
        {
            return (((x) >> 8) & 0xFF) ;
        }
        inline unsigned char LOW_Uint16(unsigned short x)
        {
            return ((x) & 0xFF);
        }
        size_t Size() const;
        void SetPtr();

        void CleanUp();

        void Move(int32_t count);
    private:
        inline int GetPosition()
        {
            return currPosition;
        }

        int SetPosition(int newpos);
    public:
        static class _Convert
        {
        public:
            static inline unsigned short ToUShort(const unsigned char* u8_ptr)
            {
                return (((unsigned short)(u8_ptr)[0]) | ((unsigned short)(u8_ptr)[1])<<8);
            }
            static inline unsigned int ToUint32(const unsigned char* u8_ptr)
            {
                return (((unsigned int)(u8_ptr)[3]) | ((unsigned int)(u8_ptr)[2])<<8 | ((unsigned int)(u8_ptr)[1])<<16 | ((unsigned int)(u8_ptr)[0])<<24);
            }

            static inline signed int ToInt32(const unsigned char* u8_ptr)
            {
                return (((signed int)(u8_ptr)[0]) | ((signed int)(u8_ptr)[1])<<8 | ((signed int)(u8_ptr)[2])<<16 | ((signed int)(u8_ptr)[3])<<24);
            }
        protected:

        private:
        } Convert;
        static BufferState_t bstate;
        Mutex mMutex;
    private:        
        vector<unsigned char> buffer;
        vector<unsigned char>::pointer dataPtr;
        int currPosition;
        bool ptrSet;
    };
}
#包括“stdafx.h”
#包括“图h”
#包括“ILMutex.hxx”
#包括“ILock.hxx”
#包括
使用名称空间进行阅读;
使用名称空间std;
命名空间Utils
{
/*
#ifdef EXP\u STL
#定义DECLSPECIFIER\uuu declspec(dllexport)
#定义EXPIMP_模板
#否则
#定义DECLSPECIFIER\uuu declspec(dllimport)
#定义EXPIMP_模板外部
#恩迪夫
EXPIMP_模板模板类DECLSPECIFIER std::vector*/
模板类ICORE_API std::分配器;
模板类ICORE_API std::vector;
/// 
/// 
/// 
类型定义枚举
{
/*缓冲区开始*/
BOB=0,
/*缓冲区结束*/
EOB=1,
/*不为人知*/
UNK=2
}缓冲状态;
ICORE_API IBuffer类
{
公众:
IBuffer(无效);
IBuffer(IBuffer*newbuff);
~IBuffer(无效);
//方法
int Write(const void*数据,size\u t size);
int Write(const void**data,size_t sz);
int Write(大小\u t位置、常数void*数据、大小sz);
int WriteShort(无符号短数据);
int WriteShort(uint32_t位置,无符号短数据);
int WriteByte(无符号字符数据);
int WriteUInt(无符号int数据);
int writelong(无符号长数据);
//************************************
//方法:ReadByte
//全名:缓冲池::ReadByte
//访问:公共
//返回:无符号字符
//限定符:
//描述:从缓冲区开始读取并返回一个字节的数据
//************************************
无符号字符ReadByte();
无符号short ReadShort();
无符号int ReadUint32();
无符号长ReadUlong();
无符号字符*读取(大小_t sz);
/**将16位值从主机转换为TCP/IP网络字节顺序(大端)。
*@param x要转换的16位(主机字节顺序)值。
*@retval@a以TCP/IP网络字节顺序排列的x。
*/
无符号短主机网络字节顺序(无符号短x);
/*内存对齐黑客*/
//************************************
//方法:HostToNetworkByteOrder
//全名:BufferPool::HostToNetworkByteOrder
//访问:公共
//返回:无符号短
//限定符:
//参数:const void*px
//描述:当我们想要PAR现有缓冲区时使用此方法
//************************************
无符号短HostToNetworkByteOrder(const void*px);
无符号短网络ToHostByteOrder(常量无符号短x);
/**将32位值从主机转换为TCP/IP网络字节顺序(big-endian)。
*@param x要转换的32位(主机字节顺序)值。
*@retval@a以TCP/IP网络字节顺序排列的x。
*/
无符号长主机网络字节顺序(无符号长x);
/*内存对齐黑客*/
//************************************
//方法:HostToNetworkByteOrderUL
//全名:缓冲池::主机
    #include "StdAfx.h"
#include "IBufferPool.h"
#include <iostream>
namespace Utils
{

    IBuffer::IBuffer(void) 
        : currPosition(0), ptrSet(false) 
    {
        /*buffer.push_back('\0');*/
        SetPtr();
    }

    IBuffer::IBuffer( IBuffer* newbuff )
        :currPosition(0)
    {
    }

    IBuffer::~IBuffer(void)
    {
        if (GetPosition() != 0)
        {
            dataPtr = buffer.data();
        }

        buffer.clear();
        dataPtr = NULL;
        SetPosition(0);
        /*std::cout<<"Ibuffer Freed \n";*/
    }

    //************************************
    // Method:    WriteByte
    // FullName:  BufferPool::WriteByte
    // Access:    public 
    // Returns:   int
    // Qualifier:
    // Parameter: unsigned char data
    // Description: write data of type byte in to buffer
    //************************************
    int IBuffer::WriteByte(unsigned char data)
    {
        Lock lock(mMutex);
        buffer.push_back(data);
        SetPtr();
        return 0;
    }

    //tested OK
    int IBuffer::WriteShort(unsigned short data)
    {
        Lock lock(mMutex);
        unsigned char temp;
        //first we convert to network byte order
        /*unsigned short tmpData = HostToNetworkByteOrder(data);*/
        //lower byte

        temp = LOW_Uint16(/*tmpData*/data);
        //buffer.push_back(temp);          /// Access violation
        buffer.insert(buffer.end(),1,temp);
        temp = HI_Uint16(/*tmpData*/data);
        //buffer.push_back(temp);          /// Access violation
        buffer.insert(buffer.end(),1,temp);
        SetPtr();
        return 0;
    }

    int IBuffer::WriteShort( uint32_t position,unsigned short data )
    {
        Lock lock(mMutex);
        vector<unsigned char>::pointer pt = buffer.data();
        pt+=position;

        *pt=LOW_Uint16(data);
        pt++;
        *pt=HI_Uint16(data);
        SetPtr();
        return 0;
    }

    int IBuffer::WriteUInt(unsigned int data)
    {
        Lock lock(mMutex);
        //unsigned int and unsigned long both have 4 byte length except that unsigned long can accept floating point numbers

        vector<unsigned char> vt = Uint32ToByteArray(data);

        for each(const unsigned char &i in vt)
        {
            buffer.push_back(i);
        }
        SetPtr();
        return 0;
    }

    int IBuffer::WriteUlong( unsigned long data )
    {
        Lock lock(mMutex);
        return WriteUInt(data);
    }

    vector<unsigned char> IBuffer::Uint32ToByteArray(unsigned int x)
    {
        Lock lock(mMutex);
        vector<unsigned char> arrayOfByte(4);
        for (int i = 0; i < 4; i++)
            arrayOfByte[3 - i] = (x >> (i * 8));
        return arrayOfByte;
    }

    uint8_t* IBuffer::ToUInt8ptr()
    {
        Lock lock(mMutex);
        uint8_t* u8arr = (uint8_t*)this->buffer.data();
        size_t sz = this->buffer.size();
        u8arr[sz] = '\0';
        return u8arr;
    }

    bool IBuffer::IsEmpty()
    {
        if(!buffer.size())
            return true;
        return false;
    }


    unsigned char* IBuffer::Value()
    {
        if(!dataPtr)
        {
            dataPtr = buffer.data();
        }
        return dataPtr;
    }


    unsigned short IBuffer::HostToNetworkByteOrder(unsigned short x)
    {
        Lock lock(mMutex);
        if(IsBigEndian()){
            return x;
        }
        else{
            return ((((unsigned short)(x) & 0xff00) >> 8)       |
                (((unsigned short)(x) & 0x00ff) << 8));
        }
    }


    unsigned short IBuffer::HostToNetworkByteOrder(const void* px)
    {
        Lock lock(mMutex);
        unsigned short y = IBuffer::Convert.ToUShort((const unsigned char*)px);//IRV_TO_UINT16((const unsigned char*)px);
        return HostToNetworkByteOrder(y);
    }


    unsigned long IBuffer::HostToNetworkByteOrder(unsigned long x)
    {
        Lock lock(mMutex);
        if(IsBigEndian()){
            return x;
        }
        else{
            return ((((unsigned int)(x) & 0xff000000) >> 24)    | \
                (((unsigned int)(x) & 0x00ff0000) >> 8)     | \
                (((unsigned int)(x) & 0x0000ff00) << 8)     | \
                (((unsigned int)(x) & 0x000000ff) << 24));
        }
    }


    unsigned long IBuffer::HostToNetworkByteOrderUL(const void* px)
    {
        Lock lock(mMutex);
        unsigned long y = IBuffer::Convert.ToUShort((const unsigned char*)px);
        return HostToNetworkByteOrder(y);
    }

    unsigned long IBuffer::NetworkToHostByteOrder(const unsigned long x)
    {
        Lock lock(mMutex);
        if(IsBigEndian()){
            return x;
        }
        else{
            return ((((unsigned int)(x) & 0x000000ff) << 24)    | \
                (((unsigned int)(x) & 0x0000ff00) << 8) | \
                (((unsigned int)(x) & 0x00ff0000) >> 8)         | \
                (((unsigned int)(x) & 0xff000000) >> 24));
        }
    }

    unsigned short IBuffer::NetworkToHostByteOrder( const unsigned short x )
    {
        Lock lock(mMutex);
        if(IsBigEndian()){
            return x;
        }
        else{
            return ((((unsigned short)(x) & 0x00ff) << 8)       |
                (((unsigned short)(x) & 0xff00) >> 8));
        }
    }

    bool IBuffer::IsBigEndian(){
        /* If LITTLE_ENDIAN or BIG_ENDIAN macros have been defined in config.h ==> use them
        * otherwise ==> dyn retrieve the endianness
        */
        short word = 0x4321;
#if LITTLE_ENDIAN
        return false;
#elif BIG_ENDIAN
        return true;
#else
        return ((*(signed char *)&word) != 0x21);
#endif
    }

/*
    IBuffer IBuffer::operator=( const IBuffer &buf )
    {
        IBuffer bf;
        bf.buffer = buf.buffer;
        return bf;
    }*/

    IBuffer& IBuffer::operator=(IBuffer& Rhs)
    {
        if (this != &Rhs)
        {
            if (this->dataPtr && this->Size()>0 && this->GetPosition()>0)
            {
                delete this;
            }
            buffer=Rhs.buffer;
            dataPtr = Rhs.dataPtr;
            currPosition = Rhs.GetPosition();
        }
        return *this;

    }

    IBuffer& IBuffer::operator=( char* rhs )
    {
        Lock lock(mMutex);
        if (this->buffer.size() != 0)
        {
            this->buffer.clear();
        }
        if (this->currPosition !=0)
        {
            this->currPosition = 0;
        }
        this->Write(rhs,strlen(rhs));
        return * this;
    }

    //Move forward data pointer by one
    void IBuffer::operator++()
    {
        dataPtr++;
        SetPosition(1);
        //dataPtr+=GetPosition();
    }

    //************************************
    // Method:    ReadByte
    // FullName:  BufferPool::ReadByte
    // Access:    public 
    // Returns:   unsigned char
    // Qualifier:
    // Parameter: 
    // Description: read data of type byte and move forward data pointer
    //************************************
    unsigned char IBuffer::ReadByte()
    {
        Lock lock(mMutex);
        unsigned char ret = (*(dataPtr));
        dataPtr++;
        SetPosition(1);
        return ret;
    }

    unsigned short IBuffer::ReadShort()
    {
        Lock lock(mMutex);
        unsigned short ret=/*HostToNetworkByteOrder((unsigned char*)(dataPtr))*/IBuffer::Convert.ToUShort((unsigned char*)(dataPtr));
        dataPtr+=2;
        SetPosition(2);
        return ret;
    }

    unsigned int IBuffer::ReadUint32()
    {
        Lock lock(mMutex);
        //I DON NOT KHNOW MUST CONVERT RECIEVED MESSAGE TO LITTLE ENDIAN OR NOT?
        unsigned int ui=/*HostToNetworkByteOrderUL((unsigned char*)(dataPtr))*/Convert.ToUint32((unsigned char*)(dataPtr));
        dataPtr+=4;
        SetPosition(4);
        return ui;
    }

    unsigned long IBuffer::ReadUlong()
    {
        Lock lock(mMutex);
        return (unsigned long) ReadUint32();
    }

    unsigned char* IBuffer::Read(size_t sz)
    {
        Lock lock(mMutex);
        //return a block of data
        unsigned char* tmp = new unsigned char;
        /*Lock lock(mMutex);*/
        for (int i = 1;i<=sz;i++)
        {
            tmp[i-1] = *dataPtr++;
        }
        //dataPtr +=sz;
        tmp[sz] = '\0';
        SetPosition(sz);
        return tmp;
    }

    IBuffer::operator char*()
    {
        Lock lock(mMutex);
        char* ret = (char*)buffer.data();
        ret[buffer.size()] = '\0';
        return ret;
    }

    IBuffer::operator void*()
    {
        return (void*)buffer.data();
    }

    int IBuffer::Write(const void* data,size_t size )
    {
        Lock lock(mMutex);
        unsigned char* udata = (unsigned char*)data;
        /*Lock lock(mMutex);*/
        for (size_t i=0;i<size;i++)
        {
            buffer.push_back(*udata++);
        }
        SetPtr();
        return 0;
    }

    int IBuffer::Write( const void** data, size_t sz )
    {
        Lock lock(mMutex);
        unsigned char* udata = (unsigned char*)data;
        /*Lock lock(mMutex);*/
        for (size_t i=0;i<sz;i++)
        {
            buffer.push_back(*udata++);
        }
        SetPtr();
        return 0;
    }

    int IBuffer::Write( size_t position , const void* data,size_t sz )
    {
        Lock lock(mMutex);
        unsigned char* udata = (unsigned char*)data;
        vector<unsigned char>::pointer pt = buffer.data();
        /*Lock lock(mMutex);*/
        pt+=position;
        for (size_t i=0;i<sz;i++)
        {
            *pt=(unsigned char)(*udata++);
            pt++;
        }
        SetPtr();
        return 0;
    }

    IBuffer::operator uint8_t*()
    {
        return (uint8_t*) buffer.data();
    }

    void IBuffer::CleanUp()
    {
        if (this->Size() > 0 && this->dataPtr)
        {
            this->buffer.clear();
            this->SetPosition(0);
            this->dataPtr = NULL;
        }
    }

    void IBuffer::SetPtr()
    {
        if (!IsEmpty() /*&& !ptrSet*/)
        {
            dataPtr=buffer.data();
            ptrSet=true;
            bstate = BOB;
        }       
    }

    size_t IBuffer::Size() const
    {
        return buffer.size();
    }

    int IBuffer::SetPosition( int newpos )
    {
        //if before read function take placed then current position must be updated to new position
        //our CURSOR move forward by newpos
        if (currPosition == 0)
        {
            currPosition = newpos;
        } 
        else
        {
            currPosition += newpos;
            if (this->Size() == currPosition)
            {
                bstate = EOB;
            }
        }

        return 0;
    }

    void IBuffer::Move( int32_t count )
    {
        SetPosition(count);
        dataPtr+=count;
    }

    BufferState_t IBuffer::bstate(BOB);
IBuffer::operator char*()
{
    Lock lock(mMutex);
    char* ret = (char*)buffer.data();
    ret[buffer.size()] = '\0';
    return ret;
}