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

88 lines
2.6 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 "Socket.h"
namespace ehs
{
class BaseUDP
{
protected:
AddrType type;
Str_8 address;
UInt_16 port;
bool bound;
public:
virtual ~BaseUDP() = default;
2024-02-01 19:36:28 -08:00
/// Initializes the socket with the defaults.
2024-01-31 22:28:19 -08:00
BaseUDP();
2024-02-01 19:36:28 -08:00
/// Properly initializes the socket.
/// @param [in] type The ip version to initialize the socket with.
2024-01-31 22:28:19 -08:00
BaseUDP(AddrType type);
BaseUDP(BaseUDP&& udp) noexcept;
BaseUDP(const BaseUDP& udp);
BaseUDP& operator=(BaseUDP&& udp) noexcept;
BaseUDP& operator=(const BaseUDP& udp);
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] type The ip version to use.
/// @param [in] address The ip address to bind to.
/// @param [in] port The port to bind to.
/// @note Used for servers.
2024-01-31 22:28:19 -08:00
virtual void Bind(AddrType type, const Str_8& address, UInt_16 port) = 0;
2024-02-01 19:36:28 -08:00
/// Sends data to the endpoint.
/// @param [in] type The ip version of the endpoint.
/// @param [in] address The ip address of the endpoint.
/// @param [in] port The port of the endpoint is bound to.
2024-01-31 22:28:19 -08:00
virtual UInt_64 Send(AddrType type, const Str_8& address, UInt_16 port, const Byte* data, UInt_64 size) = 0;
2024-02-01 19:36:28 -08:00
/// Receives data from the endpoint.
/// @param [out] type The ip version of the endpoint.
/// @param [out] address The ip address of the endpoint.
/// @param [out] port The port of the endpoint.
/// @param [out] data 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.
2024-01-31 22:28:19 -08:00
virtual UInt_64 Receive(AddrType* type, Str_8* address, UInt_16* port, Byte* data, UInt_64 size) = 0;
2024-02-01 19:36:28 -08:00
/// Retrieves whether or 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
/// Sets whether or not the socket blocks the thread when receiving data.
/// @param [in] blocking Whether or not to block.
2024-01-31 22:28:19 -08:00
virtual void SetBlocking(bool blocking) = 0;
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 the bound ip version.
/// @returns The result.
2024-01-31 22:28:19 -08:00
AddrType GetLocalAddressType() const;
2024-02-01 19:36:28 -08:00
/// Retrieves the bound ip address.
/// @returns The bound 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 bound port.
2024-01-31 22:28:19 -08:00
UInt_16 GetLocalPort() const;
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;
};
}