123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429 |
- #pragma once
- /*//-----------------------------------------------------------------------------
- Stream 定义为一个只支持原始的读/写对象, 没有缓冲
- Buffer 定义为一个能缓冲的读/写对象
- *///-----------------------------------------------------------------------------
- #include <memory>
- #include <list>
- #include "TmplBlockBuffer.tlh"
- #include "TmplBlockBuffer.tli"
- #include "ListOfPtr.tlh"
- #include "String.DString.hpp"
- #include "Utility.Either+Error.hpp"
- #ifndef _IOBUFFER_EXPORTS
- #define _IOBuffer_API _declspec(dllimport)
- #else
- #define _IOBuffer_API _declspec(dllexport)
- #endif
- #ifndef _IOBUFFER_EXPORTS
- #ifdef _WIN64
- #ifdef _DEBUG
- #pragma comment (lib, "ECOM.Utility.IOBuffer64D.lib")
- #else
- #pragma comment (lib, "ECOM.Utility.IOBuffer64.lib")
- #endif
- #else // X86
- #ifdef _DEBUG
- #pragma comment (lib, "ECOM.Utility.IOBufferD.lib")
- #else
- #pragma comment (lib, "ECOM.Utility.IOBuffer.lib")
- #endif
- #endif
- #endif // _IOBUFFER_EXPORTS
- #include "BufferItem.hpp"
- const UINT DEFAULT_BREAK_SIZE = 32768;
- namespace ECOM::Utility::IOBuffer
- {
- template <typename> constexpr bool dependent_false = false;
- class InBuffer;
- class OutBuffer;
- //-----------------------------------------------------------------------------
- // IBase
- //-----------------------------------------------------------------------------
- class _IOBuffer_API IBase
- {
- public:
- IBase ();
- virtual ~IBase ();
- public:
- virtual IBase & operator >> (BYTE & x);
- virtual IBase & operator >> (UINT16 & x);
- virtual IBase & operator >> (UINT32 & x);
- virtual IBase & operator >> (unsigned long & x);
- virtual IBase & operator >> (unsigned long long & x);
- IBase & operator >> (char & x) { return (*this) >> (BYTE &) x; };
- IBase & operator >> (INT16 & x) { return (*this) >> (UINT16 &) x; };
- IBase & operator >> (INT32 & x) { return (*this) >> (UINT32 &) x; };
- IBase & operator >> (long & x) { return (*this) >> (unsigned long &) x; };
- IBase & operator >> (long long & x) { return (*this) >> (unsigned long long &) x; };
- virtual int Read (void * Data, int cbNumBytes);
- virtual bool Fill (int _);
- virtual bool Kill (int);
- virtual eiResult Open () { return eiSuccess; }
- virtual void Close () = 0;
- virtual bool IsClosed () const { return false; } // 当流被关闭后, 返回 true
- virtual int GetSize () const = 0;
- int GetBreakSize () const { return DEFAULT_BREAK_SIZE; }
- virtual eSTR::DString ToString () const { return eSTR::DString (); } // 用于日志,跟踪等
- protected:
- // !!! 特别要注意返回值, 否则容易进入死循环, 返回 0 表示还可以继续读 !!!
- // 返回值:
- // > 0 - 本次读到的字节数
- // < 0 - 已经读完了, 不能再读了
- // = 0 - 没读完, 下次继续
- virtual int ReadRaw (void *, int) = 0;
- public:
- // 快速读
- template <typename T> T Read () { static_assert (dependent_false <T>); }
- template <> inline BYTE Read () { BYTE v = 0; operator >> (v); return v; }
- template <> inline UINT16 Read () { UINT16 v = 0; operator >> (v); return v; }
- template <> inline UINT32 Read () { UINT32 v = 0; operator >> (v); return v; }
- template <> inline unsigned long Read () { unsigned long v = 0; operator >> (v); return v; }
- template <> inline unsigned long long Read () { unsigned long long v = 0; operator >> (v); return v; }
- };
- //-----------------------------------------------------------------------------
- // InBuffer
- //-----------------------------------------------------------------------------
- class _IOBuffer_API InBuffer : public IBase
- {
- protected:
- UINT cbRecv; // 总共收到多少个字节? 用于统计
- int ReadSize;
- UINT BreakSize;
- INT InSize;
- // std::list <BufferItem> Incoming;
- ListOfPtr <BufferItem> Incoming;
- public:
- InBuffer ();
- InBuffer (InBuffer && IB);
- virtual ~InBuffer ();
- InBuffer (const InBuffer & IB) = delete;
- InBuffer & operator = (const InBuffer & IB) = delete;
- InBuffer & operator = (InBuffer && IB) = delete;
- public:
- // 快速读
- template <typename T> T Read () { static_assert (dependent_false <T>); }
- template <> inline BYTE Read () { BYTE v = 0; operator >> (v); return v; }
- template <> inline UINT16 Read () { UINT16 v = 0; operator >> (v); return v; }
- template <> inline UINT32 Read () { UINT32 v = 0; operator >> (v); return v; }
- template <> inline unsigned long Read () { unsigned long v = 0; operator >> (v); return v; }
- template <> inline unsigned long long Read () { unsigned long long v = 0; operator >> (v); return v; }
- public:
- UINT GetBreakSize (void) const { return BreakSize; };
- bool SetBreakSize (UINT ToSize) { if (! ToSize) return false; BreakSize = ToSize; return (true); };
- virtual int GetSize (void) const override { return (InSize); }; // 缓冲区里还剩多少字节?
- int GetNbOfRecv () const { return cbRecv; } // 总共收到多少个字节? 用于统计
- virtual void Reset (void);
- virtual bool Kill (int) override;
- virtual bool Fill (int) override;
- virtual void Close () override;
- bool PutBack (UINT8 u8);
- bool PutBack (UINT16 u16);
- bool PutBack (UINT32 u32);
- bool PutBack (const void * Data, UINT Bytes);
- virtual int Read (void * Data, int cbNumBytes) override;
- BYTE Peek ();
- int Peek (void * toData, UINT Bytes);
- // read data from disk, then save it to InBuffer/OutBuffer
- int TransferTo (OutBuffer & OB, int cbNumBytes);
- void TransferTo (IBlockBuffer & IB);
- void AppendFrom (OutBuffer && OB);
- void CopyTo (InBuffer & IB);
- void DumpTo (PCWSTR FileName);
- int GetReadSize (void) const { return ReadSize; }; // 我总共读了多少个字节 ?
- public:
- virtual IBase & operator >> (BYTE & x) override;
- virtual IBase & operator >> (UINT16 & x) override;
- virtual IBase & operator >> (UINT32 & x) override;
- virtual IBase & operator >> (unsigned long & x) override;
- virtual IBase & operator >> (unsigned long long & x) override;
- IBase & operator >> (char & x) { return (*this) >> (BYTE &) x; };
- IBase & operator >> (INT16 & x) { return (*this) >> (UINT16 &) x; };
- IBase & operator >> (INT32 & x) { return (*this) >> (UINT32 &) x; };
- IBase & operator >> (long & x) { return (*this) >> (unsigned long &) x; };
- IBase & operator >> (long long & x) { return (*this) >> (unsigned long long &) x; };
- private:
- template <typename T>
- void _ReadTo (T & x);
- protected:
- auto * GetFirst (void) const
- {
- return Incoming.GetFirst ();
- }
- auto * GetFirst (void)
- {
- return Incoming.GetFirst ();
- }
- void RemoveFirst (void)
- {
- Incoming.RemoveFirst ();
- }
- protected:
- UINT PrepareRead (UINT cbNumByte);
- protected:
- // 把 IB 的所有内容转移到我名下
- void MoveFrom (InBuffer & IB);
- private:
- bool ReadBlock (void);
- private:
- friend class OutBuffer;
- };
- //-----------------------------------------------------------------------------
- // OBase
- //-----------------------------------------------------------------------------
- class _IOBuffer_API OBase
- {
- public:
- OBase ();
- virtual ~OBase ();
- public:
- virtual OBase & operator << (BYTE x);
- virtual OBase & operator << (UINT16 x);
- virtual OBase & operator << (UINT32 x);
- virtual OBase & operator << (unsigned long x);
- virtual OBase & operator << (unsigned long long x);
- OBase & operator << (char x) { return (*this) << (BYTE ) x; };
- OBase & operator << (INT16 x) { return (*this) << (UINT16) x; };
- OBase & operator << (INT32 x) { return (*this) << (UINT32) x; };
- OBase & operator << (long x) { return (*this) << (unsigned long) x; };
- OBase & operator << (long long x) { return (*this) << (unsigned long long) x; };
- virtual int Write (const void * Data, int cbNumBytes);
- // virtual int GetSize () const = 0;
- virtual bool Flush () = 0;
- virtual eiResult Open () { return eiSuccess; }
- virtual void Close () = 0;
- virtual bool IsClosed () const { return false; } // 当流被关闭后, 返回 true
- virtual eSTR::DString ToString () const { return eSTR::DString (); } // 用于日志,跟踪等
- protected:
- virtual int SendRaw (const void *, int) = 0;
- };
- //-----------------------------------------------------------------------------
- // OutBuffer
- //-----------------------------------------------------------------------------
- class _IOBuffer_API OutBuffer : public OBase
- {
- protected:
- UINT cbSend; // 总共送出多少个字节? 用于统计
- UINT BreakSize;
- INT OutSize;
- // std::list <BufferItem> Outgoing;
- ListOfPtr <BufferItem> Outgoing;
- public:
- OutBuffer ();
- OutBuffer (OutBuffer && OB);
- virtual ~OutBuffer ();
- OutBuffer (const OutBuffer & OB) = delete;
- OutBuffer & operator = (const OutBuffer & OB) = delete;
- OutBuffer & operator = (OutBuffer && OB) = delete;
- public:
- UINT GetBreakSize (void) const { return BreakSize; };
- bool SetBreakSize (UINT ToSize) { if (! ToSize) return false; BreakSize = ToSize; return (true); };
- int GetSize (void) const { return (OutSize); };
- int GetNbOfSend () const { return cbSend; } // 总共送出多少个字节? 用于统计
- virtual void Reset (void);
- virtual bool Flush (void) override;
- virtual int Write (const void * Data, int cbNumBytes) override;
- virtual void Close () override;
- void TransferTo (OBlockBuffer & BLOB);
- public:
- void DirectSend (const void * Data, int cbBytes) // same as SendRaw ();
- {
- Flush (); // 一定要先把旧数据发出去, 再发当前数据
- SendRaw (Data, cbBytes);
- }
- protected:
- // 把 OB 的所有内容转移到我名下
- void MoveFrom (OutBuffer && OB);
- public:
- virtual OBase & operator << (BYTE x) override;
- virtual OBase & operator << (UINT16 x) override;
- virtual OBase & operator << (UINT32 x) override;
- virtual OBase & operator << (unsigned long x) override;
- virtual OBase & operator << (unsigned long long x) override;
- OBase & operator << (char x) { return (*this) << (BYTE ) x; };
- OBase & operator << (INT16 x) { return (*this) << (UINT16) x; };
- OBase & operator << (INT32 x) { return (*this) << (UINT32) x; };
- OBase & operator << (long x) { return (*this) << (unsigned long) x; };
- OBase & operator << (long long x) { return (*this) << (unsigned long long) x; };
- protected:
- BufferItem * GetFirst (void) const
- {
- if (Outgoing.IsEmpty ()) return nullptr;
- return Outgoing.GetFirst ();
- }
- BufferItem * GetFirst (void)
- {
- if (Outgoing.IsEmpty ()) return nullptr;
- return Outgoing.GetFirst ();
- }
- void RemoveFirst (void)
- {
- Outgoing.RemoveFirst ();
- }
- private:
- friend InBuffer;
- };
- //-----------------------------------------------------------------------------
- // IMemoryBuffer
- // 一次分配
- //-----------------------------------------------------------------------------
- class _IOBuffer_API IMemoryBuffer : public InBuffer
- {
- typedef InBuffer inherited;
- public:
- IMemoryBuffer ();
- IMemoryBuffer (IMemoryBuffer && IB);
- IMemoryBuffer (int nbAlloc) { Alloc (nbAlloc); }
- virtual ~IMemoryBuffer ();
- IMemoryBuffer (const IMemoryBuffer & IB) = delete;
- IMemoryBuffer & operator = (const IMemoryBuffer & IB) = delete;
- IMemoryBuffer & operator = (IMemoryBuffer && IB) = delete;
- public:
- void BorrowFrom (const void * pBuffer, int BufferSize);
- void * Alloc (int nbAlloc);
- void * GetBufferSetLength (int nbAlloc) { return Alloc (nbAlloc); }
- public:
- virtual void Close () override { }
- protected:
- virtual int ReadRaw (void *, int) override { return -1; };
- };
- //-----------------------------------------------------------------------------
- // OMemoryBuffer
- // 一次分配
- //-----------------------------------------------------------------------------
- class _IOBuffer_API OMemoryBuffer : public OutBuffer
- {
- typedef OutBuffer inherited;
- public:
- OMemoryBuffer ();
- OMemoryBuffer (OMemoryBuffer && OB);
- OMemoryBuffer (int nbAlloc) { Alloc (nbAlloc); }
- virtual ~OMemoryBuffer ();
- OMemoryBuffer (const OMemoryBuffer & OB) = delete;
- OMemoryBuffer & operator = (const OMemoryBuffer & OB) = delete;
- OMemoryBuffer & operator = (OMemoryBuffer && OB) = delete;
- public:
- void BorrowFrom (const void * pBuffer, int BufferSize);
- void * Alloc (int nbAlloc);
- void * GetBufferSetLength (int nbAlloc) { return Alloc (nbAlloc); }
- public:
- virtual void Close () override { }
- protected:
- virtual int SendRaw (const void *, int) override { return -1; };
- };
- }
|