Adjusted workflow.
All checks were successful
Build & Release / Windows-AMD64-Build (push) Successful in 1m8s
Build & Release / Linux-AMD64-Build (push) Successful in 1m30s
Build & Release / Linux-AARCH64-Build (push) Successful in 3m21s

This commit is contained in:
2024-02-05 22:25:30 -08:00
commit bcd71cf2b5
251 changed files with 45909 additions and 0 deletions

View File

@@ -0,0 +1,162 @@
#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;
Str_8 remoteHostName;
Str_8 remoteAddr;
UInt_16 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;
/// Initializes the socket with the defaults.
BaseTCP();
/// Properly initializes the socket.
/// @param [in] type The ip version to initialize the socket with.
BaseTCP(AddrType addrType);
BaseTCP(BaseTCP&& tcp) noexcept;
BaseTCP(const BaseTCP& tcp);
BaseTCP& operator=(BaseTCP&& tcp) noexcept;
BaseTCP& operator=(const BaseTCP& tcp);
/// Explicitly initialize the socket.
virtual void Initialize() = 0;
/// Explicitly release resources before it falls off the stack.
virtual void Release() = 0;
/// 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;
/// Listens for new incoming connections.
/// @note Used for servers.
virtual void Listen() = 0;
/// Accepts the new incoming connection.
/// @note Used for servers.
virtual BaseTCP* Accept() = 0;
/// 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.
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();
/// Retrieves the sockets ip version.
/// @returns The ip version.
AddrType GetAddressType() const;
/// Retrieves the bound ip address.
/// @returns The ip address.
Str_8 GetLocalAddress() const;
/// Retrieves the bound port.
/// @returns The port.
unsigned short GetLocalPort() const;
/// Retrieves the ip address of the connected endpoint.
/// @returns The ip address.
Str_8 GetRemoteAddress() const;
/// Retrieves the port of the connected endpoint.
/// @returns The port.
UInt_16 GetRemotePort() const;
/// Retrieves whether or not this socket is connected to a client endpoint.
/// @returns The result.
bool IsConnection() const;
/// Retrieves whether of not this socket is bound to an ip address and port.
/// @returns The result.
bool IsBound() const;
/// Retrieves whether or not this socket is listening for incoming connections.
/// @returns The result.
bool IsListening() const;
/// Retrieves whether or not this socket is connected to an endpoint.
/// @returns The result.
bool IsConnected() const;
/// 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;
/// Retrieves whether or not when receiving data blocks the thread.
/// @returns The result.
virtual bool IsBlocking() const = 0;
/// Retrieves whether or not this socket was initialized.
/// @returns The result.
virtual bool IsValid() const = 0;
private:
Str_8 RecvHeader();
Str_8 RecvBody(UInt_64 contentLength);
UInt_64 RecvChunkSize();
Str_8 RecvChunk(UInt_64 chunkSize);
};
}

View File

@@ -0,0 +1,88 @@
#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;
/// Initializes the socket with the defaults.
BaseUDP();
/// Properly initializes the socket.
/// @param [in] type The ip version to initialize the socket with.
BaseUDP(AddrType type);
BaseUDP(BaseUDP&& udp) noexcept;
BaseUDP(const BaseUDP& udp);
BaseUDP& operator=(BaseUDP&& udp) noexcept;
BaseUDP& operator=(const BaseUDP& udp);
/// Explicitly release resources before it falls off the stack.
virtual void Release() = 0;
/// 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.
virtual void Bind(AddrType type, const Str_8& address, UInt_16 port) = 0;
/// 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.
virtual UInt_64 Send(AddrType type, const Str_8& address, UInt_16 port, const Byte* data, UInt_64 size) = 0;
/// 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.
virtual UInt_64 Receive(AddrType* type, Str_8* address, UInt_16* port, Byte* data, UInt_64 size) = 0;
/// Retrieves whether or not this socket is bound to an ip address and port.
/// @returns The result.
bool IsBound() const;
/// 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;
/// Retrieves whether or not when receiving data blocks the thread.
/// @returns The result.
virtual bool IsBlocking() const = 0;
/// Retrieves the bound ip version.
/// @returns The result.
AddrType GetLocalAddressType() const;
/// Retrieves the bound ip address.
/// @returns The bound ip address.
Str_8 GetLocalAddress() const;
/// Retrieves the bound port.
/// @returns The bound port.
UInt_16 GetLocalPort() const;
/// Retrieves whether or not this socket was initialized.
/// @returns The result.
virtual bool IsValid() const = 0;
};
}

View File

@@ -0,0 +1,17 @@
#pragma once
#include "ehs/EHS.h"
#include "ehs/Str.h"
#include "Socket.h"
namespace ehs
{
class DNS
{
public:
/// Resolves a hostname to an ip address.
/// @param [in] hostname The given hostname to resolve.
/// @returns The resulting ip address.
static Str_8 Resolve(const Str_8& hostname);
};
}

View File

@@ -0,0 +1,164 @@
#pragma once
#include "ehs/EHS.h"
#include "ehs/Vector.h"
#include "ehs/Str.h"
#include "ehs/json/Json.h"
#include "Socket.h"
namespace ehs
{
enum class Verb
{
POST,
GET,
PUT,
DEL
};
class Request
{
private:
Verb verb;
Str_8 rsrc;
Vector<Str_8> queries;
Vector<Str_8> header;
ContentType cType;
Str_8 body;
public:
/// Default member initialization.
Request();
/// Initializes this request with a given verb and URI resource.
/// @param [in] verb The type of request to make.
/// @param [in] rsrc The URI endpoint to make the request at.
Request(const Verb verb, const Str_8& rsrc);
/// Initializes this request with the raw request data.
/// @param [in] data The C-style string of the request.
/// @param [in] size The size of the given C-style string.
Request(const char* data, const UInt_64 size);
/// Initializes this request with the raw request data.
/// @param [in] data The string of the request.
Request(const Str_8& data);
/// Copies members from another object of the same type.
/// @param [in] req The object to copy from.
Request(const Request& req) = default;
/// Copies members from another object of the same type.
/// @param [in] req The object to copy from.
/// @returns The request that has been assigned to.
Request& operator=(const Request& req);
/// Retrieves the verb for the request.
/// @returns The result.
Verb GetVerb() const;
/// Sets the content type for the body.
/// @param [in] cType The content type to use.
void SetContentType(const ContentType cType);
/// Retrieves the content type for the body.
/// @returns The result.
ContentType GetContentType() const;
/// Sets the URI resource.
/// @param [in] rsrc The resource.
void SetResource(const Str_8& rsrc);
/// Retrieves the URI resource.
/// @returns The result.
Str_8 GetResource() const;
/// Adds a query variable to the URI.
/// @param [in] var The variable identifier.
/// @param [in] value The value of the variable.
void AddQuery(const Str_8& var, const Str_8& value);
/// Retrieves a query variable from the URI.
/// @param [in] var The variable identifier to look for.
/// @returns The value of the query variable. Empty if it was not found.
Str_8 GetQuery(const Str_8& var);
/// Retrieves all the query variables from the URI in a vector object.
/// @returns The result.
Vector<Str_8> GetQueries() const;
/// A helper method to automatically add the required header variables for basic authentication.
/// @param [in] id The username or id.
/// @param [in] secret The secret given by an API.
void BasicAuth(const Str_8& id, const Str_8& secret);
/// A helper method to automatically add the required header variables for bearer authentication.
/// @param [in] token The token given by an API.
void BearerAuth(const Str_8& token);
/// A helper method to automatically add the required header variables for bearer authentication.
/// @param [in] token The token given by an API.
/// @param [in] clientId The client id given by an API.
void BearerAuth(const Str_8& token, const Str_8& clientId);
/// A helper method to automatically add the required header variables for bot authentication.
/// @param [in] token The token given by an API.
void BotAuth(const Str_8& token);
/// Adds a header variable.
/// @param [in] var The variable identifier.
/// @param [in] value The value of the variable.
void AddToHeader(const Str_8& var, const Str_8& value);
/// Retrieves a header variable.
/// @param [in] var The variable identifier to look for.
/// @returns The value of the header variable. Empty if it was not found.
Str_8 GetHeader(const Str_8& var) const;
/// Retrieves all the header variables in a vector object.
/// @returns The result.
Vector<Str_8> GetHeader() const;
/// Adds a body variable.
/// @param [in] var The variable identifier.
/// @param [in] value The value of the variable.
void AddToBody(const Str_8& var, const Str_8& value);
/// Adds a value to the body.
/// @param [in] data The value to add.
void AddToBody(const Str_8& data);
/// Sets the entire body.
/// @param [in] body The body to use.
void SetBody(const Str_8& body);
/// Retrieves a body variable.
/// @param [in] var The variable identifier to look for.
/// @returns The value of the body variable. Empty if it was not found.
Str_8 GetVar(const Str_8& var) const;
/// Retrieves the entire body.
/// @returns The result.
Str_8 GetBody() const;
/// Retrieves the entire body as a Json.
/// @returns The result.
Json GetJson() const;
/// Forms the raw result of the request to be sent.
/// @returns The result.
Str_8 FormResult() const;
bool IsValid() const;
private:
static Str_8 VerbToStr(const Verb verb);
static Str_8 ContentTypeToStr(const ContentType cType);
static ContentType StrToContentType(const Str_8& value);
void ReadData(const Str_8& data);
};
}

View File

@@ -0,0 +1,127 @@
#pragma once
#include "ehs/EHS.h"
#include "ehs/Vector.h"
#include "ehs/Str.h"
#include "ehs/json/Json.h"
#include "Socket.h"
namespace ehs
{
class Response
{
private:
UInt_32 code;
Str_8 server;
ContentType cType;
Vector<Str_8> header;
Str_8 body;
public:
/// Default member initialization.
Response();
/// Initializes this response with a given code and server identifier.
/// @param [in] code The code to give.
/// @param [in] server The server identifier.
Response(const UInt_32 code, const Str_8& server);
/// Initializes this response with the raw response data.
/// @param [in] data The C-style string of the response.
/// @param [in] size The size of the given C-style string.
Response(const char* data, const UInt_64 size);
/// Initializes this response with the raw response data.
/// @param [in] data The string of the response.
Response(const Str_8& data);
/// Copies members from another object of the same type.
/// @param [in] res The object to copy from.
Response(const Response& res) = default;
/// Copies members from another object of the same type.
/// @param [in] res The object to copy from.
/// @returns The response that has been assigned to.
Response& operator=(const Response& res);
/// Sets the response code to send to the endpoint.
/// @param [in] code The code for success, error or info.
void SetCode(const UInt_32 code);
/// Retrieves the response code.
/// @returns The result.
UInt_32 GetCode() const;
/// Sets the server identifier.
/// @param [in] server The server identifier to use.
void SetServer(const Str_8& server);
/// Retrieves the server identifier.
/// @returns The result.
Str_8 GetServer() const;
/// Sets the content type for the body.
/// @param [in] cType The content type to use.
void SetContentType(const ContentType cType);
/// Retrieves the content type for the body.
/// @returns The result.
ContentType GetContentType() const;
/// Adds a header variable.
/// @param [in] var The variable identifier.
/// @param [in] value The value of the variable.
void AddToHeader(const Str_8& var, const Str_8& value);
/// Retrieves a header variable.
/// @param [in] var The variable identifier to look for.
/// @returns The value of the header variable. Empty if it was not found.
Str_8 GetHeader(const Str_8& var) const;
/// Retrieves all the header variables in a vector object.
/// @returns The result.
Vector<Str_8> GetHeader() const;
/// Adds a body variable.
/// @param [in] var The variable identifier.
/// @param [in] value The value of the variable.
void AddToBody(const Str_8& var, const Str_8& value);
/// Adds a value to the body.
/// @param [in] data The value to add.
void AddToBody(const Str_8& data);
/// Sets the entire body.
/// @param [in] body The body to use.
void SetBody(const Str_8& body);
/// Retrieves a body variable.
/// @param [in] var The variable identifier to look for.
/// @returns The value of the body variable. Empty if it was not found.
Str_8 GetVar(const Str_8& var) const;
/// Retrieves the entire body.
/// @returns The result.
Str_8 GetBody() const;
/// Retrieves the entire body as a Json.
/// @returns The result.
Json GetJson() const;
/// Forms the raw result of the response to be sent.
/// @returns The result.
Str_8 FormResult() const;
bool IsValid() const;
private:
static Str_8 CodeToStr(const UInt_32 code);
static Str_8 ContentTypeToStr(const ContentType cType);
static ContentType StrToContentType(const Str_8& value);
void ReadData(const Str_8& data);
};
}

View File

@@ -0,0 +1,56 @@
#pragma once
#include "ehs/EHS.h"
#include "ehs/Str.h"
#include "TCP.h"
#include "Request.h"
#include "Response.h"
typedef struct ssl_ctx_st SSL_CTX;
typedef struct ssl_st SSL;
namespace ehs
{
/// A class for handling the HTTP(S) TCP socket layer.
class SSL : public TCP
{
private:
SSL_CTX* ctx;
::SSL* sslHdl;
public:
~SSL() override;
SSL();
SSL(const AddrType type);
SSL(TCP&& tcp) noexcept;
SSL(const TCP& tcp);
SSL(const SSL& ssl);
SSL& operator=(const SSL& ssl);
void Initialize() override;
void Release() override;
void Bind(const Str_8& address, unsigned short port) override;
SSL* Accept() override;
void Connect(const Str_8& address, const UInt_16 port) override;
UInt_64 Send(const Byte* const buffer, const UInt_32 size) override;
UInt_64 Receive(Byte* const buffer, const UInt_32 size) override;
void UseCertificate(const Byte* data, const UInt_64 size);
void UsePrivateKey(const Byte* data, const UInt_64 size);
bool IsValid();
};
}

View File

@@ -0,0 +1,51 @@
#pragma once
#ifndef EHS_IPV4_HEADER
#define EHS_IPV4_HEADER 60
#endif
#ifndef EHS_IPV6_HEADER
#define EHS_IPV6_HEADER 40
#endif
#ifndef EHS_UDP_HEADER
#define EHS_UDP_HEADER 8
#endif
#ifndef EHS_IPV4_UDP_PAYLOAD
#define EHS_IPV4_UDP_PAYLOAD (EHS_UINT_16_MAX - EHS_IPV4_HEADER - EHS_UDP_HEADER)
#endif
#ifndef EHS_IPV6_UDP_PAYLOAD
#define EHS_IPV6_UDP_PAYLOAD (EHS_UINT_16_MAX - EHS_IPV6_HEADER - EHS_UDP_HEADER)
#endif
namespace ehs
{
enum class AddrType
{
IPV6,
IPV4
};
enum class ContentType
{
APP_MULTIPART_FORMDATA,
APP_FORMURLENCODED,
APP_JAVASCRIPT,
APP_JSON,
APP_XML,
TEXT_PLAIN,
TEXT_HTML,
TEXT_XML,
NONE
};
#if defined(EHS_OS_WINDOWS)
typedef UInt_64 Socket;
#define EHS_INVALID_SOCKET EHS_UINT_64_MAX
#elif defined(EHS_OS_LINUX)
typedef SInt_32 Socket;
#define EHS_INVALID_SOCKET (SInt_32)0xffffffff
#endif
}

View File

@@ -0,0 +1,7 @@
#pragma once
#ifdef EHS_OS_WINDOWS
#include "TCP_W32.h"
#else
#include "TCP_BSD.h"
#endif

View File

@@ -0,0 +1,94 @@
#pragma once
#include "ehs/EHS.h"
#include "ehs/Str.h"
#include "ehs/Log.h"
#include "Socket.h"
#include "BaseTCP.h"
namespace ehs
{
/// A wrapper class for the transmission control protocol socket.
class TCP : public BaseTCP
{
protected:
Socket hdl;
public:
/// Frees any native handles.
~TCP() override;
/// Default members initialization.
TCP();
TCP(AddrType addrType);
TCP(TCP&& tcp) noexcept;
/// Copies some members from the given TCP object.
/// @param [in] tcp The TCP object to copy from.
TCP(const TCP& tcp);
TCP& operator=(TCP&& tcp) noexcept;
/// Copies some members from the given TCP object.
/// @param [in] tcp The TCP object to copy from.
/// @returns The TCP object that has been assigned to.
TCP& operator=(const TCP& tcp);
void Initialize() override;
/// Frees native handles and uninitializes them.
void Release() override;
/// Binds the UDP socket to a local address and port.
/// @param [in] address The local IPv4 or IPv6 address to bind to. Resolves domain names. The given address can be empty, "127.0.0.1", or "localhost" to automatically find the appropriate device.
/// @param [in] port The port to bind to.
/// @note Requires the port given to be forwarded if this is called.
void Bind(const Str_8& address, UInt_16 port) override;
/// Listens for incoming connections. Used for servers or PtP.
void Listen() override;
/// Accepts an incoming connection. Used for servers or PtP.
/// @returns The accepted client object.
TCP* Accept() override;
/// Connects to a TCP Socket that listens for incoming connections. Used for clients or PtP.
/// @param address The address of the listening TCP socket. Resolves domain names. The given address can be empty, "127.0.0.1", or "localhost" to automatically find the appropriate device.
/// @param port The port of the listening TCP socket.
void Connect(const Str_8& address, UInt_16 port) override;
/// Sends data in a C-style array with raw functionality. Meaning no internal help outside of native functions besides error checking.
/// @param [in] buffer The C-style array to send.
/// @param [in] size The size of the given C-style array.
/// @returns The size of the data actually sent in bytes.
UInt_64 Send(const Byte* buffer, UInt_32 size) override;
/// Receives data in a C-style array with raw functionality. Meaning no internal help outside of native functions besides error checking.
/// @param [out] buffer The C-style array to receive with.
/// @param [in] size The size of the given C-style array.
/// @returns The size of the data actually received in bytes.
UInt_64 Receive(Byte* buffer, UInt_32 size) override;
/// Sets whether or not receiving data blocks the next task.
/// @param [in] blocking Whether or not to block.
void SetBlocking(bool blocking) override;
/// Retrieves whether or not this socket will block when receiving data.
/// @returns The result.
bool IsBlocking() const override;
bool IsValid() const override;
private:
void Bind_v6(const Str_8& address, UInt_16 port);
void Bind_v4(const Str_8& address, UInt_16 port);
void Connect_v6(const Str_8& address, UInt_16 port);
void Connect_v4(const Str_8& address, UInt_16 port);
};
}

View File

@@ -0,0 +1,94 @@
#pragma once
#include "ehs/EHS.h"
#include "ehs/Str.h"
#include "ehs/Log.h"
#include "Socket.h"
#include "BaseTCP.h"
namespace ehs
{
/// A wrapper class for the transmission control protocol socket.
class TCP : public BaseTCP
{
protected:
Socket hdl;
public:
/// Frees any native handles.
~TCP() override;
/// Default members initialization.
TCP();
TCP(AddrType addrType);
TCP(TCP&& tcp) noexcept;
/// Copies some members from the given TCP object.
/// @param [in] tcp The TCP object to copy from.
TCP(const TCP& tcp);
TCP& operator=(TCP&& tcp) noexcept;
/// Copies some members from the given TCP object.
/// @param [in] tcp The TCP object to copy from.
/// @returns The TCP object that has been assigned to.
TCP& operator=(const TCP& tcp);
void Initialize() override;
/// Frees native handles and uninitializes them.
void Release() override;
/// Binds the UDP socket to a local address and port.
/// @param [in] address The local IPv4 or IPv6 address to bind to. Resolves domain names. The given address can be empty, "127.0.0.1", or "localhost" to automatically find the appropriate device.
/// @param [in] port The port to bind to.
/// @note Requires the port given to be forwarded if this is called.
void Bind(const Str_8& address, UInt_16 port) override;
/// Listens for incoming connections. Used for servers or PtP.
void Listen() override;
/// Accepts an incoming connection. Used for servers or PtP.
/// @returns The accepted client object.
TCP* Accept() override;
/// Connects to a TCP Socket that listens for incoming connections. Used for clients or PtP.
/// @param address The address of the listening TCP socket. Resolves domain names. The given address can be empty, "127.0.0.1", or "localhost" to automatically find the appropriate device.
/// @param port The port of the listening TCP socket.
void Connect(const Str_8& address, UInt_16 port) override;
/// Sends data in a C-style array with raw functionality. Meaning no internal help outside of native functions besides error checking.
/// @param [in] buffer The C-style array to send.
/// @param [in] size The size of the given C-style array.
/// @returns The size of the data actually sent in bytes.
UInt_64 Send(const Byte* buffer, UInt_32 size) override;
/// Receives data in a C-style array with raw functionality. Meaning no internal help outside of native functions besides error checking.
/// @param [out] buffer The C-style array to receive with.
/// @param [in] size The size of the given C-style array.
/// @returns The size of the data actually received in bytes.
UInt_64 Receive(Byte* buffer, UInt_32 size) override;
/// Sets whether or not receiving data blocks the next task.
/// @param [in] blocking Whether or not to block.
void SetBlocking(bool blocking) override;
/// Retrieves whether or not this socket will block when receiving data.
/// @returns The result.
bool IsBlocking() const override;
bool IsValid() const override;
private:
void Bind_v6(const Str_8& address, UInt_16 port);
void Bind_v4(const Str_8& address, UInt_16 port);
void Connect_v6(const Str_8& address, UInt_16 port);
void Connect_v4(const Str_8& address, UInt_16 port);
};
}

View File

@@ -0,0 +1,7 @@
#pragma once
#ifdef EHS_OS_WINDOWS
#include "UDP_W32.h"
#else
#include "UDP_BSD.h"
#endif

View File

@@ -0,0 +1,82 @@
#pragma once
#include "ehs/EHS.h"
#include "ehs/Str.h"
#include "BaseUDP.h"
namespace ehs
{
/// A wrapper class for the user datagram protocol socket.
class UDP : public BaseUDP
{
private:
Socket hdl;
public:
/// Frees any native handles.
~UDP() override;
UDP();
/// Default members initialization.
UDP(AddrType type);
UDP(UDP&& udp) noexcept;
/// Copies some members from the given UDP object.
/// @param [in] udp The UDP object to copy from.
UDP(const UDP& udp);
UDP& operator=(UDP&& udp) noexcept;
/// Copies some members from the given UDP object.
/// @param [in] udp The UDP object to copy from.
/// @returns The UDP object that has been assigned to.
UDP& operator=(const UDP& udp);
/// Frees native handles and uninitializes them.
void Release() override;
/// Binds the UDP socket to a local address and port.
/// @param [in] address The local IPv4 or IPv6 address to bind to. Resolves domain names. The given address can be empty, "127.0.0.1", or "localhost" to automatically find the appropriate device.
/// @param [in] port The port to bind to.
/// @note Requires the port given to be forwarded if this is called.
void Bind(AddrType type, const Str_8& address, UInt_16 port) override;
/// Sends data using a C-style byte array.
/// @param [in] addr The remote Ipv4 or Ipv6 address to send to. Resolves domain names. The given address can be empty, "127.0.0.1", or "localhost" to automatically find the appropriate device.
/// @param [in] port The remote port to send to.
/// @param [in] data The C-style byte array to send.
/// @param [in] size The size of the C-style byte array.
/// @note The size of data to be sent cannot exceed "UDP::maxPayloadIpv4" or "UDP::maxPayloadIpv6".
UInt_64 Send(AddrType type, const Str_8& address, UInt_16 port, const Byte* data, UInt_64 size) override;
/// Receives data using the packet helper class.
/// @param [out] addr The Ipv4 or Ipv6 address of the sender.
/// @param [out] port The port of the sender.
/// @param [out] data The C-style byte array received.
/// @param [in] size The size of the pre-allocated C-style byte array.
/// @returns The size of the data received.
/// @warning The provided C-style byte array must be freed when finished using.
UInt_64 Receive(AddrType* type, Str_8* address, UInt_16* port, Byte* data, UInt_64 size) override;
/// Sets whether or not receiving data blocks the next task.
/// @param [in] blocking Whether or not to block.
void SetBlocking(bool blocking) override;
/// Retrieves whether or not this socket will block when receiving data.
/// @returns The result.
bool IsBlocking() const override;
bool IsValid() const override;
private:
void Bind_v6(const Str_8& address, UInt_16 port) const;
void Bind_v4(const Str_8& address, UInt_16 port) const;
UInt_64 Send_v6(const Str_8& address, UInt_16 port, const Byte* data, UInt_64 size);
UInt_64 Send_v4(const Str_8& address, UInt_16 port, const Byte* data, UInt_64 size);
};
}

View File

@@ -0,0 +1,82 @@
#pragma once
#include "ehs/EHS.h"
#include "ehs/Str.h"
#include "BaseUDP.h"
namespace ehs
{
/// A wrapper class for the user datagram protocol socket.
class UDP : public BaseUDP
{
private:
Socket hdl;
public:
/// Frees any native handles.
~UDP() override;
UDP();
/// Default members initialization.
UDP(const AddrType addrType);
UDP(UDP&& udp) noexcept;
/// Copies some members from the given UDP object.
/// @param [in] udp The UDP object to copy from.
UDP(const UDP& udp);
UDP& operator=(UDP&& udp) noexcept;
/// Copies some members from the given UDP object.
/// @param [in] udp The UDP object to copy from.
/// @returns The UDP object that has been assigned to.
UDP& operator=(const UDP& udp);
/// Frees native handles and uninitializes them.
void Release() override;
/// Binds the UDP socket to a local address and port.
/// @param [in] address The local IPv4 or IPv6 address to bind to. Resolves domain names. The given address can be empty, "127.0.0.1", or "localhost" to automatically find the appropriate device.
/// @param [in] port The port to bind to.
/// @note Requires the port given to be forwarded if this is called.
void Bind(AddrType type, const Str_8& address, UInt_16 port) override;
/// Sends data using a C-style byte array.
/// @param [in] addr The remote Ipv4 or Ipv6 address to send to. Resolves domain names. The given address can be empty, "127.0.0.1", or "localhost" to automatically find the appropriate device.
/// @param [in] port The remote port to send to.
/// @param [in] data The C-style byte array to send.
/// @param [in] size The size of the C-style byte array.
/// @note The size of data to be sent cannot exceed "UDP::maxPayloadIpv4" or "UDP::maxPayloadIpv6".
UInt_64 Send(AddrType type, const Str_8& addr, UInt_16 port, const Byte* data, UInt_64 size) override;
/// Receives data using the packet helper class.
/// @param [out] addr The Ipv4 or Ipv6 address of the sender.
/// @param [out] port The port of the sender.
/// @param [out] data The C-style byte array received.
/// @param [in] size The size of the pre-allocated C-style byte array.
/// @returns The size of the data received.
/// @warning The provided C-style byte array must be freed when finished using.
UInt_64 Receive(AddrType* type, Str_8* addr, UInt_16* port, Byte* data, UInt_64 size) override;
/// Sets whether or not receiving data blocks the next task.
/// @param [in] blocking Whether or not to block.
void SetBlocking(bool blocking) override;
/// Retrieves whether or not this socket will block when receiving data.
/// @returns The result.
bool IsBlocking() const override;
bool IsValid() const override;
private:
void Bind_v6(const Str_8& address, UInt_16 port);
void Bind_v4(const Str_8& address, UInt_16 port);
UInt_64 Send_v6(const Str_8& addr, UInt_16 port, const Byte* data, UInt_64 size);
UInt_64 Send_v4(const Str_8& addr, UInt_16 port, const Byte* data, UInt_64 size);
};
}

View File

@@ -0,0 +1,113 @@
#pragma once
#include "ehs/EHS.h"
#include "ehs/Str.h"
#include "ehs/Array.h"
#include "ehs/io/socket/SSL.h"
namespace ehs
{
enum class SpotifyState
{
TRACK,
CONTEXT,
OFF
};
struct Track
{
Array<Str_8> artists;
Str_8 name;
Str_8 id;
};
class Spotify
{
private:
SSL client;
Str_8 clientId;
Str_8 secret;
Str_8 redURI;
Array<Str_8> scopes;
bool forceVerify;
Str_8 token;
Str_8 rToken;
public:
static const Str_8 trackUriPrefix;
virtual ~Spotify();
Spotify();
Spotify(const Str_8& clientId, const Str_8& secret, const Str_8& redURI, const Array<Str_8>& scopes, const bool forceVerify);
bool Authorize();
/// Sets the volume for a device.
/// @param [in] level The percentage to set the volume to.
/// @returns The response code.
UInt_32 SetVolume(const UInt_8 level);
/// Resume playback for a device.
/// @returns The response code.
UInt_32 Play();
/// Pauses playback for a device.
/// @returns The response code.
UInt_32 Pause();
/// Repeats playback for a device.
/// @param [in] status The status to set it to.
/// @returns The response code.
UInt_32 SetRepeat(const SpotifyState state);
/// Shuffles playback for a device.
/// @param [in] state The state to set shuffle to.
/// @returns The response code.
UInt_32 SetShuffle(const bool state);
UInt_32 SearchTrack(Vector<Str_8>& artists, Str_8& id, Str_8& name);
UInt_32 GetPlayingTrack(Vector<Str_8>& artists, Str_8& id, Str_8& name);
UInt_32 GetQueue(Array<Track>& tracks);
/// Adds a track to the queue for a device.
/// @param [in] uri The track id to add.
/// @returns The response code.
UInt_32 QueueTrack(const Str_8& id);
UInt_32 AddTracks(const Str_8& playlistId, const Array<Str_8>& trackIds, const UInt_32 pos = 0);
UInt_32 AddTrack(const Str_8& playlistId, const Str_8& trackId, const UInt_32 pos = 0);
/// Skips to the next track.
/// @returns The response code.
UInt_32 Skip();
/// Skips to the previous track.
/// @returns The response code.
UInt_32 Previous();
/// Seeks to a position of the currently playing track in milliseconds.
/// @param [in] pos The position in milliseconds to seek to.
/// @returns The response code.
UInt_32 Seek(const UInt_32 pos);
Str_8 GetClientId() const;
Str_8 GetSecret() const;
Str_8 GetRedURI() const;
bool IsVerificationForced() const;
bool IsActive() const;
private:
void StartConnection();
bool ReAuthorize();
};
}

View File

@@ -0,0 +1,39 @@
#pragma once
#include "ehs/EHS.h"
#include "ehs/Str.h"
#include "ehs/io/socket/SSL.h"
namespace ehs
{
class Twitch
{
private:
SSL client;
Str_8 clientId;
Str_8 secret;
Str_8 redURI;
Array<Str_8> scopes;
bool forceVerify;
Str_8 token;
public:
virtual ~Twitch();
Twitch();
Twitch(const Str_8& clientId, const Str_8& secret, const Str_8& redURI, const Array<Str_8>& scopes, const bool forceVerify);
bool Authorize();
Str_8 GetClientId() const;
Str_8 GetSecret() const;
Str_8 GetRedURI() const;
bool IsVerificationForced() const;
Str_8 GetToken() const;
};
}

View File

@@ -0,0 +1,53 @@
#pragma once
#include "ehs/EHS.h"
#include "ehs/Str.h"
#include "ehs/io/socket/TCP.h"
namespace ehs
{
class TwitchChat
{
private:
TCP client;
Str_8 username;
Str_8 token;
Str_8 channel;
bool initialized;
public:
~TwitchChat();
TwitchChat();
TwitchChat(const Str_8& username);
TwitchChat(const Str_8& username, const Str_8& token);
TwitchChat(const TwitchChat& chat);
TwitchChat& operator=(const TwitchChat& chat);
void SetToken(const Str_8& newToken);
void Initialize();
void UnInitialize();
void JoinChannel(const Str_8& newChannel);
void LeaveChannel();
void SendPong();
void SendMsg(const Str_8& msg);
void WhisperMsg(const Str_8& user, const Str_8& msg);
Str_8 RecvMsg();
Str_8 GetUsername() const;
Str_8 GetChannel() const;
};
}