1. 程式人生 > >mongo源碼學習(三)請求接收傳輸層

mongo源碼學習(三)請求接收傳輸層

stand can ssa 傳輸層 網絡 fault ets reference reat

在上一篇博客中(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源碼學習(三)請求接收傳輸層