rcpp_framework/libs/brynet/net/wrapper/HttpConnectionBuilder.hpp

94 lines
2.3 KiB
C++
Raw Normal View History

2020-11-24 15:41:18 +01:00
#pragma once
#include <brynet/net/http/HttpService.hpp>
#include <brynet/net/wrapper/ConnectionBuilder.hpp>
2021-04-30 16:10:14 +02:00
#include <utility>
2020-11-24 15:41:18 +01:00
namespace brynet { namespace net { namespace wrapper {
2021-04-30 16:10:14 +02:00
class HttpConnectionBuilder
{
public:
HttpConnectionBuilder& WithService(TcpService::Ptr service)
2020-11-24 15:41:18 +01:00
{
2021-04-30 16:10:14 +02:00
mBuilder.WithService(std::move(service));
return *this;
}
HttpConnectionBuilder& WithConnector(AsyncConnector::Ptr connector)
{
mBuilder.WithConnector(std::move(connector));
return *this;
}
HttpConnectionBuilder& WithAddr(std::string ip, size_t port)
{
mBuilder.WithAddr(std::move(ip), port);
return *this;
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
HttpConnectionBuilder& WithTimeout(std::chrono::nanoseconds timeout)
{
mBuilder.WithTimeout(timeout);
return *this;
}
HttpConnectionBuilder& AddSocketProcessCallback(const ProcessTcpSocketCallback& callback)
{
mBuilder.AddSocketProcessCallback(callback);
return *this;
}
HttpConnectionBuilder& WithEnterCallback(http::HttpSession::EnterCallback&& callback)
{
mHttpEnterCallback = std::move(callback);
return *this;
}
HttpConnectionBuilder& WithFailedCallback(FailedCallback callback)
{
mBuilder.WithFailedCallback(std::move(callback));
return *this;
}
HttpConnectionBuilder& WithMaxRecvBufferSize(size_t size)
{
mBuilder.WithMaxRecvBufferSize(size);
return *this;
}
#ifdef BRYNET_USE_OPENSSL
HttpConnectionBuilder& WithSSL()
{
mBuilder.WithSSL();
return *this;
}
#endif
HttpConnectionBuilder& WithForceSameThreadLoop()
{
mBuilder.WithForceSameThreadLoop();
return *this;
}
void asyncConnect()
{
if (mHttpEnterCallback == nullptr)
2020-11-24 15:41:18 +01:00
{
2021-04-30 16:10:14 +02:00
throw BrynetCommonException("not setting http enter callback");
2020-11-24 15:41:18 +01:00
}
2021-04-30 16:10:14 +02:00
auto callback = mHttpEnterCallback;
auto builder = mBuilder;
builder.AddEnterCallback([callback](const TcpConnection::Ptr& session) {
http::HttpService::setup(session, callback);
});
builder.asyncConnect();
}
private:
http::HttpSession::EnterCallback mHttpEnterCallback;
ConnectionBuilder mBuilder;
};
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
}}}// namespace brynet::net::wrapper