我可以在0mq(zeromq)ipc套接字上传递文件描述符吗? [英] Can I pass a file descriptor over a 0mq (zeromq) ipc socket?
问题描述
我有一个主进程和几个工作人员,使用0mq'ipc://'套接字(UNIX域套接字)进行通信.我想将文件描述符从主服务器传递到工作程序,以及一条消息.我知道可以使用原始" UNIX域套接字将文件描述符从一个进程传递到另一个进程,但是我可以使用zeromq套接字吗?
I have a master process and several workers, communicating using 0mq 'ipc://' sockets (UNIX domain sockets). I want to pass a file descriptor from the master to a worker, along with a message. I know that 'raw' UNIX domain sockets can be used to pass a file descriptor from one process to another, but can I do it with my zeromq sockets?
我不在乎可移植性,坦率地说,我不在乎它的解决方案是否有点脏.有什么办法吗?
I don't care about portability, and frankly I don't care if its a slightly dirty solution. Is there any way?
谢谢.
推荐答案
最麻烦的方法是添加一个套接字选项,将文件描述符绑定到套接字,然后替换send()
& recv()
以及sendmsg()
和recvmsg()
的每个CMSG
结构,以传递描述符.
Hackiest method would be to add a socket option to bind a file descriptor to the socket, then replace the send()
& recv()
with sendmsg()
and recvmsg()
per the required CMSG
structure to pass the descriptor.
作为全局选项,每条消息都将用描述符标记,因此您应在有效负载内添加适当的元数据,以指示接收者应使用随附的描述符.
As a global option every message would be tagged with the descriptor thus you should add suitable meta-data within the payload to indicate that the receiver should take the accompanying descriptor.
例如发送端
zmq_setsockopt (s, ZMQ_ANCILLIARYFD, &fd, sizeof (fd));
例如接收端
int incoming_fd;
size_t fd_len = sizeof (incoming_fd);
zmq_getsockopt (s, ZMQ_ANCILLIARYFD, &incoming_fd, &fd_len);
这篇关于我可以在0mq(zeromq)ipc套接字上传递文件描述符吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!