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

162 lines
4.5 KiB
C
Raw Normal View History

2024-01-31 22:28:19 -08:00
#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;
UInt_16 localPort;
2024-01-31 22:28:19 -08:00
Str_8 remoteHostName;
Str_8 remoteAddr;
UInt_16 remotePort;
2024-01-31 22:28:19 -08:00
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;
2024-02-01 19:36:28 -08:00
/// Initializes the socket with the defaults.
2024-01-31 22:28:19 -08:00
BaseTCP();
2024-02-01 19:36:28 -08:00
/// Properly initializes the socket.
/// @param [in] type The ip version to initialize the socket with.
BaseTCP(AddrType addrType);
2024-01-31 22:28:19 -08:00
BaseTCP(BaseTCP&& tcp) noexcept;
BaseTCP(const BaseTCP& tcp);
BaseTCP& operator=(BaseTCP&& tcp) noexcept;
BaseTCP& operator=(const BaseTCP& tcp);
2024-02-01 19:36:28 -08:00
/// Explicitly initialize the socket.
2024-01-31 22:28:19 -08:00
virtual void Initialize() = 0;
2024-02-01 19:36:28 -08:00
/// Explicitly release resources before it falls off the stack.
2024-01-31 22:28:19 -08:00
virtual void Release() = 0;
2024-02-01 19:36:28 -08:00
/// Binds to socket to a specified address and port.
/// @param [in] address The ip address to bind to.
/// @param [in] port The port to bind to.
/// @note Used for servers.
virtual void Bind(const Str_8& address, UInt_16 port) = 0;
2024-01-31 22:28:19 -08:00
2024-02-01 19:36:28 -08:00
/// Listens for new incoming connections.
/// @note Used for servers.
2024-01-31 22:28:19 -08:00
virtual void Listen() = 0;
2024-02-01 19:36:28 -08:00
/// Accepts the new incoming connection.
/// @note Used for servers.
2024-01-31 22:28:19 -08:00
virtual BaseTCP* Accept() = 0;
2024-02-01 19:36:28 -08:00
/// Connects to a server at the specified address and port.
/// @param [in] address The ip address to connect to.
/// @param [in] port The port to connect to.
/// @note Used for clients.
virtual void Connect(const Str_8& address, UInt_16 port) = 0;
/// Sends data to the connected endpoint.
/// @param [in] buffer The data to send to the endpoint.
/// @param [in] size The size in bytes of data being sent.
virtual UInt_64 Send(const Byte* buffer, UInt_32 size) = 0;
/// Receives data from the connected endpoint.
/// @param [out] buffer The incoming data from the endpoint.
/// @param [in] size The max size of the buffer in bytes to store the data.
/// @returns The size of the incoming data in bytes.
virtual UInt_64 Receive(Byte* buffer, UInt_32 size) = 0;
/// Sends a string to the connected endpoint.
/// @param [in] str The string to send to the endpoint.
2024-01-31 22:28:19 -08:00
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();
2024-02-01 19:36:28 -08:00
/// Retrieves the sockets ip version.
/// @returns The ip version.
2024-01-31 22:28:19 -08:00
AddrType GetAddressType() const;
2024-02-01 19:36:28 -08:00
/// Retrieves the bound ip address.
/// @returns The ip address.
2024-01-31 22:28:19 -08:00
Str_8 GetLocalAddress() const;
2024-02-01 19:36:28 -08:00
/// Retrieves the bound port.
/// @returns The port.
2024-01-31 22:28:19 -08:00
unsigned short GetLocalPort() const;
2024-02-01 19:36:28 -08:00
/// Retrieves the ip address of the connected endpoint.
/// @returns The ip address.
2024-01-31 22:28:19 -08:00
Str_8 GetRemoteAddress() const;
2024-02-01 19:36:28 -08:00
/// Retrieves the port of the connected endpoint.
/// @returns The port.
UInt_16 GetRemotePort() const;
2024-01-31 22:28:19 -08:00
2024-02-01 19:36:28 -08:00
/// Retrieves whether or not this socket is connected to a client endpoint.
/// @returns The result.
2024-01-31 22:28:19 -08:00
bool IsConnection() const;
2024-02-01 19:36:28 -08:00
/// Retrieves whether of not this socket is bound to an ip address and port.
/// @returns The result.
2024-01-31 22:28:19 -08:00
bool IsBound() const;
2024-02-01 19:36:28 -08:00
/// Retrieves whether or not this socket is listening for incoming connections.
/// @returns The result.
2024-01-31 22:28:19 -08:00
bool IsListening() const;
2024-02-01 19:36:28 -08:00
/// Retrieves whether or not this socket is connected to an endpoint.
/// @returns The result.
2024-01-31 22:28:19 -08:00
bool IsConnected() const;
2024-02-01 19:36:28 -08:00
/// Sets whether or not the socket blocks the thread when receiving data.
/// @param [in] blocking Whether or not to block.
virtual void SetBlocking(bool blocking) = 0;
2024-01-31 22:28:19 -08:00
2024-02-01 19:36:28 -08:00
/// Retrieves whether or not when receiving data blocks the thread.
/// @returns The result.
2024-01-31 22:28:19 -08:00
virtual bool IsBlocking() const = 0;
2024-02-01 19:36:28 -08:00
/// Retrieves whether or not this socket was initialized.
/// @returns The result.
2024-01-31 22:28:19 -08:00
virtual bool IsValid() const = 0;
private:
Str_8 RecvHeader();
2024-02-01 19:36:28 -08:00
Str_8 RecvBody(UInt_64 contentLength);
2024-01-31 22:28:19 -08:00
UInt_64 RecvChunkSize();
2024-02-01 19:36:28 -08:00
Str_8 RecvChunk(UInt_64 chunkSize);
2024-01-31 22:28:19 -08:00
};
}