iobuffer.h 933 B

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