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初探的更多相关文章

  1. 初探领域驱动设计(2)Repository在DDD中的应用

    概述 上一篇我们算是粗略的介绍了一下DDD,我们提到了实体.值类型和领域服务,也稍微讲到了DDD中的分层结构.但这只能算是一个很简单的介绍,并且我们在上篇的末尾还留下了一些问题,其中大家讨论比较多的, ...

  2. CSharpGL(8)使用3D纹理渲染体数据 (Volume Rendering) 初探

    CSharpGL(8)使用3D纹理渲染体数据 (Volume Rendering) 初探 2016-08-13 由于CSharpGL一直在更新,现在这个教程已经不适用最新的代码了.CSharpGL源码 ...

  3. 从273二手车的M站点初探js模块化编程

    前言 这几天在看273M站点时被他们的页面交互方式所吸引,他们的首页是采用三次加载+分页的方式.也就说分为大分页和小分页两种交互.大分页就是通过分页按钮来操作,小分页是通过下拉(向下滑动)时异步加载数 ...

  4. JavaScript学习(一) —— 环境搭建与JavaScript初探

    1.开发环境搭建 本系列教程的开发工具,我们采用HBuilder. 可以去网上下载最新的版本,然后解压一下就能直接用了.学习JavaScript,环境搭建是非常简单的,或者说,只要你有一个浏览器,一个 ...

  5. .NET文件并发与RabbitMQ(初探RabbitMQ)

    本文版权归博客园和作者吴双本人共同所有.欢迎转载,转载和爬虫请注明原文地址:http://www.cnblogs.com/tdws/p/5860668.html 想必MQ这两个字母对于各位前辈们和老司 ...

  6. React Native初探

    前言 很久之前就想研究React Native了,但是一直没有落地的机会,我一直认为一个技术要有落地的场景才有研究的意义,刚好最近迎来了新的APP,在可控的范围内,我们可以在上面做任何想做的事情. P ...

  7. 【手把手教你全文检索】Apache Lucene初探

    PS: 苦学一周全文检索,由原来的搜索小白,到初次涉猎,感觉每门技术都博大精深,其中精髓亦是不可一日而语.那小博猪就简单介绍一下这一周的学习历程,仅供各位程序猿们参考,这其中不涉及任何私密话题,因此也 ...

  8. Key/Value之王Memcached初探:三、Memcached解决Session的分布式存储场景的应用

    一.高可用的Session服务器场景简介 1.1 应用服务器的无状态特性 应用层服务器(这里一般指Web服务器)处理网站应用的业务逻辑,应用的一个最显著的特点是:应用的无状态性. PS:提到无状态特性 ...

  9. NoSQL初探之人人都爱Redis:(3)使用Redis作为消息队列服务场景应用案例

    一.消息队列场景简介 “消息”是在两台计算机间传送的数据单位.消息可以非常简单,例如只包含文本字符串:也可以更复杂,可能包含嵌入对象.消息被发送到队列中,“消息队列”是在消息的传输过程中保存消息的容器 ...

随机推荐

  1. JVM日常排查问题。基本操作和命令

    1.jstat jstat -gcutil pid 5s    //pid进程号 每隔5s监控一次内存回收情况 E 代表 Eden 区使用率:O(Old)代表老年代使用率    :P(Permanen ...

  2. 实例讲解Springboot整合MongoDB进行CRUD操作的两种方式

    1 简介 Springboot是最简单的使用Spring的方式,而MongoDB是最流行的NoSQL数据库.两者在分布式.微服务架构中使用率极高,本文将用实例介绍如何在Springboot中整合Mon ...

  3. 用Python画的,5 种非传统的可视化技术,超炫酷的动态图

    数据可以帮助我们描述这个世界.阐释自己的想法和展示自己的成果,但如果只有单调乏味的文本和数字,我们却往往能难抓住观众的眼球.而很多时候,一张漂亮的可视化图表就足以胜过千言万语.本文将介绍 5 种基于 ...

  4. Bi-shoe and Phi-shoe LightOJ - 1370

    欧拉函数. 欧拉函数打表模板: #define maxn 3000010 int p[maxn]; void oula(){ int i,j; ; i<=maxn; i++) p[i]=i; ; ...

  5. Anadi and Domino--codeforces div2

    题目链接:https://codeforces.com/contest/1230/problem/C 题目大意:21枚多米诺牌,给你一个图,将多米诺牌放到图的边上,由同一个点发出的所有边,边上多米诺牌 ...

  6. jdbctemplate打印sql

    在logback.xml里加入如下配置即可: <include resource="org/springframework/boot/logging/logback/base.xml& ...

  7. vue2.x学习笔记(十六)

    接着前面的内容:https://www.cnblogs.com/yanggb/p/12616543.html. 组件中的插槽 在2.6.0的版本中,vue为具名插槽和作用域插槽引入了一个新的统一的语法 ...

  8. frp内网穿透学习

    前言 因为自己在内网,但是目标站在外网,这时候可以通过内网穿透工具,将接收到的请求转发到内网,实现在内网的msf可以控制外网的靶机. 也看了一些Ngrok,花生壳的,发现Ngrok.cc这个看文章说有 ...

  9. requets中urlencode的问题

    前言 今天团队群里有师傅问requests怎么设置不解码,这里是语误,其实师傅想说的是,如果设置不编码. 一开始我没懂,然后师傅们解答了这个问题后,我想了会儿懂了. 在一些CTF题目中,可能会碰到这样 ...

  10. java-锁膨胀的过程

    先来看个奇怪的demo public class A { int i=0; // boolean flag =false; public synchronized void parse(){ i++; ...