首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用函数结构的libuv代替函数回调

使用函数结构的libuv代替函数回调
EN

Stack Overflow用户
提问于 2015-12-23 18:09:18
回答 1查看 810关注 0票数 1

有人知道为什么在使用函子结构而不是作为回调的实际函数时libuv分段错误。在下面的示例中,我创建了CB结构来代替connection_cb。在运算符()中发生分段错误。

代码语言:javascript
复制
#include <uv.h>
#include <stdio.h>
#include <stdlib.h>


uv_buf_t alloc_buffer(uv_handle_t * handle, size_t size);
void connection_cb(uv_stream_t * server, int status);
void read_cb(uv_stream_t * stream, ssize_t nread, uv_buf_t buf);

struct CB {
    State *state_;
    CB(State *state) : state_(state) {}
    void operator()(uv_stream_t *server, int status) {
        uv_tcp_t * client = (uv_tcp_t *)malloc(sizeof(uv_tcp_t));

        /* if status not zero there was an error */
        if (status == -1) {
            printf("error 2\n");
        }

        /* initialize the new client */
        uv_tcp_init(loop, client);

        /* now let bind the client to the server to be used for incomings */
        if (uv_accept(server, (uv_stream_t *) client) == 0) {
            /* start reading from stream */
            int r = uv_read_start((uv_stream_t *) client, (uv_alloc_cb)alloc_buffer, read_cb);

            if (r) {
                printf("error 3\n");
            }
            state_->onConnect();
        } else {
            /* close client stream on error */
            uv_close((uv_handle_t *) client, NULL);
        }
    }
};

CB cb;
uv_tcp_t server;
uv_loop_t * loop;

int main() {
    loop = uv_default_loop();

    /* convert a humanreadable ip address to a c struct */
    struct sockaddr_in addr;
    uv_ip4_addr("127.0.0.1", 3005, &addr);

    /* initialize the server */
    uv_tcp_init(loop, &server);
    /* bind the server to the address above */
    uv_tcp_bind(&server, (const struct sockaddr *)&addr, 0);

    /* let the server listen on the address for new connections */
    int r = uv_listen((uv_stream_t *) &server, 128, (uv_connection_cb)&cb);

    if (r) {
        printf("error 1\n");
        return -1;
    }

    /* execute all tasks in queue */
    return uv_run(loop, UV_RUN_DEFAULT);
}


/**
 * Callback which is executed on each new connection.
 */
void connection_cb(uv_stream_t * server, int status) {
    /* dynamically allocate a new client stream object on conn */
    uv_tcp_t * client = (uv_tcp_t *)malloc(sizeof(uv_tcp_t));

    /* if status not zero there was an error */
    if (status == -1) {
        printf("error 2\n");
    }

    /* initialize the new client */
    uv_tcp_init(loop, client);

    /* now let bind the client to the server to be used for incomings */
    if (uv_accept(server, (uv_stream_t *) client) == 0) {
        /* start reading from stream */
        //int r = uv_read_start((uv_stream_t *) client, (uv_alloc_cb)alloc_buffer, read_cb);
        int r = 0;

        if (r) {
            printf("error 3\n");
        }
    } else {
        /* close client stream on error */
        uv_close((uv_handle_t *) client, NULL);
    }
}

/**
 * Callback which is executed on each readable state.
 */
void read_cb(uv_stream_t * stream, ssize_t nread, uv_buf_t buf) {
    /* dynamically allocate memory for a new write task */
    uv_write_t * req = (uv_write_t *) malloc(sizeof(uv_write_t));

    /* if read bytes counter -1 there is an error or EOF */
    if (nread == -1) {
        printf("error 4\n");
        uv_close((uv_handle_t *) stream, NULL);
    }

    /* write sync the incoming buffer to the socket */
    int r = uv_write(req, stream, &buf, 1, NULL);

    if (r) {
        printf("error 5\n");
    }
}

/**
 * Allocates a buffer which we can use for reading.
 */
uv_buf_t alloc_buffer(uv_handle_t * handle, size_t size) {
        return uv_buf_init((char *) malloc(size), size);
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-06 05:12:18

粘贴的代码将无法工作,因为libuv需要uv_listen中的一个函数指针--您提供的是指向结构的指针。这种结构碰巧有一个operator(),但这并不使您的结构的地址成为处理器可以跳到并执行代码的地址。operator()就像您的结构的任何其他方法一样,但是您可以使用简单的().operator()来调用它,以提高代码的可读性。此外,由于operator()是非静态的,因此它期望有一个对this的隐式引用,而libuv不会提供这个引用,因为它不希望这样做。

为了实现您要做的事情,您应该提供一个普通的C函数回调,并将额外的上下文数据存储在句柄的.data字段中:

而不是:

代码语言:javascript
复制
    int r = uv_listen((uv_stream_t *) &server, 128, (uv_connection_cb)&cb);

使用:

代码语言:javascript
复制
    server.data = &cb;
    int r = uv_listen((uv_stream_t *) &server, 128, [](uv_stream_t *server, int status){
        (*(CB*)server->data)(server, status);
    });

uv手柄上的.data字段正是为此目的而提供的。

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

https://stackoverflow.com/questions/34441395

复制
相关文章

相似问题

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