diff --git a/source/libs/sync/inc/syncMessage.h b/source/libs/sync/inc/syncMessage.h index 0de8e8cf4bb984bc3fe0b73edb95786c5ae3e182..5f7360157650d1585293d0a5dceb23d09a3cabfc 100644 --- a/source/libs/sync/inc/syncMessage.h +++ b/source/libs/sync/inc/syncMessage.h @@ -23,6 +23,7 @@ extern "C" { #include #include #include +#include "cJSON.h" #include "sync.h" #include "syncRaftEntry.h" #include "taosdef.h" @@ -52,7 +53,7 @@ typedef struct SyncPing { SRaftId srcId; SRaftId destId; uint32_t dataLen; - char* data; + char data[]; } SyncPing; #define SYNC_PING_FIX_LEN (sizeof(uint32_t) + sizeof(uint32_t) + sizeof(SRaftId) + sizeof(SRaftId) + sizeof(uint32_t)) @@ -63,7 +64,7 @@ typedef struct SyncPingReply { SRaftId srcId; SRaftId destId; uint32_t dataLen; - char* data; + char data[]; } SyncPingReply; typedef struct SyncClientRequest { @@ -128,6 +129,8 @@ void syncPingDeserialize(const char* buf, uint32_t len, SyncPing* pSyncPing); void syncPing2RpcMsg(const SyncPing* pSyncPing, SRpcMsg* pRpcMsg); +cJSON* syncPing2Json(const SyncPing* pSyncPing); + #ifdef __cplusplus } #endif diff --git a/source/libs/sync/src/syncMessage.c b/source/libs/sync/src/syncMessage.c index baff4ed50b82150cc3e2a99660d2401d23ced6f6..dc584c4e7b092a9c0a479f24bca2cbc5104abe72 100644 --- a/source/libs/sync/src/syncMessage.c +++ b/source/libs/sync/src/syncMessage.c @@ -41,9 +41,11 @@ void syncPingSerialize(const SyncPing* pSyncPing, char* buf, uint32_t bufLen) { } void syncPingDeserialize(const char* buf, uint32_t len, SyncPing* pSyncPing) { + /* uint32_t* pU32 = (uint32_t*)buf; uint32_t bytes = *pU32; pSyncPing = (SyncPing*)malloc(bytes); + */ memcpy(pSyncPing, buf, len); assert(len == pSyncPing->bytes); assert(pSyncPing->bytes == SYNC_PING_FIX_LEN + pSyncPing->dataLen); @@ -60,23 +62,28 @@ void syncPing2RpcMsg(const SyncPing* pSyncPing, SRpcMsg* pRpcMsg) { free(buf); } -/* -typedef struct SRaftId { - SyncNodeId addr; // typedef uint64_t SyncNodeId; - SyncGroupId vgId; // typedef int32_t SyncGroupId; -} SRaftId; - -typedef struct SyncPing { - uint32_t bytes; - uint32_t msgType; - SRaftId srcId; - SRaftId destId; - uint32_t dataLen; - char* data; -} SyncPing; -*/ +cJSON* syncPing2Json(const SyncPing* pSyncPing) { + cJSON* pRoot = cJSON_CreateObject(); + cJSON_AddNumberToObject(pRoot, "bytes", pSyncPing->bytes); + cJSON_AddNumberToObject(pRoot, "msgType", pSyncPing->msgType); -/* + cJSON* pSrcId = cJSON_CreateObject(); + cJSON_AddNumberToObject(pSrcId, "addr", pSyncPing->srcId.addr); + cJSON_AddNumberToObject(pSrcId, "vgId", pSyncPing->srcId.vgId); + cJSON_AddItemToObject(pRoot, "srcId", pSrcId); + + cJSON* pDestId = cJSON_CreateObject(); + cJSON_AddNumberToObject(pDestId, "addr", pSyncPing->destId.addr); + cJSON_AddNumberToObject(pDestId, "vgId", pSyncPing->destId.vgId); + cJSON_AddItemToObject(pRoot, "srcId", pDestId); + + cJSON_AddNumberToObject(pRoot, "dataLen", pSyncPing->dataLen); + cJSON_AddStringToObject(pRoot, "data", pSyncPing->data); + + return pRoot; +} + +#if 0 void syncPingSerialize(const SyncPing* pSyncPing, char** ppBuf, uint32_t* bufLen) { *bufLen = sizeof(SyncPing) + pSyncPing->dataLen; *ppBuf = (char*)malloc(*bufLen); @@ -144,4 +151,4 @@ void syncPingDeserialize(const char* buf, uint32_t len, SyncPing* pSyncPing) { pStart = taosDecodeFixedU32(pStart, &u32); pSyncPing->dataLen = u32; } -*/ \ No newline at end of file +#endif \ No newline at end of file diff --git a/source/libs/sync/test/CMakeLists.txt b/source/libs/sync/test/CMakeLists.txt index e655ac01be5cef4af5ede6e5d2735bf67eb123c3..8e1c99d179d7f66fff144df0036c6b1088ba2e39 100644 --- a/source/libs/sync/test/CMakeLists.txt +++ b/source/libs/sync/test/CMakeLists.txt @@ -1,6 +1,7 @@ add_executable(syncTest "") add_executable(syncEnvTest "") add_executable(syncPingTest "") +add_executable(syncEncodeTest "") target_sources(syncTest @@ -15,6 +16,10 @@ target_sources(syncPingTest PRIVATE "syncPingTest.cpp" ) +target_sources(syncEncodeTest + PRIVATE + "syncEncodeTest.cpp" +) target_include_directories(syncTest @@ -32,6 +37,11 @@ target_include_directories(syncPingTest "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) +target_include_directories(syncEncodeTest + PUBLIC + "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${CMAKE_CURRENT_SOURCE_DIR}/../inc" +) target_link_libraries(syncTest @@ -46,6 +56,10 @@ target_link_libraries(syncPingTest sync gtest_main ) +target_link_libraries(syncEncodeTest + sync + gtest_main +) enable_testing() diff --git a/source/libs/sync/test/syncEncodeTest.cpp b/source/libs/sync/test/syncEncodeTest.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1e8007e422d101c1e545879600899abaa19b950b --- /dev/null +++ b/source/libs/sync/test/syncEncodeTest.cpp @@ -0,0 +1,60 @@ +#include +#include "syncEnv.h" +#include "syncIO.h" +#include "syncInt.h" +#include "syncMessage.h" + +void logTest() { + sTrace("--- sync log test: trace"); + sDebug("--- sync log test: debug"); + sInfo("--- sync log test: info"); + sWarn("--- sync log test: warn"); + sError("--- sync log test: error"); + sFatal("--- sync log test: fatal"); +} + +#define PING_MSG_LEN 20 + +int main() { + // taosInitLog((char*)"syncPingTest.log", 100000, 10); + tsAsyncLog = 0; + sDebugFlag = 143 + 64; + + char msg[PING_MSG_LEN]; + snprintf(msg, sizeof(msg), "%s", "test ping"); + SyncPing* pSyncPing = syncPingBuild(PING_MSG_LEN); + pSyncPing->srcId.addr = 1; + pSyncPing->srcId.vgId = 2; + pSyncPing->destId.addr = 3; + pSyncPing->destId.vgId = 4; + memcpy(pSyncPing->data, msg, PING_MSG_LEN); + + { + cJSON* pJson = syncPing2Json(pSyncPing); + char* serialized = cJSON_Print(pJson); + printf("SyncPing: \n%s\n\n", serialized); + free(serialized); + cJSON_Delete(pJson); + } + + uint32_t bufLen = pSyncPing->bytes; + char* buf = (char*)malloc(bufLen); + syncPingSerialize(pSyncPing, buf, bufLen); + + SyncPing* pSyncPing2 = (SyncPing*)malloc(pSyncPing->bytes); + syncPingDeserialize(buf, bufLen, pSyncPing2); + + { + cJSON* pJson = syncPing2Json(pSyncPing2); + char* serialized = cJSON_Print(pJson); + printf("SyncPing2: \n%s\n\n", serialized); + free(serialized); + cJSON_Delete(pJson); + } + + syncPingDestroy(pSyncPing); + syncPingDestroy(pSyncPing2); + free(buf); + + return 0; +}