iobuffer.h 864 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #pragma once
  2. #include <atomic>
  3. struct MessageHead
  4. {
  5. int16_t Command;
  6. int16_t Length;
  7. int32_t Serialize(int8_t* Data)
  8. {
  9. *(int16_t*)&Data[0] = Command;
  10. *(int16_t*)&Data[sizeof(int16_t)] = Length;
  11. return Size();
  12. }
  13. void Deserialize(int8_t* Data)
  14. {
  15. Command = *(int16_t*)&Data[0];
  16. Length = *(int16_t*)&Data[2];
  17. }
  18. static int32_t Size()
  19. {
  20. return sizeof(int16_t) * 2;
  21. }
  22. };
  23. class CRef
  24. {
  25. public:
  26. CRef();
  27. void AddRef();
  28. long Release();
  29. virtual void OnFree() = 0;
  30. protected:
  31. std::atomic<long> m_lRef;
  32. };
  33. class CIOBuffer :public CRef
  34. {
  35. public:
  36. enum
  37. {
  38. IO_BUFFER_SIZE = 4096*4
  39. };
  40. enum
  41. {
  42. BASE_SIZE = 4,
  43. ALLOC_ARG = 4
  44. };
  45. int32_t Length;
  46. CIOBuffer* NextBuf;
  47. CIOBuffer* PrevBuf;
  48. int8_t Buffer[IO_BUFFER_SIZE];
  49. virtual void OnFree() override;
  50. CIOBuffer();
  51. virtual ~CIOBuffer();
  52. public:
  53. static CIOBuffer* Alloc();
  54. };