Skip to content

Commit

Permalink
ref[protocol]: cpp protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Jul 15, 2024
1 parent 49b80da commit b83f94e
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 104 deletions.
46 changes: 23 additions & 23 deletions protocol/src/main/resources/cpp/ByteBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ namespace zfoo {
int8_t *buffer;
int32_t max_capacity;
int32_t writeOffset;
int32_t readerOffset;
int32_t readOffset;

public:
ByteBuffer(int32_t capacity = DEFAULT_BUFFER_SIZE) : max_capacity(capacity) {
Expand Down Expand Up @@ -102,7 +102,7 @@ namespace zfoo {

void clear() {
writeOffset = 0;
readerOffset = 0;
readOffset = 0;
}

int8_t *getBuffer() {
Expand All @@ -114,31 +114,31 @@ namespace zfoo {
}

int32_t getReadOffset() const {
return readerOffset;
return readOffset;
}

void setWriteOffset(int32_t writeIndex) {
if (writeIndex > max_capacity) {
string errorMessage =
"writeIndex[" + std::to_string(writeIndex) + "] out of bounds exception: readerIndex: " +
std::to_string(readerOffset) +
", writerIndex: " + std::to_string(writeOffset) +
"(expected: 0 <= readerIndex <= writerIndex <= capacity:" + std::to_string(max_capacity);
"writeIndex[" + std::to_string(writeIndex) + "] out of bounds exception: readIndex: " +
std::to_string(readOffset) +
", writIndex: " + std::to_string(writeOffset) +
"(expected: 0 <= readIndex <= writeIndex <= capacity:" + std::to_string(max_capacity);
throw errorMessage;
}
writeOffset = writeIndex;
}

void setReadOffset(int32_t readerIndex) {
if (readerIndex > writeOffset) {
void setReadOffset(int32_t readIndex) {
if (readIndex > writeOffset) {
string errorMessage =
"readIndex[" + std::to_string(readerIndex) + "] out of bounds exception: readerIndex: " +
std::to_string(readerOffset) +
", writerIndex: " + std::to_string(writeOffset) +
"(expected: 0 <= readerIndex <= writerIndex <= capacity:" + std::to_string(max_capacity);
"readIndex[" + std::to_string(readIndex) + "] out of bounds exception: readIndex: " +
std::to_string(readOffset) +
", writeIndex: " + std::to_string(writeOffset) +
"(expected: 0 <= readIndex <= writeIndex <= capacity:" + std::to_string(max_capacity);
throw errorMessage;
}
readerOffset = readerIndex;
readOffset = readIndex;
}

inline int32_t getCapacity() const {
Expand All @@ -159,7 +159,7 @@ namespace zfoo {
}

inline bool isReadable() {
return writeOffset > readerOffset;
return writeOffset > readOffset;
}

inline void writeBool(const bool &value) {
Expand All @@ -169,7 +169,7 @@ namespace zfoo {
}

inline bool readBool() {
int8_t value = buffer[readerOffset++];
int8_t value = buffer[readOffset++];
return value == 1;
}

Expand All @@ -179,7 +179,7 @@ namespace zfoo {
}

inline int8_t readByte() {
return buffer[readerOffset++];
return buffer[readOffset++];
}

inline void setByte(const int32_t &index, const int8_t &value) {
Expand All @@ -197,8 +197,8 @@ namespace zfoo {
}

inline int8_t *readBytes(const int32_t &length) {
int8_t *bytes = &buffer[readerOffset];
readerOffset += length;
int8_t *bytes = &buffer[readOffset];
readOffset += length;
return bytes;
}

Expand Down Expand Up @@ -272,7 +272,7 @@ namespace zfoo {
}

inline int32_t readInt() {
int32_t readIndex = readerOffset;
int32_t readIndex = readOffset;

int32_t b = getByte(readIndex++);
uint32_t value = b;
Expand Down Expand Up @@ -348,7 +348,7 @@ namespace zfoo {
}

inline int64_t readLong() {
int32_t readIndex = readerOffset;
int32_t readIndex = readOffset;

int64_t b = getByte(readIndex++);
uint64_t value = b;
Expand Down Expand Up @@ -1331,11 +1331,11 @@ namespace zfoo {

template<class T>
inline T read() {
T value = *((T *) &buffer[readerOffset]);
T value = *((T *) &buffer[readOffset]);
if (IS_LITTLE_ENDIAN) {
swap_bytes<sizeof(T)>(reinterpret_cast<int8_t *>(&value));
}
readerOffset += sizeof(T);
readOffset += sizeof(T);
return value;
}

Expand Down
4 changes: 2 additions & 2 deletions protocol/src/test/cpp/test/byte_buffer_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ namespace byte_buffer_test {

ByteBuffer buffer;
assert(buffer.getCapacity() == DEFAULT_BUFFER_SIZE);
assert(buffer.getWriterOffset() == 0);
assert(buffer.getReaderOffset() == 0);
assert(buffer.getWriteOffset() == 0);
assert(buffer.getReadOffset() == 0);

boolTest(buffer);
byteTest(buffer);
Expand Down
2 changes: 1 addition & 1 deletion protocol/src/test/cpp/test/serialization_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ namespace serialization_test {
obj = *((NormalObject *) read(newBuffer));

cout << "source size " << length << endl;
cout << "target size " << newBuffer.getWriterOffset() << endl;
cout << "target size " << newBuffer.getWriteOffset() << endl;
cout << "compatible test" << endl;
}

Expand Down
Loading

0 comments on commit b83f94e

Please sign in to comment.