1、类封装介绍

1、ShareMemory类有4个有参构造函数,分别为通过key值连接共享内存,和创建共享内存,通过路径来连接共享内存和创建共享内存。
2、3个公有的成员函数,
    mapShm连接到共享内存
    unmapShm断开连接
    delShm删除共享内存

3、SecKeyShm类,继承ShareMemory类,SecKeyShm主要根据自身业务写,
主要功能:
    两个公有函数,通过clienID和serverID找对应的值进行读
    写,将定义好的数据结构找到空位进行写入

2、代码


2.1、ShareMemory.h

#pragma once
#include <sys/ipc.h>
#include <sys/shm.h>

constcharRandX='x';

classShareMemory
{
public:
ShareMemory(int key);// 打开共享内存
ShareMemory(int key,int size);// 创建共享内存
ShareMemory(constchar* name);
ShareMemory(constchar* name,int size);
virtual~ShareMemory();

void* mapShm();
int unmapShm();
int delShm();

private:
int getShmID(key_t key,int shmSize,int flag);
int m_shmID;
void* m_shmAddr;

};

2.2、ShareMemory.cpp

#include <sys/ipc.h>
#include <sys/shm.h>
#include "ShareMemory.h"
#include <stdlib.h>

ShareMemory::ShareMemory(int key)
{
// 打开共享内存
    m_shmID =getShmID(key,0,0);
}

ShareMemory::ShareMemory(int key,int size)
{
// 创建共享内存
    m_shmID =getShmID(key,size,IPC_CREAT|0755);
}

ShareMemory::ShareMemory(constchar* name)
{
// 通过路文件名连接共享内存
key_t key =ftok(name,RandX);
    m_shmID =getShmID(key,0,0);
}

ShareMemory::ShareMemory(constchar* name,int size)
{
// 通过路文件名创建
key_t key =ftok(name,RandX);
    m_shmID =getShmID(key,size,IPC_CREAT|0755);

}

ShareMemory::~ShareMemory()
{

}

// 关联共享内存
void* ShareMemory::mapShm()
{
    m_shmAddr =shmat(m_shmID,NULL,0);
if(m_shmAddr==(void*)-1)
{
// 日志
returnNULL;
}
return m_shmAddr;
}

int ShareMemory::unmapShm()
{
int ret =shmdt(m_shmAddr);
return ret;
}

int ShareMemory::delShm()
{
int ret =shmctl(m_shmID,IPC_RMID,NULL);
return ret;
}

int ShareMemory::getShmID(key_t key,int shmSize,int flag)
{
    m_shmID =shmget(key,shmSize,flag);
if(m_shmID<0)
{
// 打印日志
return-1;
}
return m_shmID;
}

2.3、SecKeyShm.h

#pragma once
#include "ShareMemory.h"


classNodeSHMInfo
{
public:
int status;
int seckeyID;
char clientID[12];
char serverID[12];
char seckey[128];
};

classSecKeyShm:publicShareMemory
{
public:
SecKeyShm(int key);
SecKeyShm(int key,int maxNode);
SecKeyShm(constchar* pathName);
SecKeyShm(constchar* pathName,int maxNode);
~SecKeyShm();

int shmWrite(NodeSHMInfo* pNodeInfo);
int shmRead(const char* clientID,const char* serverID,NodeSHMInfo* pNodeInfo);

// for test
void printShm();
private:
int m_maxNode;
};

2.4、SecKeyShm.cpp

#include <stdlib.h>
#include <string.h>
#include <iostream>
#include "secKeyshm.h"
usingnamespace std;

SecKeyShm::SecKeyShm(int key):ShareMemory(key)
{

}

// maxNode 表示要传递的结构体个数
SecKeyShm::SecKeyShm(int key,int maxNode):
ShareMemory(key,sizeof(int)+maxNode*sizeof(NodeSHMInfo)),m_maxNode(maxNode)
{
// 连接共享内存
void* p =mapShm();
if(p!=NULL)
{
// 初始化共享内存区
memset(p,0x00,(sizeof(int)+sizeof(NodeSHMInfo)*maxNode));
// 将maxNode写入共享内存的头4个字节中
memcpy(p,&maxNode,sizeof(int));
// 断开与共享内存
unmapShm();
}

}

SecKeyShm::SecKeyShm(constchar* pathName,int maxNode):
ShareMemory(pathName,sizeof(int)+maxNode*sizeof(NodeSHMInfo)),m_maxNode(maxNode)
{
// 连接共享内存
void* p =mapShm();
if(p!=NULL)
{

// 初始化共享内存区
memset(p,0x00,sizeof(int)+sizeof(NodeSHMInfo)*maxNode);
// 将maxNode写入共享内存的头4个字节中
memcpy(p,&maxNode,sizeof(int));
// 断开与共享内存
unmapShm();
}
}

SecKeyShm::~SecKeyShm()
{

}

int SecKeyShm::shmWrite(NodeSHMInfo* pNodeInfo)
{
// 连接共享内存
void*p =mapShm();

// 从头4个字节获取maxNode
memcpy(&m_maxNode, p,sizeof(int));
    cout <<"maxNode:"<< m_maxNode << endl;
NodeSHMInfo*pNode =(NodeSHMInfo*)((char*)p+sizeof(int));

//  找相同的
int i =0;
for(i=0; i<m_maxNode; i++)
{
if(strcmp(pNodeInfo->clientID, pNode[i].clientID)==0&&
strcmp(pNodeInfo->serverID, pNode[i].serverID)==0)
{
memcpy(&pNode[i], pNodeInfo,sizeof(NodeSHMInfo));
break;
}
}

// 表示没有原有的
NodeSHMInfo tmp;
memset(&tmp,0x00,sizeof(NodeSHMInfo));
if(i==m_maxNode)
{
for(i=0; i<m_maxNode; i++)
if(memcmp(&pNode[i],&tmp,sizeof(NodeSHMInfo))==0)
{
memcpy(&pNode[i], pNodeInfo,sizeof(NodeSHMInfo));
break;
}
}

// 断开共享内存的关联
unmapShm();

// 没有空闲位置用
if(i==m_maxNode)
{
        cout<<"no space to use"<<endl;
return-1;
}
return0;
}

int SecKeyShm::shmRead(const char* clientID,const char* serverID,NodeSHMInfo* pNodeInfo)
{
// 连接共享内存
void* p =mapShm();

// 从头4个字节获取maxNode
memcpy(&m_maxNode,p,sizeof(int));
    cout<<"m_maxNode:"<<m_maxNode<<endl;
NodeSHMInfo* pNode =(NodeSHMInfo*)((char*)p+sizeof(int));

int i=0;
for(;i<m_maxNode;i++)
{
if(strcmp(clientID,pNode[i].clientID)==0&&
strcmp(serverID,pNode[i].serverID)==0)
{
memcpy(pNodeInfo,&pNode[i],sizeof(NodeSHMInfo));
break;
}
}

//  断开
unmapShm();
// 没有找到节点
if(i==m_maxNode)
{
        cout<<"not found"<<endl;
return-1;
}
return0;
}


void SecKeyShm::printShm()
{

void*p =mapShm();


memcpy(&m_maxNode, p,sizeof(int));
    cout <<"maxNode:"<< m_maxNode << endl;

NodeSHMInfo*pNode =(NodeSHMInfo*)((char*)p+sizeof(int));

int i =0;
for(i=0; i<m_maxNode; i++)
{
        cout <<"-------"<< i <<"-------"<< endl;
        cout <<"status:"<< pNode[i].status << endl;
        cout <<"seckeyID:"<< pNode[i].seckeyID << endl;
        cout <<"clientID:"<< pNode[i].clientID << endl;
        cout <<"serverID:"<< pNode[i].serverID << endl;
        cout <<"seckey:"<< pNode[i].seckey << endl;
}

return;
}

2.5、main.cpp

#include "secKeyshm.h"
#include <iostream>
#include <string.h>
#include <stdlib.h>

usingnamespace std;

int main(int argc,char* argv[])
{
char clientID[16]={0};
char serverID[16]={0};
strcpy(clientID,argv[1]);
strcpy(serverID,argv[2]);

// 创建
// SecKeyShm shm(0x1234,10);
SecKeyShm shm(0x1234);

NodeSHMInfo info;
memset(&info,0x00,sizeof(NodeSHMInfo));
    info.status =1;
    info.seckeyID =11;
strcpy(info.clientID,clientID);
strcpy(info.serverID,serverID);
strcpy(info.seckey,"0001");
    shm.shmWrite(&info);

    cout<<"---------------"<<endl;
// 连接
SecKeyShm shm1(0x1234);
memset(&info,0x00,sizeof(NodeSHMInfo));
    shm1.shmRead(clientID,serverID,&info);
    cout<<"status:"<<info.status<<endl;
    cout<<"seckeyID:"<<info.seckeyID<<endl;
    cout<<"clientID:"<<info.clientID<<endl;
    cout<<"serverID:"<<info.serverID<<endl;
    cout<<"seckeyID:"<<info.seckeyID<<endl;

    cout<<"-------打印所有的------"<<endl;
    shm1.printShm();

return0;
}