Improve shared data module.
This commit is contained in:
parent
dd9b3df32d
commit
c459515071
|
@ -23,11 +23,24 @@ namespace SharedDataTest
|
||||||
{
|
{
|
||||||
CreateLogModule();
|
CreateLogModule();
|
||||||
ILogInit(LOG_INSTANCE_TYPE_END);
|
ILogInit(LOG_INSTANCE_TYPE_END);
|
||||||
SharedData *sharedData = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", 9);
|
constexpr int PROJECT_ID = 9;
|
||||||
if (nullptr != sharedData)
|
SharedData *sharedDataPrimary = CreateSharedData(SHARER_NAME_PRIMARY, "./shared_file", PROJECT_ID);
|
||||||
|
SharedData *sharedDataMinor = CreateSharedData(SHARER_NAME_MINOR, "./shared_file", PROJECT_ID);
|
||||||
|
if (nullptr != sharedDataMinor)
|
||||||
{
|
{
|
||||||
sharedData->mMakeSharedData(sharedData, sizeof(int), sizeof(int));
|
sharedDataMinor->mMakeSharedData(sharedDataMinor, sizeof(int), sizeof(int));
|
||||||
sharedData->mFree(sharedData);
|
}
|
||||||
|
if (nullptr != sharedDataPrimary)
|
||||||
|
{
|
||||||
|
int readableData = -1;
|
||||||
|
sharedDataPrimary->mMakeSharedData(sharedDataPrimary, sizeof(int), sizeof(int));
|
||||||
|
sharedDataPrimary->mGetReadableDataPointer(sharedDataPrimary, &readableData, sizeof(int));
|
||||||
|
LogInfo("readableData = %d\n", readableData);
|
||||||
|
sharedDataPrimary->mFree(sharedDataPrimary);
|
||||||
|
}
|
||||||
|
if (nullptr != sharedDataMinor)
|
||||||
|
{
|
||||||
|
sharedDataMinor->mFree(sharedDataMinor);
|
||||||
}
|
}
|
||||||
ILogUnInit();
|
ILogUnInit();
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,9 +28,9 @@ extern "C"
|
||||||
typedef struct shared_data SharedData;
|
typedef struct shared_data SharedData;
|
||||||
typedef struct shared_data
|
typedef struct shared_data
|
||||||
{
|
{
|
||||||
const StatusCode (*mMakeSharedData)(SharedData *, const int, const int);
|
const StatusCode (*mMakeSharedData)(SharedData *, const unsigned int, const unsigned int);
|
||||||
const StatusCode (*mCleanSharedData)(SharedData *);
|
const StatusCode (*mCleanSharedData)(SharedData *);
|
||||||
void *(*mGetReadableDataPointer)(SharedData *);
|
void (*mGetReadableDataPointer)(SharedData *, void *, const int);
|
||||||
void *(*mGetWritableDataPointer)(SharedData *);
|
void *(*mGetWritableDataPointer)(SharedData *);
|
||||||
void (*mFree)(void *);
|
void (*mFree)(void *);
|
||||||
} SharedData;
|
} SharedData;
|
||||||
|
|
|
@ -20,40 +20,108 @@ 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)
|
||||||
{
|
{
|
||||||
mReadableSize = 0;
|
mPrimaryReadSize = 0;
|
||||||
mWritableSize = 0;
|
mMinorReadSize = 0;
|
||||||
mSharedMemeory = nullptr;
|
mSharedMemeory = nullptr;
|
||||||
}
|
}
|
||||||
void SharedDataCpp::MakeSharedMemory(const int readableSize, const int writableSize)
|
void SharedDataCpp::MakeSharedMemory(const unsigned int readableSize, const unsigned int writableSize)
|
||||||
{
|
|
||||||
mReadableSize = readableSize;
|
|
||||||
mWritableSize = writableSize;
|
|
||||||
const int SHARED_MEMORY_SIZE = readableSize + writableSize + sizeof(UserDataHeader) * THERE_TWO_USER_DATA_HEADER;
|
|
||||||
SharedMemory::MakeSharedMemory(SHARED_MEMORY_SIZE);
|
|
||||||
mSharedMemeory = SharedMemory::GetMemory();
|
|
||||||
}
|
|
||||||
void *SharedDataCpp::GetReadableMemory(void)
|
|
||||||
{
|
{
|
||||||
if (SHARER_NAME_PRIMARY == mSharerName)
|
if (SHARER_NAME_PRIMARY == mSharerName)
|
||||||
{
|
{
|
||||||
return (char *)mSharedMemeory + sizeof(UserDataHeader);
|
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();
|
||||||
|
// if (nullptr != mSharedMemeory)
|
||||||
|
// {
|
||||||
|
// UserDataHeader *writableHeader = (UserDataHeader *)((char *)mSharedMemeory + sizeof(UserDataHeader) + mPrimaryReadSize);
|
||||||
|
// memcpy(writableHeader->mUserName, USER_NAME_INIT_NAME, USER_NAME_BUF_LENGTH);
|
||||||
|
// }
|
||||||
|
// else
|
||||||
|
// {
|
||||||
|
// LogError("mSharedMemeory get failed.\n");
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
else if (SHARER_NAME_MINOR == mSharerName)
|
||||||
|
{
|
||||||
|
UserDataHeader *writableHeader = (UserDataHeader *)((char *)mSharedMemeory);
|
||||||
|
memcpy(writableHeader->mUserName, USER_NAME_INIT_NAME, USER_NAME_BUF_LENGTH);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
LogError("Make shared memory failed.\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void SharedDataCpp::GetReadableMemory(void *buf, const unsigned int &bufLength)
|
||||||
|
{
|
||||||
|
if (bufLength > mPrimaryReadSize)
|
||||||
|
{
|
||||||
|
LogError("Get readable memory failed, buf length too large.\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (SHARER_NAME_PRIMARY == mSharerName)
|
||||||
|
{
|
||||||
|
if (memcmp((char *)mSharedMemeory, USER_NAME_INIT_NAME, USER_NAME_BUF_LENGTH) == 0)
|
||||||
|
{
|
||||||
|
memcpy(buf, (char *)mSharedMemeory + sizeof(UserDataHeader), bufLength);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
LogError("Readable memory didn't init yet.\n");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (SHARER_NAME_MINOR == mSharerName)
|
if (SHARER_NAME_MINOR == mSharerName)
|
||||||
{
|
{
|
||||||
return (char *)mSharedMemeory + sizeof(UserDataHeader) * THERE_TWO_USER_DATA_HEADER + mReadableSize;
|
if (memcmp((char *)mSharedMemeory + sizeof(UserDataHeader) + mPrimaryReadSize,
|
||||||
|
USER_NAME_INIT_NAME,
|
||||||
|
USER_NAME_BUF_LENGTH) == 0)
|
||||||
|
{
|
||||||
|
memcpy(buf,
|
||||||
|
(char *)mSharedMemeory + sizeof(UserDataHeader) * THERE_TWO_USER_DATA_HEADER + mPrimaryReadSize,
|
||||||
|
bufLength);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
LogError("Readable memory didn't init yet.\n");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nullptr;
|
|
||||||
}
|
}
|
||||||
static const StatusCode MakeSharedData(SharedData *object, const int readableSize, const 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);
|
||||||
return CreateStatusCode(STATUS_CODE_OK);
|
return CreateStatusCode(STATUS_CODE_OK);
|
||||||
}
|
}
|
||||||
static void *GetSharedReadableMemory(SharedData *object)
|
static void GetSharedReadableMemory(SharedData *object, void *buf, const int bufLength)
|
||||||
{
|
{
|
||||||
SharedDataImpl *impl = ((SharedDataImpl *)(((char *)object) - sizeof(SharedDataHeader)));
|
SharedDataImpl *impl = ((SharedDataImpl *)(((char *)object) - sizeof(SharedDataHeader)));
|
||||||
return impl->mSharedData->GetReadableMemory();
|
impl->mSharedData->GetReadableMemory(buf, bufLength);
|
||||||
}
|
}
|
||||||
static void SharedDataImplFree(void *ptr)
|
static void SharedDataImplFree(void *ptr)
|
||||||
{
|
{
|
||||||
|
|
|
@ -17,12 +17,21 @@
|
||||||
#include "SharedData.h"
|
#include "SharedData.h"
|
||||||
#include "SharedMemory.h"
|
#include "SharedMemory.h"
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
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
|
typedef struct shared_data_header
|
||||||
{
|
{
|
||||||
const char *mCheckName;
|
const char *mCheckName;
|
||||||
} SharedDataHeader;
|
} SharedDataHeader;
|
||||||
typedef struct user_data_header
|
typedef struct user_data_header
|
||||||
{
|
{
|
||||||
|
char mUserName[USER_NAME_BUF_LENGTH];
|
||||||
int mIsInit;
|
int mIsInit;
|
||||||
} UserDataHeader;
|
} UserDataHeader;
|
||||||
class SharedDataCpp : public SharedMemory
|
class SharedDataCpp : public SharedMemory
|
||||||
|
@ -30,15 +39,18 @@ class SharedDataCpp : public SharedMemory
|
||||||
public:
|
public:
|
||||||
SharedDataCpp(const SHARER_NAME &sharerName, const char *path, const int &projectId);
|
SharedDataCpp(const SHARER_NAME &sharerName, const char *path, const int &projectId);
|
||||||
virtual ~SharedDataCpp() = default;
|
virtual ~SharedDataCpp() = default;
|
||||||
void MakeSharedMemory(const int readableSize, const int writableSize);
|
void MakeSharedMemory(const unsigned int readableSize, const unsigned int writableSize);
|
||||||
void *GetReadableMemory(void);
|
void GetReadableMemory(void *buf, const unsigned int &bufLength);
|
||||||
|
|
||||||
|
private:
|
||||||
|
void WritableDataInit(void);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const SHARER_NAME mSharerName;
|
const SHARER_NAME mSharerName;
|
||||||
// std::shared_ptr<SharedMemory> mReadableMemory;
|
// std::shared_ptr<SharedMemory> mReadableMemory;
|
||||||
// std::shared_ptr<SharedMemory> mWritableMemory;
|
// std::shared_ptr<SharedMemory> mWritableMemory;
|
||||||
unsigned int mReadableSize;
|
unsigned int mPrimaryReadSize;
|
||||||
unsigned int mWritableSize;
|
unsigned int mMinorReadSize;
|
||||||
void *mSharedMemeory;
|
void *mSharedMemeory;
|
||||||
};
|
};
|
||||||
typedef struct shared_data_impl SharedDataImpl;
|
typedef struct shared_data_impl SharedDataImpl;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user