管道是一种简单的进程间通讯(IPC),命名管道可在同一台计算机的不同进程之间或在跨越一个网络的不同计算机的不同进程之间,支持可靠的、单向或双向的数据通信。
理解了上图,就有一个大概的思路了,下面是根据dr_ipc.h库实现的简单例子,一个简单的例子,仅供参考,下面实现了简单的交互,具体实现请查对应的API
模块A
#define DR_IPC_IMPLEMENTATION
#include "dr_ipc.h"
#include <string>
#include <iostream>
using namespace std;
int main()
{
string swrite_pipe_name = "write123";
string sread_pipe_name = "read123";
string log_file_tmp = "";
drpipe _write_pipe = nullptr;
cout << "wait .. DR_IPC_WRITE write123" << endl;
//创建管道,堵塞等待链接
dripc_result result = drpipe_open_named_server(swrite_pipe_name.c_str(), DR_IPC_WRITE, &_write_pipe);
if (result != dripc_result_success)
{
cout << "error drpipe_open_named_server" << endl;
Sleep(1000000);
}
else
{
cout << "2" << endl;
dripc_result result = dripc_result_unknown_error;
int nCount = 1;
drpipe _read_pipe = NULL;
result = drpipe_open_named_client(sread_pipe_name.c_str(), DR_IPC_READ, &_read_pipe);//连接管道
if (result != dripc_result_success)
{
cout << "error drpipe_open_named_client" << endl;
Sleep(1000000);
}
string sParam = "123";
long lBytesWrite = 0;
dripc_result rc = drpipe_write(_write_pipe, sParam.c_str(), sParam.size(), (size_t*)&lBytesWrite);
if (dripc_result_success != rc)
{
cout << "error drpipe_write" << endl;
Sleep(1000000);
}
size_t pBytesRead = 0;
#define PIPE_MSG_MAX_SIZE (1024*1024*10)
char* _msgbuf = new char[PIPE_MSG_MAX_SIZE];
memset(_msgbuf, 0, PIPE_MSG_MAX_SIZE);
rc = drpipe_read(_read_pipe, _msgbuf, PIPE_MSG_MAX_SIZE, (size_t*)&pBytesRead);
if (dripc_result_success == rc)
{
cout << "succeed" << endl;
cout << "sParam:" <<_msgbuf << endl;
}
delete[]_msgbuf;
drpipe_close(_write_pipe);
drpipe_close(_read_pipe);
Sleep(1000000);
}
}
模块B
#define DR_IPC_IMPLEMENTATION
#include "..\dr_ipc.h"
#include <string>
#include <iostream>
using namespace std;
int main()
{
drpipe pReadPipe = nullptr;
drpipe pWritePipe = nullptr;
//连接管道
cout << "connect pipe:" << endl;
dripc_result result = drpipe_open_named_client("write123", DR_IPC_READ, &pReadPipe);
if (dripc_result_success != result)
{
cout << "pipe connect fail. read pipename:" << "write123";
Sleep(1000000);
}
cout << "connect pipe success" << endl;
//创建写管道 会卡住等待连接
result = drpipe_open_named_server("read123", DR_IPC_WRITE, &pWritePipe);
if (dripc_result_success != result)
{
cout << "pipe connect fail. write pipename:" << "read123"<< endl;
drpipe_close(pReadPipe);
pReadPipe = nullptr;
Sleep(1000000);
}
//反复从读管道中获取数据,诊断,结果写入写管道
#define RECV_BUF_MAX_SIZE (10*1024*1024)
char* pszrecvbuf = new char[RECV_BUF_MAX_SIZE];
memset(pszrecvbuf, 0, RECV_BUF_MAX_SIZE);
while (true)
{
long lReadSize = 0;
cout << "ready to read msg from pipe" << endl;
dripc_result rc = drpipe_read(pReadPipe, pszrecvbuf, RECV_BUF_MAX_SIZE, (size_t*)&lReadSize);
if (dripc_result_success != rc)
{
cout << "read pipe msg fail. error code:" << rc << endl;
break;
}
cout << "read msg from pipe:" << pszrecvbuf << endl;
string sResult = "finsih";
sResult.assign(pszrecvbuf);
cout << "Result:" << sResult << endl;
//反回结果
long lBytesWrite = 0;
rc = drpipe_write(pWritePipe, sResult.c_str(), sResult.size(), (size_t*)&lBytesWrite);
if (dripc_result_success != rc)
{
cout << "write msg to pipe fail. error code:" << rc << endl;
break;
}
cout << "finish" << endl;
}
delete[]pszrecvbuf;
drpipe_close(pReadPipe);
drpipe_close(pWritePipe);
Sleep(1000000);
}
执行结果
模块A
wait .. DR_IPC_WRITE write123
2
succeed
sParam:123
模块B
connect pipe:
connect pipe success
ready to read msg from pipe
read msg from pipe:123
diag Result:123
diag finish
ready to read msg from pipe
read pipe msg fail. error code:1
原文:https://www.cnblogs.com/beweirdo/p/14764104.html