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

98 lines
2.4 KiB
C++
Raw Normal View History

2020-11-24 15:41:18 +01:00
#pragma once
#include <brynet/net/Exception.hpp>
2021-04-30 16:10:14 +02:00
#include <brynet/net/ListenThread.hpp>
#include <brynet/net/TcpService.hpp>
#include <brynet/net/detail/ConnectionOption.hpp>
#include <utility>
2020-11-24 15:41:18 +01:00
2021-05-14 17:16:45 +02:00
template <typename Derived>
class BaseListenerBuilder {
2021-04-30 16:10:14 +02:00
public:
2021-05-14 17:16:45 +02:00
virtual ~BaseListenerBuilder() = default;
2020-11-24 15:41:18 +01:00
2021-05-14 17:16:45 +02:00
Derived &WithService(TcpService::Ptr service) {
mTcpService = std::move(service);
return static_cast<Derived &>(*this);
}
2020-11-24 15:41:18 +01:00
2021-05-14 17:16:45 +02:00
Derived &WithAddr(bool ipV6, std::string ip, size_t port) {
mIsIpV6 = ipV6;
mListenAddr = std::move(ip);
mPort = port;
return static_cast<Derived &>(*this);
}
2020-11-24 15:41:18 +01:00
2021-05-14 17:16:45 +02:00
Derived &WithReusePort() {
mEnabledReusePort = true;
return static_cast<Derived &>(*this);
}
2020-11-24 15:41:18 +01:00
2021-05-14 17:16:45 +02:00
Derived &AddSocketProcess(const ListenThread::TcpSocketProcessCallback &callback) {
mSocketProcessCallbacks.push_back(callback);
return static_cast<Derived &>(*this);
}
2020-11-24 15:41:18 +01:00
2021-05-14 17:16:45 +02:00
Derived &WithMaxRecvBufferSize(size_t size) {
mSocketOption.maxRecvBufferSize = size;
return static_cast<Derived &>(*this);
}
2021-04-30 16:10:14 +02:00
#ifdef BRYNET_USE_OPENSSL
2021-05-14 17:16:45 +02:00
Derived &WithSSL(SSLHelper::Ptr sslHelper) {
mSocketOption.sslHelper = std::move(sslHelper);
mSocketOption.useSSL = true;
return static_cast<Derived &>(*this);
}
2021-04-30 16:10:14 +02:00
#endif
2021-05-14 17:16:45 +02:00
Derived &WithForceSameThreadLoop() {
mSocketOption.forceSameThreadLoop = true;
return static_cast<Derived &>(*this);
}
2020-11-24 15:41:18 +01:00
2021-05-14 17:16:45 +02:00
Derived &AddEnterCallback(const TcpConnection::EnterCallback &callback) {
mSocketOption.enterCallback.push_back(callback);
return static_cast<Derived &>(*this);
}
2020-11-24 15:41:18 +01:00
2021-05-14 17:16:45 +02:00
void asyncRun() {
if (mTcpService == nullptr) {
throw BrynetCommonException("tcp service is nullptr");
}
if (mListenAddr.empty()) {
throw BrynetCommonException("not config listen addr");
}
2020-11-24 15:41:18 +01:00
2021-05-14 17:16:45 +02:00
auto service = mTcpService;
auto option = mSocketOption;
mListenThread = ListenThread::Create(
mIsIpV6,
mListenAddr,
mPort,
2021-05-14 17:49:39 +02:00
[service, option](TcpSocket::Ptr socket) {
2021-05-14 17:16:45 +02:00
service->addTcpConnection(std::move(socket), option);
},
mSocketProcessCallbacks,
mEnabledReusePort);
mListenThread->startListen();
}
2020-11-24 15:41:18 +01:00
2021-05-14 17:16:45 +02:00
void stop() {
if (mListenThread) {
mListenThread->stopListen();
}
}
2020-11-24 15:41:18 +01:00
2021-04-30 16:10:14 +02:00
private:
2021-05-14 17:16:45 +02:00
TcpService::Ptr mTcpService;
std::vector<ListenThread::TcpSocketProcessCallback> mSocketProcessCallbacks;
ConnectionOption mSocketOption;
std::string mListenAddr;
int mPort = 0;
bool mIsIpV6 = false;
bool mEnabledReusePort = false;
ListenThread::Ptr mListenThread;
2021-04-30 16:10:14 +02:00
};
2020-11-24 15:41:18 +01:00
2021-05-14 17:16:45 +02:00
class ListenerBuilder : public BaseListenerBuilder<ListenerBuilder> {
2021-04-30 16:10:14 +02:00
};