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 bff769d commit 49b80da
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ private String protocol_write_serialization(ProtocolRegistration registration) {
var fieldRegistrations = registration.getFieldRegistrations();
var cppBuilder = new StringBuilder();
if (registration.isCompatible()) {
cppBuilder.append("auto beforeWriteIndex = buffer.getWriterOffset();").append(LS);
cppBuilder.append("auto beforeWriteIndex = buffer.getWriteOffset();").append(LS);
cppBuilder.append(StringUtils.format("buffer.writeInt({});", registration.getPredictionLength())).append(LS);
} else {
cppBuilder.append("buffer.writeInt(-1);").append(LS);
Expand Down
127 changes: 64 additions & 63 deletions protocol/src/main/resources/cpp/ByteBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,20 @@ namespace zfoo {

class ByteBuffer {
private:
int8_t *m_buffer;
int32_t m_max_capacity;
int32_t m_writerOffset;
int32_t m_readerOffset;
int8_t *buffer;
int32_t max_capacity;
int32_t writeOffset;
int32_t readerOffset;

public:
ByteBuffer(int32_t capacity = DEFAULT_BUFFER_SIZE) : m_max_capacity(capacity) {
m_buffer = (int8_t *) calloc(m_max_capacity, sizeof(int8_t));
ByteBuffer(int32_t capacity = DEFAULT_BUFFER_SIZE) : max_capacity(capacity) {
buffer = (int8_t *) calloc(max_capacity, sizeof(int8_t));
clear();
}

~ByteBuffer() {
free(m_buffer);
m_buffer = nullptr;
free(buffer);
buffer = nullptr;
}


Expand All @@ -77,128 +77,128 @@ namespace zfoo {
ByteBuffer &operator=(const ByteBuffer &buffer) = delete;

void adjustPadding(int32_t predictionLength, int32_t beforeWriteIndex) {
int32_t currentWriteIndex = getWriterOffset();
int32_t currentWriteIndex = getWriteOffset();
int32_t predictionCount = writeIntCount(predictionLength);
int32_t length = currentWriteIndex - beforeWriteIndex - predictionCount;
int32_t lengthCount = writeIntCount(length);
int32_t padding = lengthCount - predictionCount;
if (padding == 0) {
setWriterOffset(beforeWriteIndex);
setWriteOffset(beforeWriteIndex);
writeInt(length);
setWriterOffset(currentWriteIndex);
setWriteOffset(currentWriteIndex);
} else {
int8_t *targetPtr = (int8_t *) calloc(length, sizeof(int8_t));
memcpy(targetPtr, &m_buffer[currentWriteIndex - length], length);
setWriterOffset(beforeWriteIndex);
memcpy(targetPtr, &buffer[currentWriteIndex - length], length);
setWriteOffset(beforeWriteIndex);
writeInt(length);
writeBytes(targetPtr, length);
free(targetPtr);
}
}

bool compatibleRead(int32_t beforeReadIndex, int32_t length) {
return length != -1 && getReaderOffset() < length + beforeReadIndex;
return length != -1 && getReadOffset() < length + beforeReadIndex;
}

void clear() {
m_writerOffset = 0;
m_readerOffset = 0;
writeOffset = 0;
readerOffset = 0;
}

int8_t *getBuffer() {
return m_buffer;
return buffer;
}

int32_t getWriterOffset() const {
return m_writerOffset;
int32_t getWriteOffset() const {
return writeOffset;
}

int32_t getReaderOffset() const {
return m_readerOffset;
int32_t getReadOffset() const {
return readerOffset;
}

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

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

inline int32_t getCapacity() const {
return m_max_capacity - m_writerOffset;
return max_capacity - writeOffset;
}

inline void ensureCapacity(const int32_t &capacity) {
while (capacity - getCapacity() > 0) {
int32_t newSize = m_max_capacity * 2;
int8_t *pBuf = (int8_t *) realloc(m_buffer, newSize);
int32_t newSize = max_capacity * 2;
int8_t *pBuf = (int8_t *) realloc(buffer, newSize);
if (!pBuf) {
string errorMessage = "relloc failed!";
throw errorMessage;
}
m_buffer = pBuf;
m_max_capacity = newSize;
buffer = pBuf;
max_capacity = newSize;
}
}

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

inline void writeBool(const bool &value) {
ensureCapacity(1);
int8_t v = value ? 1 : 0;
m_buffer[m_writerOffset++] = v;
buffer[writeOffset++] = v;
}

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

inline void writeByte(const int8_t &value) {
ensureCapacity(1);
m_buffer[m_writerOffset++] = value;
buffer[writeOffset++] = value;
}

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

inline void setByte(const int32_t &index, const int8_t &value) {
m_buffer[index] = value;
buffer[index] = value;
}

inline int8_t getByte(const int32_t &index) {
return m_buffer[index];
return buffer[index];
}

inline void writeBytes(const int8_t *buffer, const int32_t &length) {
inline void writeBytes(const int8_t *buf, const int32_t &length) {
ensureCapacity(length);
memcpy(&m_buffer[m_writerOffset], buffer, length);
m_writerOffset += length;
memcpy(&buffer[writeOffset], buf, length);
writeOffset += length;
}

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

Expand All @@ -222,36 +222,36 @@ namespace zfoo {
return;
}

int32_t writeIndex = m_writerOffset;
int32_t writeIndex = writeOffset;
ensureCapacity(5);

setByte(writeIndex++, (int8_t) (value | 0x80));
uint32_t b = value >> 14;
if (b == 0) {
setByte(writeIndex++, (int8_t) a);
setWriterOffset(writeIndex);
setWriteOffset(writeIndex);
return;
}

setByte(writeIndex++, (int8_t) (a | 0x80));
a = value >> 21;
if (a == 0) {
setByte(writeIndex++, (int8_t) b);
setWriterOffset(writeIndex);
setWriteOffset(writeIndex);
return;
}

setByte(writeIndex++, (int8_t) (b | 0x80));
b = value >> 28;
if (b == 0) {
setByte(writeIndex++, (int8_t) a);
setWriterOffset(writeIndex);
setWriteOffset(writeIndex);
return;
}

setByte(writeIndex++, (int8_t) (a | 0x80));
setByte(writeIndex++, (int8_t) b);
setWriterOffset(writeIndex);
setWriteOffset(writeIndex);
}

inline int32_t writeIntCount(const int32_t &intValue) {
Expand All @@ -272,7 +272,7 @@ namespace zfoo {
}

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

int32_t b = getByte(readIndex++);
uint32_t value = b;
Expand All @@ -291,7 +291,7 @@ namespace zfoo {
}
}
}
setReaderOffset(readIndex);
setReadOffset(readIndex);
value = ((value >> 1) ^ -((int32_t) value & 1));
return (int32_t) value;
}
Expand Down Expand Up @@ -348,7 +348,7 @@ namespace zfoo {
}

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

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

setReaderOffset(readIndex);
setReadOffset(readIndex);
value = ((value >> 1) ^ -((int64_t) value & 1));
return (int64_t) value;
}
Expand Down Expand Up @@ -475,6 +475,7 @@ namespace zfoo {
}

inline list<bool> readBoolList() {

int32_t length = readInt();
list<bool> list;
for (auto i = 0; i < length; i++) {
Expand Down Expand Up @@ -1324,17 +1325,17 @@ namespace zfoo {
if (IS_LITTLE_ENDIAN) {
swap_bytes<sizeof(T)>(reinterpret_cast<int8_t *>(&value));
}
memcpy(&m_buffer[m_writerOffset], (int8_t *) &value, sizeof(T));
m_writerOffset += sizeof(T);
memcpy(&buffer[writeOffset], (int8_t *) &value, sizeof(T));
writeOffset += sizeof(T);
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ class ${protocol_name}Registration : public IProtocolRegistration {
if (length == 0) {
return packet;
}
auto beforeReadIndex = buffer.getReaderOffset();
auto beforeReadIndex = buffer.getReadOffset();
${protocol_read_deserialization}
if (length > 0) {
buffer.setReaderOffset(beforeReadIndex + length);
buffer.setReadOffset(beforeReadIndex + length);
}
return packet;
}
Expand Down

0 comments on commit 49b80da

Please sign in to comment.