Single-machine multi-process communication program

        Recently, the company is working on a project and needs to implement a single-machine multi-process communication program (the number of processes does not exceed 30)

        The project is based on Fedora20, gcc4.8, cmake3.15

Based on the previous company's previous architecture, my idea is to provide a virtual base class, pseudocode such as:

class Base {
public:
    using callback = void (Base::*)(jsonobj)
    Base(string name) : m_strName(name) {
        InitBusiness();
    }
    virtual ~Base() {}
    // 调用之后就一直处理同异步消息
    // block:是否阻塞调用
    bool start(bool block = true);
    /*  
        同步调用
        name:        进程名(应唯一)
        function:    方法名
        param:       参数(json对象)
        result:      调用结果(json对象)
        timeout:     调用超时返回
    */
    int SyncCall(string name, string function, jsonobj param, jsonobj& result, int timeout = 10);
    /*  
        异步调用
        name:        进程名(应唯一)
        function:    方法名
        param:       参数(json对象)
        result:      调用结果(json对象)
        timeout:     调用超时返回
    */
    int AsyncCall(string name, string function, jsonobj param, callback cb);
    /*
        进程初始化,初始化进程自己的业务
    */
protected:
    virtual int InitBusiness() = 0;
    
private:
    const std::string m_strName;
    UnixDomainSocketWrapper* m_pCommunicator;
    // ......
};

Then all process business classes need to inherit this base class to obtain an interface for synchronous and asynchronous communication with other processes. Business class processes such as:

//进程一
class CTest1 : public Base {
public:
	CTest1(string name) : Base(name) {}
	virtual ~CTest1() {}
	// 对外接口
	int Test1(jsonobj, jsonobj&);
protected:
	virtual int InitBusiness() {
		// 初始化业务
		Business1();
		Business2();
		std::thread(&CTest1::CycleBusiness, this).detach();
	}
	
private:
	// 回调
	void OnTest2CB(jsonobj param);
	// 业务
	void Business1() {
		jsonobj param, result;
		SyncCall("CTest2", "Test2", param, result);
		AsyncCall("CTest2", "Test2", param, OnTest2CB);
	}
	void Business2();
	void CycleBusiness();
};

int main() {
	Base* obj = new CTest1("CTest1");
	obj->start();
	delete obj;
	return 0;
}
//进程二
class CTest2 : public Base {
public:
	CTest2(string name) : Base(name) {}
	virtual ~CTest2() {}
	// 对外接口
	int Test2(jsonobj, jsonobj&);
	
protected:
	virtual int InitBusiness() {
		// 初始化业务
		Business1();
		Business2();
		std::thread(&CTest2::CycleBusiness, this).detach();
	}
	
private:
// 回调
	void OnTest1CB(jsonobj param);
	void Business1() {
		jsonobj param, result;
		SyncCall("CTest1", "Test1", param, result);
		AsyncCall("CTest1", "Test1", param, OnTest1CB);
	}
	void Business2();
	void CycleBusiness();
};

int main() {
	Base* obj = new CTest2("CTest2");
	obj->start();
	delete obj;
	return 0;
}

 The above pseudo code is just for example, record it here

The bottom layer of the project communication is the Unix local socket, and it is currently considering using zeromq as an alternative.

Zeromq implements a parallel development framework (concurrency framework) in the form of an embedded network programming library, which
can provide in-process (inproc), inter-process (IPC), network (TCP) and broadcast message channels,
and supports fan-out ( Fan-out), publish-subscribe (pub-sub), task distribution (task distribution), request/response (request-reply) and other communication modes.
The performance of ZeroMQ is sufficient to build cluster products, and
its asynchronous I/O model can provide sufficient scalability for multi-core message systems.
ZeroMQ supports APIs in more than 30 languages ​​and can be used in most operating systems.
While providing these excellent features, ZeroMQ is open source and follows the LGPLv3 license.

The stated goal of ZeroMQ is "to be part of the standard networking stack and later into the Linux kernel".

I don’t know why I can’t get out of the editor when I use the edge browser to click to publish an article on windows, so I am puzzled. . .

Guess you like

Origin blog.csdn.net/Sheleon1995/article/details/120469285