From 6327f450ce430ea7363dc310c388a1e5cd4cdf58 Mon Sep 17 00:00:00 2001 From: godotg Date: Sun, 14 Jul 2024 20:03:00 +0800 Subject: [PATCH] ref[cpp]: rename writeBoolean/readBoolean/writeOffset/readOffset to writeBool/readBool/getWriteOffset/getReadOffset --- .../src/test/cpp/zfoocpp/packet/EmptyObject.h | 50 +++++++ .../test/cpp/zfoocpp/packet/NormalObject.h | 137 ++++++++++++++++++ .../src/test/cpp/zfoocpp/packet/ObjectA.h | 69 +++++++++ .../src/test/cpp/zfoocpp/packet/ObjectB.h | 60 ++++++++ .../test/cpp/zfoocpp/packet/SimpleObject.h | 56 +++++++ 5 files changed, 372 insertions(+) create mode 100644 protocol/src/test/cpp/zfoocpp/packet/EmptyObject.h create mode 100644 protocol/src/test/cpp/zfoocpp/packet/NormalObject.h create mode 100644 protocol/src/test/cpp/zfoocpp/packet/ObjectA.h create mode 100644 protocol/src/test/cpp/zfoocpp/packet/ObjectB.h create mode 100644 protocol/src/test/cpp/zfoocpp/packet/SimpleObject.h diff --git a/protocol/src/test/cpp/zfoocpp/packet/EmptyObject.h b/protocol/src/test/cpp/zfoocpp/packet/EmptyObject.h new file mode 100644 index 00000000..2aec1f0a --- /dev/null +++ b/protocol/src/test/cpp/zfoocpp/packet/EmptyObject.h @@ -0,0 +1,50 @@ +#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.getReaderOffset(); + + if (length > 0) { + buffer.setReaderOffset(beforeReadIndex + length); + } + return packet; + } + }; +} + +#endif \ No newline at end of file diff --git a/protocol/src/test/cpp/zfoocpp/packet/NormalObject.h b/protocol/src/test/cpp/zfoocpp/packet/NormalObject.h new file mode 100644 index 00000000..82350b89 --- /dev/null +++ b/protocol/src/test/cpp/zfoocpp/packet/NormalObject.h @@ -0,0 +1,137 @@ +#ifndef ZFOO_NormalObject +#define ZFOO_NormalObject + +#include "zfoocpp/ByteBuffer.h" +#include "zfoocpp/Packet/ObjectA.h" +#include "zfoocpp/Packet/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.getWriterOffset(); + 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.getReaderOffset(); + 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.setReaderOffset(beforeReadIndex + length); + } + return packet; + } + }; +} + +#endif \ No newline at end of file diff --git a/protocol/src/test/cpp/zfoocpp/packet/ObjectA.h b/protocol/src/test/cpp/zfoocpp/packet/ObjectA.h new file mode 100644 index 00000000..5814981e --- /dev/null +++ b/protocol/src/test/cpp/zfoocpp/packet/ObjectA.h @@ -0,0 +1,69 @@ +#ifndef ZFOO_ObjectA +#define ZFOO_ObjectA + +#include "zfoocpp/ByteBuffer.h" +#include "zfoocpp/Packet/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.getWriterOffset(); + 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.getReaderOffset(); + 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.setReaderOffset(beforeReadIndex + length); + } + return packet; + } + }; +} + +#endif \ No newline at end of file diff --git a/protocol/src/test/cpp/zfoocpp/packet/ObjectB.h b/protocol/src/test/cpp/zfoocpp/packet/ObjectB.h new file mode 100644 index 00000000..79ae0a8d --- /dev/null +++ b/protocol/src/test/cpp/zfoocpp/packet/ObjectB.h @@ -0,0 +1,60 @@ +#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.getWriterOffset(); + 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.getReaderOffset(); + bool result0 = buffer.readBool(); + packet->flag = result0; + if (buffer.compatibleRead(beforeReadIndex, length)) { + int32_t result1 = buffer.readInt(); + packet->innerCompatibleValue = result1; + } + if (length > 0) { + buffer.setReaderOffset(beforeReadIndex + length); + } + return packet; + } + }; +} + +#endif \ No newline at end of file diff --git a/protocol/src/test/cpp/zfoocpp/packet/SimpleObject.h b/protocol/src/test/cpp/zfoocpp/packet/SimpleObject.h new file mode 100644 index 00000000..640b5b1f --- /dev/null +++ b/protocol/src/test/cpp/zfoocpp/packet/SimpleObject.h @@ -0,0 +1,56 @@ +#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.getReaderOffset(); + int32_t result0 = buffer.readInt(); + packet->c = result0; + bool result1 = buffer.readBool(); + packet->g = result1; + if (length > 0) { + buffer.setReaderOffset(beforeReadIndex + length); + } + return packet; + } + }; +} + +#endif \ No newline at end of file