IO.Buffer.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. #pragma once
  2. /*//-----------------------------------------------------------------------------
  3. Stream 定义为一个只支持原始的读/写对象, 没有缓冲
  4. Buffer 定义为一个能缓冲的读/写对象
  5. *///-----------------------------------------------------------------------------
  6. #include <memory>
  7. #include <list>
  8. #include "TmplBlockBuffer.tlh"
  9. #include "TmplBlockBuffer.tli"
  10. #include "ListOfPtr.tlh"
  11. #include "String.DString.hpp"
  12. #include "Utility.Either+Error.hpp"
  13. #ifndef _IOBUFFER_EXPORTS
  14. #define _IOBuffer_API _declspec(dllimport)
  15. #else
  16. #define _IOBuffer_API _declspec(dllexport)
  17. #endif
  18. #ifndef _IOBUFFER_EXPORTS
  19. #ifdef _WIN64
  20. #ifdef _DEBUG
  21. #pragma comment (lib, "ECOM.Utility.IOBuffer64D.lib")
  22. #else
  23. #pragma comment (lib, "ECOM.Utility.IOBuffer64.lib")
  24. #endif
  25. #else // X86
  26. #ifdef _DEBUG
  27. #pragma comment (lib, "ECOM.Utility.IOBufferD.lib")
  28. #else
  29. #pragma comment (lib, "ECOM.Utility.IOBuffer.lib")
  30. #endif
  31. #endif
  32. #endif // _IOBUFFER_EXPORTS
  33. #include "BufferItem.hpp"
  34. const UINT DEFAULT_BREAK_SIZE = 32768;
  35. namespace ECOM::Utility::IOBuffer
  36. {
  37. template <typename> constexpr bool dependent_false = false;
  38. class InBuffer;
  39. class OutBuffer;
  40. //-----------------------------------------------------------------------------
  41. // IBase
  42. //-----------------------------------------------------------------------------
  43. class _IOBuffer_API IBase
  44. {
  45. public:
  46. IBase ();
  47. virtual ~IBase ();
  48. public:
  49. virtual IBase & operator >> (BYTE & x);
  50. virtual IBase & operator >> (UINT16 & x);
  51. virtual IBase & operator >> (UINT32 & x);
  52. virtual IBase & operator >> (unsigned long & x);
  53. virtual IBase & operator >> (unsigned long long & x);
  54. IBase & operator >> (char & x) { return (*this) >> (BYTE &) x; };
  55. IBase & operator >> (INT16 & x) { return (*this) >> (UINT16 &) x; };
  56. IBase & operator >> (INT32 & x) { return (*this) >> (UINT32 &) x; };
  57. IBase & operator >> (long & x) { return (*this) >> (unsigned long &) x; };
  58. IBase & operator >> (long long & x) { return (*this) >> (unsigned long long &) x; };
  59. virtual int Read (void * Data, int cbNumBytes);
  60. virtual bool Fill (int _);
  61. virtual bool Kill (int);
  62. virtual eiResult Open () { return eiSuccess; }
  63. virtual void Close () = 0;
  64. virtual bool IsClosed () const { return false; } // 当流被关闭后, 返回 true
  65. virtual int GetSize () const = 0;
  66. int GetBreakSize () const { return DEFAULT_BREAK_SIZE; }
  67. virtual eSTR::DString ToString () const { return eSTR::DString (); } // 用于日志,跟踪等
  68. protected:
  69. // !!! 特别要注意返回值, 否则容易进入死循环, 返回 0 表示还可以继续读 !!!
  70. // 返回值:
  71. // > 0 - 本次读到的字节数
  72. // < 0 - 已经读完了, 不能再读了
  73. // = 0 - 没读完, 下次继续
  74. virtual int ReadRaw (void *, int) = 0;
  75. public:
  76. // 快速读
  77. template <typename T> T Read () { static_assert (dependent_false <T>); }
  78. template <> inline BYTE Read () { BYTE v = 0; operator >> (v); return v; }
  79. template <> inline UINT16 Read () { UINT16 v = 0; operator >> (v); return v; }
  80. template <> inline UINT32 Read () { UINT32 v = 0; operator >> (v); return v; }
  81. template <> inline unsigned long Read () { unsigned long v = 0; operator >> (v); return v; }
  82. template <> inline unsigned long long Read () { unsigned long long v = 0; operator >> (v); return v; }
  83. };
  84. //-----------------------------------------------------------------------------
  85. // InBuffer
  86. //-----------------------------------------------------------------------------
  87. class _IOBuffer_API InBuffer : public IBase
  88. {
  89. protected:
  90. UINT cbRecv; // 总共收到多少个字节? 用于统计
  91. int ReadSize;
  92. UINT BreakSize;
  93. INT InSize;
  94. // std::list <BufferItem> Incoming;
  95. ListOfPtr <BufferItem> Incoming;
  96. public:
  97. InBuffer ();
  98. InBuffer (InBuffer && IB);
  99. virtual ~InBuffer ();
  100. InBuffer (const InBuffer & IB) = delete;
  101. InBuffer & operator = (const InBuffer & IB) = delete;
  102. InBuffer & operator = (InBuffer && IB) = delete;
  103. public:
  104. // 快速读
  105. template <typename T> T Read () { static_assert (dependent_false <T>); }
  106. template <> inline BYTE Read () { BYTE v = 0; operator >> (v); return v; }
  107. template <> inline UINT16 Read () { UINT16 v = 0; operator >> (v); return v; }
  108. template <> inline UINT32 Read () { UINT32 v = 0; operator >> (v); return v; }
  109. template <> inline unsigned long Read () { unsigned long v = 0; operator >> (v); return v; }
  110. template <> inline unsigned long long Read () { unsigned long long v = 0; operator >> (v); return v; }
  111. public:
  112. UINT GetBreakSize (void) const { return BreakSize; };
  113. bool SetBreakSize (UINT ToSize) { if (! ToSize) return false; BreakSize = ToSize; return (true); };
  114. virtual int GetSize (void) const override { return (InSize); }; // 缓冲区里还剩多少字节?
  115. int GetNbOfRecv () const { return cbRecv; } // 总共收到多少个字节? 用于统计
  116. virtual void Reset (void);
  117. virtual bool Kill (int) override;
  118. virtual bool Fill (int) override;
  119. virtual void Close () override;
  120. bool PutBack (UINT8 u8);
  121. bool PutBack (UINT16 u16);
  122. bool PutBack (UINT32 u32);
  123. bool PutBack (const void * Data, UINT Bytes);
  124. virtual int Read (void * Data, int cbNumBytes) override;
  125. BYTE Peek ();
  126. int Peek (void * toData, UINT Bytes);
  127. // read data from disk, then save it to InBuffer/OutBuffer
  128. int TransferTo (OutBuffer & OB, int cbNumBytes);
  129. void TransferTo (IBlockBuffer & IB);
  130. void AppendFrom (OutBuffer && OB);
  131. void CopyTo (InBuffer & IB);
  132. void DumpTo (PCWSTR FileName);
  133. int GetReadSize (void) const { return ReadSize; }; // 我总共读了多少个字节 ?
  134. public:
  135. virtual IBase & operator >> (BYTE & x) override;
  136. virtual IBase & operator >> (UINT16 & x) override;
  137. virtual IBase & operator >> (UINT32 & x) override;
  138. virtual IBase & operator >> (unsigned long & x) override;
  139. virtual IBase & operator >> (unsigned long long & x) override;
  140. IBase & operator >> (char & x) { return (*this) >> (BYTE &) x; };
  141. IBase & operator >> (INT16 & x) { return (*this) >> (UINT16 &) x; };
  142. IBase & operator >> (INT32 & x) { return (*this) >> (UINT32 &) x; };
  143. IBase & operator >> (long & x) { return (*this) >> (unsigned long &) x; };
  144. IBase & operator >> (long long & x) { return (*this) >> (unsigned long long &) x; };
  145. private:
  146. template <typename T>
  147. void _ReadTo (T & x);
  148. protected:
  149. auto * GetFirst (void) const
  150. {
  151. return Incoming.GetFirst ();
  152. }
  153. auto * GetFirst (void)
  154. {
  155. return Incoming.GetFirst ();
  156. }
  157. void RemoveFirst (void)
  158. {
  159. Incoming.RemoveFirst ();
  160. }
  161. protected:
  162. UINT PrepareRead (UINT cbNumByte);
  163. protected:
  164. // 把 IB 的所有内容转移到我名下
  165. void MoveFrom (InBuffer & IB);
  166. private:
  167. bool ReadBlock (void);
  168. private:
  169. friend class OutBuffer;
  170. };
  171. //-----------------------------------------------------------------------------
  172. // OBase
  173. //-----------------------------------------------------------------------------
  174. class _IOBuffer_API OBase
  175. {
  176. public:
  177. OBase ();
  178. virtual ~OBase ();
  179. public:
  180. virtual OBase & operator << (BYTE x);
  181. virtual OBase & operator << (UINT16 x);
  182. virtual OBase & operator << (UINT32 x);
  183. virtual OBase & operator << (unsigned long x);
  184. virtual OBase & operator << (unsigned long long x);
  185. OBase & operator << (char x) { return (*this) << (BYTE ) x; };
  186. OBase & operator << (INT16 x) { return (*this) << (UINT16) x; };
  187. OBase & operator << (INT32 x) { return (*this) << (UINT32) x; };
  188. OBase & operator << (long x) { return (*this) << (unsigned long) x; };
  189. OBase & operator << (long long x) { return (*this) << (unsigned long long) x; };
  190. virtual int Write (const void * Data, int cbNumBytes);
  191. // virtual int GetSize () const = 0;
  192. virtual bool Flush () = 0;
  193. virtual eiResult Open () { return eiSuccess; }
  194. virtual void Close () = 0;
  195. virtual bool IsClosed () const { return false; } // 当流被关闭后, 返回 true
  196. virtual eSTR::DString ToString () const { return eSTR::DString (); } // 用于日志,跟踪等
  197. protected:
  198. virtual int SendRaw (const void *, int) = 0;
  199. };
  200. //-----------------------------------------------------------------------------
  201. // OutBuffer
  202. //-----------------------------------------------------------------------------
  203. class _IOBuffer_API OutBuffer : public OBase
  204. {
  205. protected:
  206. UINT cbSend; // 总共送出多少个字节? 用于统计
  207. UINT BreakSize;
  208. INT OutSize;
  209. // std::list <BufferItem> Outgoing;
  210. ListOfPtr <BufferItem> Outgoing;
  211. public:
  212. OutBuffer ();
  213. OutBuffer (OutBuffer && OB);
  214. virtual ~OutBuffer ();
  215. OutBuffer (const OutBuffer & OB) = delete;
  216. OutBuffer & operator = (const OutBuffer & OB) = delete;
  217. OutBuffer & operator = (OutBuffer && OB) = delete;
  218. public:
  219. UINT GetBreakSize (void) const { return BreakSize; };
  220. bool SetBreakSize (UINT ToSize) { if (! ToSize) return false; BreakSize = ToSize; return (true); };
  221. int GetSize (void) const { return (OutSize); };
  222. int GetNbOfSend () const { return cbSend; } // 总共送出多少个字节? 用于统计
  223. virtual void Reset (void);
  224. virtual bool Flush (void) override;
  225. virtual int Write (const void * Data, int cbNumBytes) override;
  226. virtual void Close () override;
  227. void TransferTo (OBlockBuffer & BLOB);
  228. public:
  229. void DirectSend (const void * Data, int cbBytes) // same as SendRaw ();
  230. {
  231. Flush (); // 一定要先把旧数据发出去, 再发当前数据
  232. SendRaw (Data, cbBytes);
  233. }
  234. protected:
  235. // 把 OB 的所有内容转移到我名下
  236. void MoveFrom (OutBuffer && OB);
  237. public:
  238. virtual OBase & operator << (BYTE x) override;
  239. virtual OBase & operator << (UINT16 x) override;
  240. virtual OBase & operator << (UINT32 x) override;
  241. virtual OBase & operator << (unsigned long x) override;
  242. virtual OBase & operator << (unsigned long long x) override;
  243. OBase & operator << (char x) { return (*this) << (BYTE ) x; };
  244. OBase & operator << (INT16 x) { return (*this) << (UINT16) x; };
  245. OBase & operator << (INT32 x) { return (*this) << (UINT32) x; };
  246. OBase & operator << (long x) { return (*this) << (unsigned long) x; };
  247. OBase & operator << (long long x) { return (*this) << (unsigned long long) x; };
  248. protected:
  249. BufferItem * GetFirst (void) const
  250. {
  251. if (Outgoing.IsEmpty ()) return nullptr;
  252. return Outgoing.GetFirst ();
  253. }
  254. BufferItem * GetFirst (void)
  255. {
  256. if (Outgoing.IsEmpty ()) return nullptr;
  257. return Outgoing.GetFirst ();
  258. }
  259. void RemoveFirst (void)
  260. {
  261. Outgoing.RemoveFirst ();
  262. }
  263. private:
  264. friend InBuffer;
  265. };
  266. //-----------------------------------------------------------------------------
  267. // IMemoryBuffer
  268. // 一次分配
  269. //-----------------------------------------------------------------------------
  270. class _IOBuffer_API IMemoryBuffer : public InBuffer
  271. {
  272. typedef InBuffer inherited;
  273. public:
  274. IMemoryBuffer ();
  275. IMemoryBuffer (IMemoryBuffer && IB);
  276. IMemoryBuffer (int nbAlloc) { Alloc (nbAlloc); }
  277. virtual ~IMemoryBuffer ();
  278. IMemoryBuffer (const IMemoryBuffer & IB) = delete;
  279. IMemoryBuffer & operator = (const IMemoryBuffer & IB) = delete;
  280. IMemoryBuffer & operator = (IMemoryBuffer && IB) = delete;
  281. public:
  282. void BorrowFrom (const void * pBuffer, int BufferSize);
  283. void * Alloc (int nbAlloc);
  284. void * GetBufferSetLength (int nbAlloc) { return Alloc (nbAlloc); }
  285. public:
  286. virtual void Close () override { }
  287. protected:
  288. virtual int ReadRaw (void *, int) override { return -1; };
  289. };
  290. //-----------------------------------------------------------------------------
  291. // OMemoryBuffer
  292. // 一次分配
  293. //-----------------------------------------------------------------------------
  294. class _IOBuffer_API OMemoryBuffer : public OutBuffer
  295. {
  296. typedef OutBuffer inherited;
  297. public:
  298. OMemoryBuffer ();
  299. OMemoryBuffer (OMemoryBuffer && OB);
  300. OMemoryBuffer (int nbAlloc) { Alloc (nbAlloc); }
  301. virtual ~OMemoryBuffer ();
  302. OMemoryBuffer (const OMemoryBuffer & OB) = delete;
  303. OMemoryBuffer & operator = (const OMemoryBuffer & OB) = delete;
  304. OMemoryBuffer & operator = (OMemoryBuffer && OB) = delete;
  305. public:
  306. void BorrowFrom (const void * pBuffer, int BufferSize);
  307. void * Alloc (int nbAlloc);
  308. void * GetBufferSetLength (int nbAlloc) { return Alloc (nbAlloc); }
  309. public:
  310. virtual void Close () override { }
  311. protected:
  312. virtual int SendRaw (const void *, int) override { return -1; };
  313. };
  314. }