首页 > 代码库 > 服务器打开共享内存的初始化

服务器打开共享内存的初始化

服务器共享内存以及客户端打开服务器获得通知过程打开GS就会进行共享内存的初始化,过程很复杂,看了很多次,今天看的时候终于了解了大概了bool DataLayer::init(){    int pid = GetCurrentProcessId();    auto path = Plug::GetCurrentPath();    path += L"pid.txt";    auto file = _wfopen(path.c_str(), L"wb");    char buf[10] = {0};    itoa(pid, buf, 10);    fwrite(buf, 6, 1, file);//把当前GS进程的进程号写入到pid.txt,NET进程可以通过名字获得共享内存的名字    fclose(file);    m_spShareMemInter = NEWSP(shareMemInter);    m_spShareMemInter->init("网络", pid);    //创建网络子进程    if(!CreateNetProcess())    {        Plug::PlugMessageBox(L"启动Net进程失败!");        return false;    }    return true;}//共享内存的初始化void shareMemInter::init(const char* name, int pid){    std::string name2 = name;    m_a2b.init(name2, pid);    name2 += "-回发";    m_b2a.init(name2, pid, 2);}//单边共享内存的初始化void shareMemInterOneway::init(std::string name, int pid, int more){    //初始化三种共享内存    m_ProcessMemPool.init(name, pid, more);    m_pid = pid;    //针对mutex的共享内存,主要是为了互斥的访问m_ShareMemQue    m_shm = new shared_memory_object(open_or_create, getMemName(name + "mux", m_pid).c_str(), read_write);    m_shm->truncate(sizeof(mem镜像));    mapped_region* m_region = new mapped_region(*m_shm, read_write);    void* addr = m_region->get_address();    m_mem镜像 = new (addr) mem镜像;    //此处又创建队列共享内存,这个是用的时候从内存池取一块内存然后放入到这个队列里面来,在这个地方卡了很久    m_segment = new managed_shared_memory(open_or_create, getMemName(name + "MySharedMemory9", m_pid).c_str(), 1024 * 1024 * 10 * more);    m_ShareMemQue = m_segment->find_or_construct<mem_queue>(getMemName(name + "MySharedMemory92", m_pid).c_str())(mem_allocator(m_segment->get_segment_manager())); }//这个过程看了很久,很久,也是刚看懂的void ProcessMemPool::init(int memSize, int buffer_num, std::string name, int pid){    m_memSize = memSize;    m_buffer_num = buffer_num;    m_name = name;    m_pid = pid;    //共享内存存储bool是否初始化    auto shm = new shared_memory_object(open_or_create, getMemName(m_name + "mux2", pid).c_str(), read_write);    shm->truncate(sizeof(bool));    mapped_region* m_region = new mapped_region(*shm, read_write);    void* addr = m_region->get_address();    m_镜像 = (ProcessMemPoolmem镜像*)(addr);    //大小1024*buffer_num托管共享内存,存储的是boost里面的queue    /*    一般stl里面的容器是不能放到共享内存里面的,因为他的内存不是在共享内存上分配的,主要跟他的分配器有关系    此时就需要使用boost自己定义的对应的容器    这个m_queue就是共享内存索引的作用,通过其中的index获取共享内存的地址    */    m_segment = new managed_shared_memory(open_or_create, getMemName(m_name + "ProcessMemPool9", pid).c_str(), 1024*buffer_num/*1024*1024*10*2*/);    m_queue = m_segment->find_or_construct<mem_queue>(getMemName(m_name + "m_queue", pid).c_str())(mem_allocator(m_segment->get_segment_manager()));      //    ---------------------------------    //共享内存    auto shm2 = new shared_memory_object(open_or_create, getMemName(name + "memall2", pid).c_str(), read_write);    shm2->truncate(m_memSize * (m_buffer_num + 10));    mapped_region* m_region2 = new mapped_region(*shm2, read_write);    m_start_addr = m_region2->get_address();    if(m_镜像->is_inited)//判断共享内存是否已被初始化    {        return ;    }    m_镜像->is_inited = true;    //初始化共享queue,就是向其中插入shareDataEx    for (int i = 0; i < m_buffer_num; i++)    {        shareDataEx sd;        sd.index = i;        sd.size = 0;        sd.total_size = m_memSize;        m_queue->push_back(sd);    }}由于这里的共享内存都是open_or_create所以NET进程初始化共享内存的时候当其看到同名的共享内存的时候就会打开,没有的话就会创建

 

服务器打开共享内存的初始化