mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-06-09 18:15:30 +00:00
feat[cpp]: compatible field of inside protocol class
This commit is contained in:
@@ -77,6 +77,29 @@ namespace zfoo {
|
||||
|
||||
ByteBuffer &operator=(const ByteBuffer &buffer) = delete;
|
||||
|
||||
void adjustPadding(int32_t predictionLength, int32_t beforeWriteIndex) {
|
||||
int32_t currentWriteIndex = writerIndex();
|
||||
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);
|
||||
writeInt(length);
|
||||
writerIndex(currentWriteIndex);
|
||||
} else {
|
||||
int8_t *targetPtr = (int8_t *) calloc(length, sizeof(int8_t));
|
||||
memcpy(targetPtr, &m_buffer[currentWriteIndex - length], length);
|
||||
writerIndex(beforeWriteIndex);
|
||||
writeInt(length);
|
||||
writeBytes(targetPtr, length);
|
||||
free(targetPtr);
|
||||
}
|
||||
}
|
||||
|
||||
bool compatibleRead(int32_t beforeReadIndex, int32_t length) {
|
||||
return length != -1 && readerIndex() < length + beforeReadIndex;
|
||||
}
|
||||
|
||||
void clear() {
|
||||
m_writerIndex = 0;
|
||||
@@ -229,17 +252,17 @@ namespace zfoo {
|
||||
}
|
||||
|
||||
inline int32_t writeIntCount(const int32_t &intValue) {
|
||||
uint32_t v = (uint32_t) ((intValue << 1) ^ (intValue >> 31));
|
||||
if (v >> 7 == 0) {
|
||||
uint32_t value = (uint32_t) ((intValue << 1) ^ (intValue >> 31));
|
||||
if (value >> 7 == 0) {
|
||||
return 1;
|
||||
}
|
||||
if (v >> 14 == 0) {
|
||||
if (value >> 14 == 0) {
|
||||
return 2;
|
||||
}
|
||||
if (v >> 21 == 0) {
|
||||
if (value >> 21 == 0) {
|
||||
return 3;
|
||||
}
|
||||
if (v >> 28 == 0) {
|
||||
if (value >> 28 == 0) {
|
||||
return 4;
|
||||
}
|
||||
return 5;
|
||||
@@ -401,12 +424,6 @@ namespace zfoo {
|
||||
return str;
|
||||
}
|
||||
|
||||
inline bool writePacketFlag(const IProtocol *packet) {
|
||||
bool flag = packet == nullptr;
|
||||
writeBool(!flag);
|
||||
return flag;
|
||||
}
|
||||
|
||||
inline void writePacket(IProtocol *packet, const int16_t &protocolId) {
|
||||
IProtocolRegistration *protocolRegistration = getProtocol(protocolId);
|
||||
protocolRegistration->write(*this, packet);
|
||||
|
||||
@@ -36,7 +36,8 @@ namespace zfoo {
|
||||
}
|
||||
|
||||
void write(ByteBuffer &buffer, IProtocol *packet) override {
|
||||
if (buffer.writePacketFlag(packet)) {
|
||||
if (packet == nullptr) {
|
||||
buffer.writeInt(0);
|
||||
return;
|
||||
}
|
||||
auto *message = ({} *) packet;
|
||||
|
||||
Reference in New Issue
Block a user