From 6698929a701468ee39223aa9209d3b25e647d064 Mon Sep 17 00:00:00 2001 From: an-tao <20741618@qq.com> Date: Sat, 17 Nov 2018 00:10:15 +0800 Subject: [PATCH] Add the quit() method to HttpAppFramework --- lib/inc/drogon/HttpAppFramework.h | 40 ++++++++++++++++++++++++++----- lib/src/HttpAppFrameworkImpl.h | 17 ++++++++----- trantor | 2 +- 3 files changed, 46 insertions(+), 13 deletions(-) diff --git a/lib/inc/drogon/HttpAppFramework.h b/lib/inc/drogon/HttpAppFramework.h index 83ef39c6..dea39eda 100755 --- a/lib/inc/drogon/HttpAppFramework.h +++ b/lib/inc/drogon/HttpAppFramework.h @@ -61,7 +61,35 @@ inline std::string getGitCommit() class HttpAppFramework : public trantor::NonCopyable { public: + ///Get the instance of HttpAppFramework + /** + * HttpAppFramework works at singleton mode, so any calling of this + * method will get the same instance; + * Calling drogon::HttpAppFramework::instance() + * can be replaced by a simple interface -- drogon::app() + */ static HttpAppFramework &instance(); + + ///Run the event loop + /** + * Calling this method will start the event loop which drive the network; + * The thread calling this method must be the first thread to call instance() method; + * Usually the thread calling this method is main thread of the application; + */ + virtual void run() = 0; + + ///Quit the event loop + /** + * Calling this method will result in stopping all network IO in the + * framework and interrupting the blocking of the run() method. Usually, + * after calling this method, the application will exit. + * + * NOTE: + * This method can be called in any thread and anywhere. + * This method should not be called before calling run(). + */ + virtual void quit() = 0; + virtual void setThreadNum(size_t threadNum) = 0; virtual void setSSLFiles(const std::string &certPath, const std::string &keyPath) = 0; @@ -70,7 +98,7 @@ class HttpAppFramework : public trantor::NonCopyable bool useSSL = false, const std::string &certFile = "", const std::string &keyFile = "") = 0; - virtual void run() = 0; + virtual ~HttpAppFramework(); virtual void registerWebSocketController(const std::string &pathName, const std::string &crtlName, @@ -81,8 +109,8 @@ class HttpAppFramework : public trantor::NonCopyable const std::vector &filtersAndMethods = std::vector()) = 0; template void registerHttpMethod(const std::string &pathPattern, - FUNCTION &&function, - const std::vector &filtersAndMethods = std::vector()) + FUNCTION &&function, + const std::vector &filtersAndMethods = std::vector()) { LOG_TRACE << "pathPattern:" << pathPattern; HttpBinderBasePtr binder; @@ -154,9 +182,9 @@ class HttpAppFramework : public trantor::NonCopyable private: virtual void registerHttpController(const std::string &pathPattern, - const HttpBinderBasePtr &binder, - const std::vector &validMethods = std::vector(), - const std::vector &filters = std::vector()) = 0; + const HttpBinderBasePtr &binder, + const std::vector &validMethods = std::vector(), + const std::vector &filters = std::vector()) = 0; }; inline HttpAppFramework &app() diff --git a/lib/src/HttpAppFrameworkImpl.h b/lib/src/HttpAppFrameworkImpl.h index 404431b1..279b14f4 100644 --- a/lib/src/HttpAppFrameworkImpl.h +++ b/lib/src/HttpAppFrameworkImpl.h @@ -85,6 +85,11 @@ class HttpAppFrameworkImpl : public HttpAppFramework } trantor::EventLoop *loop(); + virtual void quit() override + { + assert(_loop.isRunning()); + _loop.quit(); + } #if USE_POSTGRESQL virtual orm::DbClientPtr getDbClient(const std::string &name = "default") override; virtual void createDbClient(const std::string &dbType, @@ -98,9 +103,9 @@ class HttpAppFrameworkImpl : public HttpAppFramework #endif private: virtual void registerHttpController(const std::string &pathPattern, - const HttpBinderBasePtr &binder, - const std::vector &validMethods = std::vector(), - const std::vector &filters = std::vector()) override; + const HttpBinderBasePtr &binder, + const std::vector &validMethods = std::vector(), + const std::vector &filters = std::vector()) override; std::vector> _listeners; void onAsyncRequest(const HttpRequestPtr &req, const std::function &callback); @@ -112,9 +117,9 @@ class HttpAppFrameworkImpl : public HttpAppFramework void onConnection(const TcpConnectionPtr &conn); void readSendFile(const std::string &filePath, const HttpRequestPtr &req, const HttpResponsePtr &resp); void addHttpPath(const std::string &path, - const HttpBinderBasePtr &binder, - const std::vector &validMethods, - const std::vector &filters); + const HttpBinderBasePtr &binder, + const std::vector &validMethods, + const std::vector &filters); void initRegex(); //if uuid package found,we can use a uuid string as session id; //set _sessionTimeout=0 to make location session valid forever based on cookies; diff --git a/trantor b/trantor index fe51026d..33cd9c74 160000 --- a/trantor +++ b/trantor @@ -1 +1 @@ -Subproject commit fe51026dd655621885555aa2eaa155122cadc813 +Subproject commit 33cd9c748e5b462714f8d35f29ecb78c2046594a