现在的位置: 首页 > 自动控制 > 工业·编程 > 正文

uWebSockets && uSockets 代码分析(3)主要数据结构转换函数

2020-01-03 17:35 工业·编程 ⁄ 共 2936字 ⁄ 字号 暂无评论

void *us_loop_ext(struct us_loop_t *loop) {

    return loop + 1;

}

void *us_socket_ext(int ssl, struct us_socket_t *s) {

#ifndef LIBUS_NO_SSL

    if (ssl) {

        return us_internal_ssl_socket_ext((struct us_internal_ssl_socket_t *) s);

    }

#endif

 

    return s + 1;

}

 

void *us_internal_ssl_socket_ext(struct us_internal_ssl_socket_t *s) {

    return s + 1;

}

 

// same here, should or shouldn't it contain s?

struct us_internal_ssl_socket_t {

    struct us_socket_t s;

    SSL *ssl;

    int ssl_write_wants_read; // we use this for now

};

struct us_loop_t *us_socket_context_loop(int ssl, struct us_socket_context_t *context) {

    return context->loop;

}

struct us_socket_context_t *us_create_socket_context(int ssl, struct us_loop_t *loop, int context_ext_size, struct us_socket_context_options_t options) {

#ifndef LIBUS_NO_SSL

    if (ssl) {

        return (struct us_socket_context_t *) us_internal_create_ssl_socket_context(loop, context_ext_size, options);

    }

#endif

 

    struct us_socket_context_t *context = malloc(sizeof(struct us_socket_context_t) + context_ext_size);

    context->loop = loop;

    context->head = 0;

    context->iterator = 0;

    context->next = 0;

    context->ignore_data = default_ignore_data_handler;

 

    us_internal_loop_link(loop, context);

    return context;

}

struct us_socket_context_t *us_create_child_socket_context(int ssl, struct us_socket_context_t *context, int context_ext_size) {

#ifndef LIBUS_NO_SSL

    if (ssl) {

        return (struct us_socket_context_t *) us_internal_create_child_ssl_socket_context((struct us_internal_ssl_socket_context_t *) context, context_ext_size);

    }

#endif

 

    /* For TCP we simply create a new context as nothing is shared */

    struct us_socket_context_options_t options = {0};

    return us_create_socket_context(ssl, context->loop, context_ext_size, options);

}

void *us_socket_context_ext(int ssl, struct us_socket_context_t *context) {

#ifndef LIBUS_NO_SSL

    if (ssl) {

        return us_internal_ssl_socket_context_ext((struct us_internal_ssl_socket_context_t *) context);

    }

#endif

 

    return context + 1;

}

struct us_socket_context_t *us_socket_context(int ssl, struct us_socket_t *s) {

    return s->context;

}

struct us_loop_t *us_create_loop(void *hint, void (*wakeup_cb)(struct us_loop_t *loop), void (*pre_cb)(struct us_loop_t *loop), void (*post_cb)(struct us_loop_t *loop), unsigned int ext_size) {

    struct us_loop_t *loop = (struct us_loop_t *) malloc(sizeof(struct us_loop_t) + ext_size);

 

    loop->uv_loop = hint ? hint : uv_loop_new();

    loop->is_default = hint != 0;

 

    loop->uv_pre = malloc(sizeof(uv_prepare_t));

    uv_prepare_init(loop->uv_loop, loop->uv_pre);

    uv_prepare_start(loop->uv_pre, prepare_cb);

    uv_unref((uv_handle_t *) loop->uv_pre);

    loop->uv_pre->data = loop;

 

    loop->uv_check = malloc(sizeof(uv_check_t));

    uv_check_init(loop->uv_loop, loop->uv_check);

    uv_unref((uv_handle_t *) loop->uv_check);

    uv_check_start(loop->uv_check, check_cb);

    loop->uv_check->data = loop;

 

    // here we create two unreffed handles - timer and async

    us_internal_loop_data_init(loop, wakeup_cb, pre_cb, post_cb);

 

    // if we do not own this loop, we need to integrate and set up timer

    if (hint) {

        us_loop_integrate(loop);

    }

 

    return loop;

}

给我留言

留言无头像?