libevhtp初探
libevent的evhttp不适合多线程,libevhtp重新设计了libevent的http API,采用了和memcached类似的多线程模型。
worker线程的管道读事件的回调函数为htp__run_in_thread_:
#ifndef EVHTP_DISABLE_EVTHR
static void
htp__run_in_thread_(evthr_t * thr, void * arg, void * shared)
{
evhtp_t * htp = shared;
evhtp_connection_t * connection = arg; connection->evbase = evthr_get_base(thr);
connection->thread = thr; if (htp__connection_accept_(connection->evbase, connection) < )
{
evhtp_connection_free(connection); return;
} if (htp__run_post_accept_(htp, connection) < )
{
evhtp_connection_free(connection); return;
}
} #endif
htp__connection_accept_函数如下:
static int
htp__connection_accept_(struct event_base * evbase, evhtp_connection_t * connection)
{
struct timeval * c_recv_timeo;
struct timeval * c_send_timeo; if (htp__run_pre_accept_(connection->htp, connection) < )
{
evutil_closesocket(connection->sock); return -;
} #ifndef EVHTP_DISABLE_SSL
if (connection->htp->ssl_ctx != NULL)
{
connection->ssl = SSL_new(connection->htp->ssl_ctx);
connection->bev = bufferevent_openssl_socket_new(evbase,
connection->sock,
connection->ssl,
BUFFEREVENT_SSL_ACCEPTING,
connection->htp->bev_flags);
SSL_set_app_data(connection->ssl, connection);
goto end;
}
#endif connection->bev = bufferevent_socket_new(evbase,
connection->sock,
connection->htp->bev_flags); htp_log_debug("enter sock=%d\n", connection->sock); #ifndef EVHTP_DISABLE_SSL
end:
#endif if (connection->recv_timeo.tv_sec || connection->recv_timeo.tv_usec)
{
c_recv_timeo = &connection->recv_timeo;
} else if (connection->htp->recv_timeo.tv_sec ||
connection->htp->recv_timeo.tv_usec)
{
c_recv_timeo = &connection->htp->recv_timeo;
} else {
c_recv_timeo = NULL;
} if (connection->send_timeo.tv_sec || connection->send_timeo.tv_usec)
{
c_send_timeo = &connection->send_timeo;
} else if (connection->htp->send_timeo.tv_sec ||
connection->htp->send_timeo.tv_usec)
{
c_send_timeo = &connection->htp->send_timeo;
} else {
c_send_timeo = NULL;
} evhtp_connection_set_timeouts(connection, c_recv_timeo, c_send_timeo); connection->resume_ev = event_new(evbase, -, EV_READ | EV_PERSIST,
htp__connection_resumecb_, connection);
event_add(connection->resume_ev, NULL); bufferevent_enable(connection->bev, EV_READ);
bufferevent_setcb(connection->bev,
htp__connection_readcb_,
htp__connection_writecb_,
htp__connection_eventcb_, connection); return ;
}
此时,conn_fd的读事件已经添加到worker线程的event_base中。
当读事件发生后,最终会运行htp__connection_readcb_,然后调用htparser_run,再调用htp__request_parse_path_:
static int
htp__request_parse_path_(htparser * p, const char * data, size_t len)
{
evhtp_connection_t * c = htparser_get_userdata(p);
evhtp_path_t * path; if (htp__require_uri_(c) != )
{
return -;
} if (evhtp_unlikely(!(path = htp__path_new_(data, len))))
{
c->request->status = EVHTP_RES_FATAL; return -;
} c->request->uri->path = path;
c->request->uri->scheme = htparser_get_scheme(p);
c->request->method = htparser_get_method(p); htp__lock_(c->htp);
{
htp__request_set_callbacks_(c->request);
}
htp__unlock_(c->htp); if ((c->request->status = htp__hook_path_(c->request, path)) != EVHTP_RES_OK)
{
return -;
} return ;
}
通过htp__request_set_callbacks_,我们可以设置每个请求的回调函数为evhtp_s结构中的回调函数。
htparser_run后续会调用htp__request_parse_fini_:
static int
htp__request_parse_fini_(htparser * p)
{
evhtp_connection_t * c = htparser_get_userdata(p); if (c->flags & EVHTP_CONN_FLAG_PAUSED)
{
return -;
} /* check to see if we should use the body of the request as the query
* arguments.
*/
if (htp__should_parse_query_body_(c->request) == )
{
const char * body;
size_t body_len;
evhtp_uri_t * uri;
struct evbuffer * buf_in; uri = c->request->uri;
buf_in = c->request->buffer_in; body_len = evbuffer_get_length(buf_in);
body = (const char *)evbuffer_pullup(buf_in, body_len); uri->query_raw = calloc(body_len + , );
evhtp_alloc_assert(uri->query_raw); memcpy(uri->query_raw, body, body_len); uri->query = evhtp_parse_query(body, body_len);
} /*
* XXX c->request should never be NULL, but we have found some path of
* execution where this actually happens. We will check for now, but the bug
* path needs to be tracked down.
*
*/
if (c->request && c->request->cb)
{
(c->request->cb)(c->request, c->request->cbarg);
} if (c->flags & EVHTP_CONN_FLAG_PAUSED)
{
return -;
} return ;
}
可以看到,最终request的回调函数被执行。
libevhtp初探的更多相关文章
- 初探领域驱动设计(2)Repository在DDD中的应用
概述 上一篇我们算是粗略的介绍了一下DDD,我们提到了实体.值类型和领域服务,也稍微讲到了DDD中的分层结构.但这只能算是一个很简单的介绍,并且我们在上篇的末尾还留下了一些问题,其中大家讨论比较多的, ...
- CSharpGL(8)使用3D纹理渲染体数据 (Volume Rendering) 初探
CSharpGL(8)使用3D纹理渲染体数据 (Volume Rendering) 初探 2016-08-13 由于CSharpGL一直在更新,现在这个教程已经不适用最新的代码了.CSharpGL源码 ...
- 从273二手车的M站点初探js模块化编程
前言 这几天在看273M站点时被他们的页面交互方式所吸引,他们的首页是采用三次加载+分页的方式.也就说分为大分页和小分页两种交互.大分页就是通过分页按钮来操作,小分页是通过下拉(向下滑动)时异步加载数 ...
- JavaScript学习(一) —— 环境搭建与JavaScript初探
1.开发环境搭建 本系列教程的开发工具,我们采用HBuilder. 可以去网上下载最新的版本,然后解压一下就能直接用了.学习JavaScript,环境搭建是非常简单的,或者说,只要你有一个浏览器,一个 ...
- .NET文件并发与RabbitMQ(初探RabbitMQ)
本文版权归博客园和作者吴双本人共同所有.欢迎转载,转载和爬虫请注明原文地址:http://www.cnblogs.com/tdws/p/5860668.html 想必MQ这两个字母对于各位前辈们和老司 ...
- React Native初探
前言 很久之前就想研究React Native了,但是一直没有落地的机会,我一直认为一个技术要有落地的场景才有研究的意义,刚好最近迎来了新的APP,在可控的范围内,我们可以在上面做任何想做的事情. P ...
- 【手把手教你全文检索】Apache Lucene初探
PS: 苦学一周全文检索,由原来的搜索小白,到初次涉猎,感觉每门技术都博大精深,其中精髓亦是不可一日而语.那小博猪就简单介绍一下这一周的学习历程,仅供各位程序猿们参考,这其中不涉及任何私密话题,因此也 ...
- Key/Value之王Memcached初探:三、Memcached解决Session的分布式存储场景的应用
一.高可用的Session服务器场景简介 1.1 应用服务器的无状态特性 应用层服务器(这里一般指Web服务器)处理网站应用的业务逻辑,应用的一个最显著的特点是:应用的无状态性. PS:提到无状态特性 ...
- NoSQL初探之人人都爱Redis:(3)使用Redis作为消息队列服务场景应用案例
一.消息队列场景简介 “消息”是在两台计算机间传送的数据单位.消息可以非常简单,例如只包含文本字符串:也可以更复杂,可能包含嵌入对象.消息被发送到队列中,“消息队列”是在消息的传输过程中保存消息的容器 ...
随机推荐
- 22.1 Extends 继承的 方法重写、@Override的使用、
/* * 继承中成员方法的特点 * 子类中没有这个方法,调用父类的 * 子类中重写了这个方法,调用子类的 * 方法的重写:在子父类当中,子类的方法和父类的完全一样,子类重写了父类的方法(覆盖),当子类 ...
- Linux C++ 网络编程学习系列(5)——多路IO之epoll边沿触发
多路IO之epoll边沿触发+非阻塞 源码地址:https://github.com/whuwzp/linuxc/tree/master/epoll_ET_LT_NOBLOCK_example 源码说 ...
- 正整数的二进制表示中1的个数计算(使用移位或者n&(n-1))
第一种:使用n&(n-1)表示来计算有多少个1 int n=127; int count=0; while (n!=0){ count++; n=n&(n-1); } 第二种:使用移位 ...
- H - Bear and Three Balls
Limak is a little polar bear. He has n balls, the i-th ball has size ti. Limak wants to give one bal ...
- 2016NOIP普及组T2回文日期
回文日期 分类:枚举,函数 [题目描述] 日常生活中,通过年.月.日这三个要素可以表示出一个唯一确定的日期. 牛牛习惯用8位数字表示一个日期,其中,前4位代表年份,接下来2位代表月份,最后2位代表日期 ...
- 调用sleep后,我做了一个噩梦
sleep系统调用 我是一个线程,生活在Linux帝国.一直以来辛勤工作,日子过得平平淡淡,可今天早上发生了一件事让我回想起来都后怕. 早上,我还是如往常一样执行着人类编写的代码指令,不多时走到了一个 ...
- 常见web漏洞的整理之SQL注入
SQL注入: 简介: 全称Structured Query Language,即结构化查询语言,是一种特殊的编程语言,用于数据库中的标准数据查询语言.也被作为关系式数据库管理系统的标准语言. 原理: ...
- Linux网络编程(1)
Preview 课程要求,所以学了一下UNIX网络编程,老师说挺简单的,实际上手之后才发现这里面关系没那么简单.从CS:APP11章网络编程,再加上不停地man,对当前的学习做个总结,也顺带当个报告了 ...
- LABEL和UUID
基本用法 blkid 查看LABEL # blkid -s LABEL /dev/hda3: LABEL="/" /dev/hda1: LABEL="/boot1&quo ...
- python 规范篇 如何合理使用 assert
assert 的合理使用,可以增加代码的健壮度,同时也方便了程序出错时开发人员的定位排查. 什么是 assert? Python 的 assert 语句,可以说是一个 debug 的好工具,主要用于测 ...