libevent源码学习-第四天

来源:互联网 发布:android 无网络提示 编辑:程序博客网 时间:2024/06/11 23:31

<http-internal.h>

 

该文件包含libevent内部处理http请求的相关定义。希望基于libevent建立一个http服务器对象,需要使用该文件中的部分定义。但是,相关的结构体在http.h中已经有了声明,

因此对于该文件中的内容可以不必了解。

 

http服务器对象的结构如下:

 

struct evhttp {

    TAILQ_ENTRY(evhttp) next_vhost; 

    /* All listeners for this host */

    TAILQ_HEAD(boundq, evhttp_bound_socket) sockets; 

    TAILQ_HEAD(httpcbq, evhttp_cb) callbacks; 

    /* All live connections on this host. */

    struct evconq connections; 

    TAILQ_HEAD(vhostsq, evhttp) virtualhosts; 

    TAILQ_HEAD(aliasq, evhttp_server_alias) aliases; 

    /* NULL if this server is not a vhost */

    char *vhost_pattern; 

    int timeout; 

    size_t default_max_headers_size;

    ev_uint64_t default_max_body_size; 

    /* Bitmask of all HTTP methods that we accept and pass to user

     * callbacks. */

    ev_uint16_t allowed_methods; 

    /* Fallback callback if all the other callbacks for this connection

       don't match. */

    void (*gencb)(struct evhttp_request *req, void *);

    void *gencbarg; 

    struct event_base *base;

};

 

1. next_vhost

指向下一个vhost的指针。

 

2. sockets

当前服务器对象监听的端口列表。

 

3. callbacks

当前服务器对象的回调函数列表。

 

4. connections

当前服务器对象上的所有活动连接。

 

5. default_max_headers_size

默认最大http请求头大小。

 

6.default_max_body_size;

默认最大http请求体大小。

 

7.gencb

默认的回调函数。 

8.gencbar

默认回调函数的参数。 

9.base

服务器对象所属的event_base对象。 

<evhttp.h>

提供了使用libevent建立http服务器的相应函数接口。 

struct evhttp* evhttp_new(struct event_base* base)

创建一个http的server,并指定相应的event_base,内部调用evhttp_new_object函数生成并初始化evhttp对象。

 

int evhttp_bind_socket(struct evhttp *http, const char *address, ev_uint16_t port)

为http服务器绑定端口和地址,并执行listen以及accept操作。最大等待连接数量为128(hard code)。本函数执行内会有多轮的函数调用,较为复杂,但实际上该函数执行完之后

可以获取连接请求的相应的信息。当有连接请求时,握手阶段在该过程设置的回调函数内完成。

 

struct evhttp * evhttp_start(const char *address, unsigned short port)

生成一个http的server,并绑定相应的ip和端口。


To be Continue。。。。

原创粉丝点击