如何将 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 一起使用?