#ifndef MS_TCP_SERVER_HANDLE_HPP
#define MS_TCP_SERVER_HANDLE_HPP
#include "common.hpp"
#include "handles/TcpConnectionHandle.hpp"
#include <uv.h>
#include <ankerl/unordered_dense.h>
#include <string>
class TcpServerHandle : public TcpConnectionHandle::Listener
{
public:
explicit TcpServerHandle(uv_tcp_t* uvHandle);
~TcpServerHandle() override;
public:
void Dump(int indentation = 0) const;
const struct sockaddr* GetLocalAddress() const
{
return reinterpret_cast<const struct sockaddr*>(&this->localAddr);
}
int GetLocalFamily() const
{
return reinterpret_cast<const struct sockaddr*>(&this->localAddr)->sa_family;
}
const std::string& GetLocalIp() const
{
return this->localIp;
}
uint16_t GetLocalPort() const
{
return this->localPort;
}
size_t GetNumConnections() const
{
return this->connections.size();
}
uint32_t GetSendBufferSize() const;
void SetSendBufferSize(uint32_t size);
uint32_t GetRecvBufferSize() const;
void SetRecvBufferSize(uint32_t size);
protected:
void AcceptTcpConnection(TcpConnectionHandle* connection);
private:
void InternalClose();
bool SetLocalAddress();
protected:
virtual void UserOnTcpConnectionAlloc() = 0;
virtual void UserOnTcpConnectionClosed(TcpConnectionHandle* connection) = 0;
public:
void OnUvConnection(int status);
public:
void OnTcpConnectionClosed(TcpConnectionHandle* connection) override;
protected:
struct sockaddr_storage localAddr{};
std::string localIp;
uint16_t localPort{ 0u };
private:
uv_tcp_t* uvHandle{ nullptr };
ankerl::unordered_dense::set<TcpConnectionHandle*> connections;
bool closed{ false };
};
#endif