如何将常规文件描述符硬塞到 zmq::poller_t 中(特别是使用 cppzmq)?
How can I shoehorn a regular file descriptor into zmq::poller_t (specifically using cppzmq)?
我有一些使用来自 cppzmq 的 template<typename t = no_user_data> class poller_t
的现有代码。它实际上是用 t
实例化的 int
,这并不重要。
在 libzmq 中,有一种方法 zmq_poller_add_fd(void *, int, void *, short)
允许我等待常规文件描述符以及已经使用的 zmq::socket_t 对象。
我需要向 poller_t 对象的 wait_all
添加一个常规文件描述符,但不幸的是,我在cppzmq.
人们用什么方法来解决这个问题?
答案是无法使用 cppzmq 工具完成,但您可以编写自己的 poller_t class 实现,因为它是 FOSS,只需使用额外的扩展即可您需要的功能。它很有魅力。
起点是将 poller_t
模板 class 的实施版本复制到新的头文件中,然后在适当的位置添加以下内容。您可能需要根据 cppzmq 库的发展方式进行细微修改以适应。使用 using
声明将必要的类型引入您的命名空间。
template<
typename Dummy = void,
typename =
typename std::enable_if<!std::is_same<T, no_user_data>::value, Dummy>::type>
void add(int fd, event_flags events, T *user_data)
{
add_fd_impl(fd, events, user_data);
}
void add(int fd, event_flags events)
{
add_fd_impl(fd, events, nullptr);
}
void remove(int fd)
{
if (0 != zmq_poller_remove_fd(poller_ptr.get(), fd)) {
throw error_t();
}
}
void modify(int fd, event_flags events)
{
if (0
!= zmq_poller_modify_fd(poller_ptr.get(), fd,
static_cast<short>(events))) {
throw error_t();
}
}
void add_fd_impl(int fd, event_flags events, T *user_data)
{
if (0
!= zmq_poller_add_fd(poller_ptr.get(), fd, user_data,
static_cast<short>(events))) {
throw error_t();
}
}
我有一些使用来自 cppzmq 的 template<typename t = no_user_data> class poller_t
的现有代码。它实际上是用 t
实例化的 int
,这并不重要。
在 libzmq 中,有一种方法 zmq_poller_add_fd(void *, int, void *, short)
允许我等待常规文件描述符以及已经使用的 zmq::socket_t 对象。
我需要向 poller_t 对象的 wait_all
添加一个常规文件描述符,但不幸的是,我在cppzmq.
人们用什么方法来解决这个问题?
答案是无法使用 cppzmq 工具完成,但您可以编写自己的 poller_t class 实现,因为它是 FOSS,只需使用额外的扩展即可您需要的功能。它很有魅力。
起点是将 poller_t
模板 class 的实施版本复制到新的头文件中,然后在适当的位置添加以下内容。您可能需要根据 cppzmq 库的发展方式进行细微修改以适应。使用 using
声明将必要的类型引入您的命名空间。
template<
typename Dummy = void,
typename =
typename std::enable_if<!std::is_same<T, no_user_data>::value, Dummy>::type>
void add(int fd, event_flags events, T *user_data)
{
add_fd_impl(fd, events, user_data);
}
void add(int fd, event_flags events)
{
add_fd_impl(fd, events, nullptr);
}
void remove(int fd)
{
if (0 != zmq_poller_remove_fd(poller_ptr.get(), fd)) {
throw error_t();
}
}
void modify(int fd, event_flags events)
{
if (0
!= zmq_poller_modify_fd(poller_ptr.get(), fd,
static_cast<short>(events))) {
throw error_t();
}
}
void add_fd_impl(int fd, event_flags events, T *user_data)
{
if (0
!= zmq_poller_add_fd(poller_ptr.get(), fd, user_data,
static_cast<short>(events))) {
throw error_t();
}
}