EHS/include/ehs/io/socket/BaseTCP.h

112 lines
2.3 KiB
C++

#pragma once
#include "ehs/EHS.h"
#include "ehs/Str.h"
#include "Request.h"
#include "Response.h"
#include "Socket.h"
namespace ehs
{
class BaseTCP
{
protected:
AddrType addrType;
Str_8 localAddr;
unsigned short localPort;
Str_8 remoteHostName;
Str_8 remoteAddr;
unsigned short remotePort;
bool connection;
bool bound;
bool listening;
bool connected;
public:
static const UInt_16 HTTPS_Port = 443;
static const UInt_16 HTTP_Port = 80;
static const UInt_16 MaxHeaderSize = 8192;
virtual ~BaseTCP() = default;
BaseTCP();
BaseTCP(const AddrType addrType);
BaseTCP(BaseTCP&& tcp) noexcept;
BaseTCP(const BaseTCP& tcp);
BaseTCP& operator=(BaseTCP&& tcp) noexcept;
BaseTCP& operator=(const BaseTCP& tcp);
virtual void Initialize() = 0;
virtual void Release() = 0;
virtual void Bind(const Str_8& address, unsigned short port) = 0;
virtual void Listen() = 0;
virtual BaseTCP* Accept() = 0;
virtual void Connect(const Str_8& address, const unsigned short port) = 0;
virtual UInt_64 Send(const Byte* const buffer, const UInt_32 size) = 0;
virtual UInt_64 Receive(Byte* const buffer, const UInt_32 size) = 0;
void SendStr(const Str_8& str);
/// Sends a HTTP response to the connected endpoint.
/// @param [in] res The response to send.
void SendRes(const Response& res);
/// Sends a HTTP request to the connected endpoint.
/// @param [in] req The request to send.
void SendReq(Request& req);
/// Receives a HTTP response from the connected endpoint.
/// @returns The response received.
Response RecvRes();
/// Receives a HTTP request from the connected endpoint.
/// @returns The request received.
Request RecvReq();
AddrType GetAddressType() const;
Str_8 GetLocalAddress() const;
unsigned short GetLocalPort() const;
Str_8 GetRemoteAddress() const;
unsigned short GetRemotePort() const;
bool IsConnection() const;
bool IsBound() const;
bool IsListening() const;
bool IsConnected() const;
virtual void SetBlocking(const bool blocking) = 0;
virtual bool IsBlocking() const = 0;
virtual bool IsValid() const = 0;
private:
Str_8 RecvHeader();
Str_8 RecvBody(const UInt_64 contentLength);
UInt_64 RecvChunkSize();
Str_8 RecvChunk(const UInt_64 chunkSize);
};
}