libevent示例代码的一个问题:如何调用?

发布于 2024-11-28 01:36:08 字数 5626 浏览 0 评论 0原文

我正在学习libev,但是代码很难理解,所以我选择先学习libevent,它的代码相对更清晰。但是在尝试示例时遇到问题(http://www.wangafu. net/~nickm/libevent-book/01_intro.html)。

do_read() 中的代码 event_add(state->write_event, NULL) 是如何调用 do_write() 函数的?

    /* For sockaddr_in */
#include <netinet/in.h>
/* For socket functions */
#include <sys/socket.h>
/* For fcntl */
#include <fcntl.h>

#include <event2/event.h>

#include <assert.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

#define MAX_LINE 16384

void do_read(evutil_socket_t fd, short events, void *arg);
void do_write(evutil_socket_t fd, short events, void *arg);

char
rot13_char(char c)
{
    return c;
    /* We don't want to use isalpha here; setting the locale would change
     * which characters are considered alphabetical. */
    if ((c >= 'a' && c <= 'm') || (c >= 'A' && c <= 'M'))
        return c + 13;
    else if ((c >= 'n' && c <= 'z') || (c >= 'N' && c <= 'Z'))
        return c - 13;
    else
        return c;
}

struct fd_state {
    char buffer[MAX_LINE];
    size_t buffer_used;

    size_t n_written;
    size_t write_upto;

    struct event *read_event;
    struct event *write_event;
};

struct fd_state *
alloc_fd_state(struct event_base *base, evutil_socket_t fd)
{
    struct fd_state *state = malloc(sizeof(struct fd_state));
    if (!state)
        return NULL;
    state->read_event = event_new(base, fd, EV_READ|EV_PERSIST, do_read, state);
    if (!state->read_event) {
        free(state);
        return NULL;
    }
    state->write_event =
        event_new(base, fd, EV_WRITE|EV_PERSIST, do_write, state);

    if (!state->write_event) {
        event_free(state->read_event);
        free(state);
        return NULL;
    }

    state->buffer_used = state->n_written = state->write_upto = 0;

    assert(state->write_event);
    return state;
}

void
free_fd_state(struct fd_state *state)
{
    event_free(state->read_event);
    event_free(state->write_event);
    free(state);
}

void
do_read(evutil_socket_t fd, short events, void *arg)
{
    struct fd_state *state = arg;
    char buf[1024];
    int i;
    ssize_t result;
    while (1) {
        assert(state->write_event);
        result = recv(fd, buf, sizeof(buf), 0);
        if (result <= 0)
            break;

        for (i=0; i < result; ++i)  {
            if (state->buffer_used < sizeof(state->buffer))
                state->buffer[state->buffer_used++] = rot13_char(buf[i]);
            if (buf[i] == '\n') {
                assert(state->write_event);
                **event_add(state->write_event, NULL);**
                state->write_upto = state->buffer_used;
            }
        }
    }

    if (result == 0) {
        free_fd_state(state);
    } else if (result < 0) {
        if (errno == EAGAIN) // XXXX use evutil macro
            return;
        perror("recv");
        free_fd_state(state);
    }
}

void
**do_write(evutil_socket_t fd, short events, void *arg)**
{
    struct fd_state *state = arg;

    while (state->n_written < state->write_upto) {
        ssize_t result = send(fd, state->buffer + state->n_written,
                              state->write_upto - state->n_written, 0);
        if (result < 0) {
            if (errno == EAGAIN) // XXX use evutil macro
                return;
            free_fd_state(state);
            return;
        }
        assert(result != 0);

        state->n_written += result;
    }

    if (state->n_written == state->buffer_used)
        state->n_written = state->write_upto = state->buffer_used = 1;

    event_del(state->write_event);
}

void
do_accept(evutil_socket_t listener, short event, void *arg)
{
    struct event_base *base = arg;
    struct sockaddr_storage ss;
    socklen_t slen = sizeof(ss);
    int fd = accept(listener, (struct sockaddr*)&ss, &slen);
    if (fd < 0) { // XXXX eagain??
        perror("accept");
    } else if (fd > FD_SETSIZE) {
        close(fd); // XXX replace all closes with EVUTIL_CLOSESOCKET */
    } else {
        struct fd_state *state;
        evutil_make_socket_nonblocking(fd);
        state = alloc_fd_state(base, fd);
        assert(state); /*XXX err*/
        assert(state->write_event);
        event_add(state->read_event, NULL);
    }
}

void
run(void)
{
    evutil_socket_t listener;
    struct sockaddr_in sin;
    struct event_base *base;
    struct event *listener_event;

    base = event_base_new();
    if (!base)
        return; /*XXXerr*/

    sin.sin_family = AF_INET;
    sin.sin_addr.s_addr = 0;
    sin.sin_port = htons(40713);

    listener = socket(AF_INET, SOCK_STREAM, 0);
    evutil_make_socket_nonblocking(listener);

#ifndef WIN32
    {
        int one = 1;
        setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
    }
#endif

    if (bind(listener, (struct sockaddr*)&sin, sizeof(sin)) < 0) {
        perror("bind");
        return;
    }

    if (listen(listener, 16)<0) {
        perror("listen");
        return;
    }

    listener_event = event_new(base, listener, EV_READ|EV_PERSIST, do_accept, (void*)base);
    /*XXX check it */
    event_add(listener_event, NULL);

    event_base_dispatch(base);
}

int
main(int c, char **v)
{
    setvbuf(stdout, NULL, _IONBF, 0);

    run();
    return 0;
}

I'm learning libev however the code is so hard to understand, so I choose to learn libevent first whose code is relatively clearer. But I encounter a problem when try the example (http://www.wangafu.net/~nickm/libevent-book/01_intro.html).

How is the code event_add(state->write_event, NULL) in do_read() make do_write() function invoked?

    /* For sockaddr_in */
#include <netinet/in.h>
/* For socket functions */
#include <sys/socket.h>
/* For fcntl */
#include <fcntl.h>

#include <event2/event.h>

#include <assert.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

#define MAX_LINE 16384

void do_read(evutil_socket_t fd, short events, void *arg);
void do_write(evutil_socket_t fd, short events, void *arg);

char
rot13_char(char c)
{
    return c;
    /* We don't want to use isalpha here; setting the locale would change
     * which characters are considered alphabetical. */
    if ((c >= 'a' && c <= 'm') || (c >= 'A' && c <= 'M'))
        return c + 13;
    else if ((c >= 'n' && c <= 'z') || (c >= 'N' && c <= 'Z'))
        return c - 13;
    else
        return c;
}

struct fd_state {
    char buffer[MAX_LINE];
    size_t buffer_used;

    size_t n_written;
    size_t write_upto;

    struct event *read_event;
    struct event *write_event;
};

struct fd_state *
alloc_fd_state(struct event_base *base, evutil_socket_t fd)
{
    struct fd_state *state = malloc(sizeof(struct fd_state));
    if (!state)
        return NULL;
    state->read_event = event_new(base, fd, EV_READ|EV_PERSIST, do_read, state);
    if (!state->read_event) {
        free(state);
        return NULL;
    }
    state->write_event =
        event_new(base, fd, EV_WRITE|EV_PERSIST, do_write, state);

    if (!state->write_event) {
        event_free(state->read_event);
        free(state);
        return NULL;
    }

    state->buffer_used = state->n_written = state->write_upto = 0;

    assert(state->write_event);
    return state;
}

void
free_fd_state(struct fd_state *state)
{
    event_free(state->read_event);
    event_free(state->write_event);
    free(state);
}

void
do_read(evutil_socket_t fd, short events, void *arg)
{
    struct fd_state *state = arg;
    char buf[1024];
    int i;
    ssize_t result;
    while (1) {
        assert(state->write_event);
        result = recv(fd, buf, sizeof(buf), 0);
        if (result <= 0)
            break;

        for (i=0; i < result; ++i)  {
            if (state->buffer_used < sizeof(state->buffer))
                state->buffer[state->buffer_used++] = rot13_char(buf[i]);
            if (buf[i] == '\n') {
                assert(state->write_event);
                **event_add(state->write_event, NULL);**
                state->write_upto = state->buffer_used;
            }
        }
    }

    if (result == 0) {
        free_fd_state(state);
    } else if (result < 0) {
        if (errno == EAGAIN) // XXXX use evutil macro
            return;
        perror("recv");
        free_fd_state(state);
    }
}

void
**do_write(evutil_socket_t fd, short events, void *arg)**
{
    struct fd_state *state = arg;

    while (state->n_written < state->write_upto) {
        ssize_t result = send(fd, state->buffer + state->n_written,
                              state->write_upto - state->n_written, 0);
        if (result < 0) {
            if (errno == EAGAIN) // XXX use evutil macro
                return;
            free_fd_state(state);
            return;
        }
        assert(result != 0);

        state->n_written += result;
    }

    if (state->n_written == state->buffer_used)
        state->n_written = state->write_upto = state->buffer_used = 1;

    event_del(state->write_event);
}

void
do_accept(evutil_socket_t listener, short event, void *arg)
{
    struct event_base *base = arg;
    struct sockaddr_storage ss;
    socklen_t slen = sizeof(ss);
    int fd = accept(listener, (struct sockaddr*)&ss, &slen);
    if (fd < 0) { // XXXX eagain??
        perror("accept");
    } else if (fd > FD_SETSIZE) {
        close(fd); // XXX replace all closes with EVUTIL_CLOSESOCKET */
    } else {
        struct fd_state *state;
        evutil_make_socket_nonblocking(fd);
        state = alloc_fd_state(base, fd);
        assert(state); /*XXX err*/
        assert(state->write_event);
        event_add(state->read_event, NULL);
    }
}

void
run(void)
{
    evutil_socket_t listener;
    struct sockaddr_in sin;
    struct event_base *base;
    struct event *listener_event;

    base = event_base_new();
    if (!base)
        return; /*XXXerr*/

    sin.sin_family = AF_INET;
    sin.sin_addr.s_addr = 0;
    sin.sin_port = htons(40713);

    listener = socket(AF_INET, SOCK_STREAM, 0);
    evutil_make_socket_nonblocking(listener);

#ifndef WIN32
    {
        int one = 1;
        setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
    }
#endif

    if (bind(listener, (struct sockaddr*)&sin, sizeof(sin)) < 0) {
        perror("bind");
        return;
    }

    if (listen(listener, 16)<0) {
        perror("listen");
        return;
    }

    listener_event = event_new(base, listener, EV_READ|EV_PERSIST, do_accept, (void*)base);
    /*XXX check it */
    event_add(listener_event, NULL);

    event_base_dispatch(base);
}

int
main(int c, char **v)
{
    setvbuf(stdout, NULL, _IONBF, 0);

    run();
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海夜未眠 2024-12-05 01:36:08

我不确定我是否在回答您提出的同一问题 - 我的理解是:

do_read()中调用event_add(state->write_event, NULL)如何导致do_write()被调用?< /p>

弄清楚这一点的关键是理解 do_read() 函数实际上在做什么。 do_read() 是与有数据要读取的套接字关联的回调函数:这是通过allocate_fd_state() 设置的:

struct fd_state *
alloc_fd_state(struct event_base *base, evutil_socket_t fd)
{
    /*
     * Allocate a new fd_state structure, which will hold our read and write events
     * /
    struct fd_state *state = malloc(sizeof(struct fd_state));

    [...]
    /*
     * Initialize a read event on the given file descriptor: associate the event with
     * the given base, and set up the do_read callback to be invoked whenever
     * data is available to be read on the file descriptor.
     * /
    state->read_event = event_new(base, fd, EV_READ|EV_PERSIST, do_read, state);
    [...]
    /*
     * Set up another event on the same file descriptor and base, which invoked the
     * do_write callback anytime the file descriptor is ready to be written to.
     */
    state->write_event =
        event_new(base, fd, EV_WRITE|EV_PERSIST, do_write, state);
    [...]
    return state;
}

但此时,这些事件都没有被 event_add 处理()'ed 到 event_base 基础。做什么的说明都写好了,但没有人看。那么如何读取内容呢?在建立传入连接后,state->read_eventevent_add() 添加到基础。看一下 do_accept()

void
do_accept(evutil_socket_t listener, short event, void *arg)
{
    [ ... accept a new connection and give it a file descriptor fd ... ]

    /*
     * If the file descriptor is invalid, close it.
     */
    if (fd < 0) { // XXXX eagain??
        perror("accept");
    } else if (fd > FD_SETSIZE) {
        close(fd); // XXX replace all closes with EVUTIL_CLOSESOCKET */
    /*
     * Otherwise, if the connection was successfully accepted...
     */
    } else {
        [ ... allocate a new fd_state structure, and make the file descriptor non-blocking ...]
        /*
         * Here's where the magic happens. The read_event created back in alloc_fd_state()
         * is finally added to the base associated with it.
         */
        event_add(state->read_event, NULL);
    }
}

接受新连接后,程序会告诉 libevent 等待,直到连接上有可用数据,然后运行 ​​do_read() 回调。此时,仍然无法调用do_write()。需要对其进行 event_add() 编辑。这发生在 do_read() 中:

void
do_read(evutil_socket_t fd, short events, void *arg)
{
    /* Create a temporary buffer to receive some data */
    char buf[1024];

    while (1) {
        [ ... Receive the data, copying it into buf ... ]
        [ ... if there is no more data to receive, or there was an error, exit this loop... ]

        [ ... else, result = number of bytes received ... ]
        for (i=0; i < result; ++i)  {
            [ ... if there's room in the buffer, copy in the rot13() encoded
                  version of the received data ... ]
            /*
             * Boom, headshot. If we've reached the end of the incoming data
             * (assumed to be a newline), then ...
             */
            if (buf[i] == '\n') {
                [...]
                /*
                 * Have libevent start monitoring the write_event, which calls do_write
                 * as soon as the file descriptor is ready to be written to.
                 */
                event_add(state->write_event, NULL);
                [...]
            }
        }
    }
    [...]    
}

因此,从文件描述符读取一些数据后,程序开始等待,直到
文件描述符已准备好写入,然后调用do_write()。程序
流程看起来像这样:

[ set up an event_base and start waiting for events ]
[ if someone tries to connect ]
    [ accept the connection ]

[ ... wait until there is data to read on the connection ... ]

[ read in data from the connection until there is no more left ]

[ ....wait until the connection is ready to be written to ... ]

[ write out our rot13() encoded response ]

我希望a)这是对你的问题的正确解释,b)这是一个有用的答案。

I'm not sure if I'm answering the same question you asked - I understand it as:

How does calling event_add(state->write_event, NULL) in do_read() lead to do_write() being invoked?

The key to figuring this out is understanding what the do_read() function is actually doing. do_read() is a callback function associated with a socket which has data to be read: this is set up with allocate_fd_state():

struct fd_state *
alloc_fd_state(struct event_base *base, evutil_socket_t fd)
{
    /*
     * Allocate a new fd_state structure, which will hold our read and write events
     * /
    struct fd_state *state = malloc(sizeof(struct fd_state));

    [...]
    /*
     * Initialize a read event on the given file descriptor: associate the event with
     * the given base, and set up the do_read callback to be invoked whenever
     * data is available to be read on the file descriptor.
     * /
    state->read_event = event_new(base, fd, EV_READ|EV_PERSIST, do_read, state);
    [...]
    /*
     * Set up another event on the same file descriptor and base, which invoked the
     * do_write callback anytime the file descriptor is ready to be written to.
     */
    state->write_event =
        event_new(base, fd, EV_WRITE|EV_PERSIST, do_write, state);
    [...]
    return state;
}

At this point, though, neither of these events have been event_add()'ed to the event_base base. The instructions for what to do are all written out, but no one is looking at them. So how does anything get read? state->read_event is event_add()'ed to the base after an incoming connection is made. Look at do_accept():

void
do_accept(evutil_socket_t listener, short event, void *arg)
{
    [ ... accept a new connection and give it a file descriptor fd ... ]

    /*
     * If the file descriptor is invalid, close it.
     */
    if (fd < 0) { // XXXX eagain??
        perror("accept");
    } else if (fd > FD_SETSIZE) {
        close(fd); // XXX replace all closes with EVUTIL_CLOSESOCKET */
    /*
     * Otherwise, if the connection was successfully accepted...
     */
    } else {
        [ ... allocate a new fd_state structure, and make the file descriptor non-blocking ...]
        /*
         * Here's where the magic happens. The read_event created back in alloc_fd_state()
         * is finally added to the base associated with it.
         */
        event_add(state->read_event, NULL);
    }
}

So right after accepting a new connection, the program tells libevent to wait until there's data available on the connection, and then run the do_read() callback. At this point, it's still impossible for do_write() to be called. It needs to be event_add()'ed. This happens in do_read():

void
do_read(evutil_socket_t fd, short events, void *arg)
{
    /* Create a temporary buffer to receive some data */
    char buf[1024];

    while (1) {
        [ ... Receive the data, copying it into buf ... ]
        [ ... if there is no more data to receive, or there was an error, exit this loop... ]

        [ ... else, result = number of bytes received ... ]
        for (i=0; i < result; ++i)  {
            [ ... if there's room in the buffer, copy in the rot13() encoded
                  version of the received data ... ]
            /*
             * Boom, headshot. If we've reached the end of the incoming data
             * (assumed to be a newline), then ...
             */
            if (buf[i] == '\n') {
                [...]
                /*
                 * Have libevent start monitoring the write_event, which calls do_write
                 * as soon as the file descriptor is ready to be written to.
                 */
                event_add(state->write_event, NULL);
                [...]
            }
        }
    }
    [...]    
}

So, after reading in some data from a file descriptor, the program starts waiting until
the file descriptor is ready to be written to, and then invokes do_write(). Program
flow looks like this:

[ set up an event_base and start waiting for events ]
[ if someone tries to connect ]
    [ accept the connection ]

[ ... wait until there is data to read on the connection ... ]

[ read in data from the connection until there is no more left ]

[ ....wait until the connection is ready to be written to ... ]

[ write out our rot13() encoded response ]

I hope that a) that was the correct interpretation of your question, and b) this was a helpful answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文