C++ 任何用于进程间调用的简单易用的 RPC 库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5398673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
any good and simple RPC library for inter-process calls?
提问by Andriy Tylychko
I need to send a (probably one) simple one-way command from client processes to server process with arguments of builtin C++ types (so serialization is pretty simple). C++, Windows XP+.
我需要使用内置 C++ 类型的参数将一个(可能是一个)简单的单向命令从客户端进程发送到服务器进程(因此序列化非常简单)。C++、Windows XP+。
I'm looking for a library that doesn't require complicated configuration, provides simple interface, doesn't require hours to days of learning and doesn't have commercial usage restrictions. Simple solution for simple problem.
我正在寻找一个不需要复杂配置、提供简单界面、不需要数小时到数天的学习并且没有商业使用限制的库。简单问题的简单解决方案。
Boost.Interprocessis too low-level for this simple task because doesn't provide RPC interface. Sockets are probably an overkill too because I don't need to communicate between machines. The same about DCOM, CORBA et al. Named pipes? Never used them, any good library over WinAPI? OpenMPI?
Boost.Interprocess对于这个简单的任务来说太低级了,因为它不提供 RPC 接口。套接字也可能是一种矫枉过正,因为我不需要在机器之间进行通信。DCOM、CORBA 等人也是如此。命名管道?从来没有用过它们,有没有比 WinAPI 好的库?开放式MPI?
采纳答案by Tim Sylvester
I don't think sockets are really overkill. The alternatives all have their own problems and sockets are far better supported than named pipes, shared memory, etc., because almost everyone is using them. The speed of sockets on local system is probably not an issue.
我不认为套接字真的矫枉过正。替代方案都有自己的问题,套接字比命名管道、共享内存等得到更好的支持,因为几乎每个人都在使用它们。本地系统上的套接字速度可能不是问题。
There's Apache Thrift:
有 Apache Thrift:
http://incubator.apache.org/thrift/
http://incubator.apache.org/thrift/
There are a few RPC implementations wrapped around Google's protobuf library as the marshaling mechanism:
有一些 RPC 实现围绕 Google 的 protobuf 库作为封送机制:
https://github.com/google/protobuf/blob/master/docs/third_party.md#rpc-implementations
https://github.com/google/protobuf/blob/master/docs/third_party.md#rpc-implementations
There's XML-RPC:
有 XML-RPC:
http://xmlrpc-c.sourceforge.net/
http://xmlrpc-c.sourceforge.net/
If your messages are reallysimple, I might consider using UDP packets, then there are no connections to manage.
如果你的消息真的很简单,我可能会考虑使用 UDP 数据包,那么就没有要管理的连接。
回答by Imbrondir
You might like ZeroMQfor something like this. Perhaps not as much a complete RPC, as a raw byte messaging framework you could use to make an RPC. It's simple, lightweight and with an impressive performance. You can easilly implement an RPC on top of it. Here's an example server straight from the manual:
你可能会喜欢ZeroMQ 这样的东西。也许不是完整的 RPC,而是可以用来制作 RPC 的原始字节消息传递框架。它简单、轻便且具有令人印象深刻的性能。您可以在其上轻松实现 RPC。这是直接来自手册的示例服务器:
//
// Hello World server in C++
// Binds REP socket to tcp://*:5555
// Expects "Hello" from client, replies with "World"
//
#include <zmq.hpp>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
int main () {
// Prepare our context and socket
zmq::context_t context (1);
zmq::socket_t socket (context, ZMQ_REP);
socket.bind ("tcp://*:5555");
while (true) {
zmq::message_t request;
// Wait for next request from client
socket.recv (&request);
printf ("Received Hello");
// Do some 'work'
sleep (1);
// Send reply back to client
zmq::message_t reply (5);
memcpy ((void *) reply.data (), "World", 5);
socket.send (reply);
}
return 0;
}
This example uses tcp://*.5555, but uses more efficient IPC techniques if you use:
此示例使用 tcp://*.5555,但如果您使用以下内容,则使用更高效的 IPC 技术:
socket.bind("ipc://route.to.ipc");
or even faster inter thread protocol:
甚至更快的线程间协议:
socket.bind("inproc://path.for.client.to.connect");
回答by dalle
If you only need to support Windows I'd use the Windows built-in RPC, I've written two introductory articles about this:
如果你只需要支持 Windows 我会使用 Windows 内置的 RPC,我已经写了两篇关于这个的介绍性文章:
http://www.codeproject.com/KB/IP/rpcintro1.aspx
http://www.codeproject.com/KB/IP/rpcintro2.aspx
http://www.codeproject.com/KB/IP/rpcintro1.aspx
http://www.codeproject.com/KB/IP/rpcintro2.aspx
You could use the ncalrpc
protocol if you only need local inter-process communication.
ncalrpc
如果您只需要本地进程间通信,则可以使用该协议。
回答by log0
Boost.MPI. Simple, fast, scalable.
升压.MPI。简单、快速、可扩展。
#include <boost/mpi/environment.hpp>
#include <boost/mpi/communicator.hpp>
#include <iostream>
#include <sstream>
namespace mpi = boost::mpi;
int main(int argc, char* argv[])
{
mpi::environment env(argc, argv);
mpi::communicator world;
std::stringstream ss;
ss << "Hello, I am process " << world.rank() << " of " << world.size() << ".";
world.send(1, 0, ss.str());
}
回答by Ben
If you are working on windows only, and really need a C++ interface, use COM/DCOM. It is based on RPC (in turn based on DCE RPC).
如果您只在 Windows 上工作,并且确实需要 C++ 接口,请使用 COM/DCOM。它基于RPC(反过来基于DCE RPC)。
It is extremely simple to use -- provided you take the time to learn the basics.
它使用起来非常简单——只要您花时间学习基础知识。
- ATL: http://msdn.microsoft.com/en-us/library/3ax346b7(VS.71).aspx
- Interface Definition Language: http://msdn.microsoft.com/en-us/library/aa367091(VS.85).aspx
回答by mkaes
回答by edgar.holleis
You probably don't even need a library. Windows has an IPC mechanism built deeply into its core APIs (windows.h). You can basically post a windows message into the message-queue of a different processes main window. Windows even defines a standard message to do just that: WM_COPYDATA.
你可能甚至不需要图书馆。Windows 在其核心 API (windows.h) 中内置了一个 IPC 机制。您基本上可以将 Windows 消息发布到不同进程主窗口的消息队列中。Windows 甚至定义了一个标准消息来做到这一点:WM_COPYDATA。
- MSDN docu on WM_COPYDATA
- MSDN demo code
- More demo code the following StackOverflow response
- WM_COPYDATA 上的MSDN 文档
- MSDN演示代码
- 更多演示代码如下 StackOverflow响应
The sending process basically does:
发送过程基本上是:
The receiving process (window):
接收过程(窗口):
- On Vista and later has to modify its message filter using ChangeWindowsMessageEx
- Override its WindowProc
- In order to handle the incoming WM_COPYDATA
- 在 Vista 及更高版本上必须使用ChangeWindowsMessageEx修改其消息过滤器
- 覆盖其WindowProc
- 为了处理传入的WM_COPYDATA
回答by asukharev
Also, you might look at msgpack-rpc
另外,您可能会查看msgpack-rpc
Update
更新
While Thrift/Protobuf are more flexible, I think, but there are require to write some code in specific format. For example, Protobuf needs some .proto file, which can be compile with specific compiler from package, that genegate some classes. In some cases it might be more difficult that other parts of code. msgpack-rpc is much simpler. It doesn't require write some extra code. Here is example:
虽然 Thrift/Protobuf 更灵活,我认为,但需要以特定格式编写一些代码。例如,Protobuf 需要一些 .proto 文件,该文件可以用特定的编译器从包中编译,生成一些类。在某些情况下,代码的其他部分可能更难。msgpack-rpc 简单得多。它不需要编写一些额外的代码。这是示例:
#include <iostream>
#include <msgpack/rpc/server.h>
#include <msgpack/rpc/client.h>
class Server: public msgpack::rpc::dispatcher {
public:
typedef msgpack::rpc::request request_;
Server() {};
virtual ~Server() {};
void dispatch(request_ req)
try {
std::string method;
req.method().convert(&method);
if (method == "id") {
id(req);
} else if (method == "name") {
name(req);
} else if (method == "err") {
msgpack::type::tuple<> params;
req.params().convert(¶ms);
err(req);
} else {
req.error(msgpack::rpc::NO_METHOD_ERROR);
}
}
catch (msgpack::type_error& e) {
req.error(msgpack::rpc::ARGUMENT_ERROR);
return;
}
catch (std::exception& e) {
req.error(std::string(e.what()));
return;
}
void id(request_ req) {
req.result(1);
}
void name(request_ req) {
req.result(std::string("name"));
}
void err(request_ req) {
req.error(std::string("always fail"));
}
};
int main() {
// { run RPC server
msgpack::rpc::server server;
std::auto_ptr<msgpack::rpc::dispatcher> dispatcher(new Server);
server.serve(dispatcher.get());
server.listen("0.0.0.0", 18811);
server.start(1);
// }
msgpack::rpc::client c("127.0.0.1", 18811);
int64_t id = c.call("id").get<int64_t>();
std::string name = c.call("name").get<std::string>();
std::cout << "ID: " << id << std::endl;
std::cout << "name: " << name << std::endl;
return 0;
}
Output
输出
ID: 1
name: name
More complicated examples you can find here https://github.com/msgpack/msgpack-rpc/tree/master/cpp/test
你可以在这里找到更复杂的例子https://github.com/msgpack/msgpack-rpc/tree/master/cpp/test
回答by Chris O
There's also Microsoft Messaging Queueing, which is fairly straightforward to use when all processes are on the local machine.
还有Microsoft Messaging Queuing,当所有进程都在本地机器上时,使用起来相当简单。