基于CS模式的共享内存与事件
//service.cpp
#include <windows.h>
#include <iostream>
using namespace std;
#define BUF_SIZE 4096 //size of Share memory File mapping
#define FM_RECV_NAME "Global\\Send" //name of the file mapping to receive
#define FM_SEND_NAME "Global\\Recv" //name of the file mapping to send
#define MsgEventRecv "Global\\MessageFromEv" //event of receiving msg from file mapping
#define MsgEventSend "Global\\Message2Ev" //event of sending msg to file mapping
#define RECVMSG WM_USER+1
#define SENDMSG WM_USER+2
DWORD WINAPI MsgHandleProc(
LPVOID lpParameter
);
HANDLE m_hdlUkeyEventRecv;
HANDLE m_hdlFileMapRecv;
int main()
{
m_hdlUkeyEventRecv = OpenEvent(EVENT_ALL_ACCESS, FALSE, MsgEventSend);
if (m_hdlUkeyEventRecv == NULL)
{
m_hdlUkeyEventRecv = CreateEvent(NULL, FALSE, FALSE, MsgEventSend);
}
auto m_hdlUkeyEventSend = CreateEvent(NULL, FALSE, FALSE, MsgEventRecv);
m_hdlFileMapRecv = OpenFileMapping(
FILE_MAP_ALL_ACCESS,
false,
FM_SEND_NAME);
if (m_hdlFileMapRecv == NULL)
{
m_hdlFileMapRecv = CreateFileMapping(
INVALID_HANDLE_VALUE, // use paging file
NULL, // default security
PAGE_READWRITE, // read/write access
0, // maximum object size (high-order DWORD)
BUF_SIZE, // maximum object size (low-order DWORD)
FM_SEND_NAME); // name of mapping object
}
auto m_hdlFileMapSend = CreateFileMapping(
INVALID_HANDLE_VALUE, // use paging file
NULL, // default security
PAGE_READWRITE, // read/write access
0, // maximum object size (high-order DWORD)
BUF_SIZE, // maximum object size (low-order DWORD)
FM_RECV_NAME); // name of mapping object
if (m_hdlFileMapRecv == NULL || m_hdlFileMapSend == NULL)
{
printf("CreateFileMapping fails,GetLastError is:%d", GetLastError());
}
DWORD m_msgHdlThreadId;
CreateThread(NULL, 0, MsgHandleProc, NULL, 0, &m_msgHdlThreadId);
int msg{ 0 };
while (1)
{
cout << "请输入消息:" << endl;
cin >> msg;
switch (msg)
{
case RECVMSG:
{
std::string dumpStr = "service send msg.....";
char* pBuf = (char*)MapViewOfFile(m_hdlFileMapSend,
FILE_MAP_ALL_ACCESS,
0,
0,
BUF_SIZE);
if (pBuf == NULL)
{
printf("MapViewOfFile error,GetLastError is:%d", GetLastError());
continue;
}
CopyMemory(pBuf, dumpStr.c_str(), dumpStr.length());
SetEvent(m_hdlUkeyEventSend);
UnmapViewOfFile(pBuf);
break;
}
default:
break;
}
if (msg < 0)
break;
}
return 0;
}
DWORD WINAPI MsgHandleProc(
LPVOID lpParameter
)
{
while (1)
{
DWORD ret = WaitForSingleObject(m_hdlUkeyEventRecv, INFINITE);
char* pBuf = (char*)MapViewOfFile(m_hdlFileMapRecv,
FILE_MAP_ALL_ACCESS,
0,
0,
BUF_SIZE);
if (pBuf == NULL)
{
printf("MapViewOfFile error,GetLastError is:%d", GetLastError());
continue;
}
cout << pBuf << endl;
UnmapViewOfFile(pBuf);
}
return 0;
}
//client.cpp
#include <windows.h>
#include <iostream>
using namespace std;
#define BUF_SIZE 4096 //size of Share memory File mapping
#define FM_RECV_NAME "Global\\Send" //name of the file mapping to receive
#define FM_SEND_NAME "Global\\Recv" //name of the file mapping to send
#define MsgEventRecv "Global\\MessageFromEv" //event of receiving msg from file mapping
#define MsgEventSend "Global\\Message2Ev" //event of sending msg to file mapping
#define RECVMSG WM_USER+1
#define SENDMSG WM_USER+2
DWORD WINAPI MsgHandleProc(
LPVOID lpParameter
);
HANDLE m_hdlUkeyEventRecv;
HANDLE m_hdlFileMapRecv;
int main()
{
m_hdlUkeyEventRecv = OpenEvent(EVENT_ALL_ACCESS, FALSE, MsgEventRecv);
if (m_hdlUkeyEventRecv==NULL)
{
m_hdlUkeyEventRecv = CreateEvent(NULL, FALSE, FALSE, MsgEventRecv);
}
auto m_hdlUkeyEventSend = CreateEvent(NULL, FALSE, FALSE, MsgEventSend);
m_hdlFileMapRecv = OpenFileMapping(
FILE_MAP_ALL_ACCESS,
false,
FM_RECV_NAME);
if (m_hdlFileMapRecv==NULL)
{
m_hdlFileMapRecv = CreateFileMapping(
INVALID_HANDLE_VALUE, // use paging file
NULL, // default security
PAGE_READWRITE, // read/write access
0, // maximum object size (high-order DWORD)
BUF_SIZE, // maximum object size (low-order DWORD)
FM_RECV_NAME); // name of mapping object
}
auto m_hdlFileMapSend = CreateFileMapping(
INVALID_HANDLE_VALUE, // use paging file
NULL, // default security
PAGE_READWRITE, // read/write access
0, // maximum object size (high-order DWORD)
BUF_SIZE, // maximum object size (low-order DWORD)
FM_SEND_NAME); // name of mapping object
if (m_hdlFileMapRecv == NULL || m_hdlFileMapSend == NULL)
{
printf("CreateFileMapping fails,GetLastError is:%d", GetLastError());
}
DWORD m_msgHdlThreadId;
CreateThread(NULL, 0, MsgHandleProc, NULL, 0, &m_msgHdlThreadId);
int msg{ 0 };
while (1)
{
cout << "请输入消息:" << endl;
cin >> msg;
switch (msg)
{
case RECVMSG:
{
std::string dumpStr ="client send msg.....";
char* pBuf = (char*)MapViewOfFile(m_hdlFileMapSend,
FILE_MAP_ALL_ACCESS,
0,
0,
BUF_SIZE);
if (pBuf == NULL)
{
printf("MapViewOfFile error,GetLastError is:%d", GetLastError());
continue;
}
CopyMemory(pBuf, dumpStr.c_str(), dumpStr.length());
SetEvent(m_hdlUkeyEventSend);
UnmapViewOfFile(pBuf);
break;
}
default:
break;
}
if (msg < 0)
break;
}
return 0;
}
DWORD WINAPI MsgHandleProc(
LPVOID lpParameter
)
{
while (1)
{
DWORD ret = WaitForSingleObject(m_hdlUkeyEventRecv, INFINITE);
char* pBuf = (char*)MapViewOfFile(m_hdlFileMapRecv,
FILE_MAP_ALL_ACCESS,
0,
0,
BUF_SIZE);
if (pBuf == NULL)
{
printf("MapViewOfFile error,GetLastError is:%d", GetLastError());
continue;
}
cout << pBuf << endl;
UnmapViewOfFile(pBuf);
}
return 0;
}
参考:
1.https://blog.csdn.net/wuzhu2017/article/details/107865827
基于CS模式的共享内存与事件
最新推荐文章于 2024-01-16 08:00:00 发布