节点与C应用程序之间的进程间通信 [英] Inter-process communication between node and C application

查看:95
本文介绍了节点与C应用程序之间的进程间通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个要互相交流的软件组件,

I have 2 software componnects I'd like to make talk to each other,

  1. node.js Web应用程序
  2. 用C语言编写的专用服务器(一段相当简单的代码,用于处理一些我不太想用其他语言包装的晦涩的库)

我想要进行的对话非常简单,

The conversations I'd like to have are pretty simple,

  • 节点:设置资源ID A
  • C应用程序:好的,这是参考编号

  • 节点:删除此引用
  • C应用程序:确定,队友.

在2之间传递消息有没有轻松的方法?

Is there any painless way to pass messages between the 2?

我目前的理论构想是这样的:

My theoretical idea currently is something along these lines:

  1. 在2个进程之间有1个tcp/unix套接字,只有1个会话是为了避免始终打开和关闭会话(也担心一次太多).
  2. 每个匹配的节点请求和C应用程序响应具有相同的ID(单个tcp会话上的tcp式会话)
  3. 每个相关的节点请求都将向C app生成消息,并将响应对象存储在一些以会话ID为键的哈希中.
  4. 具有单个节点线程来收集C应用程序答复,通过会话ID查找响应对象并响应客户端

效率低下吗?

节点中实际上有线程支持吗? (简短的Google并未显示任何具体结果)

Is there actually thread support in node? (a short google didn't bring up any concrete results)

推荐答案

基本上,您在正确的方向上,您需要某种进程间通信.这实际上取决于IPC的复杂程度.如果只是一些方法的调用并在方法之间共享简单的数据(即整数,字符串),则可以使用unix套接字,该套接字易于实现,并且由nodejs本地支持,并且很容易从C使用.好(因为所需的标头几乎总是可用的)

Basically you are in the right direction, you need some kind of inter-process-communication. It really depends how complex your IPC is. If it is just about some calling of methods and sharing simple (i.e. integer, strings) data between methods, you could live with unix sockets, that are fairly easy to implement and are supported by nodejs natively and are pretty easy to use from C as well (because the needed headers are almost always already available)

ZeroMQ为您提供传输抽象(允许进行IPC或真正的基于网络的传输),甚至为晦涩的语言提供大量绑定.为C和Node提供的示例应立即帮助您.但是,您需要相应的头文件和库.最有可能需要先构建.

ZeroMQ gives you transport abstraction (allowing for IPC or true network-based transports) and a lot of bindings for even obscure languages. The provided examples for C and Node should get you going in no time. However you need the corresponding headers and libraries; that most likely need to be build first.

如果要在程序之间交换的数据将变得更加复杂(读取结构或数组,嵌套对象),那么您将需要一些实际的RPC实现.有很多RPC实现.从功能上讲,Apache Thrift在我看来非常有前途,尽管最终我无法在合理的时间内构建Thrift编译器.

If the data that you are going to exchange between your programs is going to be more complex (read structs or arrays, nested objects), then you will need some real RPC implementation. There are a bunch of RPC implementations out there; feature-wise Apache Thrift looked very promising to me, although in the end I could not get the thrift compiler being build in a reasonable time.

因此,最后,对我来说,有一个非常相似的用例,我最终使用ZeroMQ进行了传输抽象,并在此之上使用了JSON-RPC作为RPC机制. JSON-RPC对于nodejs和CI使用的都是原生的, https://github.com/pijyoi/jsonrpc ,它基于ZeroMQ和Jansson.该文档不存在,但是如果您走得那么远,就不用担心.

So in the end, for me, having a rather similar use case, I ended up with ZeroMQ for the transport abstraction and on top of that using JSON-RPC as RPC mechanism. JSON-RPC is pretty native for nodejs and for C I used https://github.com/pijyoi/jsonrpc , that builds on ZeroMQ and Jansson. The documentation is non-existent but if you come that far, you shouldn't be afraid.

在性能方面,我认为这不会成为问题.您的用例听起来不对,就像您在短时间内有很多请求一样.由于ZeroMQ还提供传统IPC作为传输方式,因此您也可以使用它来提高性能.

Performance-wise I don't think that this is going to be a problem; your use case does not sound, like you have a lot of requests in a short time. As ZeroMQ also offers traditional IPC as a transport, you could also use that to improve performance.

这篇关于节点与C应用程序之间的进程间通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆