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
|
|
|
|
|
|
|
namespace brynet { namespace net { namespace wrapper {
|
|
|
|
|
2021-04-30 16:10:14 +02:00
|
|
|
template<typename Derived>
|
|
|
|
class BaseListenerBuilder
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual ~BaseListenerBuilder() = default;
|
2020-11-24 15:41:18 +01:00
|
|
|
|
2021-04-30 16:10:14 +02:00
|
|
|
Derived& WithService(TcpService::Ptr service)
|
2020-11-24 15:41:18 +01:00
|
|
|
{
|
2021-04-30 16:10:14 +02:00
|
|
|
mTcpService = std::move(service);
|
|
|
|
return static_cast<Derived&>(*this);
|
|
|
|
}
|
2020-11-24 15:41:18 +01:00
|
|
|
|
2021-04-30 16:10:14 +02:00
|
|
|
Derived& WithAddr(bool ipV6, std::string ip, size_t port)
|
2020-11-24 15:41:18 +01:00
|
|
|
{
|
2021-04-30 16:10:14 +02:00
|
|
|
mIsIpV6 = ipV6;
|
|
|
|
mListenAddr = std::move(ip);
|
|
|
|
mPort = port;
|
|
|
|
return static_cast<Derived&>(*this);
|
|
|
|
}
|
2020-11-24 15:41:18 +01:00
|
|
|
|
2021-04-30 16:10:14 +02:00
|
|
|
Derived& WithReusePort()
|
|
|
|
{
|
|
|
|
mEnabledReusePort = true;
|
|
|
|
return static_cast<Derived&>(*this);
|
|
|
|
}
|
2020-11-24 15:41:18 +01:00
|
|
|
|
2021-04-30 16:10:14 +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-04-30 16:10:14 +02:00
|
|
|
Derived& WithMaxRecvBufferSize(size_t size)
|
|
|
|
{
|
|
|
|
mSocketOption.maxRecvBufferSize = size;
|
|
|
|
return static_cast<Derived&>(*this);
|
|
|
|
}
|
|
|
|
#ifdef BRYNET_USE_OPENSSL
|
|
|
|
Derived& WithSSL(SSLHelper::Ptr sslHelper)
|
|
|
|
{
|
|
|
|
mSocketOption.sslHelper = std::move(sslHelper);
|
|
|
|
mSocketOption.useSSL = true;
|
|
|
|
return static_cast<Derived&>(*this);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
Derived& WithForceSameThreadLoop()
|
|
|
|
{
|
|
|
|
mSocketOption.forceSameThreadLoop = true;
|
|
|
|
return static_cast<Derived&>(*this);
|
|
|
|
}
|
2020-11-24 15:41:18 +01:00
|
|
|
|
2021-04-30 16:10:14 +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-04-30 16:10:14 +02:00
|
|
|
void asyncRun()
|
|
|
|
{
|
|
|
|
if (mTcpService == nullptr)
|
2020-11-24 15:41:18 +01:00
|
|
|
{
|
2021-04-30 16:10:14 +02:00
|
|
|
throw BrynetCommonException("tcp service is nullptr");
|
2020-11-24 15:41:18 +01:00
|
|
|
}
|
2021-04-30 16:10:14 +02:00
|
|
|
if (mListenAddr.empty())
|
2020-11-24 15:41:18 +01:00
|
|
|
{
|
2021-04-30 16:10:14 +02:00
|
|
|
throw BrynetCommonException("not config listen addr");
|
2020-11-24 15:41:18 +01:00
|
|
|
}
|
|
|
|
|
2021-04-30 16:10:14 +02:00
|
|
|
auto service = mTcpService;
|
|
|
|
auto option = mSocketOption;
|
|
|
|
mListenThread = ListenThread::Create(
|
|
|
|
mIsIpV6,
|
|
|
|
mListenAddr,
|
|
|
|
mPort,
|
|
|
|
[service, option](brynet::net::TcpSocket::Ptr socket) {
|
|
|
|
service->addTcpConnection(std::move(socket), option);
|
|
|
|
},
|
|
|
|
mSocketProcessCallbacks,
|
|
|
|
mEnabledReusePort);
|
|
|
|
mListenThread->startListen();
|
|
|
|
}
|
2020-11-24 15:41:18 +01:00
|
|
|
|
2021-04-30 16:10:14 +02:00
|
|
|
void stop()
|
|
|
|
{
|
|
|
|
if (mListenThread)
|
2020-11-24 15:41:18 +01:00
|
|
|
{
|
2021-04-30 16:10:14 +02:00
|
|
|
mListenThread->stopListen();
|
2020-11-24 15:41:18 +01:00
|
|
|
}
|
2021-04-30 16:10:14 +02:00
|
|
|
}
|
2020-11-24 15:41:18 +01:00
|
|
|
|
2021-04-30 16:10:14 +02:00
|
|
|
private:
|
|
|
|
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;
|
|
|
|
};
|
2020-11-24 15:41:18 +01:00
|
|
|
|
2021-04-30 16:10:14 +02:00
|
|
|
class ListenerBuilder : public BaseListenerBuilder<ListenerBuilder>
|
|
|
|
{
|
|
|
|
};
|
2020-11-24 15:41:18 +01:00
|
|
|
|
2021-04-30 16:10:14 +02:00
|
|
|
}}}// namespace brynet::net::wrapper
|