注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

gmd20的个人空间

// 编程和生活

 
 
 

日志

 
 

facebook/proxygen源码阅读  

2015-01-19 22:29:29|  分类: 程序设计 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
http基本协议的实现相关代码
https://github.com/facebook/proxygen/tree/master/proxygen/lib/http/

基于上面的lib/http封装的http server 接口
https://github.com/facebook/proxygen/tree/master/proxygen/httpserver

/**
* An HTTPTransaction represents a single request/response pair
* for some HTTP-like protocol. It works with a Transport that
* performs the network processing and wire-protocol formatting
* and a Handler that implements some sort of application logic.
*
* The typical sequence of events for a simple application is:
*
* * The application accepts a connection and creates a Transport.
* * The Transport reads from the connection, parses whatever
* protocol the client is speaking, and creates a Transaction
* to represent the first request.
* * Once the Transport has received the full request headers,
* it creates a Handler, plugs the handler into the Transaction,
* and calls the Transaction's onIngressHeadersComplete() method.
* * The Transaction calls the Handler's onHeadersComplete() method
* and the Handler begins processing the request.
* * If there is a request body, the Transport streams it through
* the Transaction to the Handler.
* * When the Handler is ready to produce a response, it streams
* the response through the Transaction to the Transport.
* * When the Transaction has seen the end of both the request
* and the response, it detaches itself from the Handler and
* Transport and deletes itself.
* * The Handler deletes itself at some point after the Transaction
* has detached from it.
* * The Transport may, depending on the protocol, process other
* requests after -- or even in parallel with -- that first
* request. Each request gets its own Transaction and Handler.
*
* For some applications, like proxying, a Handler implementation
* may obtain one or more upstream connections, each represented
* by another Transport, and create outgoing requests on the upstream
* connection(s), with each request represented as a new Transaction.
*
* With a multiplexing protocol like SPDY on both sides of a proxy,
* the cardinality relationship can be:
*
* +-----------+ +-----------+ +-------+
* (Client-side) | Transport |1---*|Transaction|1---1|Handler|
* +-----------+ +-----------+ +-------+
* 1
* |
* |
* 1
* +---------+ +-----------+
* (Server-side) |Transport|1---*|Transaction|
* +---------+ +-----------+
*
* A key design goal of HTTPTransaction is to serve as a protocol-
* independent abstraction that insulates Handlers from the semantics
* different of HTTP-like protocols.
*/


https://github.com/facebook/proxygen/blob/master/proxygen/lib/http/session/HTTPTransaction.h

// An HTTP request or response minus the body
https://github.com/facebook/proxygen/blob/master/proxygen/lib/http/HTTPMessage.h

https://github.com/facebook/proxygen/blob/master/proxygen/lib/http/session/HTTPSession.h

真正的调用http parser解析协议相关的在这里。
https://github.com/facebook/proxygen/blob/master/proxygen/lib/http/codec/HTTP1xCodec.h
https://github.com/facebook/proxygen/blob/master/proxygen/lib/http/codec/SPDYCodec.h



层和层之间大量使用callback回调函数,典型用法,

1.  使用继承的方法来实现回调
class HTTPSession:
private FlowControlFilter::Callback,
private HTTPCodec::Callback,
private folly::EventBase::LoopCallback,
public ByteEventTracker::Callback,
public HTTPTransaction::Transport,
public apache::thrift::async::TAsyncTransport::ReadCallback,
public folly::wangle::ManagedConnection {
// 实现了大量的回调接口,
}
// 再把自己的实现注册下去,
codec_.setCallback(this);

if (controller_) {
controller_->attachSession(this);
}









2. 采用成员的方式提供回调,

class HTTPTransaction :
public AsyncTimeoutSet::Callback {
public:
typedef HTTPTransactionHandler Handler;
typedef HTTPPushTransactionHandler PushHandler;


Handler* handler_{nullptr};


void setHandler(Handler* handler) {
handler_ = handler;
if (handler_) {
handler_->setTransaction(this);
}
}

  评论这张
 
阅读(713)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017