diff --git a/examples/client_example/main.cc b/examples/client_example/main.cc index b97d719e..39e4c08c 100644 --- a/examples/client_example/main.cc +++ b/examples/client_example/main.cc @@ -16,7 +16,7 @@ int main() ad.sin_family = AF_INET; ad.sin_addr=addr; trantor::InetAddress netaddr(ad); - auto client=HttpAppFramework::instance().newHttpClient(netaddr.toIp(),80); + auto client=HttpClient::newHttpClient(netaddr.toIp(),80); auto req=HttpRequest::newHttpRequest(); req->setMethod(drogon::HttpRequest::kGet); int count=0; diff --git a/lib/inc/drogon/HttpAppFramework.h b/lib/inc/drogon/HttpAppFramework.h index f44355f1..5356f45e 100755 --- a/lib/inc/drogon/HttpAppFramework.h +++ b/lib/inc/drogon/HttpAppFramework.h @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -83,7 +84,6 @@ namespace drogon virtual void setDocumentRoot(const std::string &rootPath)=0; virtual void setFileTypes(const std::vector &types)=0; virtual void enableDynamicSharedLibLoading(const std::vector &libPaths)=0; - virtual HttpClientPtr newHttpClient(const std::string &ip,uint16_t port,bool useSSL=false)=0; - virtual HttpClientPtr newHttpClient(const trantor::InetAddress &addr,bool useSSL=false)=0; + virtual trantor::EventLoop *loop()=0; }; } diff --git a/lib/inc/drogon/HttpClient.h b/lib/inc/drogon/HttpClient.h index dbd25a56..5d6ba800 100644 --- a/lib/inc/drogon/HttpClient.h +++ b/lib/inc/drogon/HttpClient.h @@ -26,14 +26,16 @@ namespace drogon{ Timeout }; typedef std::function HttpReqCallback; + class HttpClient; + typedef std::shared_ptr HttpClientPtr; class HttpClient:public trantor::NonCopyable { public: virtual void sendRequest(const HttpRequestPtr &req,const HttpReqCallback &callback)=0; virtual ~HttpClient(){} - + static HttpClientPtr newHttpClient(const std::string &ip,uint16_t port,bool useSSL=false) ; + static HttpClientPtr newHttpClient(const trantor::InetAddress &addr,bool useSSL=false) ; protected: HttpClient()= default; }; - typedef std::shared_ptr HttpClientPtr; } \ No newline at end of file diff --git a/lib/src/HttpAppFramework.cc b/lib/src/HttpAppFramework.cc index d0473663..b9b5728b 100755 --- a/lib/src/HttpAppFramework.cc +++ b/lib/src/HttpAppFramework.cc @@ -58,9 +58,10 @@ namespace drogon virtual void setDocumentRoot(const std::string &rootPath) override {_rootPath=rootPath;} virtual void setFileTypes(const std::vector &types) override; virtual void enableDynamicSharedLibLoading(const std::vector &libPaths) override; - virtual HttpClientPtr newHttpClient(const std::string &ip,uint16_t port,bool useSSL=false) override; - virtual HttpClientPtr newHttpClient(const trantor::InetAddress &addr,bool useSSL=false) override; + ~HttpAppFrameworkImpl(){} + + virtual trantor::EventLoop *loop() override; private: std::vector> _listeners; void onAsyncRequest(const HttpRequest& req,const std::function & callback); @@ -654,14 +655,9 @@ std::string HttpAppFrameworkImpl::stringToHex(unsigned char* ptr, long long leng return idString; } -HttpClientPtr HttpAppFrameworkImpl::newHttpClient(const std::string &ip,uint16_t port,bool useSSL) +trantor::EventLoop *HttpAppFrameworkImpl::loop() { - return std::make_shared(&_loop,trantor::InetAddress(ip,port),useSSL); -} - -HttpClientPtr HttpAppFrameworkImpl::newHttpClient(const trantor::InetAddress &addr,bool useSSL) -{ - return std::make_shared(&_loop,addr,useSSL); + return &_loop; } HttpAppFramework& HttpAppFramework::instance() { diff --git a/lib/src/HttpClientImpl.cc b/lib/src/HttpClientImpl.cc index c1328760..c984bfd5 100644 --- a/lib/src/HttpClientImpl.cc +++ b/lib/src/HttpClientImpl.cc @@ -1,6 +1,7 @@ #include "HttpClientImpl.h" #include "HttpRequestImpl.h" #include "HttpContext.h" +#include using namespace drogon; using namespace std::placeholders; HttpClientImpl::HttpClientImpl(trantor::EventLoop *loop, @@ -126,3 +127,13 @@ void HttpClientImpl::onRecvMessage(const trantor::TcpConnectionPtr &connPtr,tran } } + +HttpClientPtr HttpClient::newHttpClient(const std::string &ip,uint16_t port,bool useSSL) +{ + return std::make_shared(HttpAppFramework::instance().loop(),trantor::InetAddress(ip,port),useSSL); +} + +HttpClientPtr HttpClient::newHttpClient(const trantor::InetAddress &addr,bool useSSL) +{ + return std::make_shared(HttpAppFramework::instance().loop(),addr,useSSL); +} \ No newline at end of file