diff --git a/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CodeGenerateCpp.java b/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CodeGenerateCpp.java index ffd83ec7..96b7810a 100644 --- a/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CodeGenerateCpp.java +++ b/protocol/src/main/java/com/zfoo/protocol/serializer/cpp/CodeGenerateCpp.java @@ -298,7 +298,7 @@ public class CodeGenerateCpp implements ICodeGenerate { var fieldRegistrations = registration.getFieldRegistrations(); var cppBuilder = new StringBuilder(); if (registration.isCompatible()) { - cppBuilder.append("auto beforeWriteIndex = buffer.writerIndex();").append(LS); + cppBuilder.append("auto beforeWriteIndex = buffer.getWriterOffset();").append(LS); cppBuilder.append(StringUtils.format("buffer.writeInt({});", registration.getPredictionLength())).append(LS); } else { cppBuilder.append("buffer.writeInt(-1);").append(LS); diff --git a/protocol/src/main/resources/cpp/ByteBuffer.h b/protocol/src/main/resources/cpp/ByteBuffer.h index 4933b9d2..fad7982c 100644 --- a/protocol/src/main/resources/cpp/ByteBuffer.h +++ b/protocol/src/main/resources/cpp/ByteBuffer.h @@ -11,7 +11,6 @@ // 网络传输默认使用大端传输 namespace zfoo { - using std::string; using std::vector; using std::list; @@ -58,8 +57,8 @@ namespace zfoo { private: int8_t *m_buffer; int32_t m_max_capacity; - int32_t m_writerIndex; - int32_t m_readerIndex; + int32_t m_writerOffset; + int32_t m_readerOffset; public: ByteBuffer(int32_t capacity = DEFAULT_BUFFER_SIZE) : m_max_capacity(capacity) { @@ -78,19 +77,19 @@ namespace zfoo { ByteBuffer &operator=(const ByteBuffer &buffer) = delete; void adjustPadding(int32_t predictionLength, int32_t beforeWriteIndex) { - int32_t currentWriteIndex = writerIndex(); + int32_t currentWriteIndex = getWriterOffset(); int32_t predictionCount = writeIntCount(predictionLength); int32_t length = currentWriteIndex - beforeWriteIndex - predictionCount; int32_t lengthCount = writeIntCount(length); int32_t padding = lengthCount - predictionCount; if (padding == 0) { - writerIndex(beforeWriteIndex); + setWriterOffset(beforeWriteIndex); writeInt(length); - writerIndex(currentWriteIndex); + setWriterOffset(currentWriteIndex); } else { int8_t *targetPtr = (int8_t *) calloc(length, sizeof(int8_t)); - memcpy(targetPtr, &m_buffer[currentWriteIndex - length], length); - writerIndex(beforeWriteIndex); + memcpy(targetPtr, &m_buffer[currentWriteIndex - length], length); + setWriterOffset(beforeWriteIndex); writeInt(length); writeBytes(targetPtr, length); free(targetPtr); @@ -98,48 +97,52 @@ namespace zfoo { } bool compatibleRead(int32_t beforeReadIndex, int32_t length) { - return length != -1 && readerIndex() < length + beforeReadIndex; + return length != -1 && getReaderOffset() < length + beforeReadIndex; } void clear() { - m_writerIndex = 0; - m_readerIndex = 0; + m_writerOffset = 0; + m_readerOffset = 0; } - int32_t writerIndex() const { - return m_writerIndex; + int8_t *getBuffer() { + return m_buffer; } - int32_t readerIndex() const { - return m_readerIndex; + int32_t getWriterOffset() const { + return m_writerOffset; } - void writerIndex(int32_t writeIndex) { + int32_t getReaderOffset() const { + return m_readerOffset; + } + + void setWriterOffset(int32_t writeIndex) { if (writeIndex > m_max_capacity) { string errorMessage = "writeIndex[" + std::to_string(writeIndex) + "] out of bounds exception: readerIndex: " + - std::to_string(m_readerIndex) + - ", writerIndex: " + std::to_string(m_writerIndex) + + std::to_string(m_readerOffset) + + ", writerIndex: " + std::to_string(m_writerOffset) + "(expected: 0 <= readerIndex <= writerIndex <= capacity:" + std::to_string(m_max_capacity); throw errorMessage; } - m_writerIndex = writeIndex; + m_writerOffset = writeIndex; } - void readerIndex(int32_t readerIndex) { - if (readerIndex > m_writerIndex) { + void setReaderOffset(int32_t readerIndex) { + if (readerIndex > m_writerOffset) { string errorMessage = "readIndex[" + std::to_string(readerIndex) + "] out of bounds exception: readerIndex: " + - std::to_string(m_readerIndex) + - ", writerIndex: " + std::to_string(m_writerIndex) + + std::to_string(m_readerOffset) + + ", writerIndex: " + std::to_string(m_writerOffset) + "(expected: 0 <= readerIndex <= writerIndex <= capacity:" + std::to_string(m_max_capacity); throw errorMessage; } - m_readerIndex = readerIndex; + m_readerOffset = readerIndex; } inline int32_t getCapacity() const { - return m_max_capacity - m_writerIndex; + return m_max_capacity - m_writerOffset; } inline void ensureCapacity(const int32_t &capacity) { @@ -156,27 +159,27 @@ namespace zfoo { } inline bool isReadable() { - return m_writerIndex > m_readerIndex; + return m_writerOffset > m_readerOffset; } inline void writeBool(const bool &value) { ensureCapacity(1); int8_t v = value ? 1 : 0; - m_buffer[m_writerIndex++] = v; + m_buffer[m_writerOffset++] = v; } inline bool readBool() { - int8_t value = m_buffer[m_readerIndex++]; + int8_t value = m_buffer[m_readerOffset++]; return value == 1; } inline void writeByte(const int8_t &value) { ensureCapacity(1); - m_buffer[m_writerIndex++] = value; + m_buffer[m_writerOffset++] = value; } inline int8_t readByte() { - return m_buffer[m_readerIndex++]; + return m_buffer[m_readerOffset++]; } inline void setByte(const int32_t &index, const int8_t &value) { @@ -189,13 +192,13 @@ namespace zfoo { inline void writeBytes(const int8_t *buffer, const int32_t &length) { ensureCapacity(length); - memcpy(&m_buffer[m_writerIndex], buffer, length); - m_writerIndex += length; + memcpy(&m_buffer[m_writerOffset], buffer, length); + m_writerOffset += length; } inline int8_t *readBytes(const int32_t &length) { - int8_t *bytes = &m_buffer[m_readerIndex]; - m_readerIndex += length; + int8_t *bytes = &m_buffer[m_readerOffset]; + m_readerOffset += length; return bytes; } @@ -219,14 +222,14 @@ namespace zfoo { return; } - int32_t writeIndex = m_writerIndex; + int32_t writeIndex = m_writerOffset; ensureCapacity(5); setByte(writeIndex++, (int8_t) (value | 0x80)); uint32_t b = value >> 14; if (b == 0) { setByte(writeIndex++, (int8_t) a); - writerIndex(writeIndex); + setWriterOffset(writeIndex); return; } @@ -234,7 +237,7 @@ namespace zfoo { a = value >> 21; if (a == 0) { setByte(writeIndex++, (int8_t) b); - writerIndex(writeIndex); + setWriterOffset(writeIndex); return; } @@ -242,13 +245,13 @@ namespace zfoo { b = value >> 28; if (b == 0) { setByte(writeIndex++, (int8_t) a); - writerIndex(writeIndex); + setWriterOffset(writeIndex); return; } setByte(writeIndex++, (int8_t) (a | 0x80)); setByte(writeIndex++, (int8_t) b); - writerIndex(writeIndex); + setWriterOffset(writeIndex); } inline int32_t writeIntCount(const int32_t &intValue) { @@ -269,7 +272,7 @@ namespace zfoo { } inline int32_t readInt() { - int32_t readIndex = m_readerIndex; + int32_t readIndex = m_readerOffset; int32_t b = getByte(readIndex++); uint32_t value = b; @@ -288,7 +291,7 @@ namespace zfoo { } } } - readerIndex(readIndex); + setReaderOffset(readIndex); value = ((value >> 1) ^ -((int32_t) value & 1)); return (int32_t) value; } @@ -345,7 +348,7 @@ namespace zfoo { } inline int64_t readLong() { - int32_t readIndex = m_readerIndex; + int32_t readIndex = m_readerOffset; int64_t b = getByte(readIndex++); uint64_t value = b; @@ -382,7 +385,7 @@ namespace zfoo { } } - readerIndex(readIndex); + setReaderOffset(readIndex); value = ((value >> 1) ^ -((int64_t) value & 1)); return (int64_t) value; } @@ -437,19 +440,19 @@ namespace zfoo { //---------------------------------boolean-------------------------------------- - inline void writeBooleanArray(const vector &array) { + inline void writeBoolArray(const vector &array) { if (array.empty()) { writeByte(0); return; } int32_t length = array.size(); writeInt(length); - for (auto value : array) { + for (auto value: array) { writeBool(value); } } - inline vector readBooleanArray() { + inline vector readBoolArray() { int32_t length = readInt(); int8_t *bytes = readBytes(length); vector array; @@ -459,19 +462,20 @@ namespace zfoo { return array; } - inline void writeBooleanList(const list &list) { + inline void writeBoolList(const list &list) { if (list.empty()) { writeByte(0); return; } int32_t length = list.size(); writeInt(length); - for (auto value : list) { + for (auto value: list) { writeBool(value); } } - inline list readBooleanList() { + inline list readBoolList() { + int32_t length = readInt(); list list; for (auto i = 0; i < length; i++) { @@ -480,19 +484,19 @@ namespace zfoo { return list; } - inline void writeBooleanSet(const set &set) { + inline void writeBoolSet(const set &set) { if (set.empty()) { writeByte(0); return; } int32_t length = set.size(); writeInt(length); - for (auto value : set) { + for (auto value: set) { writeBool(value); } } - inline set readBooleanSet() { + inline set readBoolSet() { int32_t length = readInt(); set set; for (auto i = 0; i < length; i++) { @@ -509,7 +513,7 @@ namespace zfoo { } int32_t length = array.size(); writeInt(length); - for (auto value : array) { + for (auto value: array) { writeByte(value); } } @@ -531,7 +535,7 @@ namespace zfoo { } int32_t length = list.size(); writeInt(length); - for (auto value : list) { + for (auto value: list) { writeByte(value); } } @@ -552,7 +556,7 @@ namespace zfoo { } int32_t length = set.size(); writeInt(length); - for (auto value : set) { + for (auto value: set) { writeByte(value); } } @@ -574,7 +578,7 @@ namespace zfoo { } int32_t length = array.size(); writeInt(length); - for (auto value : array) { + for (auto value: array) { writeShort(value); } } @@ -595,7 +599,7 @@ namespace zfoo { } int32_t length = list.size(); writeInt(length); - for (auto value : list) { + for (auto value: list) { writeShort(value); } } @@ -616,7 +620,7 @@ namespace zfoo { } int32_t length = set.size(); writeInt(length); - for (auto value : set) { + for (auto value: set) { writeShort(value); } } @@ -638,7 +642,7 @@ namespace zfoo { } int32_t length = array.size(); writeInt(length); - for (auto value : array) { + for (auto value: array) { writeInt(value); } } @@ -659,7 +663,7 @@ namespace zfoo { } int32_t length = list.size(); writeInt(length); - for (auto value : list) { + for (auto value: list) { writeInt(value); } } @@ -680,7 +684,7 @@ namespace zfoo { } int32_t length = set.size(); writeInt(length); - for (auto value : set) { + for (auto value: set) { writeInt(value); } } @@ -702,7 +706,7 @@ namespace zfoo { } int32_t length = array.size(); writeInt(length); - for (auto value : array) { + for (auto value: array) { writeLong(value); } } @@ -723,7 +727,7 @@ namespace zfoo { } int32_t length = list.size(); writeInt(length); - for (auto value : list) { + for (auto value: list) { writeLong(value); } } @@ -744,7 +748,7 @@ namespace zfoo { } int32_t length = set.size(); writeInt(length); - for (auto value : set) { + for (auto value: set) { writeLong(value); } } @@ -766,7 +770,7 @@ namespace zfoo { } int32_t length = array.size(); writeInt(length); - for (auto value : array) { + for (auto value: array) { writeFloat(value); } } @@ -787,7 +791,7 @@ namespace zfoo { } int32_t length = list.size(); writeInt(length); - for (auto value : list) { + for (auto value: list) { writeFloat(value); } } @@ -808,7 +812,7 @@ namespace zfoo { } int32_t length = list.size(); writeInt(length); - for (auto value : list) { + for (auto value: list) { writeFloat(value); } } @@ -830,7 +834,7 @@ namespace zfoo { } int32_t length = array.size(); writeInt(length); - for (auto value : array) { + for (auto value: array) { writeDouble(value); } } @@ -851,7 +855,7 @@ namespace zfoo { } int32_t length = list.size(); writeInt(length); - for (auto value : list) { + for (auto value: list) { writeDouble(value); } } @@ -872,7 +876,7 @@ namespace zfoo { } int32_t length = set.size(); writeInt(length); - for (auto value : set) { + for (auto value: set) { writeDouble(value); } } @@ -894,7 +898,7 @@ namespace zfoo { } int32_t length = array.size(); writeInt(length); - for (auto value : array) { + for (auto value: array) { writeString(value); } } @@ -915,7 +919,7 @@ namespace zfoo { } int32_t length = list.size(); writeInt(length); - for (auto value : list) { + for (auto value: list) { writeString(value); } } @@ -936,7 +940,7 @@ namespace zfoo { } int32_t length = set.size(); writeInt(length); - for (auto value : set) { + for (auto value: set) { writeString(value); } } @@ -956,7 +960,7 @@ namespace zfoo { return; } writeInt(map.size()); - for (const auto&[key, value] : map) { + for (const auto &[key, value]: map) { writeInt(key); writeInt(value); } @@ -979,7 +983,7 @@ namespace zfoo { return; } writeInt(map.size()); - for (const auto&[key, value] : map) { + for (const auto &[key, value]: map) { writeInt(key); writeLong(value); } @@ -1002,7 +1006,7 @@ namespace zfoo { return; } writeInt(map.size()); - for (const auto&[key, value] : map) { + for (const auto &[key, value]: map) { writeInt(key); writeString(value); } @@ -1026,7 +1030,7 @@ namespace zfoo { return; } writeInt(map.size()); - for (const auto&[key, value] : map) { + for (const auto &[key, value]: map) { writeInt(key); writePacket((IProtocol *) &value, protocolId); } @@ -1051,7 +1055,7 @@ namespace zfoo { return; } writeInt(map.size()); - for (const auto&[key, value] : map) { + for (const auto &[key, value]: map) { writeLong(key); writeInt(value); } @@ -1074,7 +1078,7 @@ namespace zfoo { return; } writeInt(map.size()); - for (const auto&[key, value] : map) { + for (const auto &[key, value]: map) { writeLong(key); writeLong(value); } @@ -1097,7 +1101,7 @@ namespace zfoo { return; } writeInt(map.size()); - for (const auto&[key, value] : map) { + for (const auto &[key, value]: map) { writeLong(key); writeString(value); } @@ -1121,7 +1125,7 @@ namespace zfoo { return; } writeInt(map.size()); - for (const auto&[key, value] : map) { + for (const auto &[key, value]: map) { writeLong(key); writePacket((IProtocol *) &value, protocolId); } @@ -1146,7 +1150,7 @@ namespace zfoo { return; } writeInt(map.size()); - for (const auto&[key, value] : map) { + for (const auto &[key, value]: map) { writeString(key); writeInt(value); } @@ -1169,7 +1173,7 @@ namespace zfoo { return; } writeInt(map.size()); - for (const auto&[key, value] : map) { + for (const auto &[key, value]: map) { writeString(key); writeLong(value); } @@ -1192,7 +1196,7 @@ namespace zfoo { return; } writeInt(map.size()); - for (const auto&[key, value] : map) { + for (const auto &[key, value]: map) { writeString(key); writeString(value); } @@ -1216,7 +1220,7 @@ namespace zfoo { return; } writeInt(map.size()); - for (const auto&[key, value] : map) { + for (const auto &[key, value]: map) { writeString(key); writePacket((IProtocol *) &value, protocolId); } @@ -1245,7 +1249,7 @@ namespace zfoo { } int32_t length = array.size(); writeInt(length); - for (auto value : array) { + for (auto value: array) { writePacket((IProtocol *) &value, protocolId); } } @@ -1270,7 +1274,7 @@ namespace zfoo { } int32_t length = list.size(); writeInt(length); - for (auto value : list) { + for (auto value: list) { writePacket((IProtocol *) &value, protocolId); } } @@ -1295,7 +1299,7 @@ namespace zfoo { } int32_t length = set.size(); writeInt(length); - for (auto value : set) { + for (auto value: set) { writePacket((IProtocol *) &value, protocolId); } } @@ -1321,17 +1325,17 @@ namespace zfoo { if (IS_LITTLE_ENDIAN) { swap_bytes(reinterpret_cast(&value)); } - memcpy(&m_buffer[m_writerIndex], (int8_t *) &value, sizeof(T)); - m_writerIndex += sizeof(T); + memcpy(&m_buffer[m_writerOffset], (int8_t *) &value, sizeof(T)); + m_writerOffset += sizeof(T); } template inline T read() { - T value = *((T *) &m_buffer[m_readerIndex]); + T value = *((T *) &m_buffer[m_readerOffset]); if (IS_LITTLE_ENDIAN) { swap_bytes(reinterpret_cast(&value)); } - m_readerIndex += sizeof(T); + m_readerOffset += sizeof(T); return value; } @@ -1343,7 +1347,6 @@ namespace zfoo { } } }; - } #endif diff --git a/protocol/src/main/resources/cpp/ProtocolRegistrationTemplate.h b/protocol/src/main/resources/cpp/ProtocolRegistrationTemplate.h index 423bfe98..a20a9d97 100644 --- a/protocol/src/main/resources/cpp/ProtocolRegistrationTemplate.h +++ b/protocol/src/main/resources/cpp/ProtocolRegistrationTemplate.h @@ -19,10 +19,10 @@ public: if (length == 0) { return packet; } - auto beforeReadIndex = buffer.readerIndex(); + auto beforeReadIndex = buffer.getReaderOffset(); ${protocol_read_deserialization} if (length > 0) { - buffer.readerIndex(beforeReadIndex + length); + buffer.setReaderOffset(beforeReadIndex + length); } return packet; } diff --git a/protocol/src/test/cpp/zfoocpp/ByteBuffer.h b/protocol/src/test/cpp/zfoocpp/ByteBuffer.h index 4933b9d2..7a7511d7 100644 --- a/protocol/src/test/cpp/zfoocpp/ByteBuffer.h +++ b/protocol/src/test/cpp/zfoocpp/ByteBuffer.h @@ -11,7 +11,6 @@ // 网络传输默认使用大端传输 namespace zfoo { - using std::string; using std::vector; using std::list; @@ -58,8 +57,8 @@ namespace zfoo { private: int8_t *m_buffer; int32_t m_max_capacity; - int32_t m_writerIndex; - int32_t m_readerIndex; + int32_t m_writerOffset; + int32_t m_readerOffset; public: ByteBuffer(int32_t capacity = DEFAULT_BUFFER_SIZE) : m_max_capacity(capacity) { @@ -78,19 +77,19 @@ namespace zfoo { ByteBuffer &operator=(const ByteBuffer &buffer) = delete; void adjustPadding(int32_t predictionLength, int32_t beforeWriteIndex) { - int32_t currentWriteIndex = writerIndex(); + int32_t currentWriteIndex = getWriterOffset(); int32_t predictionCount = writeIntCount(predictionLength); int32_t length = currentWriteIndex - beforeWriteIndex - predictionCount; int32_t lengthCount = writeIntCount(length); int32_t padding = lengthCount - predictionCount; if (padding == 0) { - writerIndex(beforeWriteIndex); + setWriterOffset(beforeWriteIndex); writeInt(length); - writerIndex(currentWriteIndex); + setWriterOffset(currentWriteIndex); } else { int8_t *targetPtr = (int8_t *) calloc(length, sizeof(int8_t)); - memcpy(targetPtr, &m_buffer[currentWriteIndex - length], length); - writerIndex(beforeWriteIndex); + memcpy(targetPtr, &m_buffer[currentWriteIndex - length], length); + setWriterOffset(beforeWriteIndex); writeInt(length); writeBytes(targetPtr, length); free(targetPtr); @@ -98,48 +97,52 @@ namespace zfoo { } bool compatibleRead(int32_t beforeReadIndex, int32_t length) { - return length != -1 && readerIndex() < length + beforeReadIndex; + return length != -1 && getReaderOffset() < length + beforeReadIndex; } void clear() { - m_writerIndex = 0; - m_readerIndex = 0; + m_writerOffset = 0; + m_readerOffset = 0; } - int32_t writerIndex() const { - return m_writerIndex; + int8_t *getBuffer() { + return m_buffer; } - int32_t readerIndex() const { - return m_readerIndex; + int32_t getWriterOffset() const { + return m_writerOffset; } - void writerIndex(int32_t writeIndex) { + int32_t getReaderOffset() const { + return m_readerOffset; + } + + void setWriterOffset(int32_t writeIndex) { if (writeIndex > m_max_capacity) { string errorMessage = "writeIndex[" + std::to_string(writeIndex) + "] out of bounds exception: readerIndex: " + - std::to_string(m_readerIndex) + - ", writerIndex: " + std::to_string(m_writerIndex) + + std::to_string(m_readerOffset) + + ", writerIndex: " + std::to_string(m_writerOffset) + "(expected: 0 <= readerIndex <= writerIndex <= capacity:" + std::to_string(m_max_capacity); throw errorMessage; } - m_writerIndex = writeIndex; + m_writerOffset = writeIndex; } - void readerIndex(int32_t readerIndex) { - if (readerIndex > m_writerIndex) { + void setReaderOffset(int32_t readerIndex) { + if (readerIndex > m_writerOffset) { string errorMessage = "readIndex[" + std::to_string(readerIndex) + "] out of bounds exception: readerIndex: " + - std::to_string(m_readerIndex) + - ", writerIndex: " + std::to_string(m_writerIndex) + + std::to_string(m_readerOffset) + + ", writerIndex: " + std::to_string(m_writerOffset) + "(expected: 0 <= readerIndex <= writerIndex <= capacity:" + std::to_string(m_max_capacity); throw errorMessage; } - m_readerIndex = readerIndex; + m_readerOffset = readerIndex; } inline int32_t getCapacity() const { - return m_max_capacity - m_writerIndex; + return m_max_capacity - m_writerOffset; } inline void ensureCapacity(const int32_t &capacity) { @@ -156,27 +159,27 @@ namespace zfoo { } inline bool isReadable() { - return m_writerIndex > m_readerIndex; + return m_writerOffset > m_readerOffset; } inline void writeBool(const bool &value) { ensureCapacity(1); int8_t v = value ? 1 : 0; - m_buffer[m_writerIndex++] = v; + m_buffer[m_writerOffset++] = v; } inline bool readBool() { - int8_t value = m_buffer[m_readerIndex++]; + int8_t value = m_buffer[m_readerOffset++]; return value == 1; } inline void writeByte(const int8_t &value) { ensureCapacity(1); - m_buffer[m_writerIndex++] = value; + m_buffer[m_writerOffset++] = value; } inline int8_t readByte() { - return m_buffer[m_readerIndex++]; + return m_buffer[m_readerOffset++]; } inline void setByte(const int32_t &index, const int8_t &value) { @@ -189,13 +192,13 @@ namespace zfoo { inline void writeBytes(const int8_t *buffer, const int32_t &length) { ensureCapacity(length); - memcpy(&m_buffer[m_writerIndex], buffer, length); - m_writerIndex += length; + memcpy(&m_buffer[m_writerOffset], buffer, length); + m_writerOffset += length; } inline int8_t *readBytes(const int32_t &length) { - int8_t *bytes = &m_buffer[m_readerIndex]; - m_readerIndex += length; + int8_t *bytes = &m_buffer[m_readerOffset]; + m_readerOffset += length; return bytes; } @@ -219,14 +222,14 @@ namespace zfoo { return; } - int32_t writeIndex = m_writerIndex; + int32_t writeIndex = m_writerOffset; ensureCapacity(5); setByte(writeIndex++, (int8_t) (value | 0x80)); uint32_t b = value >> 14; if (b == 0) { setByte(writeIndex++, (int8_t) a); - writerIndex(writeIndex); + setWriterOffset(writeIndex); return; } @@ -234,7 +237,7 @@ namespace zfoo { a = value >> 21; if (a == 0) { setByte(writeIndex++, (int8_t) b); - writerIndex(writeIndex); + setWriterOffset(writeIndex); return; } @@ -242,13 +245,13 @@ namespace zfoo { b = value >> 28; if (b == 0) { setByte(writeIndex++, (int8_t) a); - writerIndex(writeIndex); + setWriterOffset(writeIndex); return; } setByte(writeIndex++, (int8_t) (a | 0x80)); setByte(writeIndex++, (int8_t) b); - writerIndex(writeIndex); + setWriterOffset(writeIndex); } inline int32_t writeIntCount(const int32_t &intValue) { @@ -269,7 +272,7 @@ namespace zfoo { } inline int32_t readInt() { - int32_t readIndex = m_readerIndex; + int32_t readIndex = m_readerOffset; int32_t b = getByte(readIndex++); uint32_t value = b; @@ -288,7 +291,7 @@ namespace zfoo { } } } - readerIndex(readIndex); + setReaderOffset(readIndex); value = ((value >> 1) ^ -((int32_t) value & 1)); return (int32_t) value; } @@ -345,7 +348,7 @@ namespace zfoo { } inline int64_t readLong() { - int32_t readIndex = m_readerIndex; + int32_t readIndex = m_readerOffset; int64_t b = getByte(readIndex++); uint64_t value = b; @@ -382,7 +385,7 @@ namespace zfoo { } } - readerIndex(readIndex); + setReaderOffset(readIndex); value = ((value >> 1) ^ -((int64_t) value & 1)); return (int64_t) value; } @@ -437,19 +440,19 @@ namespace zfoo { //---------------------------------boolean-------------------------------------- - inline void writeBooleanArray(const vector &array) { + inline void writeBoolArray(const vector &array) { if (array.empty()) { writeByte(0); return; } int32_t length = array.size(); writeInt(length); - for (auto value : array) { + for (auto value: array) { writeBool(value); } } - inline vector readBooleanArray() { + inline vector readBoolArray() { int32_t length = readInt(); int8_t *bytes = readBytes(length); vector array; @@ -459,19 +462,19 @@ namespace zfoo { return array; } - inline void writeBooleanList(const list &list) { + inline void writeBoolList(const list &list) { if (list.empty()) { writeByte(0); return; } int32_t length = list.size(); writeInt(length); - for (auto value : list) { + for (auto value: list) { writeBool(value); } } - inline list readBooleanList() { + inline list readBoolList() { int32_t length = readInt(); list list; for (auto i = 0; i < length; i++) { @@ -480,19 +483,19 @@ namespace zfoo { return list; } - inline void writeBooleanSet(const set &set) { + inline void writeBoolSet(const set &set) { if (set.empty()) { writeByte(0); return; } int32_t length = set.size(); writeInt(length); - for (auto value : set) { + for (auto value: set) { writeBool(value); } } - inline set readBooleanSet() { + inline set readBoolSet() { int32_t length = readInt(); set set; for (auto i = 0; i < length; i++) { @@ -509,7 +512,7 @@ namespace zfoo { } int32_t length = array.size(); writeInt(length); - for (auto value : array) { + for (auto value: array) { writeByte(value); } } @@ -531,7 +534,7 @@ namespace zfoo { } int32_t length = list.size(); writeInt(length); - for (auto value : list) { + for (auto value: list) { writeByte(value); } } @@ -552,7 +555,7 @@ namespace zfoo { } int32_t length = set.size(); writeInt(length); - for (auto value : set) { + for (auto value: set) { writeByte(value); } } @@ -574,7 +577,7 @@ namespace zfoo { } int32_t length = array.size(); writeInt(length); - for (auto value : array) { + for (auto value: array) { writeShort(value); } } @@ -595,7 +598,7 @@ namespace zfoo { } int32_t length = list.size(); writeInt(length); - for (auto value : list) { + for (auto value: list) { writeShort(value); } } @@ -616,7 +619,7 @@ namespace zfoo { } int32_t length = set.size(); writeInt(length); - for (auto value : set) { + for (auto value: set) { writeShort(value); } } @@ -638,7 +641,7 @@ namespace zfoo { } int32_t length = array.size(); writeInt(length); - for (auto value : array) { + for (auto value: array) { writeInt(value); } } @@ -659,7 +662,7 @@ namespace zfoo { } int32_t length = list.size(); writeInt(length); - for (auto value : list) { + for (auto value: list) { writeInt(value); } } @@ -680,7 +683,7 @@ namespace zfoo { } int32_t length = set.size(); writeInt(length); - for (auto value : set) { + for (auto value: set) { writeInt(value); } } @@ -702,7 +705,7 @@ namespace zfoo { } int32_t length = array.size(); writeInt(length); - for (auto value : array) { + for (auto value: array) { writeLong(value); } } @@ -723,7 +726,7 @@ namespace zfoo { } int32_t length = list.size(); writeInt(length); - for (auto value : list) { + for (auto value: list) { writeLong(value); } } @@ -744,7 +747,7 @@ namespace zfoo { } int32_t length = set.size(); writeInt(length); - for (auto value : set) { + for (auto value: set) { writeLong(value); } } @@ -766,7 +769,7 @@ namespace zfoo { } int32_t length = array.size(); writeInt(length); - for (auto value : array) { + for (auto value: array) { writeFloat(value); } } @@ -787,7 +790,7 @@ namespace zfoo { } int32_t length = list.size(); writeInt(length); - for (auto value : list) { + for (auto value: list) { writeFloat(value); } } @@ -808,7 +811,7 @@ namespace zfoo { } int32_t length = list.size(); writeInt(length); - for (auto value : list) { + for (auto value: list) { writeFloat(value); } } @@ -830,7 +833,7 @@ namespace zfoo { } int32_t length = array.size(); writeInt(length); - for (auto value : array) { + for (auto value: array) { writeDouble(value); } } @@ -851,7 +854,7 @@ namespace zfoo { } int32_t length = list.size(); writeInt(length); - for (auto value : list) { + for (auto value: list) { writeDouble(value); } } @@ -872,7 +875,7 @@ namespace zfoo { } int32_t length = set.size(); writeInt(length); - for (auto value : set) { + for (auto value: set) { writeDouble(value); } } @@ -894,7 +897,7 @@ namespace zfoo { } int32_t length = array.size(); writeInt(length); - for (auto value : array) { + for (auto value: array) { writeString(value); } } @@ -915,7 +918,7 @@ namespace zfoo { } int32_t length = list.size(); writeInt(length); - for (auto value : list) { + for (auto value: list) { writeString(value); } } @@ -936,7 +939,7 @@ namespace zfoo { } int32_t length = set.size(); writeInt(length); - for (auto value : set) { + for (auto value: set) { writeString(value); } } @@ -956,7 +959,7 @@ namespace zfoo { return; } writeInt(map.size()); - for (const auto&[key, value] : map) { + for (const auto &[key, value]: map) { writeInt(key); writeInt(value); } @@ -979,7 +982,7 @@ namespace zfoo { return; } writeInt(map.size()); - for (const auto&[key, value] : map) { + for (const auto &[key, value]: map) { writeInt(key); writeLong(value); } @@ -1002,7 +1005,7 @@ namespace zfoo { return; } writeInt(map.size()); - for (const auto&[key, value] : map) { + for (const auto &[key, value]: map) { writeInt(key); writeString(value); } @@ -1026,7 +1029,7 @@ namespace zfoo { return; } writeInt(map.size()); - for (const auto&[key, value] : map) { + for (const auto &[key, value]: map) { writeInt(key); writePacket((IProtocol *) &value, protocolId); } @@ -1051,7 +1054,7 @@ namespace zfoo { return; } writeInt(map.size()); - for (const auto&[key, value] : map) { + for (const auto &[key, value]: map) { writeLong(key); writeInt(value); } @@ -1074,7 +1077,7 @@ namespace zfoo { return; } writeInt(map.size()); - for (const auto&[key, value] : map) { + for (const auto &[key, value]: map) { writeLong(key); writeLong(value); } @@ -1097,7 +1100,7 @@ namespace zfoo { return; } writeInt(map.size()); - for (const auto&[key, value] : map) { + for (const auto &[key, value]: map) { writeLong(key); writeString(value); } @@ -1121,7 +1124,7 @@ namespace zfoo { return; } writeInt(map.size()); - for (const auto&[key, value] : map) { + for (const auto &[key, value]: map) { writeLong(key); writePacket((IProtocol *) &value, protocolId); } @@ -1146,7 +1149,7 @@ namespace zfoo { return; } writeInt(map.size()); - for (const auto&[key, value] : map) { + for (const auto &[key, value]: map) { writeString(key); writeInt(value); } @@ -1169,7 +1172,7 @@ namespace zfoo { return; } writeInt(map.size()); - for (const auto&[key, value] : map) { + for (const auto &[key, value]: map) { writeString(key); writeLong(value); } @@ -1192,7 +1195,7 @@ namespace zfoo { return; } writeInt(map.size()); - for (const auto&[key, value] : map) { + for (const auto &[key, value]: map) { writeString(key); writeString(value); } @@ -1216,7 +1219,7 @@ namespace zfoo { return; } writeInt(map.size()); - for (const auto&[key, value] : map) { + for (const auto &[key, value]: map) { writeString(key); writePacket((IProtocol *) &value, protocolId); } @@ -1245,7 +1248,7 @@ namespace zfoo { } int32_t length = array.size(); writeInt(length); - for (auto value : array) { + for (auto value: array) { writePacket((IProtocol *) &value, protocolId); } } @@ -1270,7 +1273,7 @@ namespace zfoo { } int32_t length = list.size(); writeInt(length); - for (auto value : list) { + for (auto value: list) { writePacket((IProtocol *) &value, protocolId); } } @@ -1295,7 +1298,7 @@ namespace zfoo { } int32_t length = set.size(); writeInt(length); - for (auto value : set) { + for (auto value: set) { writePacket((IProtocol *) &value, protocolId); } } @@ -1321,17 +1324,17 @@ namespace zfoo { if (IS_LITTLE_ENDIAN) { swap_bytes(reinterpret_cast(&value)); } - memcpy(&m_buffer[m_writerIndex], (int8_t *) &value, sizeof(T)); - m_writerIndex += sizeof(T); + memcpy(&m_buffer[m_writerOffset], (int8_t *) &value, sizeof(T)); + m_writerOffset += sizeof(T); } template inline T read() { - T value = *((T *) &m_buffer[m_readerIndex]); + T value = *((T *) &m_buffer[m_readerOffset]); if (IS_LITTLE_ENDIAN) { swap_bytes(reinterpret_cast(&value)); } - m_readerIndex += sizeof(T); + m_readerOffset += sizeof(T); return value; } @@ -1343,7 +1346,6 @@ namespace zfoo { } } }; - } #endif diff --git a/protocol/src/test/cpp/zfoocpp/EmptyObject.h b/protocol/src/test/cpp/zfoocpp/EmptyObject.h deleted file mode 100644 index 82814fca..00000000 --- a/protocol/src/test/cpp/zfoocpp/EmptyObject.h +++ /dev/null @@ -1,50 +0,0 @@ -#ifndef ZFOO_EmptyObject -#define ZFOO_EmptyObject - -#include "zfoocpp/ByteBuffer.h" - -namespace zfoo { - - class EmptyObject : public IProtocol { - public: - - - ~EmptyObject() override = default; - - int16_t protocolId() override { - return 0; - } - }; - - class EmptyObjectRegistration : public IProtocolRegistration { - public: - int16_t protocolId() override { - return 0; - } - - void write(ByteBuffer &buffer, IProtocol *packet) override { - if (packet == nullptr) { - buffer.writeInt(0); - return; - } - auto *message = (EmptyObject *) packet; - buffer.writeInt(-1); - } - - IProtocol *read(ByteBuffer &buffer) override { - auto *packet = new EmptyObject(); - auto length = buffer.readInt(); - if (length == 0) { - return packet; - } - auto beforeReadIndex = buffer.readerIndex(); - - if (length > 0) { - buffer.readerIndex(beforeReadIndex + length); - } - return packet; - } - }; -} - -#endif \ No newline at end of file diff --git a/protocol/src/test/cpp/zfoocpp/NormalObject.h b/protocol/src/test/cpp/zfoocpp/NormalObject.h deleted file mode 100644 index e54450ab..00000000 --- a/protocol/src/test/cpp/zfoocpp/NormalObject.h +++ /dev/null @@ -1,136 +0,0 @@ -#ifndef ZFOO_NormalObject -#define ZFOO_NormalObject - -#include "zfoocpp/ByteBuffer.h" -#include "zfoocpp/ObjectA.h" -#include "zfoocpp/ObjectB.h" -namespace zfoo { - - class NormalObject : public IProtocol { - public: - int8_t a; - vector aaa; - int16_t b; - int32_t c; - int64_t d; - float e; - double f; - bool g; - string jj; - ObjectA kk; - list l; - list ll; - list lll; - list llll; - map m; - map mm; - set s; - set ssss; - int32_t outCompatibleValue; - int32_t outCompatibleValue2; - - ~NormalObject() override = default; - - int16_t protocolId() override { - return 101; - } - }; - - class NormalObjectRegistration : public IProtocolRegistration { - public: - int16_t protocolId() override { - return 101; - } - - void write(ByteBuffer &buffer, IProtocol *packet) override { - if (packet == nullptr) { - buffer.writeInt(0); - return; - } - auto *message = (NormalObject *) packet; - auto beforeWriteIndex = buffer.writerIndex(); - buffer.writeInt(857); - buffer.writeByte(message->a); - buffer.writeByteArray(message->aaa); - buffer.writeShort(message->b); - buffer.writeInt(message->c); - buffer.writeLong(message->d); - buffer.writeFloat(message->e); - buffer.writeDouble(message->f); - buffer.writeBool(message->g); - buffer.writeString(message->jj); - buffer.writePacket(&message->kk, 102); - buffer.writeIntList(message->l); - buffer.writeLongList(message->ll); - buffer.writePacketList(message->lll, 102); - buffer.writeStringList(message->llll); - buffer.writeIntStringMap(message->m); - buffer.writeIntPacketMap(message->mm, 102); - buffer.writeIntSet(message->s); - buffer.writeStringSet(message->ssss); - buffer.writeInt(message->outCompatibleValue); - buffer.writeInt(message->outCompatibleValue2); - buffer.adjustPadding(857, beforeWriteIndex); - } - - IProtocol *read(ByteBuffer &buffer) override { - auto *packet = new NormalObject(); - auto length = buffer.readInt(); - if (length == 0) { - return packet; - } - auto beforeReadIndex = buffer.readerIndex(); - int8_t result0 = buffer.readByte(); - packet->a = result0; - auto array1 = buffer.readByteArray(); - packet->aaa = array1; - auto result2 = buffer.readShort(); - packet->b = result2; - int32_t result3 = buffer.readInt(); - packet->c = result3; - auto result4 = buffer.readLong(); - packet->d = result4; - float result5 = buffer.readFloat(); - packet->e = result5; - double result6 = buffer.readDouble(); - packet->f = result6; - bool result7 = buffer.readBool(); - packet->g = result7; - auto result8 = buffer.readString(); - packet->jj = result8; - auto result9 = buffer.readPacket(102); - auto *result10 = (ObjectA *) result9.get(); - packet->kk = *result10; - auto list11 = buffer.readIntList(); - packet->l = list11; - auto list12 = buffer.readLongList(); - packet->ll = list12; - auto list13 = buffer.readPacketList(102); - packet->lll = list13; - auto list14 = buffer.readStringList(); - packet->llll = list14; - auto map15 = buffer.readIntStringMap(); - packet->m = map15; - auto map16 = buffer.readIntPacketMap(102); - packet->mm = map16; - auto set17 = buffer.readIntSet(); - packet->s = set17; - auto set18 = buffer.readStringSet(); - packet->ssss = set18; - if (buffer.compatibleRead(beforeReadIndex, length)) { - int32_t result19 = buffer.readInt(); - packet->outCompatibleValue = result19; - } - if (buffer.compatibleRead(beforeReadIndex, length)) { - int32_t result20 = buffer.readInt(); - packet->outCompatibleValue2 = result20; - } - if (length > 0) { - buffer.readerIndex(beforeReadIndex + length); - } - return packet; - } - }; -} - -#endif \ No newline at end of file diff --git a/protocol/src/test/cpp/zfoocpp/ObjectA.h b/protocol/src/test/cpp/zfoocpp/ObjectA.h deleted file mode 100644 index 88f80a6a..00000000 --- a/protocol/src/test/cpp/zfoocpp/ObjectA.h +++ /dev/null @@ -1,69 +0,0 @@ -#ifndef ZFOO_ObjectA -#define ZFOO_ObjectA - -#include "zfoocpp/ByteBuffer.h" -#include "zfoocpp/ObjectB.h" -namespace zfoo { - - class ObjectA : public IProtocol { - public: - int32_t a; - map m; - ObjectB objectB; - int32_t innerCompatibleValue; - - ~ObjectA() override = default; - - int16_t protocolId() override { - return 102; - } - }; - - class ObjectARegistration : public IProtocolRegistration { - public: - int16_t protocolId() override { - return 102; - } - - void write(ByteBuffer &buffer, IProtocol *packet) override { - if (packet == nullptr) { - buffer.writeInt(0); - return; - } - auto *message = (ObjectA *) packet; - auto beforeWriteIndex = buffer.writerIndex(); - buffer.writeInt(201); - buffer.writeInt(message->a); - buffer.writeIntStringMap(message->m); - buffer.writePacket(&message->objectB, 103); - buffer.writeInt(message->innerCompatibleValue); - buffer.adjustPadding(201, beforeWriteIndex); - } - - IProtocol *read(ByteBuffer &buffer) override { - auto *packet = new ObjectA(); - auto length = buffer.readInt(); - if (length == 0) { - return packet; - } - auto beforeReadIndex = buffer.readerIndex(); - int32_t result0 = buffer.readInt(); - packet->a = result0; - auto map1 = buffer.readIntStringMap(); - packet->m = map1; - auto result2 = buffer.readPacket(103); - auto *result3 = (ObjectB *) result2.get(); - packet->objectB = *result3; - if (buffer.compatibleRead(beforeReadIndex, length)) { - int32_t result4 = buffer.readInt(); - packet->innerCompatibleValue = result4; - } - if (length > 0) { - buffer.readerIndex(beforeReadIndex + length); - } - return packet; - } - }; -} - -#endif \ No newline at end of file diff --git a/protocol/src/test/cpp/zfoocpp/ObjectB.h b/protocol/src/test/cpp/zfoocpp/ObjectB.h deleted file mode 100644 index 36d85eb1..00000000 --- a/protocol/src/test/cpp/zfoocpp/ObjectB.h +++ /dev/null @@ -1,60 +0,0 @@ -#ifndef ZFOO_ObjectB -#define ZFOO_ObjectB - -#include "zfoocpp/ByteBuffer.h" - -namespace zfoo { - - class ObjectB : public IProtocol { - public: - bool flag; - int32_t innerCompatibleValue; - - ~ObjectB() override = default; - - int16_t protocolId() override { - return 103; - } - }; - - class ObjectBRegistration : public IProtocolRegistration { - public: - int16_t protocolId() override { - return 103; - } - - void write(ByteBuffer &buffer, IProtocol *packet) override { - if (packet == nullptr) { - buffer.writeInt(0); - return; - } - auto *message = (ObjectB *) packet; - auto beforeWriteIndex = buffer.writerIndex(); - buffer.writeInt(4); - buffer.writeBool(message->flag); - buffer.writeInt(message->innerCompatibleValue); - buffer.adjustPadding(4, beforeWriteIndex); - } - - IProtocol *read(ByteBuffer &buffer) override { - auto *packet = new ObjectB(); - auto length = buffer.readInt(); - if (length == 0) { - return packet; - } - auto beforeReadIndex = buffer.readerIndex(); - bool result0 = buffer.readBool(); - packet->flag = result0; - if (buffer.compatibleRead(beforeReadIndex, length)) { - int32_t result1 = buffer.readInt(); - packet->innerCompatibleValue = result1; - } - if (length > 0) { - buffer.readerIndex(beforeReadIndex + length); - } - return packet; - } - }; -} - -#endif \ No newline at end of file diff --git a/protocol/src/test/cpp/zfoocpp/ProtocolManager.h b/protocol/src/test/cpp/zfoocpp/ProtocolManager.h index 1152cbf9..664168c2 100644 --- a/protocol/src/test/cpp/zfoocpp/ProtocolManager.h +++ b/protocol/src/test/cpp/zfoocpp/ProtocolManager.h @@ -2,11 +2,11 @@ #define ZFOO_PROTOCOLMANAGER_H #include "ByteBuffer.h" -#include "zfoocpp/EmptyObject.h" -#include "zfoocpp/NormalObject.h" -#include "zfoocpp/ObjectA.h" -#include "zfoocpp/ObjectB.h" -#include "zfoocpp/SimpleObject.h" +#include "zfoocpp/packet/EmptyObject.h" +#include "zfoocpp/packet/NormalObject.h" +#include "zfoocpp/packet/ObjectA.h" +#include "zfoocpp/packet/ObjectB.h" +#include "zfoocpp/packet/SimpleObject.h" namespace zfoo { const int16_t MAX_PROTOCOL_NUM = 32767; diff --git a/protocol/src/test/cpp/zfoocpp/SimpleObject.h b/protocol/src/test/cpp/zfoocpp/SimpleObject.h deleted file mode 100644 index 83bb2b2c..00000000 --- a/protocol/src/test/cpp/zfoocpp/SimpleObject.h +++ /dev/null @@ -1,56 +0,0 @@ -#ifndef ZFOO_SimpleObject -#define ZFOO_SimpleObject - -#include "zfoocpp/ByteBuffer.h" - -namespace zfoo { - - class SimpleObject : public IProtocol { - public: - int32_t c; - bool g; - - ~SimpleObject() override = default; - - int16_t protocolId() override { - return 104; - } - }; - - class SimpleObjectRegistration : public IProtocolRegistration { - public: - int16_t protocolId() override { - return 104; - } - - void write(ByteBuffer &buffer, IProtocol *packet) override { - if (packet == nullptr) { - buffer.writeInt(0); - return; - } - auto *message = (SimpleObject *) packet; - buffer.writeInt(-1); - buffer.writeInt(message->c); - buffer.writeBool(message->g); - } - - IProtocol *read(ByteBuffer &buffer) override { - auto *packet = new SimpleObject(); - auto length = buffer.readInt(); - if (length == 0) { - return packet; - } - auto beforeReadIndex = buffer.readerIndex(); - int32_t result0 = buffer.readInt(); - packet->c = result0; - bool result1 = buffer.readBool(); - packet->g = result1; - if (length > 0) { - buffer.readerIndex(beforeReadIndex + length); - } - return packet; - } - }; -} - -#endif \ No newline at end of file