#pragma once /*//----------------------------------------------------------------------------- Stream 定义为一个只支持原始的读/写对象, 没有缓冲 Buffer 定义为一个能缓冲的读/写对象 *///----------------------------------------------------------------------------- #include #include #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 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 T Read () { static_assert (dependent_false ); } 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 Incoming; ListOfPtr 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 T Read () { static_assert (dependent_false ); } 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 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 Outgoing; ListOfPtr 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; }; }; }