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

65 lines
1.6 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/ServiceBuilder.hpp>
2021-04-30 16:10:14 +02:00
#include <utility>
2020-11-24 15:41:18 +01:00
2021-05-14 17:16:45 +02:00
class HttpListenerBuilder {
2021-04-30 16:10:14 +02:00
public:
2021-05-14 17:16:45 +02:00
HttpListenerBuilder &WithService(TcpService::Ptr service) {
mBuilder.WithService(std::move(service));
return *this;
}
2021-04-30 16:10:14 +02:00
2021-05-14 17:49:39 +02:00
HttpListenerBuilder &WithEnterCallback(HttpSession::EnterCallback &&callback) {
2021-05-14 17:16:45 +02:00
mHttpEnterCallback = std::move(callback);
return *this;
}
2021-04-30 16:10:14 +02:00
2021-05-14 17:16:45 +02:00
HttpListenerBuilder &AddSocketProcess(const ListenThread::TcpSocketProcessCallback &callback) {
mBuilder.AddSocketProcess(callback);
return *this;
}
2021-04-30 16:10:14 +02:00
2021-05-14 17:16:45 +02:00
HttpListenerBuilder &WithMaxRecvBufferSize(size_t size) {
mBuilder.WithMaxRecvBufferSize(size);
return *this;
}
2021-04-30 16:10:14 +02:00
#ifdef BRYNET_USE_OPENSSL
2021-05-14 17:16:45 +02:00
HttpListenerBuilder &WithSSL(SSLHelper::Ptr sslHelper) {
mBuilder.WithSSL(std::move(sslHelper));
return *this;
}
2021-04-30 16:10:14 +02:00
#endif
2021-05-14 17:16:45 +02:00
HttpListenerBuilder &WithForceSameThreadLoop() {
mBuilder.WithForceSameThreadLoop();
return *this;
}
2021-04-30 16:10:14 +02:00
2021-05-14 17:16:45 +02:00
HttpListenerBuilder &WithAddr(bool ipV6, std::string ip, size_t port) {
mBuilder.WithAddr(ipV6, std::move(ip), port);
return *this;
}
2020-11-24 15:41:18 +01:00
2021-05-14 17:16:45 +02:00
HttpListenerBuilder &WithReusePort() {
mBuilder.WithReusePort();
return *this;
}
2021-04-30 16:10:14 +02:00
2021-05-14 17:16:45 +02:00
void asyncRun() {
if (mHttpEnterCallback == nullptr) {
throw BrynetCommonException("not setting http enter callback");
}
2020-11-24 15:41:18 +01:00
2021-05-14 17:16:45 +02:00
auto callback = mHttpEnterCallback;
mBuilder.AddEnterCallback([callback](const TcpConnection::Ptr &session) {
2021-05-14 17:49:39 +02:00
HttpService::setup(session, callback);
2021-05-14 17:16:45 +02:00
});
mBuilder.asyncRun();
}
2021-04-30 16:10:14 +02:00
private:
2021-05-14 17:49:39 +02:00
HttpSession::EnterCallback mHttpEnterCallback;
2021-05-14 17:16:45 +02:00
ListenerBuilder mBuilder;
2021-04-30 16:10:14 +02:00
};