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
|
|
|
namespace brynet {
|
|
|
|
namespace net {
|
|
|
|
namespace wrapper {
|
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:16:45 +02:00
|
|
|
HttpListenerBuilder &WithEnterCallback(http::HttpSession::EnterCallback &&callback) {
|
|
|
|
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) {
|
|
|
|
http::HttpService::setup(session, callback);
|
|
|
|
});
|
|
|
|
mBuilder.asyncRun();
|
|
|
|
}
|
2021-04-30 16:10:14 +02:00
|
|
|
|
|
|
|
private:
|
2021-05-14 17:16:45 +02:00
|
|
|
http::HttpSession::EnterCallback mHttpEnterCallback;
|
|
|
|
ListenerBuilder mBuilder;
|
2021-04-30 16:10:14 +02:00
|
|
|
};
|
2020-11-24 15:41:18 +01:00
|
|
|
|
2021-05-14 17:16:45 +02:00
|
|
|
} // namespace wrapper
|
|
|
|
} // namespace net
|
|
|
|
} // namespace brynet
|