buffer_header.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // Created by #Suyghur, on 4/7/21.
  3. //
  4. #include "buffer_header.h"
  5. dolin_common::BufferHeader::BufferHeader(void *data, size_t size) : data_ptr((char *) data), data_size(size) {}
  6. dolin_common::BufferHeader::~BufferHeader() = default;
  7. void dolin_common::BufferHeader::InitHeader(dolin_common::Header &header) {
  8. if ((sizeof(char) + sizeof(size_t) + sizeof(size_t) + header.log_path_len) > data_size) {
  9. return;
  10. }
  11. memcpy(data_ptr, &header.magic, sizeof(char));
  12. memcpy(data_ptr + sizeof(char), &header.log_len, sizeof(size_t));
  13. memcpy(data_ptr + sizeof(char) + sizeof(size_t), &header.log_path_len, sizeof(size_t));
  14. memcpy(data_ptr + sizeof(char) + sizeof(size_t) + sizeof(size_t), header.log_path, header.log_path_len);
  15. char compress = 0;
  16. if (header.compress) {
  17. compress = 1;
  18. }
  19. memcpy(data_ptr + sizeof(char) + sizeof(size_t) + sizeof(size_t) + header.log_path_len, &compress, sizeof(char));
  20. }
  21. /**
  22. * 获取原始的锚点
  23. */
  24. void *dolin_common::BufferHeader::GetOriginPtr() {
  25. return data_ptr;
  26. }
  27. /**
  28. * 获取当前锚点
  29. */
  30. void *dolin_common::BufferHeader::GetPtr() {
  31. return data_ptr + GetHeaderLen();
  32. }
  33. /**
  34. * 获取写入的锚点
  35. */
  36. void *dolin_common::BufferHeader::GetWritePtr() {
  37. return data_ptr + GetHeaderLen() + GetLogLen();
  38. }
  39. dolin_common::Header *dolin_common::BufferHeader::GetHeader() {
  40. auto *header = new Header();
  41. if (IsAvailable()) {
  42. header->magic = kMagicHeader;
  43. size_t log_len = 0;
  44. memcpy(&log_len, data_ptr + sizeof(char), sizeof(size_t));
  45. header->log_len = log_len;
  46. size_t log_path_len = 0;
  47. memcpy(&log_path_len, data_ptr + sizeof(char) + sizeof(size_t), sizeof(size_t));
  48. header->log_path_len = log_path_len;
  49. char *log_path = new char[log_path_len + 1];
  50. memset(log_path, 0, log_path_len + 1);
  51. memcpy(log_path, data_ptr + sizeof(char) + sizeof(size_t) + sizeof(size_t), log_path_len);
  52. header->log_path = log_path;
  53. char compress = (data_ptr + sizeof(char) + sizeof(size_t) + sizeof(size_t) + log_path_len)[0];
  54. header->compress = compress == 1;
  55. }
  56. return header;
  57. }
  58. size_t dolin_common::BufferHeader::GetHeaderLen() {
  59. if (IsAvailable()) {
  60. return CalculateHeaderLen(GetLogPathLen());
  61. }
  62. return 0;
  63. }
  64. void dolin_common::BufferHeader::SetLogLen(size_t len) {
  65. if (IsAvailable()) {
  66. memcpy(data_ptr + sizeof(char), &len, sizeof(size_t));
  67. }
  68. }
  69. size_t dolin_common::BufferHeader::GetLogLen() {
  70. if (IsAvailable()) {
  71. size_t len = 0;
  72. memcpy(&len, data_ptr + sizeof(char), sizeof(size_t));
  73. //log长度总是大于0并小于buffer_size减去header长度的
  74. if (len > 0 && len < (data_size - GetHeaderLen())) {
  75. return len;
  76. }
  77. }
  78. return 0;
  79. }
  80. size_t dolin_common::BufferHeader::GetLogPathLen() {
  81. if (IsAvailable()) {
  82. size_t len = 0;
  83. memcpy(&len, data_ptr + sizeof(char) + sizeof(size_t), sizeof(size_t));
  84. //log path 的长度不能大于整个buffer减去header中其他数据的长度
  85. if (len > 0 && len < data_size - CalculateHeaderLen(0)) {
  86. return len;
  87. }
  88. }
  89. return 0;
  90. }
  91. char *dolin_common::BufferHeader::GetLogPath() {
  92. if (IsAvailable()) {
  93. size_t log_path_len = GetLogPathLen();
  94. if (log_path_len > 0) {
  95. char *log_path = new char[log_path_len + 1];
  96. memset(log_path, 0, log_path_len + 1);
  97. memcpy(log_path, data_ptr + sizeof(char) + sizeof(size_t) + sizeof(size_t), log_path_len);
  98. return log_path;
  99. }
  100. }
  101. return nullptr;
  102. }
  103. bool dolin_common::BufferHeader::IsCompress() {
  104. if (IsAvailable()) {
  105. return ((data_ptr + sizeof(char) + sizeof(size_t) + sizeof(size_t) + GetLogPathLen())[0]) == 1;
  106. }
  107. return false;
  108. }
  109. bool dolin_common::BufferHeader::IsAvailable() {
  110. return data_ptr[0] == kMagicHeader;
  111. }
  112. size_t dolin_common::BufferHeader::CalculateHeaderLen(size_t path_len) {
  113. return sizeof(char) + sizeof(size_t) + sizeof(size_t) + path_len + sizeof(char);
  114. }