From 8aaf9b4074c0c685ddc6ebb36e3e3eab951607d1 Mon Sep 17 00:00:00 2001 From: Fancy code <258828110.@qq.com> Date: Thu, 18 Apr 2024 22:35:31 +0800 Subject: [PATCH] Improve:SharedData module. --- test/utils/SharedData/src/SharedData_Test.cpp | 58 ++-- utils/ConfigBase/src/IConfigBase.cpp | 2 +- utils/SharedData/CMakeLists.txt | 2 - utils/SharedData/include/SharedData.h | 107 +++--- utils/SharedData/src/ISharedData.cpp | 51 +++ utils/SharedData/src/ISharedData.h | 41 +++ utils/SharedData/src/SharedData.cpp | 92 +++-- utils/SharedData/src/SharedDataImpl.cpp | 320 ++++++++---------- utils/SharedData/src/SharedDataImpl.h | 131 ++++--- 9 files changed, 439 insertions(+), 365 deletions(-) create mode 100644 utils/SharedData/src/ISharedData.cpp create mode 100644 utils/SharedData/src/ISharedData.h diff --git a/test/utils/SharedData/src/SharedData_Test.cpp b/test/utils/SharedData/src/SharedData_Test.cpp index b419d96e..0a39f4fb 100644 --- a/test/utils/SharedData/src/SharedData_Test.cpp +++ b/test/utils/SharedData/src/SharedData_Test.cpp @@ -24,22 +24,22 @@ TEST(SharedDataTest, Demo) CreateLogModule(); ILogInit(LOG_INSTANCE_TYPE_END); constexpr int PROJECT_ID = 9; - SharedData *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID); - SharedData *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID); + void *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID); + void *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID); if (nullptr != sharedDataMinor) { int writableData = 99; - sharedDataMinor->mMakeSharedData(sharedDataMinor, sizeof(int), sizeof(int)); - sharedDataMinor->mSetWritableData(sharedDataMinor, &writableData, sizeof(int)); + IMakeSharedData(sharedDataMinor, sizeof(int), sizeof(int)); + ISetWritableData(sharedDataMinor, &writableData, sizeof(int)); } if (nullptr != sharedDataPrimary) { int readableData = -1; - sharedDataPrimary->mMakeSharedData(sharedDataPrimary, sizeof(int), sizeof(int)); - sharedDataPrimary->mGetReadableData(sharedDataPrimary, &readableData, sizeof(int)); + IMakeSharedData(sharedDataPrimary, sizeof(int), sizeof(int)); + IGetReadableData(sharedDataPrimary, &readableData, sizeof(int)); LogInfo("readableData = %d\n", readableData); - sharedDataPrimary->mFree(sharedDataPrimary); + IShareDataFree(sharedDataPrimary); } if (nullptr != sharedDataMinor) { - sharedDataMinor->mFree(sharedDataMinor); + IShareDataFree(sharedDataMinor); } ILogUnInit(); } @@ -50,8 +50,8 @@ TEST(SharedDataTest, Demo2) CreateLogModule(); ILogInit(LOG_INSTANCE_TYPE_END); constexpr int PROJECT_ID = 9; - SharedData *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID); - SharedData *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID); + void *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID); + void *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID); if (nullptr != sharedDataMinor) { IMakeSharedData(sharedDataMinor, sizeof(int), sizeof(int)); ISetWritableData(sharedDataMinor, (void *)&WRITABLE_DATA, sizeof(int)); @@ -76,8 +76,8 @@ TEST(SharedDataTest, Demo3) CreateLogModule(); ILogInit(LOG_INSTANCE_TYPE_END); constexpr int PROJECT_ID = 9; - SharedData *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID); - SharedData *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID); + void *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID); + void *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID); if (nullptr != sharedDataMinor) { constexpr int WRITABLE_DATA_LENGTH = 9; char writableData[WRITABLE_DATA_LENGTH] = {0}; @@ -107,8 +107,8 @@ TEST(SharedDataTest, Demo4) CreateLogModule(); ILogInit(LOG_INSTANCE_TYPE_END); constexpr int PROJECT_ID = 9; - SharedData *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID); - SharedData *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID); + void *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID); + void *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID); if (nullptr != sharedDataMinor) { IMakeSharedData(sharedDataMinor, sizeof(int) * 2, sizeof(int)); ISetWritableData(sharedDataMinor, (void *)&WRITABLE_DATA, sizeof(int)); @@ -135,7 +135,7 @@ TEST(SharedDataTest, Demo5) CreateLogModule(); ILogInit(LOG_INSTANCE_TYPE_END); constexpr int PROJECT_ID = 9; - SharedData *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID); + void *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID); if (nullptr != sharedDataPrimary) { constexpr int DEFAULT_DATA = 0; int readableData = DEFAULT_DATA; @@ -155,8 +155,8 @@ TEST(SharedDataTest, Demo6) CreateLogModule(); ILogInit(LOG_INSTANCE_TYPE_END); constexpr int PROJECT_ID = 9; - SharedData *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID); - SharedData *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID); + void *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID); + void *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID); if (nullptr != sharedDataPrimary) { constexpr int DEFAULT_DATA = 0; int readableData = DEFAULT_DATA; @@ -180,8 +180,8 @@ TEST(SharedDataTest, Demo7) CreateLogModule(); ILogInit(LOG_INSTANCE_TYPE_END); constexpr int PROJECT_ID = 9; - SharedData *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID); - SharedData *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID); + void *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID); + void *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID); if (nullptr != sharedDataPrimary) { IMakeSharedData(sharedDataPrimary, sizeof(int), sizeof(int)); ISetWritableData(sharedDataPrimary, (void *)&WRITABLE_DATA, sizeof(int)); @@ -209,8 +209,8 @@ TEST(SharedDataTest, UNIT_SharedData_DEME_Demo7) CreateLogModule(); ILogInit(LOG_INSTANCE_TYPE_END); constexpr int PROJECT_ID = 9; - SharedData *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID); - SharedData *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID); + void *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID); + void *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID); if (nullptr != sharedDataPrimary) { IMakeSharedData(sharedDataPrimary, sizeof(int) * 2, sizeof(int)); ISetWritableData(sharedDataPrimary, (void *)&WRITABLE_DATA, sizeof(int)); @@ -237,8 +237,8 @@ TEST(SharedDataTest, UNIT_SharedData_DEME_Demo8) CreateLogModule(); ILogInit(LOG_INSTANCE_TYPE_END); constexpr int PROJECT_ID = 9; - SharedData *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID); - SharedData *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID); + void *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID); + void *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID); if (nullptr != sharedDataPrimary) { int writableData[2] = {0}; IMakeSharedData(sharedDataPrimary, sizeof(int), sizeof(int) * 2); @@ -267,8 +267,8 @@ TEST(SharedDataTest, UNIT_SharedData_DEME_Demo9) CreateLogModule(); ILogInit(LOG_INSTANCE_TYPE_END); constexpr int PROJECT_ID = 9; - SharedData *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID); - SharedData *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID); + void *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID); + void *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID); if (nullptr != sharedDataPrimary) { IMakeSharedData(sharedDataPrimary, sizeof(int), sizeof(int)); ISetWritableData(sharedDataPrimary, (void *)&WRITABLE_DATA, sizeof(int)); @@ -296,8 +296,8 @@ TEST(SharedDataTest, UNIT_SharedData_DEME_Demo10) CreateLogModule(); ILogInit(LOG_INSTANCE_TYPE_END); constexpr int PROJECT_ID = 9; - SharedData *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID); - SharedData *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID); + void *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID); + void *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID); if (nullptr != sharedDataPrimary) { IMakeSharedData(sharedDataPrimary, sizeof(int), sizeof(int)); ISetWritableData(sharedDataPrimary, (void *)&WRITABLE_DATA, sizeof(int)); @@ -323,8 +323,8 @@ TEST(SharedDataTest, UNIT_SharedData_DEME_Demo11) CreateLogModule(); ILogInit(LOG_INSTANCE_TYPE_END); constexpr int PROJECT_ID = 9; - SharedData *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID); - SharedData *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID); + void *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID); + void *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID); IMakeSharedData(sharedDataMinor, sizeof(int), sizeof(int)); if (nullptr != sharedDataPrimary) { IMakeSharedData(sharedDataPrimary, sizeof(int), sizeof(int)); diff --git a/utils/ConfigBase/src/IConfigBase.cpp b/utils/ConfigBase/src/IConfigBase.cpp index 51a26bcc..2144913e 100644 --- a/utils/ConfigBase/src/IConfigBase.cpp +++ b/utils/ConfigBase/src/IConfigBase.cpp @@ -105,7 +105,7 @@ const char *GetConfigBaseModuleName(void) } std::shared_ptr *NewConfigBase(const char *fileName) { - LogInfo("Create the uart device object.\n"); + LogInfo("Create the config base object.\n"); ConfigBase *impl = (ConfigBase *)malloc(sizeof(ConfigBase)); if (nullptr == impl) { LogError("NewConfigBase::malloc failed.\n"); diff --git a/utils/SharedData/CMakeLists.txt b/utils/SharedData/CMakeLists.txt index f80f2eff..2bd1ce4f 100644 --- a/utils/SharedData/CMakeLists.txt +++ b/utils/SharedData/CMakeLists.txt @@ -13,8 +13,6 @@ include_directories( # ${EXTERNAL_SOURCE_PATH}/libconfig/libconfig-1.7.3/lib/.libs # ) - - aux_source_directory(./src SRC_FILES) set(TARGET_NAME SharedData) diff --git a/utils/SharedData/include/SharedData.h b/utils/SharedData/include/SharedData.h index 347feeef..0701a318 100644 --- a/utils/SharedData/include/SharedData.h +++ b/utils/SharedData/include/SharedData.h @@ -1,67 +1,42 @@ -/* - * Copyright (c) 2023 Fancy Code. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#ifndef SHARED_DATA_H -#define SHARED_DATA_H -#include "StatusCode.h" -#ifdef __cplusplus -extern "C" { -#endif -enum SHARED_DATA_CODE -{ - SHARED_DATA_CODE_INIT_FAILED = STATUS_CODE_END, - SHARED_DATA_CODE_WRONG_PEER_PARAMETERS, - SHARED_DATA_CODE_END -}; -enum SHARER_NAME -{ - SHARER_NAME_PRIMARY = 0, - SHARER_NAME_MINOR, - SHARER_NAME_END -}; -typedef struct shared_data SharedData; -typedef struct shared_data -{ - const StatusCode (*mMakeSharedData)(SharedData *, const unsigned int, const unsigned int); - const StatusCode (*mCleanSharedData)(SharedData *); - const StatusCode (*mGetReadableData)(SharedData *, void *, const unsigned int); - void (*mSetWritableData)(SharedData *, void *, const unsigned int); - void (*mFree)(void *); -} SharedData; -SharedData *CreateSharedData(const enum SHARER_NAME name, const char *path, const int projectId); -static inline const StatusCode IMakeSharedData(SharedData *object, const unsigned int readableSize, - const unsigned int writableSize) -{ - return object->mMakeSharedData(object, readableSize, writableSize); -} -static inline const StatusCode ICleanSharedData(SharedData *object) -{ - return object->mCleanSharedData(object); -} -static inline const StatusCode IGetReadableData(SharedData *object, void *buf, const unsigned int bufLength) -{ - return object->mGetReadableData(object, buf, bufLength); -} -static inline void ISetWritableData(SharedData *object, void *buf, const unsigned int bufLength) -{ - object->mSetWritableData(object, buf, bufLength); -} -static inline void IShareDataFree(SharedData *object) -{ - object->mFree(object); -} -#ifdef __cplusplus -} -#endif +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef SHARED_DATA_H +#define SHARED_DATA_H +#include "StatusCode.h" +#ifdef __cplusplus +extern "C" { +#endif +enum SHARED_DATA_CODE +{ + SHARED_DATA_CODE_INIT_FAILED = STATUS_CODE_END, + SHARED_DATA_CODE_WRONG_PEER_PARAMETERS, + SHARED_DATA_CODE_END +}; +enum SHARER_NAME +{ + SHARER_NAME_PRIMARY = 0, + SHARER_NAME_MINOR, + SHARER_NAME_END +}; +void *CreateSharedData(const enum SHARER_NAME name, const char *path, const int projectId); +const StatusCode IMakeSharedData(void *object, const unsigned int readableSize, const unsigned int writableSize); +const StatusCode ICleanSharedData(void *object); +const StatusCode IGetReadableData(void *object, void *buf, const unsigned int bufLength); +void ISetWritableData(void *object, void *buf, const unsigned int bufLength); +void IShareDataFree(void *object); +#ifdef __cplusplus +} +#endif #endif \ No newline at end of file diff --git a/utils/SharedData/src/ISharedData.cpp b/utils/SharedData/src/ISharedData.cpp new file mode 100644 index 00000000..d5c18d32 --- /dev/null +++ b/utils/SharedData/src/ISharedData.cpp @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "ISharedData.h" +#include "ILog.h" +#include "SharedDataImpl.h" // TODO: 互相包含,需要修改 +#include +void ISharedData::MakeSharedMemory(const unsigned int readableSize, const unsigned int writableSize) +{ +} +const StatusCode ISharedData::GetReadableMemory(void *buf, const unsigned int &bufLength) +{ + return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); +} +void ISharedData::SetWritableMemory(void *buf, const unsigned int &bufLength) +{ +} +StatusCode ISharedData::FreeSharedMemory(void) +{ + return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); +} +static const char *SHARED_DATA_NAME = "shared_data"; +const char *GetSharedDataModuleName(void) +{ + return SHARED_DATA_NAME; +} +std::shared_ptr *NewSharedData(const enum SHARER_NAME &name, const char *path, const int &projectId) +{ + LogInfo("Create the shared data object.\n"); + SharedData *impl = (SharedData *)malloc(sizeof(SharedData)); + if (nullptr == impl) { + LogError("NewConfigBase::malloc failed.\n"); + return nullptr; + } + SharedData tmp; + memcpy((void *)impl, (void *)&tmp, sizeof(SharedData)); + impl->mHeader.mCheckName = SHARED_DATA_NAME; + impl->mISharedData = std::make_shared(name, path, projectId); + return (std::shared_ptr *)(((char *)impl) + sizeof(ISharedDataHeader)); +} \ No newline at end of file diff --git a/utils/SharedData/src/ISharedData.h b/utils/SharedData/src/ISharedData.h new file mode 100644 index 00000000..93d7ec66 --- /dev/null +++ b/utils/SharedData/src/ISharedData.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef I_SHARED_DATA_H +#define I_SHARED_DATA_H +#include "SharedData.h" +#include "StatusCode.h" +#include +class ISharedData +{ +public: + ISharedData() = default; + virtual ~ISharedData() = default; + virtual void MakeSharedMemory(const unsigned int readableSize, const unsigned int writableSize); + virtual const StatusCode GetReadableMemory(void *buf, const unsigned int &bufLength); + virtual void SetWritableMemory(void *buf, const unsigned int &bufLength); + virtual StatusCode FreeSharedMemory(void); +}; +typedef struct i_shared_data_header +{ + const char *mCheckName; +} ISharedDataHeader; +typedef struct config_base +{ + ISharedDataHeader mHeader; + std::shared_ptr mISharedData; +} SharedData; +const char *GetSharedDataModuleName(void); +std::shared_ptr *NewSharedData(const enum SHARER_NAME &name, const char *path, const int &projectId); +#endif \ No newline at end of file diff --git a/utils/SharedData/src/SharedData.cpp b/utils/SharedData/src/SharedData.cpp index 143069aa..211f204a 100644 --- a/utils/SharedData/src/SharedData.cpp +++ b/utils/SharedData/src/SharedData.cpp @@ -1,20 +1,74 @@ -/* - * Copyright (c) 2023 Fancy Code. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#include "SharedData.h" -#include "SharedDataImpl.h" -SharedData *CreateSharedData(const enum SHARER_NAME name, const char *path, const int projectId) -{ - return (SharedData *)NewSharedDataImpl(name, path, projectId); +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "SharedData.h" +#include "ILog.h" +#include "ISharedData.h" +static bool ObjectCheck(void *object) +{ + if (nullptr == object) { + LogError("nullptr object!\n"); + return false; + } + if (*((const char **)(((char *)object) - sizeof(ISharedDataHeader))) != GetSharedDataModuleName()) { + LogError("Illegal object!\n"); + return false; + } + return true; +} +void *CreateSharedData(const enum SHARER_NAME name, const char *path, const int projectId) +{ + std::shared_ptr *sharedDataObject = NewSharedData(name, path, projectId); + if (nullptr != sharedDataObject) { + // (*sharedDataObject)->OpenConfigFile(); + } + return sharedDataObject; +} +const StatusCode IMakeSharedData(void *object, const unsigned int readableSize, const unsigned int writableSize) +{ + if (ObjectCheck(object) == true) { + (*(std::shared_ptr *)object)->MakeSharedMemory(readableSize, writableSize); + return CreateStatusCode(STATUS_CODE_OK); + } + return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); +} +const StatusCode ICleanSharedData(void *object) +{ + if (ObjectCheck(object) == true) { + // (*(std::shared_ptr *)object)->MakeSharedMemory(readableSize, writableSize); + return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); + } + return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); +} +const StatusCode IGetReadableData(void *object, void *buf, const unsigned int bufLength) +{ + if (ObjectCheck(object) == true) { + return (*(std::shared_ptr *)object)->GetReadableMemory(buf, bufLength); + } + return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); +} +void ISetWritableData(void *object, void *buf, const unsigned int bufLength) +{ + if (ObjectCheck(object) == true) { + (*(std::shared_ptr *)object)->SetWritableMemory(buf, bufLength); + } +} +void IShareDataFree(void *object) +{ + if (ObjectCheck(object) == true) { + (*(std::shared_ptr *)object)->FreeSharedMemory(); + (*(std::shared_ptr *)object).reset(); + free(((char *)object) - sizeof(ISharedDataHeader)); + } } \ No newline at end of file diff --git a/utils/SharedData/src/SharedDataImpl.cpp b/utils/SharedData/src/SharedDataImpl.cpp index 2b109c47..a3dbdaba 100644 --- a/utils/SharedData/src/SharedDataImpl.cpp +++ b/utils/SharedData/src/SharedDataImpl.cpp @@ -1,180 +1,142 @@ -/* - * Copyright (c) 2023 Fancy Code. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#include "SharedDataImpl.h" -#include "ILog.h" -#include "SharedDataCode.h" -#include -static const char *SHARED_DATA_NAME = "shared_data"; -constexpr short THERE_TWO_USER_DATA_HEADER = 2; -SharedDataCpp::SharedDataCpp(const SHARER_NAME &sharerName, const char *path, const int &projectId) - : SharedMemory(path, projectId), mSharerName(sharerName) -{ - mPrimaryReadSize = 0; - mMinorReadSize = 0; - mSharedMemeory = nullptr; -} -void SharedDataCpp::MakeSharedMemory(const unsigned int readableSize, const unsigned int writableSize) -{ - if (SHARER_NAME_PRIMARY == mSharerName) { - mPrimaryReadSize = readableSize; - mMinorReadSize = writableSize; - } - else if (SHARER_NAME_MINOR == mSharerName) { - mPrimaryReadSize = writableSize; - mMinorReadSize = readableSize; - } - else { - LogError("Make shared memory failed.\n"); - return; - } - const int SHARED_MEMORY_SIZE = readableSize + writableSize + sizeof(UserDataHeader) * THERE_TWO_USER_DATA_HEADER; - SharedMemory::MakeSharedMemory(SHARED_MEMORY_SIZE); - mSharedMemeory = SharedMemory::GetMemory(); - WritableDataInit(); -} -void SharedDataCpp::WritableDataInit(void) -{ - if (nullptr == mSharedMemeory) { - LogError("mSharedMemeory is nullptr, failed.\n"); - return; - } - if (SHARER_NAME_PRIMARY == mSharerName) { - UserDataHeader *writableHeader = - (UserDataHeader *)((char *)mSharedMemeory + sizeof(UserDataHeader) + mPrimaryReadSize); - memcpy(writableHeader->mUserName, USER_NAME_INIT_NAME, USER_NAME_BUF_LENGTH); - writableHeader->mDataLength = mMinorReadSize; - } - else if (SHARER_NAME_MINOR == mSharerName) { - UserDataHeader *writableHeader = (UserDataHeader *)((char *)mSharedMemeory); - memcpy(writableHeader->mUserName, USER_NAME_INIT_NAME, USER_NAME_BUF_LENGTH); - writableHeader->mDataLength = mPrimaryReadSize; - } - else { - LogError("Make shared memory failed.\n"); - return; - } -} -const StatusCode SharedDataCpp::GetReadableMemory(void *buf, const unsigned int &bufLength) -{ - if (nullptr == mSharedMemeory) { - LogError("mSharedMemeory is nullptr, failed.\n"); - return CreateStatusCode(STATUS_CODE_NOT_OK); - } - if (SHARER_NAME_PRIMARY == mSharerName && bufLength == mPrimaryReadSize) { - UserDataHeader *readableHeader = (UserDataHeader *)((char *)mSharedMemeory); - LogInfo("Want to read %d and can be read %d\n", bufLength, readableHeader->mDataLength); - if (memcmp(readableHeader->mUserName, USER_NAME_INIT_NAME, USER_NAME_BUF_LENGTH) == 0 && - bufLength == readableHeader->mDataLength) { - memcpy(buf, (char *)mSharedMemeory + sizeof(UserDataHeader), bufLength); - } - else { - LogError("Readable memory didn't init yet or init error.\n"); - return CreateSharedDataCode(SHARED_DATA_CODE_WRONG_PEER_PARAMETERS); - } - } - else if (SHARER_NAME_MINOR == mSharerName && bufLength == mMinorReadSize) { - UserDataHeader *readableHeader = - (UserDataHeader *)((char *)mSharedMemeory + sizeof(UserDataHeader) + mPrimaryReadSize); - LogInfo("Want to read %d and can be read %d\n", bufLength, readableHeader->mDataLength); - if (memcmp(readableHeader->mUserName, USER_NAME_INIT_NAME, USER_NAME_BUF_LENGTH) == 0 && - bufLength == readableHeader->mDataLength) { - memcpy(buf, - (char *)mSharedMemeory + sizeof(UserDataHeader) * THERE_TWO_USER_DATA_HEADER + mPrimaryReadSize, - bufLength); - } - else { - LogError("Readable memory didn't init yet or init error.\n"); - return CreateSharedDataCode(SHARED_DATA_CODE_WRONG_PEER_PARAMETERS); - } - } - else { - LogError("Get readable memory failed.\n"); - return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); - } - return CreateStatusCode(STATUS_CODE_OK); -} -void SharedDataCpp::SetWritableMemory(void *buf, const unsigned int &bufLength) -{ - if (nullptr == mSharedMemeory) { - LogError("mSharedMemeory is nullptr, failed.\n"); - return; - } - if (SHARER_NAME_PRIMARY == mSharerName && bufLength == mMinorReadSize) { - UserDataHeader *readableHeader = (UserDataHeader *)((char *)mSharedMemeory); - if (memcmp(readableHeader->mUserName, USER_NAME_INIT_NAME, USER_NAME_BUF_LENGTH) == 0) { - if (mPrimaryReadSize == readableHeader->mDataLength) { - memcpy((char *)mSharedMemeory + sizeof(UserDataHeader) * THERE_TWO_USER_DATA_HEADER + mPrimaryReadSize, - buf, - bufLength); - } - else { - LogError("Peer not match.\n"); - } - } - else { - memcpy((char *)mSharedMemeory + sizeof(UserDataHeader) * THERE_TWO_USER_DATA_HEADER + mPrimaryReadSize, - buf, - bufLength); - } - } - else if (SHARER_NAME_MINOR == mSharerName && bufLength == mPrimaryReadSize) { - memcpy((char *)mSharedMemeory + sizeof(UserDataHeader), buf, bufLength); - } - else { - LogError("Set writable memory failed.\n"); - } -} -static const StatusCode MakeSharedData(SharedData *object, const unsigned int readableSize, - const unsigned int writableSize) -{ - SharedDataImpl *impl = ((SharedDataImpl *)(((char *)object) - sizeof(SharedDataHeader))); - impl->mSharedData->MakeSharedMemory(readableSize, writableSize); - return CreateStatusCode(STATUS_CODE_OK); -} -static const StatusCode GetSharedReadableMemory(SharedData *object, void *buf, const unsigned int bufLength) -{ - SharedDataImpl *impl = ((SharedDataImpl *)(((char *)object) - sizeof(SharedDataHeader))); - return impl->mSharedData->GetReadableMemory(buf, bufLength); -} -static void SetSharedWritableMemory(SharedData *object, void *buf, const unsigned int bufLength) -{ - SharedDataImpl *impl = ((SharedDataImpl *)(((char *)object) - sizeof(SharedDataHeader))); - impl->mSharedData->SetWritableMemory(buf, bufLength); -} -static void SharedDataImplFree(void *ptr) -{ - SharedDataImpl *object = ((SharedDataImpl *)(((char *)ptr) - sizeof(SharedDataHeader))); - if (SHARED_DATA_NAME == object->mHeader.mCheckName) { - object->mSharedData->CleanSharedMemory(); - object->mSharedData = nullptr; - free(((char *)ptr) - sizeof(SharedDataHeader)); - } - else { - LogError("Unknow ptr.\n"); - } -} -SharedData *NewSharedDataImpl(const SHARER_NAME &name, const char *path, const int &projectId) -{ - SharedDataImpl *impl = (SharedDataImpl *)malloc(sizeof(SharedDataImpl)); - SharedDataImpl tmp; - memcpy((void *)impl, (void *)&tmp, sizeof(SharedDataImpl)); - impl->mHeader.mCheckName = SHARED_DATA_NAME; - impl->mBase.mMakeSharedData = MakeSharedData; - impl->mBase.mGetReadableData = GetSharedReadableMemory; - impl->mBase.mSetWritableData = SetSharedWritableMemory; - impl->mBase.mFree = SharedDataImplFree; - impl->mSharedData = std::make_shared(name, path, projectId); - return (SharedData *)(((char *)impl) + sizeof(SharedDataHeader)); +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "SharedDataImpl.h" +#include "ILog.h" +#include "SharedDataCode.h" +#include +static const char *SHARED_DATA_NAME = "shared_data"; +constexpr short THERE_TWO_USER_DATA_HEADER = 2; +SharedDataImpl::SharedDataImpl(const SHARER_NAME &sharerName, const char *path, const int &projectId) + : SharedMemory(path, projectId), mSharerName(sharerName) +{ + mPrimaryReadSize = 0; + mMinorReadSize = 0; + mSharedMemeory = nullptr; +} +void SharedDataImpl::MakeSharedMemory(const unsigned int readableSize, const unsigned int writableSize) +{ + if (SHARER_NAME_PRIMARY == mSharerName) { + mPrimaryReadSize = readableSize; + mMinorReadSize = writableSize; + } + else if (SHARER_NAME_MINOR == mSharerName) { + mPrimaryReadSize = writableSize; + mMinorReadSize = readableSize; + } + else { + LogError("Make shared memory failed.\n"); + return; + } + const int SHARED_MEMORY_SIZE = readableSize + writableSize + sizeof(UserDataHeader) * THERE_TWO_USER_DATA_HEADER; + SharedMemory::MakeSharedMemory(SHARED_MEMORY_SIZE); + mSharedMemeory = SharedMemory::GetMemory(); + WritableDataInit(); +} +const StatusCode SharedDataImpl::GetReadableMemory(void *buf, const unsigned int &bufLength) +{ + if (nullptr == mSharedMemeory) { + LogError("mSharedMemeory is nullptr, failed.\n"); + return CreateStatusCode(STATUS_CODE_NOT_OK); + } + if (SHARER_NAME_PRIMARY == mSharerName && bufLength == mPrimaryReadSize) { + UserDataHeader *readableHeader = (UserDataHeader *)((char *)mSharedMemeory); + LogInfo("Want to read %d and can be read %d\n", bufLength, readableHeader->mDataLength); + if (memcmp(readableHeader->mUserName, USER_NAME_INIT_NAME, USER_NAME_BUF_LENGTH) == 0 && + bufLength == readableHeader->mDataLength) { + memcpy(buf, (char *)mSharedMemeory + sizeof(UserDataHeader), bufLength); + } + else { + LogError("Readable memory didn't init yet or init error.\n"); + return CreateSharedDataCode(SHARED_DATA_CODE_WRONG_PEER_PARAMETERS); + } + } + else if (SHARER_NAME_MINOR == mSharerName && bufLength == mMinorReadSize) { + UserDataHeader *readableHeader = + (UserDataHeader *)((char *)mSharedMemeory + sizeof(UserDataHeader) + mPrimaryReadSize); + LogInfo("Want to read %d and can be read %d\n", bufLength, readableHeader->mDataLength); + if (memcmp(readableHeader->mUserName, USER_NAME_INIT_NAME, USER_NAME_BUF_LENGTH) == 0 && + bufLength == readableHeader->mDataLength) { + memcpy(buf, + (char *)mSharedMemeory + sizeof(UserDataHeader) * THERE_TWO_USER_DATA_HEADER + mPrimaryReadSize, + bufLength); + } + else { + LogError("Readable memory didn't init yet or init error.\n"); + return CreateSharedDataCode(SHARED_DATA_CODE_WRONG_PEER_PARAMETERS); + } + } + else { + LogError("Get readable memory failed.\n"); + return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER); + } + return CreateStatusCode(STATUS_CODE_OK); +} +void SharedDataImpl::SetWritableMemory(void *buf, const unsigned int &bufLength) +{ + if (nullptr == mSharedMemeory) { + LogError("mSharedMemeory is nullptr, failed.\n"); + return; + } + if (SHARER_NAME_PRIMARY == mSharerName && bufLength == mMinorReadSize) { + UserDataHeader *readableHeader = (UserDataHeader *)((char *)mSharedMemeory); + if (memcmp(readableHeader->mUserName, USER_NAME_INIT_NAME, USER_NAME_BUF_LENGTH) == 0) { + if (mPrimaryReadSize == readableHeader->mDataLength) { + memcpy((char *)mSharedMemeory + sizeof(UserDataHeader) * THERE_TWO_USER_DATA_HEADER + mPrimaryReadSize, + buf, + bufLength); + } + else { + LogError("Peer not match.\n"); + } + } + else { + memcpy((char *)mSharedMemeory + sizeof(UserDataHeader) * THERE_TWO_USER_DATA_HEADER + mPrimaryReadSize, + buf, + bufLength); + } + } + else if (SHARER_NAME_MINOR == mSharerName && bufLength == mPrimaryReadSize) { + memcpy((char *)mSharedMemeory + sizeof(UserDataHeader), buf, bufLength); + } + else { + LogError("Set writable memory failed.\n"); + } +} +StatusCode SharedDataImpl::FreeSharedMemory(void) +{ + return CleanSharedMemory(); +} +void SharedDataImpl::WritableDataInit(void) +{ + if (nullptr == mSharedMemeory) { + LogError("mSharedMemeory is nullptr, failed.\n"); + return; + } + if (SHARER_NAME_PRIMARY == mSharerName) { + UserDataHeader *writableHeader = + (UserDataHeader *)((char *)mSharedMemeory + sizeof(UserDataHeader) + mPrimaryReadSize); + memcpy(writableHeader->mUserName, USER_NAME_INIT_NAME, USER_NAME_BUF_LENGTH); + writableHeader->mDataLength = mMinorReadSize; + } + else if (SHARER_NAME_MINOR == mSharerName) { + UserDataHeader *writableHeader = (UserDataHeader *)((char *)mSharedMemeory); + memcpy(writableHeader->mUserName, USER_NAME_INIT_NAME, USER_NAME_BUF_LENGTH); + writableHeader->mDataLength = mPrimaryReadSize; + } + else { + LogError("Make shared memory failed.\n"); + return; + } } \ No newline at end of file diff --git a/utils/SharedData/src/SharedDataImpl.h b/utils/SharedData/src/SharedDataImpl.h index 41072b51..9abf720d 100644 --- a/utils/SharedData/src/SharedDataImpl.h +++ b/utils/SharedData/src/SharedDataImpl.h @@ -1,70 +1,63 @@ -/* - * Copyright (c) 2023 Fancy Code. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#ifndef SHARED_DATA_IMPL_H -#define SHARED_DATA_IMPL_H -#include "SharedData.h" -#include "SharedMemory.h" -#include -#include -constexpr int USER_NAME_BUF_LENGTH = 4; -#define USER_NAME_INIT_NAME "XING" -enum USER_DATA_INIT -{ - USER_DATA_INIT_SUCCEED = 0, - USER_DATA_INIT_FAILED, - USER_DATA_INIT_END -}; -typedef struct shared_data_header -{ - const char *mCheckName; -} SharedDataHeader; -// TODO: Use mutex to luck memory data? -// typedef struct shared_memory_header -// { -// std::mutex mMutex; -// }; -typedef struct user_data_header -{ - char mUserName[USER_NAME_BUF_LENGTH]; - unsigned int mDataLength; - int mIsInit; -} UserDataHeader; -class SharedDataCpp : public SharedMemory -{ -public: - SharedDataCpp(const SHARER_NAME &sharerName, const char *path, const int &projectId); - virtual ~SharedDataCpp() = default; - void MakeSharedMemory(const unsigned int readableSize, const unsigned int writableSize); - const StatusCode GetReadableMemory(void *buf, const unsigned int &bufLength); - void SetWritableMemory(void *buf, const unsigned int &bufLength); - -private: - void WritableDataInit(void); - -private: - const SHARER_NAME mSharerName; - unsigned int mPrimaryReadSize; - unsigned int mMinorReadSize; - void *mSharedMemeory; -}; -typedef struct shared_data_impl SharedDataImpl; -typedef struct shared_data_impl -{ - SharedDataHeader mHeader; - SharedData mBase; - std::shared_ptr mSharedData; -} SharedDataImpl; -SharedData *NewSharedDataImpl(const SHARER_NAME &name, const char *path, const int &projectId); +/* + * Copyright (c) 2023 Fancy Code. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef SHARED_DATA_IMPL_H +#define SHARED_DATA_IMPL_H +#include "ISharedData.h" +#include "SharedMemory.h" +#include +// #include +constexpr int USER_NAME_BUF_LENGTH = 4; +#define USER_NAME_INIT_NAME "XING" +enum USER_DATA_INIT +{ + USER_DATA_INIT_SUCCEED = 0, + USER_DATA_INIT_FAILED, + USER_DATA_INIT_END +}; +typedef struct shared_data_header +{ + const char *mCheckName; +} SharedDataHeader; +// TODO: Use mutex to luck memory data? +// typedef struct shared_memory_header +// { +// std::mutex mMutex; +// }; +typedef struct user_data_header +{ + char mUserName[USER_NAME_BUF_LENGTH]; + unsigned int mDataLength; + int mIsInit; +} UserDataHeader; +class SharedDataImpl : public ISharedData, public SharedMemory +{ +public: + SharedDataImpl(const SHARER_NAME &sharerName, const char *path, const int &projectId); + virtual ~SharedDataImpl() = default; + void MakeSharedMemory(const unsigned int readableSize, const unsigned int writableSize) override; + const StatusCode GetReadableMemory(void *buf, const unsigned int &bufLength) override; + void SetWritableMemory(void *buf, const unsigned int &bufLength) override; + StatusCode FreeSharedMemory(void) override; + +private: + void WritableDataInit(void); + +private: + const SHARER_NAME mSharerName; + unsigned int mPrimaryReadSize; + unsigned int mMinorReadSize; + void *mSharedMemeory; +}; #endif \ No newline at end of file