C + + pipeline communication class encapsulation

  • Briefly, the pipeline communication class refers to one kind of IPC communication, that is, the communication between two different processes
  • Now realize the pipeline communication class, so that it can be used later.
  • Header files (referencing related libraries)
// stdafx.h: include file of standard system include file,
// Or frequently used but not often changed
// Project specific include files
//

#pragma once

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>

#include <iostream>
#include <Windows.h>
#include <string>

#include <memory>
// TODO: reference other header files required by the program here

  • Declaration of pipeline communication class
#include "stdafx.h"

enum PIPEUSERTYPE{
	USER_CLIENT, //Client
	USER_SERVER, //Server side
};

//Define pipeline communication class
class PipeIPC{
public:
	PipeIPC();
	~PipeIPC();

	bool InitPipeIPC(std::wstring pipe_name,PIPEUSERTYPE states); //Initialize pipeline communication

	//Read and write data
	bool WriteData(std::string datas); //Write data
	bool ReadData(std::string& datas); //Read data

private:
	std::wstring m_pipe_name;
	HANDLE m_hPipeHandle;
	PIPEUSERTYPE m_state; //Status setting
};
  • Class declaration is also very simple. It mainly implements the two operations of writing data and reading data. At least, the format of writing data and so on are easy to understand
  • Next is the implementation
#include "stdafx.h"
#include "PipeDemo.h"
using namespace std;

//Realization
PipeIPC::PipeIPC()
{
	m_pipe_name = L""; //Set space
	m_hPipeHandle = nullptr;
	m_state = USER_CLIENT; //Client by default
}

PipeIPC::~PipeIPC()
{
	DisconnectNamedPipe(m_hPipeHandle);
	CloseHandle(m_hPipeHandle);//Closing handle
}

bool PipeIPC::InitPipeIPC(std::wstring pipe_name,PIPEUSERTYPE states)
{
	m_state = states;
	std::wstring spice_name = L"\\\\.\\pipe\\";//String to be spliced
	spice_name.append(pipe_name); //Add to
	m_pipe_name.append(spice_name);
	if (m_state == USER_SERVER)
	{
		m_hPipeHandle = CreateNamedPipe(m_pipe_name.c_str(),PIPE_ACCESS_DUPLEX, //Two-way mode
			PIPE_TYPE_MESSAGE|PIPE_READMODE_MESSAGE|PIPE_WAIT,
			PIPE_UNLIMITED_INSTANCES,//Set the maximum number of instances to hold
			0,
			0,
			NULL,nullptr);
		//If pipeline creation fails
		if (m_hPipeHandle == INVALID_HANDLE_VALUE)
		{
			cout << "" << endl;
			return false;
		}
		//Download successful
		cout << "Initialize the server PIPE Success!" << endl;
	}
	else
	{
		m_hPipeHandle = CreateFile(

			m_pipe_name.c_str(), //Name of the object (pipe) created or opened

			GENERIC_READ | //Object access method: read access

			GENERIC_WRITE, //Object access: write access

			0, //Whether the object is shared: 0 means it is not shared

			NULL, //Pointer to a security? Attributes structure

			OPEN_EXISTING, //Object creation method: open  existing means open object (pipe)

			FILE_ATTRIBUTE_NORMAL, //Set properties and flags of objects

			NULL);
		//If pipeline creation fails
		if (m_hPipeHandle == INVALID_HANDLE_VALUE)
		{
			return false;
		}
		//Download successful
		cout << "Initialize client PIPE Success!" << endl;
	}

	return true;
}

//Read data
bool PipeIPC::ReadData(std::string& datas)
{
	DWORD rLen = 0;//Define data length
	//Guard for reading
	char szBuffer[4096] = {0};
	if (!ReadFile(m_hPipeHandle,szBuffer,4096,&rLen,nullptr))
	{
		cout << "read failure" << endl;
	}
	cout << "Read data as:" << szBuffer << endl;
	datas = szBuffer;
	return true;
}

//Write data
bool PipeIPC::WriteData(std::string datas)
{
	//If it's the server, you need to wait for the connection
	if (m_state == USER_SERVER)
	{
		if (!ConnectNamedPipe(m_hPipeHandle,NULL)) //Waiting for client connection
		{
			cout << "Client is not connected yet" << endl;
			//connection failed
			return false;
		}
	}
	//Connection succeeded!
	DWORD wLen;
	//Read data
	WriteFile(m_hPipeHandle,datas.c_str(),strlen(datas.c_str()),&wLen,NULL);
    return true;
}
  • Call method (for server)
	unique_ptr<PipeIPC> m_p(new PipeIPC());
	std::wstring Names = L"NAMEs11s";
	std::string datas = "ddsss";
	m_p->InitPipeIPC(Names,USER_SERVER);
	m_p->WriteData(datas);
	m_p->ReadData(datas);
	system("pause");
  • Call method (for client)
	unique_ptr<PipeIPC> m_p(new PipeIPC());
	std::wstring Names = L"NAMEs11s";
	std::string datas = "ddsss";
	m_p->InitPipeIPC(Names,USER_CLIENT);
	m_p->ReadData(datas);
	std::string datasss = "ceshi";
	m_p->WriteData(datasss);
	system("pause");
	return 0;
  • Screenshot of operation result:
360 original articles published, 78 praised, 350000 visitors+
His message board follow

Tags: Windows

Posted on Sat, 11 Jan 2020 06:55:34 -0800 by sathyan