首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在mqueue中使用Libev io

如何在mqueue中使用Libev io
EN

Stack Overflow用户
提问于 2013-10-28 16:44:34
回答 1查看 1.2K关注 1票数 2

如何在mqueues中使用ev_io?我试着在没有运气的情况下做下面的事。

代码语言:javascript
复制
#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回调从不会在发送消息到队列时被调用。是否可以在libev中使用POSIX mqueue?

EN

回答 1

Stack Overflow用户

发布于 2017-06-18 10:43:42

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

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

轮询消息队列描述符 在Linux上,消息队列描述符实际上是一个文件描述符,可以使用select(2)、轮询(2)或epoll(7)进行监视。这可不是便携式的。

来自mq_overview手册页

您可以利用ev_io监视器侦听传入的消息。

这是LINUX特有的非便携代码.

2)您可以使用函数mq_notify来配置一个信号,当消息队列接收到新消息时,该信号将被发送。这个信号可以由来自libev的ev_signal观察者处理。

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

请参阅mq_notify手册页。

3)切换到面向数据报的套接字,UNIX网络.它们提供了发送消息的大致相等的能力(也称为数据报)。您将获得无连接、尽力而为、不可靠的消息传递服务。

这种方法是非常便携的。

Macintosh注记:

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19640448

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档