From 4a04ddd207d28057c850fdede2202f02a0e233ff Mon Sep 17 00:00:00 2001 From: abevol Date: Tue, 17 Oct 2023 06:48:44 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=96=B9=E6=B3=95=20putS?= =?UTF-8?q?tring/getString/move/step=20=E4=BC=98=E5=8C=96=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E6=80=A7=E8=83=BD=20getBytes=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E8=A7=84=E8=8C=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ByteBuffer.h | 140 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 84 insertions(+), 56 deletions(-) diff --git a/ByteBuffer.h b/ByteBuffer.h index e010ef8..452b0ae 100644 --- a/ByteBuffer.h +++ b/ByteBuffer.h @@ -1,5 +1,3 @@ - - #ifndef __BYTEBUFFER_H__ #define __BYTEBUFFER_H__ @@ -12,40 +10,40 @@ // Default size of the buffer #define DEFAULT_BUFFER_SIZE 2048 +#define DEFAULT_MARK_POSITION ((uint32_t)-1) class ByteBuffer { public: ByteBuffer(uint32_t capacity = DEFAULT_BUFFER_SIZE, const char* name = "") - : mark_(-1), + : name_(name), + mark_(DEFAULT_MARK_POSITION), limit_(capacity), position_(0), - capacity_(capacity), - name_(name) + capacity_(capacity) { - p_buffer_ = NULL; - p_buffer_ = (uint8_t*)calloc(capacity_, sizeof(uint8_t)); + p_buffer_ = nullptr; + p_buffer_ = static_cast(calloc(capacity_, sizeof(uint8_t))); } ByteBuffer(uint8_t* arr, uint32_t length, const char* name = "") - : mark_(-1), + : name_(name), + mark_(DEFAULT_MARK_POSITION), limit_(length), position_(0), - capacity_(length), - name_(name) + capacity_(length) { - p_buffer_ = NULL; - p_buffer_ = (uint8_t*)calloc(capacity_, sizeof(uint8_t)); + p_buffer_ = nullptr; + p_buffer_ = static_cast(calloc(capacity_, sizeof(uint8_t))); - putBytes(arr, capacity_); - clear(); + putBytes(arr, length); } ~ByteBuffer() { if (p_buffer_) { free(p_buffer_); - p_buffer_ = NULL; + p_buffer_ = nullptr; } } @@ -69,17 +67,29 @@ class ByteBuffer } ByteBuffer& putBytes(const uint8_t* buf, uint32_t len) { - for (uint32_t i = 0; i < len; i++) - append(buf[i]); - + append(buf, len); return *this; } ByteBuffer& putBytes(const uint8_t* buf, uint32_t len, uint32_t index) { position_ = index; - for (uint32_t i = 0; i < len; i++) - append(buf[i]); - + append(buf, len); + return *this; + } + ByteBuffer& putString(const std::string& buf) + { + append(reinterpret_cast(buf.data()), static_cast(buf.size())); + return *this; + } + ByteBuffer& putString(const std::string& buf, uint32_t len) + { + append(reinterpret_cast(buf.data()), len); + return *this; + } + ByteBuffer& putString(const std::string& buf, uint32_t len, uint32_t index) + { + position_ = index; + append(reinterpret_cast(buf.data()), len); return *this; } ByteBuffer& putChar(char value) @@ -154,34 +164,30 @@ class ByteBuffer } void getBytes(uint8_t* buf, uint32_t len) { - // 合法性检测 - if (!p_buffer_ || position_ + len > limit_) - return; - - for (uint32_t i = 0; i < len; i++) - { - buf[i] = p_buffer_[position_++]; - } + if (0 == memcpy_s(buf, len, p_buffer_ + position_, remaining())) + position_ = position_ + len; } void getBytes(uint32_t index, uint8_t* buf, uint32_t len) const { - // 合法性检测 - if (!p_buffer_ || index + len > limit_) - return; - - uint32_t pos = index; - for (uint32_t i = 0; i < len; i++) - { - buf[i] = p_buffer_[pos++]; - } + memcpy_s(buf, len, p_buffer_ + index, limit_ - index); + } + std::string getString(uint32_t len) + { + std::string result((char*)p_buffer_ + position_, len); + position_ += len; + return result; + } + std::string getString(uint32_t index, uint32_t len) const + { + return {p_buffer_ + index, p_buffer_ + index + len}; } char getChar() { - return read(); + return read(); } char getChar(uint32_t index) const { - return read(index); + return read(index); } uint16_t getShort() { @@ -254,7 +260,7 @@ class ByteBuffer ByteBuffer& clear() { position_ = 0; - mark_ = -1; + mark_ = DEFAULT_MARK_POSITION; limit_ = capacity_; return *this; } @@ -262,7 +268,7 @@ class ByteBuffer { limit_ = position_; position_ = 0; - mark_ = -1; + mark_ = DEFAULT_MARK_POSITION; return *this; } ByteBuffer& mark() @@ -273,20 +279,20 @@ class ByteBuffer ByteBuffer& discardMark() { - mark_ = -1; + mark_ = DEFAULT_MARK_POSITION; return *this; } - ByteBuffer& reset() + ByteBuffer& rewind() { - if (mark_ >= 0) + if (mark_ != DEFAULT_MARK_POSITION) position_ = mark_; return *this; } - ByteBuffer& rewind() + ByteBuffer& reset() { - mark_ = -1; + mark_ = DEFAULT_MARK_POSITION; position_ = 0; return *this; @@ -307,14 +313,13 @@ class ByteBuffer p_buffer_[i] = p_buffer_[position_ + i]; } position_ = limit_ - position_; - } while (0); + } while (false); limit_ = capacity_; return *this; } - - bool hasRemaining() + bool hasRemaining() const { return limit_ > position_; } @@ -330,25 +335,39 @@ class ByteBuffer { return position_; } + uint32_t move(const uint32_t newPos) + { + if (newPos <= limit_) + position_ = newPos; + return position_; + } + uint32_t step(const int32_t steps) + { + int64_t newPos = position_ + steps; + if (newPos >= 0 && newPos <= limit_) + position_ = static_cast(newPos); + return position_; + } uint32_t limit() const { return limit_; } - ByteBuffer& limit(uint32_t newLimit) + ByteBuffer& limit(const uint32_t newLimit) { if (position_ > newLimit) position_ = newLimit; if (mark_ > newLimit) - mark_ = -1; + mark_ = DEFAULT_MARK_POSITION; return *this; } - ByteBuffer& position(uint32_t newPosition) + ByteBuffer& position(const uint32_t newPosition) { position_ = newPosition; + return *this; } void printInfo() const @@ -367,7 +386,7 @@ class ByteBuffer if (!p_buffer_ || index + sizeof(T) > limit_) return 0; - return *((T*)&p_buffer_[index]); + return *reinterpret_cast(&p_buffer_[index]); } template @@ -378,7 +397,6 @@ class ByteBuffer return data; } - template void append(T data) { @@ -391,6 +409,16 @@ class ByteBuffer memcpy(&p_buffer_[position_], (uint8_t*)&data, s); position_ += s; } + void append(const uint8_t* data, uint32_t len) + { + if (!p_buffer_) + return; + + checkSize(len); + + memcpy(&p_buffer_[position_], data, len); + position_ += len; + } template void insert(T data, uint32_t index) @@ -430,7 +458,7 @@ class ByteBuffer const uint32_t BUFFER_SIZE_INCREASE = 2048; std::string name_; uint8_t* p_buffer_; - int32_t mark_; + uint32_t mark_; uint32_t limit_; uint32_t position_; uint32_t capacity_; From 87dea1f4c36916b7a2963c77e9092b7110f75993 Mon Sep 17 00:00:00 2001 From: abevol Date: Tue, 17 Oct 2023 06:56:55 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BB=8E=E6=95=B0=E7=BB=84=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=8C=96=E5=90=8E=E8=87=AA=E5=8A=A8=E8=BD=AC=E5=85=A5?= =?UTF-8?q?=E8=AF=BB=E5=8F=96=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ByteBuffer.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ByteBuffer.h b/ByteBuffer.h index 452b0ae..420c681 100644 --- a/ByteBuffer.h +++ b/ByteBuffer.h @@ -37,6 +37,7 @@ class ByteBuffer p_buffer_ = static_cast(calloc(capacity_, sizeof(uint8_t))); putBytes(arr, length); + flip(); } ~ByteBuffer() { @@ -294,7 +295,6 @@ class ByteBuffer { mark_ = DEFAULT_MARK_POSITION; position_ = 0; - return *this; } From ca83415fd4a64b74f23cebaed60b690b1caaa8a0 Mon Sep 17 00:00:00 2001 From: abevol Date: Tue, 17 Oct 2023 08:00:44 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=85=AC=E5=BC=80?= =?UTF-8?q?=E7=9A=84=E6=A8=A1=E6=9D=BF=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ByteBuffer.h | 50 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/ByteBuffer.h b/ByteBuffer.h index 420c681..1d1db35 100644 --- a/ByteBuffer.h +++ b/ByteBuffer.h @@ -49,19 +49,27 @@ class ByteBuffer } // Write Methods - ByteBuffer& put(ByteBuffer* bb) + template + ByteBuffer& put(T data) const { - for (uint32_t i = 0; i < bb->limit(); i++) - append(bb->get(i)); - + return append(data); + } + template + ByteBuffer& put(T data, uint32_t index) const + { + return insert(data, index); + } + ByteBuffer& putBuffer(const ByteBuffer* bb) + { + putBytes(bb->p_buffer_, bb->limit()); return *this; } - ByteBuffer& put(uint8_t value) + ByteBuffer& putByte(uint8_t value) { append(value); return *this; } - ByteBuffer& put(uint8_t value, uint32_t index) + ByteBuffer& putByte(uint8_t value, uint32_t index) { insert(value, index); return *this; @@ -155,11 +163,21 @@ class ByteBuffer } // Read Methods - uint8_t get() + template + T get() const + { + return read(); + } + template + T get(uint32_t index) const + { + return read(index); + } + uint8_t getByte() { return read(); } - uint8_t get(uint32_t index) const + uint8_t getByte(uint32_t index) const { return read(index); } @@ -231,7 +249,7 @@ class ByteBuffer return read(index); } - bool equals(ByteBuffer* other) + bool equals(const ByteBuffer* other) const { uint32_t len = limit(); if (len != other->limit()) @@ -239,18 +257,18 @@ class ByteBuffer for (uint32_t i = 0; i < len; i++) { - if (get(i) != other->get(i)) + if (getByte(i) != other->getByte(i)) return false; } return true; } - ByteBuffer* duplicate() + ByteBuffer* duplicate() const { ByteBuffer* newBuffer = new ByteBuffer(capacity_); // copy data - newBuffer->put(this); + newBuffer->putBuffer(this); newBuffer->limit(limit_); newBuffer->position(position_); @@ -370,6 +388,11 @@ class ByteBuffer return *this; } + uint8_t* buffer() const + { + return p_buffer_; + } + void printInfo() const { std::cout << "ByteBuffer " << name_ << ":\n" @@ -423,9 +446,6 @@ class ByteBuffer template void insert(T data, uint32_t index) { - uint32_t s = sizeof(data); - checkSize(index, s); - position_ = index; append(data); }