Shared data module format.
This commit is contained in:
parent
56f399dd15
commit
893319acb6
|
@ -4,7 +4,7 @@ Language: Cpp
|
||||||
AccessModifierOffset: -4
|
AccessModifierOffset: -4
|
||||||
AlignAfterOpenBracket: Align
|
AlignAfterOpenBracket: Align
|
||||||
AlignConsecutiveMacros: false
|
AlignConsecutiveMacros: false
|
||||||
AlignConsecutiveAssignments: false
|
AlignConsecutiveAssignments: true
|
||||||
AlignConsecutiveDeclarations: false
|
AlignConsecutiveDeclarations: false
|
||||||
AlignEscapedNewlines: Right
|
AlignEscapedNewlines: Right
|
||||||
AlignOperands: true
|
AlignOperands: true
|
||||||
|
@ -22,17 +22,17 @@ AlwaysBreakAfterDefinitionReturnType: None
|
||||||
AlwaysBreakAfterReturnType: None
|
AlwaysBreakAfterReturnType: None
|
||||||
AlwaysBreakBeforeMultilineStrings: false
|
AlwaysBreakBeforeMultilineStrings: false
|
||||||
AlwaysBreakTemplateDeclarations: MultiLine
|
AlwaysBreakTemplateDeclarations: MultiLine
|
||||||
BinPackArguments: true
|
BinPackArguments: false
|
||||||
BinPackParameters: true
|
BinPackParameters: true
|
||||||
BraceWrapping:
|
BraceWrapping:
|
||||||
AfterCaseLabel: false
|
AfterCaseLabel: false
|
||||||
AfterClass: false
|
AfterClass: true
|
||||||
AfterControlStatement: false
|
AfterControlStatement: false
|
||||||
AfterEnum: true
|
AfterEnum: true
|
||||||
AfterFunction: true
|
AfterFunction: true
|
||||||
AfterNamespace: false
|
AfterNamespace: false
|
||||||
AfterObjCDeclaration: true
|
AfterObjCDeclaration: true
|
||||||
AfterStruct: false
|
AfterStruct: true
|
||||||
AfterUnion: false
|
AfterUnion: false
|
||||||
AfterExternBlock: false
|
AfterExternBlock: false
|
||||||
BeforeCatch: false
|
BeforeCatch: false
|
||||||
|
@ -50,7 +50,7 @@ BreakConstructorInitializersBeforeComma: false
|
||||||
BreakConstructorInitializers: BeforeColon
|
BreakConstructorInitializers: BeforeColon
|
||||||
BreakAfterJavaFieldAnnotations: false
|
BreakAfterJavaFieldAnnotations: false
|
||||||
BreakStringLiterals: true
|
BreakStringLiterals: true
|
||||||
ColumnLimit: 80
|
ColumnLimit: 120
|
||||||
CommentPragmas: '^ IWYU pragma:'
|
CommentPragmas: '^ IWYU pragma:'
|
||||||
CompactNamespaces: false
|
CompactNamespaces: false
|
||||||
ConstructorInitializerAllOnOneLineOrOnePerLine: false
|
ConstructorInitializerAllOnOneLineOrOnePerLine: false
|
||||||
|
|
|
@ -33,10 +33,19 @@ add_custom_target(
|
||||||
-p ${CMAKE_SOURCE_DIR_IPCSDK}/cmake-shell
|
-p ${CMAKE_SOURCE_DIR_IPCSDK}/cmake-shell
|
||||||
WORKING_DIRECTORY ${UTILS_SOURCE_PATH}/SharedData
|
WORKING_DIRECTORY ${UTILS_SOURCE_PATH}/SharedData
|
||||||
)
|
)
|
||||||
|
file(GLOB_RECURSE HEADER_FILES *.h)
|
||||||
|
add_custom_target(
|
||||||
|
SharedData_code_format
|
||||||
|
COMMAND ${CLANG_FORMAT_EXE}
|
||||||
|
-style=file
|
||||||
|
-i ${SRC_FILES} ${HEADER_FILES}
|
||||||
|
WORKING_DIRECTORY ${UTILS_SOURCE_PATH}/SharedData
|
||||||
|
)
|
||||||
add_custom_command(
|
add_custom_command(
|
||||||
TARGET ${TARGET_NAME}
|
TARGET ${TARGET_NAME}
|
||||||
PRE_BUILD
|
PRE_BUILD
|
||||||
COMMAND make SharedData_code_check
|
COMMAND make SharedData_code_check
|
||||||
|
COMMAND make SharedData_code_format
|
||||||
WORKING_DIRECTORY ${PROJECT_ROOT_PATH}/cmake-shell/
|
WORKING_DIRECTORY ${PROJECT_ROOT_PATH}/cmake-shell/
|
||||||
)
|
)
|
||||||
endif()
|
endif()
|
||||||
|
|
|
@ -16,53 +16,45 @@
|
||||||
#define SHARED_DATA_H
|
#define SHARED_DATA_H
|
||||||
#include "StatusCode.h"
|
#include "StatusCode.h"
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C"
|
extern "C" {
|
||||||
{
|
|
||||||
#endif
|
#endif
|
||||||
enum SHARED_DATA_CODE
|
enum SHARED_DATA_CODE
|
||||||
{
|
{
|
||||||
SHARED_DATA_CODE_INIT_FAILED = STATUS_CODE_END,
|
SHARED_DATA_CODE_INIT_FAILED = STATUS_CODE_END,
|
||||||
SHARED_DATA_CODE_WRONG_PEER_PARAMETERS,
|
SHARED_DATA_CODE_WRONG_PEER_PARAMETERS,
|
||||||
SHARED_DATA_CODE_END
|
SHARED_DATA_CODE_END
|
||||||
};
|
};
|
||||||
enum SHARER_NAME
|
enum SHARER_NAME
|
||||||
{
|
{
|
||||||
SHARER_NAME_PRIMARY = 0,
|
SHARER_NAME_PRIMARY = 0,
|
||||||
SHARER_NAME_MINOR,
|
SHARER_NAME_MINOR,
|
||||||
SHARER_NAME_END
|
SHARER_NAME_END
|
||||||
};
|
};
|
||||||
typedef struct shared_data SharedData;
|
typedef struct shared_data SharedData;
|
||||||
typedef struct shared_data
|
typedef struct shared_data
|
||||||
{
|
{
|
||||||
const StatusCode (*mMakeSharedData)(SharedData *, const unsigned int, const unsigned int);
|
const StatusCode (*mMakeSharedData)(SharedData *, const unsigned int, const unsigned int);
|
||||||
const StatusCode (*mCleanSharedData)(SharedData *);
|
const StatusCode (*mCleanSharedData)(SharedData *);
|
||||||
const StatusCode (*mGetReadableData)(SharedData *, void *, const unsigned int);
|
const StatusCode (*mGetReadableData)(SharedData *, void *, const unsigned int);
|
||||||
void (*mSetWritableData)(SharedData *, void *, const unsigned int);
|
void (*mSetWritableData)(SharedData *, void *, const unsigned int);
|
||||||
void (*mFree)(void *);
|
void (*mFree)(void *);
|
||||||
} SharedData;
|
} SharedData;
|
||||||
SharedData *CreateSharedData(const enum SHARER_NAME name, const char *path, const int projectId);
|
SharedData *CreateSharedData(const enum SHARER_NAME name, const char *path, const int projectId);
|
||||||
static inline const StatusCode IMakeSharedData(SharedData *object,
|
static inline const StatusCode IMakeSharedData(SharedData *object, const unsigned int readableSize,
|
||||||
const unsigned int readableSize,
|
const unsigned int writableSize)
|
||||||
const unsigned int writableSize)
|
{
|
||||||
{
|
return object->mMakeSharedData(object, readableSize, writableSize);
|
||||||
return object->mMakeSharedData(object, readableSize, writableSize);
|
}
|
||||||
}
|
static inline const StatusCode ICleanSharedData(SharedData *object) { return object->mCleanSharedData(object); }
|
||||||
static inline const StatusCode ICleanSharedData(SharedData *object)
|
static inline const StatusCode IGetReadableData(SharedData *object, void *buf, const unsigned int bufLength)
|
||||||
{
|
{
|
||||||
return object->mCleanSharedData(object);
|
return object->mGetReadableData(object, buf, bufLength);
|
||||||
}
|
}
|
||||||
static inline const StatusCode IGetReadableData(SharedData *object, void *buf, const unsigned int bufLength)
|
static inline void ISetWritableData(SharedData *object, void *buf, const unsigned int bufLength)
|
||||||
{
|
{
|
||||||
return object->mGetReadableData(object, buf, bufLength);
|
object->mSetWritableData(object, buf, bufLength);
|
||||||
}
|
}
|
||||||
static inline void ISetWritableData(SharedData *object, void *buf, const unsigned int bufLength)
|
static inline void IFree(SharedData *object) { object->mFree(object); }
|
||||||
{
|
|
||||||
object->mSetWritableData(object, buf, bufLength);
|
|
||||||
}
|
|
||||||
static inline void IFree(SharedData *object)
|
|
||||||
{
|
|
||||||
object->mFree(object);
|
|
||||||
}
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -16,13 +16,10 @@
|
||||||
#include "ILog.h"
|
#include "ILog.h"
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
static const char *StatusCodeString[SHARED_DATA_CODE_END - STATUS_CODE_END + 1] = {
|
static const char *StatusCodeString[SHARED_DATA_CODE_END - STATUS_CODE_END + 1] = {
|
||||||
"SHARED_DATA_CODE_INIT_FAILED",
|
"SHARED_DATA_CODE_INIT_FAILED", "SHARED_DATA_CODE_WRONG_PEER_PARAMETERS", "SHARED_DATA_CODE_END"};
|
||||||
"SHARED_DATA_CODE_WRONG_PEER_PARAMETERS",
|
|
||||||
"SHARED_DATA_CODE_END"};
|
|
||||||
static const char *_PrintStringCode_(const StatusCode this)
|
static const char *_PrintStringCode_(const StatusCode this)
|
||||||
{
|
{
|
||||||
if (STATUS_CODE_END <= this.mStatusCode && this.mStatusCode <= SHARED_DATA_CODE_END)
|
if (STATUS_CODE_END <= this.mStatusCode && this.mStatusCode <= SHARED_DATA_CODE_END) {
|
||||||
{
|
|
||||||
LogInfo("Status code = [ %s ]\n", StatusCodeString[this.mStatusCode - STATUS_CODE_END]);
|
LogInfo("Status code = [ %s ]\n", StatusCodeString[this.mStatusCode - STATUS_CODE_END]);
|
||||||
return StatusCodeString[this.mStatusCode - STATUS_CODE_END];
|
return StatusCodeString[this.mStatusCode - STATUS_CODE_END];
|
||||||
}
|
}
|
||||||
|
@ -31,32 +28,25 @@ static const char *_PrintStringCode_(const StatusCode this)
|
||||||
}
|
}
|
||||||
static const bool CodeEqual(const StatusCode code, const char *value)
|
static const bool CodeEqual(const StatusCode code, const char *value)
|
||||||
{
|
{
|
||||||
if (STATUS_CODE_END >= code.mStatusCode || code.mStatusCode >= SHARED_DATA_CODE_END)
|
if (STATUS_CODE_END >= code.mStatusCode || code.mStatusCode >= SHARED_DATA_CODE_END) {
|
||||||
{
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (strlen(value) != strlen(StatusCodeString[code.mStatusCode - STATUS_CODE_END]))
|
if (strlen(value) != strlen(StatusCodeString[code.mStatusCode - STATUS_CODE_END])) {
|
||||||
{
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (memcmp(value, StatusCodeString[code.mStatusCode - STATUS_CODE_END], strlen(value)) == 0)
|
if (memcmp(value, StatusCodeString[code.mStatusCode - STATUS_CODE_END], strlen(value)) == 0) {
|
||||||
{
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
static StatusCode NewStatusCode(const long int code)
|
static StatusCode NewStatusCode(const long int code)
|
||||||
{
|
{
|
||||||
StatusCode result = {
|
StatusCode result = {_PrintStringCode_, CodeEqual, code};
|
||||||
_PrintStringCode_,
|
|
||||||
CodeEqual,
|
|
||||||
code};
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
const StatusCode CreateSharedDataCode(const long int code)
|
const StatusCode CreateSharedDataCode(const long int code)
|
||||||
{
|
{
|
||||||
if (STATUS_CODE_END <= code && code <= SHARED_DATA_CODE_END)
|
if (STATUS_CODE_END <= code && code <= SHARED_DATA_CODE_END) {
|
||||||
{
|
|
||||||
return NewStatusCode(code);
|
return NewStatusCode(code);
|
||||||
}
|
}
|
||||||
LogError("undefined code.\n");
|
LogError("undefined code.\n");
|
||||||
|
|
|
@ -3,9 +3,9 @@
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
* You may obtain a copy of the License at
|
* You may obtain a copy of the License at
|
||||||
*
|
*
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
*
|
*
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
@ -18,22 +18,9 @@
|
||||||
#include "StatusCode.h"
|
#include "StatusCode.h"
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C"
|
extern "C" {
|
||||||
{
|
|
||||||
#endif
|
#endif
|
||||||
const StatusCode CreateSharedDataCode(const long int code);
|
const StatusCode CreateSharedDataCode(const long int code);
|
||||||
// static inline const char *PrintStringCode(const StatusCode code)
|
|
||||||
// {
|
|
||||||
// return code.mPrintStringCode(code);
|
|
||||||
// }
|
|
||||||
// static inline bool IsCodeOK(const StatusCode code)
|
|
||||||
// {
|
|
||||||
// return SHARED_DATA_CODE_OK == code.mStatusCode ? true : false;
|
|
||||||
// }
|
|
||||||
// static inline bool StatusCodeEqual(const StatusCode code, const char *value)
|
|
||||||
// {
|
|
||||||
// return code.mCodeEqual(code, value);
|
|
||||||
// }
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -16,29 +16,26 @@
|
||||||
#include "ILog.h"
|
#include "ILog.h"
|
||||||
#include "SharedDataCode.h"
|
#include "SharedDataCode.h"
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
static const char *SHARED_DATA_NAME = "shared_data";
|
static const char *SHARED_DATA_NAME = "shared_data";
|
||||||
constexpr short THERE_TWO_USER_DATA_HEADER = 2;
|
constexpr short THERE_TWO_USER_DATA_HEADER = 2;
|
||||||
SharedDataCpp::SharedDataCpp(const SHARER_NAME &sharerName, const char *path, const int &projectId)
|
SharedDataCpp::SharedDataCpp(const SHARER_NAME &sharerName, const char *path, const int &projectId)
|
||||||
: SharedMemory(path, projectId), mSharerName(sharerName)
|
: SharedMemory(path, projectId), mSharerName(sharerName)
|
||||||
{
|
{
|
||||||
mPrimaryReadSize = 0;
|
mPrimaryReadSize = 0;
|
||||||
mMinorReadSize = 0;
|
mMinorReadSize = 0;
|
||||||
mSharedMemeory = nullptr;
|
mSharedMemeory = nullptr;
|
||||||
}
|
}
|
||||||
void SharedDataCpp::MakeSharedMemory(const unsigned int readableSize, const unsigned int writableSize)
|
void SharedDataCpp::MakeSharedMemory(const unsigned int readableSize, const unsigned int writableSize)
|
||||||
{
|
{
|
||||||
if (SHARER_NAME_PRIMARY == mSharerName)
|
if (SHARER_NAME_PRIMARY == mSharerName) {
|
||||||
{
|
|
||||||
mPrimaryReadSize = readableSize;
|
mPrimaryReadSize = readableSize;
|
||||||
mMinorReadSize = writableSize;
|
mMinorReadSize = writableSize;
|
||||||
}
|
}
|
||||||
else if (SHARER_NAME_MINOR == mSharerName)
|
else if (SHARER_NAME_MINOR == mSharerName) {
|
||||||
{
|
|
||||||
mPrimaryReadSize = writableSize;
|
mPrimaryReadSize = writableSize;
|
||||||
mMinorReadSize = readableSize;
|
mMinorReadSize = readableSize;
|
||||||
}
|
}
|
||||||
else
|
else {
|
||||||
{
|
|
||||||
LogError("Make shared memory failed.\n");
|
LogError("Make shared memory failed.\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -49,70 +46,60 @@ void SharedDataCpp::MakeSharedMemory(const unsigned int readableSize, const unsi
|
||||||
}
|
}
|
||||||
void SharedDataCpp::WritableDataInit(void)
|
void SharedDataCpp::WritableDataInit(void)
|
||||||
{
|
{
|
||||||
if (nullptr == mSharedMemeory)
|
if (nullptr == mSharedMemeory) {
|
||||||
{
|
|
||||||
LogError("mSharedMemeory is nullptr, failed.\n");
|
LogError("mSharedMemeory is nullptr, failed.\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (SHARER_NAME_PRIMARY == mSharerName)
|
if (SHARER_NAME_PRIMARY == mSharerName) {
|
||||||
{
|
UserDataHeader *writableHeader =
|
||||||
UserDataHeader *writableHeader = (UserDataHeader *)((char *)mSharedMemeory + sizeof(UserDataHeader) + mPrimaryReadSize);
|
(UserDataHeader *)((char *)mSharedMemeory + sizeof(UserDataHeader) + mPrimaryReadSize);
|
||||||
memcpy(writableHeader->mUserName, USER_NAME_INIT_NAME, USER_NAME_BUF_LENGTH);
|
memcpy(writableHeader->mUserName, USER_NAME_INIT_NAME, USER_NAME_BUF_LENGTH);
|
||||||
writableHeader->mDataLength = mMinorReadSize;
|
writableHeader->mDataLength = mMinorReadSize;
|
||||||
}
|
}
|
||||||
else if (SHARER_NAME_MINOR == mSharerName)
|
else if (SHARER_NAME_MINOR == mSharerName) {
|
||||||
{
|
|
||||||
UserDataHeader *writableHeader = (UserDataHeader *)((char *)mSharedMemeory);
|
UserDataHeader *writableHeader = (UserDataHeader *)((char *)mSharedMemeory);
|
||||||
memcpy(writableHeader->mUserName, USER_NAME_INIT_NAME, USER_NAME_BUF_LENGTH);
|
memcpy(writableHeader->mUserName, USER_NAME_INIT_NAME, USER_NAME_BUF_LENGTH);
|
||||||
writableHeader->mDataLength = mPrimaryReadSize;
|
writableHeader->mDataLength = mPrimaryReadSize;
|
||||||
}
|
}
|
||||||
else
|
else {
|
||||||
{
|
|
||||||
LogError("Make shared memory failed.\n");
|
LogError("Make shared memory failed.\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const StatusCode SharedDataCpp::GetReadableMemory(void *buf, const unsigned int &bufLength)
|
const StatusCode SharedDataCpp::GetReadableMemory(void *buf, const unsigned int &bufLength)
|
||||||
{
|
{
|
||||||
if (nullptr == mSharedMemeory)
|
if (nullptr == mSharedMemeory) {
|
||||||
{
|
|
||||||
LogError("mSharedMemeory is nullptr, failed.\n");
|
LogError("mSharedMemeory is nullptr, failed.\n");
|
||||||
return CreateStatusCode(STATUS_CODE_NOT_OK);
|
return CreateStatusCode(STATUS_CODE_NOT_OK);
|
||||||
}
|
}
|
||||||
if (SHARER_NAME_PRIMARY == mSharerName && bufLength == mPrimaryReadSize)
|
if (SHARER_NAME_PRIMARY == mSharerName && bufLength == mPrimaryReadSize) {
|
||||||
{
|
|
||||||
UserDataHeader *readableHeader = (UserDataHeader *)((char *)mSharedMemeory);
|
UserDataHeader *readableHeader = (UserDataHeader *)((char *)mSharedMemeory);
|
||||||
LogInfo("Want to read %d and can be read %d\n", bufLength, readableHeader->mDataLength);
|
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 &&
|
if (memcmp(readableHeader->mUserName, USER_NAME_INIT_NAME, USER_NAME_BUF_LENGTH) == 0 &&
|
||||||
bufLength == readableHeader->mDataLength)
|
bufLength == readableHeader->mDataLength) {
|
||||||
{
|
|
||||||
memcpy(buf, (char *)mSharedMemeory + sizeof(UserDataHeader), bufLength);
|
memcpy(buf, (char *)mSharedMemeory + sizeof(UserDataHeader), bufLength);
|
||||||
}
|
}
|
||||||
else
|
else {
|
||||||
{
|
|
||||||
LogError("Readable memory didn't init yet or init error.\n");
|
LogError("Readable memory didn't init yet or init error.\n");
|
||||||
return CreateSharedDataCode(SHARED_DATA_CODE_WRONG_PEER_PARAMETERS);
|
return CreateSharedDataCode(SHARED_DATA_CODE_WRONG_PEER_PARAMETERS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (SHARER_NAME_MINOR == mSharerName && bufLength == mMinorReadSize)
|
else if (SHARER_NAME_MINOR == mSharerName && bufLength == mMinorReadSize) {
|
||||||
{
|
UserDataHeader *readableHeader =
|
||||||
UserDataHeader *readableHeader = (UserDataHeader *)((char *)mSharedMemeory + sizeof(UserDataHeader) + mPrimaryReadSize);
|
(UserDataHeader *)((char *)mSharedMemeory + sizeof(UserDataHeader) + mPrimaryReadSize);
|
||||||
LogInfo("Want to read %d and can be read %d\n", bufLength, readableHeader->mDataLength);
|
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 &&
|
if (memcmp(readableHeader->mUserName, USER_NAME_INIT_NAME, USER_NAME_BUF_LENGTH) == 0 &&
|
||||||
bufLength == readableHeader->mDataLength)
|
bufLength == readableHeader->mDataLength) {
|
||||||
{
|
|
||||||
memcpy(buf,
|
memcpy(buf,
|
||||||
(char *)mSharedMemeory + sizeof(UserDataHeader) * THERE_TWO_USER_DATA_HEADER + mPrimaryReadSize,
|
(char *)mSharedMemeory + sizeof(UserDataHeader) * THERE_TWO_USER_DATA_HEADER + mPrimaryReadSize,
|
||||||
bufLength);
|
bufLength);
|
||||||
}
|
}
|
||||||
else
|
else {
|
||||||
{
|
|
||||||
LogError("Readable memory didn't init yet or init error.\n");
|
LogError("Readable memory didn't init yet or init error.\n");
|
||||||
return CreateSharedDataCode(SHARED_DATA_CODE_WRONG_PEER_PARAMETERS);
|
return CreateSharedDataCode(SHARED_DATA_CODE_WRONG_PEER_PARAMETERS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else {
|
||||||
{
|
|
||||||
LogError("Get readable memory failed.\n");
|
LogError("Get readable memory failed.\n");
|
||||||
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
|
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
|
||||||
}
|
}
|
||||||
|
@ -120,44 +107,37 @@ const StatusCode SharedDataCpp::GetReadableMemory(void *buf, const unsigned int
|
||||||
}
|
}
|
||||||
void SharedDataCpp::SetWritableMemory(void *buf, const unsigned int &bufLength)
|
void SharedDataCpp::SetWritableMemory(void *buf, const unsigned int &bufLength)
|
||||||
{
|
{
|
||||||
if (nullptr == mSharedMemeory)
|
if (nullptr == mSharedMemeory) {
|
||||||
{
|
|
||||||
LogError("mSharedMemeory is nullptr, failed.\n");
|
LogError("mSharedMemeory is nullptr, failed.\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (SHARER_NAME_PRIMARY == mSharerName && bufLength == mMinorReadSize)
|
if (SHARER_NAME_PRIMARY == mSharerName && bufLength == mMinorReadSize) {
|
||||||
{
|
|
||||||
UserDataHeader *readableHeader = (UserDataHeader *)((char *)mSharedMemeory);
|
UserDataHeader *readableHeader = (UserDataHeader *)((char *)mSharedMemeory);
|
||||||
if (memcmp(readableHeader->mUserName, USER_NAME_INIT_NAME, USER_NAME_BUF_LENGTH) == 0)
|
if (memcmp(readableHeader->mUserName, USER_NAME_INIT_NAME, USER_NAME_BUF_LENGTH) == 0) {
|
||||||
{
|
if (mPrimaryReadSize == readableHeader->mDataLength) {
|
||||||
if (mPrimaryReadSize == readableHeader->mDataLength)
|
|
||||||
{
|
|
||||||
memcpy((char *)mSharedMemeory + sizeof(UserDataHeader) * THERE_TWO_USER_DATA_HEADER + mPrimaryReadSize,
|
memcpy((char *)mSharedMemeory + sizeof(UserDataHeader) * THERE_TWO_USER_DATA_HEADER + mPrimaryReadSize,
|
||||||
buf,
|
buf,
|
||||||
bufLength);
|
bufLength);
|
||||||
}
|
}
|
||||||
else
|
else {
|
||||||
{
|
|
||||||
LogError("Peer not match.\n");
|
LogError("Peer not match.\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else {
|
||||||
{
|
|
||||||
memcpy((char *)mSharedMemeory + sizeof(UserDataHeader) * THERE_TWO_USER_DATA_HEADER + mPrimaryReadSize,
|
memcpy((char *)mSharedMemeory + sizeof(UserDataHeader) * THERE_TWO_USER_DATA_HEADER + mPrimaryReadSize,
|
||||||
buf,
|
buf,
|
||||||
bufLength);
|
bufLength);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (SHARER_NAME_MINOR == mSharerName && bufLength == mPrimaryReadSize)
|
else if (SHARER_NAME_MINOR == mSharerName && bufLength == mPrimaryReadSize) {
|
||||||
{
|
|
||||||
memcpy((char *)mSharedMemeory + sizeof(UserDataHeader), buf, bufLength);
|
memcpy((char *)mSharedMemeory + sizeof(UserDataHeader), buf, bufLength);
|
||||||
}
|
}
|
||||||
else
|
else {
|
||||||
{
|
|
||||||
LogError("Set writable memory failed.\n");
|
LogError("Set writable memory failed.\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
static const StatusCode MakeSharedData(SharedData *object, const unsigned int readableSize, const unsigned int writableSize)
|
static const StatusCode MakeSharedData(SharedData *object, const unsigned int readableSize,
|
||||||
|
const unsigned int writableSize)
|
||||||
{
|
{
|
||||||
SharedDataImpl *impl = ((SharedDataImpl *)(((char *)object) - sizeof(SharedDataHeader)));
|
SharedDataImpl *impl = ((SharedDataImpl *)(((char *)object) - sizeof(SharedDataHeader)));
|
||||||
impl->mSharedData->MakeSharedMemory(readableSize, writableSize);
|
impl->mSharedData->MakeSharedMemory(readableSize, writableSize);
|
||||||
|
@ -176,14 +156,12 @@ static void SetSharedWritableMemory(SharedData *object, void *buf, const unsigne
|
||||||
static void SharedDataImplFree(void *ptr)
|
static void SharedDataImplFree(void *ptr)
|
||||||
{
|
{
|
||||||
SharedDataImpl *object = ((SharedDataImpl *)(((char *)ptr) - sizeof(SharedDataHeader)));
|
SharedDataImpl *object = ((SharedDataImpl *)(((char *)ptr) - sizeof(SharedDataHeader)));
|
||||||
if (SHARED_DATA_NAME == object->mHeader.mCheckName)
|
if (SHARED_DATA_NAME == object->mHeader.mCheckName) {
|
||||||
{
|
|
||||||
object->mSharedData->CleanSharedMemory();
|
object->mSharedData->CleanSharedMemory();
|
||||||
object->mSharedData = nullptr;
|
object->mSharedData = nullptr;
|
||||||
free(((char *)ptr) - sizeof(SharedDataHeader));
|
free(((char *)ptr) - sizeof(SharedDataHeader));
|
||||||
}
|
}
|
||||||
else
|
else {
|
||||||
{
|
|
||||||
LogError("Unknow ptr.\n");
|
LogError("Unknow ptr.\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -192,11 +170,11 @@ SharedData *NewSharedDataImpl(const SHARER_NAME &name, const char *path, const i
|
||||||
SharedDataImpl *impl = (SharedDataImpl *)malloc(sizeof(SharedDataImpl));
|
SharedDataImpl *impl = (SharedDataImpl *)malloc(sizeof(SharedDataImpl));
|
||||||
SharedDataImpl tmp;
|
SharedDataImpl tmp;
|
||||||
memcpy((void *)impl, (void *)&tmp, sizeof(SharedDataImpl));
|
memcpy((void *)impl, (void *)&tmp, sizeof(SharedDataImpl));
|
||||||
impl->mHeader.mCheckName = SHARED_DATA_NAME;
|
impl->mHeader.mCheckName = SHARED_DATA_NAME;
|
||||||
impl->mBase.mMakeSharedData = MakeSharedData;
|
impl->mBase.mMakeSharedData = MakeSharedData;
|
||||||
impl->mBase.mGetReadableData = GetSharedReadableMemory;
|
impl->mBase.mGetReadableData = GetSharedReadableMemory;
|
||||||
impl->mBase.mSetWritableData = SetSharedWritableMemory;
|
impl->mBase.mSetWritableData = SetSharedWritableMemory;
|
||||||
impl->mBase.mFree = SharedDataImplFree;
|
impl->mBase.mFree = SharedDataImplFree;
|
||||||
impl->mSharedData = std::make_shared<SharedDataCpp>(name, path, projectId);
|
impl->mSharedData = std::make_shared<SharedDataCpp>(name, path, projectId);
|
||||||
return (SharedData *)(((char *)impl) + sizeof(SharedDataHeader));
|
return (SharedData *)(((char *)impl) + sizeof(SharedDataHeader));
|
||||||
}
|
}
|
|
@ -13,41 +13,36 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
#include "SharedMemory.h"
|
#include "SharedMemory.h"
|
||||||
#include "SharedDataCode.h"
|
|
||||||
#include "ILog.h"
|
#include "ILog.h"
|
||||||
#include <unistd.h>
|
#include "SharedDataCode.h"
|
||||||
|
#include <errno.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <errno.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/ipc.h>
|
#include <sys/ipc.h>
|
||||||
#include <sys/shm.h>
|
#include <sys/shm.h>
|
||||||
|
#include <unistd.h>
|
||||||
constexpr int SHMGET_FAILED = -1;
|
constexpr int SHMGET_FAILED = -1;
|
||||||
SharedMemory::SharedMemory(const char *path, const int &projectId)
|
SharedMemory::SharedMemory(const char *path, const int &projectId) : mPath(path), mProjectId(projectId)
|
||||||
: mPath(path), mProjectId(projectId)
|
|
||||||
{
|
{
|
||||||
mId = SHMGET_FAILED;
|
mId = SHMGET_FAILED;
|
||||||
}
|
}
|
||||||
StatusCode SharedMemory::MakeSharedMemory(const int &size)
|
StatusCode SharedMemory::MakeSharedMemory(const int &size)
|
||||||
{
|
{
|
||||||
char touchPath[128] = {0};
|
char touchPath[128] = {0};
|
||||||
if (access(mPath, F_OK) != 0)
|
if (access(mPath, F_OK) != 0) {
|
||||||
{
|
|
||||||
sprintf(touchPath, "%s %s", "touch", mPath);
|
sprintf(touchPath, "%s %s", "touch", mPath);
|
||||||
system(touchPath);
|
system(touchPath);
|
||||||
}
|
}
|
||||||
key_t key = ftok(mPath, mProjectId);
|
key_t key = ftok(mPath, mProjectId);
|
||||||
if (key < 0)
|
if (key < 0) {
|
||||||
{
|
|
||||||
LogError("ftok failed.\n");
|
LogError("ftok failed.\n");
|
||||||
return CreateStatusCode(STATUS_CODE_NOT_OK);
|
return CreateStatusCode(STATUS_CODE_NOT_OK);
|
||||||
}
|
}
|
||||||
mId = shmget(key, size, IPC_CREAT | 0666);
|
mId = shmget(key, size, IPC_CREAT | 0666);
|
||||||
if (mId == SHMGET_FAILED)
|
if (mId == SHMGET_FAILED) {
|
||||||
{
|
|
||||||
constexpr int MAYBE_CODE_22_MEANS_PEER_SIZE_WAS_NOT_MATCH = 22;
|
constexpr int MAYBE_CODE_22_MEANS_PEER_SIZE_WAS_NOT_MATCH = 22;
|
||||||
if (MAYBE_CODE_22_MEANS_PEER_SIZE_WAS_NOT_MATCH == errno)
|
if (MAYBE_CODE_22_MEANS_PEER_SIZE_WAS_NOT_MATCH == errno) {
|
||||||
{
|
|
||||||
LogInfo("errno = %d, errmsg = %s\n", errno, strerror(errno));
|
LogInfo("errno = %d, errmsg = %s\n", errno, strerror(errno));
|
||||||
return CreateSharedDataCode(SHARED_DATA_CODE_WRONG_PEER_PARAMETERS);
|
return CreateSharedDataCode(SHARED_DATA_CODE_WRONG_PEER_PARAMETERS);
|
||||||
}
|
}
|
||||||
|
@ -59,13 +54,11 @@ StatusCode SharedMemory::MakeSharedMemory(const int &size)
|
||||||
}
|
}
|
||||||
StatusCode SharedMemory::CleanSharedMemory(void)
|
StatusCode SharedMemory::CleanSharedMemory(void)
|
||||||
{
|
{
|
||||||
if (SHMGET_FAILED == mId)
|
if (SHMGET_FAILED == mId) {
|
||||||
{
|
|
||||||
LogError("mId error.\n");
|
LogError("mId error.\n");
|
||||||
return CreateStatusCode(STATUS_CODE_NOT_OK);
|
return CreateStatusCode(STATUS_CODE_NOT_OK);
|
||||||
}
|
}
|
||||||
if (shmctl(mId, IPC_RMID, NULL) < 0)
|
if (shmctl(mId, IPC_RMID, NULL) < 0) {
|
||||||
{
|
|
||||||
LogError("shmctl failed.\n");
|
LogError("shmctl failed.\n");
|
||||||
return CreateStatusCode(STATUS_CODE_NOT_OK);
|
return CreateStatusCode(STATUS_CODE_NOT_OK);
|
||||||
}
|
}
|
||||||
|
@ -73,8 +66,7 @@ StatusCode SharedMemory::CleanSharedMemory(void)
|
||||||
}
|
}
|
||||||
void *SharedMemory::GetMemory(void)
|
void *SharedMemory::GetMemory(void)
|
||||||
{
|
{
|
||||||
if (SHMGET_FAILED == mId)
|
if (SHMGET_FAILED == mId) {
|
||||||
{
|
|
||||||
LogError("mId error.\n");
|
LogError("mId error.\n");
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user