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作为消息队列服务场景应用案例
一.消息队列场景简介 “消息”是在两台计算机间传送的数据单位.消息可以非常简单,例如只包含文本字符串:也可以更复杂,可能包含嵌入对象.消息被发送到队列中,“消息队列”是在消息的传输过程中保存消息的容器 ...
随机推荐
- Struts2-学习笔记系列(15)-ajax支持和JSON
7.1stream类型的result 使用stream就无需jsp页面,直接在action想浏览者生成指定的响应 @Override public java.lang.String execute() ...
- python如何操作excel 基础代码
一 基础操作1打开excel表格并获取表格名称 wookbook = load_workbook(filename = 表格文件路径) (注意只能打开存在的表格,不能用该方法创建一个新表格文件) wo ...
- ClickOnce的安装路径
win 7下C:\Users\Administrator.U5G4L4PUY34SH5C\AppData\Local\Apps\2.0\KPVZOAYK.0JE\56B55RCH.A7A\winr.. ...
- TcxLookupComboBox
1.绑定数据源显示 cxLookupComboBox1.Properties.DropDownAutoSize:=true; //设置下拉列表为自适应宽度 cxLookupComboBox1.Prop ...
- 用Python画的,5 种非传统的可视化技术,超炫酷的动态图
数据可以帮助我们描述这个世界.阐释自己的想法和展示自己的成果,但如果只有单调乏味的文本和数字,我们却往往能难抓住观众的眼球.而很多时候,一张漂亮的可视化图表就足以胜过千言万语.本文将介绍 5 种基于 ...
- L18 批量归一化和残差网络
批量归一化(BatchNormalization) 对输入的标准化(浅层模型) 处理后的任意一个特征在数据集中所有样本上的均值为0.标准差为1. 标准化处理输入数据使各个特征的分布相近 批量归一化(深 ...
- D - Romantic
The Sky is Sprite. The Birds is Fly in the Sky. The Wind is Wonderful. Blew Throw the Trees Trees ar ...
- Windows线程+进程通信
一 Windows线程进程 1)定义 按照MS的定义, Windows中的进程简单地说就是一个内存中的可执行程序, 提供程序运行的各种资源. 进程拥有虚拟的地址空间, 可执行代码, 数据, 对象句柄集 ...
- Dom节点操作总结
Dom 一:Dom的概念 Dom的简介: 全称为 document object model 文档对象模型,是操作文档的一整套方法 - 文档 - html,document时一个对象,是dom ...
- vs 类型定义及语句,随机数
1 类型定义: 1)小数: 类型 变量名 赋值 decimal d : d=1.2m float ...