#ifndef MS_TCP_CONNECTION_HPP
#define MS_TCP_CONNECTION_HPP
#include "common.hpp"
#include <uv.h>
#include <string>
class TcpConnectionHandler
{
protected:
using onSendCallback = const std::function<void(bool sent)>;
public:
class Listener
{
public:
virtual ~Listener() = default;
public:
virtual void OnTcpConnectionClosed(TcpConnectionHandler* connection) = 0;
};
public:
struct UvWriteData
{
explicit UvWriteData(size_t storeSize)
{
this->store = new uint8_t[storeSize];
}
UvWriteData(const UvWriteData&) = delete;
~UvWriteData()
{
delete[] this->store;
delete this->cb;
}
uv_write_t req;
uint8_t* store{ nullptr };
TcpConnectionHandler::onSendCallback* cb{ nullptr };
};
public:
explicit TcpConnectionHandler(size_t bufferSize);
TcpConnectionHandler& operator=(const TcpConnectionHandler&) = delete;
TcpConnectionHandler(const TcpConnectionHandler&) = delete;
virtual ~TcpConnectionHandler();
public:
void Close();
virtual void Dump() const;
void Setup(
Listener* listener,
struct sockaddr_storage* localAddr,
const std::string& localIp,
uint16_t localPort);
bool IsClosed() const
{
return this->closed;
}
uv_tcp_t* GetUvHandle() const
{
return this->uvHandle;
}
void Start();
void Write(const uint8_t* data, size_t len, TcpConnectionHandler::onSendCallback* cb);
void Write(
const uint8_t* data1,
size_t len1,
const uint8_t* data2,
size_t len2,
TcpConnectionHandler::onSendCallback* cb);
void ErrorReceiving();
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;
}
const struct sockaddr* GetPeerAddress() const
{
return reinterpret_cast<const struct sockaddr*>(&this->peerAddr);
}
const std::string& GetPeerIp() const
{
return this->peerIp;
}
uint16_t GetPeerPort() const
{
return this->peerPort;
}
size_t GetRecvBytes() const
{
return this->recvBytes;
}
size_t GetSentBytes() const
{
return this->sentBytes;
}
private:
bool SetPeerAddress();
public:
void OnUvReadAlloc(size_t suggestedSize, uv_buf_t* buf);
void OnUvRead(ssize_t nread, const uv_buf_t* buf);
void OnUvWrite(int status, onSendCallback* cb);
protected:
virtual void UserOnTcpConnectionRead() = 0;
protected:
size_t bufferSize{ 0u };
uint8_t* buffer{ nullptr };
size_t bufferDataLen{ 0u };
std::string localIp;
uint16_t localPort{ 0u };
struct sockaddr_storage peerAddr;
std::string peerIp;
uint16_t peerPort{ 0u };
private:
Listener* listener{ nullptr };
uv_tcp_t* uvHandle{ nullptr };
struct sockaddr_storage* localAddr{ nullptr };
bool closed{ false };
size_t recvBytes{ 0u };
size_t sentBytes{ 0u };
bool isClosedByPeer{ false };
bool hasError{ false };
};
#endif