c - 如何将 Libev io 与 mqueue 一起使用

标签 c posix message-queue libev

我如何将 ev_io 与 mqueue 一起使用?我正在尝试执行以下操作,但运气不佳。

#include <fcntl.h>
#include <sys/stat.h>
#include <mqueue.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "ev.h"

#define MAX_Q_SIZE 255
#define MY_QUEUE "/test_queue"

typedef struct __test_ctxt_t
{
    ev_timer    timeout_watcher[32];
    ev_io       stdin_watcher;
    struct ev_loop *loop;
    mqd_t       mq;
    int         data;
}test_ctxt_t;

static test_ctxt_t *g_ctxt = NULL;

static void mq_callback(EV_P_ struct ev_io *w, int revents)
{
    test_ctxt_t *ctxt = (test_ctxt_t *)w;
    struct      mq_attr attr;
    char        msg[256];
    int         rcvd_msg_size;

    rcvd_msg_size = mq_receive(ctxt->mq, msg, MAX_Q_SIZE, NULL);
    if (rcvd_msg_size >= 0)
    {
        msg[rcvd_msg_size] = '\0';
        printf("Received: %s\n", msg);
        if (strcmp(msg, "stop") == 0)
        {
            printf("Exiting....\n");
            ev_unloop (EV_A_ EVUNLOOP_ONE);
        }
    }
}

static void timeout_cb1 (EV_P_ struct ev_timer *w, int revents)
{
  puts ("timeout timeout_cb1");
  //ev_unloop (EV_A_ EVUNLOOP_ONE);
}

static void timeout_cb2 (EV_P_ struct ev_timer *w, int revents)
{
  puts ("timeout timeout_cb2");
  //ev_unloop (EV_A_ EVUNLOOP_ONE);
}
static void timeout_cb3 (EV_P_ struct ev_timer *w, int revents)
{
  puts ("timeout timeout_cb3");
  //ev_unloop (EV_A_ EVUNLOOP_ONE);
}

int main (void)
{
    struct      mq_attr attr;

    g_ctxt = (test_ctxt_t *)calloc(1, sizeof(test_ctxt_t));

    g_ctxt->loop = ev_default_loop (0);

    /* initialize the queue attributes */
    attr.mq_flags = 0;
    attr.mq_maxmsg = 10;
    attr.mq_msgsize = 255;
    g_ctxt->mq = mq_open(MY_QUEUE, O_CREAT | O_RDONLY, 0644, &attr);
    if (g_ctxt->mq == -1)
    {
        printf("Unable to open Queue");
        return -1;
    }

    ev_io_init(&g_ctxt->stdin_watcher, mq_callback, g_ctxt->mq, EV_READ);
    ev_io_start(g_ctxt->loop, &g_ctxt->stdin_watcher);

    ev_timer_init (&g_ctxt->timeout_watcher[0], timeout_cb1, 10, 0.);
    ev_timer_start (g_ctxt->loop, &g_ctxt->timeout_watcher[0]);

    ev_loop (g_ctxt->loop, 0);

    return 0;
}

我能够获得计时器回调,但在向队列发送消息时永远不会调用 io 回调。是否可以将 POSIX mqueue 与 libev 一起使用?

最佳答案

基本上有三种方法,如何做到这一点:

1) 在Linux平台上,消息队列处理程序是一个有效的文件描述符。

Polling message queue descriptors

On Linux, a message queue descriptor is actually a file descriptor, and can be monitored using select(2), poll(2), or epoll(7). This is not portable.

来自 mq_overview手册页

您可以利用 ev_io 观察器来监听传入的消息。

这是特定于 LINUX 且不可移植的代码。

2) 您可以使用函数mq_notify 来配置一个信号,只要消息队列收到一条新消息就会发送该信号。此信号可由 libev 的 ev_signal 观察器处理。

这似乎是更可移植的代码,但由于信号交互,性能会降低一些。

参见 mq_notify手册页。

3) 切换到面向数据报的套接字,UNIXnetwork .它们提供了大致相同的发送消息(又名数据报)的能力。您将获得无连接、尽力传递、不可靠的消息传递服务。

这种方法非常便携。

Macintosh 笔记:
macOS 中没有 POSIX 消息队列实现,因此此类代码的可移植性在此 Apple 平台上受到限制。有可能在 NSOperationQueue 或 Grand Central Dispatch 上模仿相同的功能,但这肯定会导致大量的工作。

关于c - 如何将 Libev io 与 mqueue 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19640448/

相关文章:

c++ - __do_global_ctors_aux 未显示在 objdump 中

c - connect() 怎么会失败并将 errno 设置为 EADDRINUSE

python - 如何捕获一系列 Celery 任务执行过程中产生的所有 python 日志记录?

performance - RabbitMQ 与 MSMQ 的比较

C 无限循环中的客户端/服务器失败

为 ARM (Raspberry Pi) 交叉编译 R

对 C 限制限定符感到困惑

c++ - 使用 Auto 和 Lambda 处理信号?

c - 退出时从子进程传播 WTERMSIG 退出代码

python - 如何使用 RabbitMQ 在预定时间运行任务