Modify some methods

This commit is contained in:
antao 2018-07-25 21:00:37 +08:00
parent 4f189afdf5
commit d1f4ccf3d7
5 changed files with 23 additions and 14 deletions

View File

@ -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;

View File

@ -26,6 +26,7 @@
#include <drogon/version.h>
#include <drogon/NotFound.h>
#include <drogon/HttpClient.h>
#include <trantor/net/EventLoop.h>
#include <memory>
#include <string>
#include <functional>
@ -83,7 +84,6 @@ namespace drogon
virtual void setDocumentRoot(const std::string &rootPath)=0;
virtual void setFileTypes(const std::vector<std::string> &types)=0;
virtual void enableDynamicSharedLibLoading(const std::vector<std::string> &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;
};
}

View File

@ -26,14 +26,16 @@ namespace drogon{
Timeout
};
typedef std::function<void(ReqResult,const HttpResponse &response)> HttpReqCallback;
class HttpClient;
typedef std::shared_ptr<HttpClient> 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<HttpClient> HttpClientPtr;
}

View File

@ -58,9 +58,10 @@ namespace drogon
virtual void setDocumentRoot(const std::string &rootPath) override {_rootPath=rootPath;}
virtual void setFileTypes(const std::vector<std::string> &types) override;
virtual void enableDynamicSharedLibLoading(const std::vector<std::string> &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<std::tuple<std::string,uint16_t,bool>> _listeners;
void onAsyncRequest(const HttpRequest& req,const std::function<void (HttpResponse &)> & 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<HttpClientImpl>(&_loop,trantor::InetAddress(ip,port),useSSL);
}
HttpClientPtr HttpAppFrameworkImpl::newHttpClient(const trantor::InetAddress &addr,bool useSSL)
{
return std::make_shared<HttpClientImpl>(&_loop,addr,useSSL);
return &_loop;
}
HttpAppFramework& HttpAppFramework::instance() {

View File

@ -1,6 +1,7 @@
#include "HttpClientImpl.h"
#include "HttpRequestImpl.h"
#include "HttpContext.h"
#include <drogon/HttpAppFramework.h>
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<HttpClientImpl>(HttpAppFramework::instance().loop(),trantor::InetAddress(ip,port),useSSL);
}
HttpClientPtr HttpClient::newHttpClient(const trantor::InetAddress &addr,bool useSSL)
{
return std::make_shared<HttpClientImpl>(HttpAppFramework::instance().loop(),addr,useSSL);
}