码迷,mamicode.com
首页 > 其他好文 > 详细

mongo源码学习(三)请求接收传输层

时间:2018-10-25 15:42:56      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:star   using   enc   概念   rate   抽象   option   lan   接收   

在上一篇博客中(mongo源码学习(二)db.cpp之mongoDbMain方法分析),我们把db.cpp中的mongoDbMain的执行过程分析了一下,最后会调用initAndListen(serverGlobalParams.port)方法来监听端口,默认的是27017了。程序执行到这块就断开了,追踪不下去了。在另一个地方肯定有accept方法来接收客户端的请求。然后找了半天发现了文件夹transportlayer,发现了里面有相关的代码。

transport_layer.h

// mongo的命名空间
namespace mongo {

class OperationContext;

// transport的命名空间, 我是cpp小白, 命名空间还可以嵌套, TODO:namespace学习一下
namespace transport {

// 连接的SSL模型, TODO:SSL可以了解一下哦
enum ConnectSSLMode { kGlobalSSLMode, kEnableSSL, kDisableSSL };

// 这个应该声明的意思, TODO:C++声明类
class Reactor;

// using还可以这么用, shared_ptr是啥, 可以了解一下
// 妈的, 写不下去了, 就我这这样的还来看mongo的C++源码
using ReactorHandle = std::shared_ptr<Reactor>;

/**
 * The TransportLayer moves Messages between transport::Endpoints and the database.
 * This class owns an Acceptor that generates new endpoints from which it can
 * source Messages.
 *
 * The TransportLayer creates Session objects and maps them internally to
 * endpoints. New Sessions are passed to the database (via a ServiceEntryPoint)
 * to be run. The database must then call additional methods on the TransportLayer
 * to manage the Session in a get-Message, handle-Message, return-Message cycle.
 * It must do this on its own thread(s).
 *
 * References to the TransportLayer should be stored on service context objects.
 */
/**
 * TransportLayer在transport::Endpoints和database(也就是mongo了)之间传递消息(message)。
 * 这个类有一个Acceptor, 这个Acceptor可以从原始的message中生成一个新的endpoints。
 *
 * TransportLay创建新的Session对象,并且在内部将它们映射到endpoints。新的Session通过一个ServiceEntryPoint
 * 传递到database来运行的。database后面必须调用TransportLay的其他方法来管理get-Message,
 * handle-Message和return-Message周期中的Session。而且必须在它自己的线程中做这些事。
 */
class TransportLayer {
    // 这是个什么鬼
    MONGO_DISALLOW_COPYING(TransportLayer);

public:

    // 声明各种状态
    static const Status SessionUnknownStatus;
    static const Status ShutdownStatus;
    static const Status TicketSessionUnknownStatus;
    static const Status TicketSessionClosedStatus;
    // 把Session对象设为友元
    friend class Session;
    // virtual, TODO:虚方法了解一下
    virtual ~TransportLayer() = default;
    // 连接
    virtual StatusWith<SessionHandle> connect(HostAndPort peer,
                                              ConnectSSLMode sslMode,
                                              Milliseconds timeout) = 0;
    // 异步连接
    virtual Future<SessionHandle> asyncConnect(HostAndPort peer,
                                               ConnectSSLMode sslMode,
                                               const ReactorHandle& reactor,
                                               Milliseconds timeout) = 0;

    /**
     * Start the TransportLayer. After this point, the TransportLayer will begin accepting active
     * sessions from new transport::Endpoints.
     */
    /**
     * 启动TransportLayer。在这之后,TransportLayer将开始从新的transport::Endpoints接收活跃的session。
     */
    virtual Status start() = 0;

    /**
     * Shut the TransportLayer down. After this point, the TransportLayer will
     * end all active sessions and won‘t accept new transport::Endpoints. Any
     * future calls to wait() or asyncWait() will fail. This method is synchronous and
     * will not return until all sessions have ended and any network connections have been
     * closed.
     */
    /**
     * 关闭TransportLay。在这之后,TransportLayer将终止所有活跃的sessions并且不再接收新的EndPoints。
     * 任何将来对wait()和asyncWait()的调用都会失败。这个方法是同步的,并且直到所有的sessions都结束了
     * 之后并且任何网络连接都关闭了才会返回。
     */
    virtual void shutdown() = 0;

    /**
     * Optional method for subclasses to setup their state before being ready to accept
     * connections.
     */
    /**
     * 这个是可选的方法,子类可以用这个方法在准备接收连接的时候去设置它们的状态。
     */
    virtual Status setup() = 0;

    enum WhichReactor { kIngress, kEgress, kNewReactor };
    virtual ReactorHandle getReactor(WhichReactor which) = 0;

    virtual BatonHandle makeBaton(OperationContext* opCtx) {
        return nullptr;
    }

protected:
    TransportLayer() = default;
};
}  // namespace transport
}  // namespace mongo

这里看着有点绕,主要的几个角色有:

TransportLay:有Acceptor接收请求,产生新的EndPoints,创建Session,并将Session在内部映射到EndPoints上;同时TransportLay也在EndPoints和database之间传递消息。

Session:get-Message,handle-Message和return-Message,但是这些需要TransportLayer来管理。

EndPoints:抽象出来的端的概念。

Acceptor:接收请求的。

database:我们的mongo后台

mongo源码学习(三)请求接收传输层

标签:star   using   enc   概念   rate   抽象   option   lan   接收   

原文地址:https://www.cnblogs.com/tuhooo/p/9849654.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!