First commit.
This commit is contained in:
112
include/ehs/io/socket/BaseTCP.h
Normal file
112
include/ehs/io/socket/BaseTCP.h
Normal file
@@ -0,0 +1,112 @@
|
||||
#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);
|
||||
};
|
||||
}
|
54
include/ehs/io/socket/BaseUDP.h
Normal file
54
include/ehs/io/socket/BaseUDP.h
Normal file
@@ -0,0 +1,54 @@
|
||||
#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;
|
||||
|
||||
BaseUDP();
|
||||
|
||||
BaseUDP(AddrType type);
|
||||
|
||||
BaseUDP(BaseUDP&& udp) noexcept;
|
||||
|
||||
BaseUDP(const BaseUDP& udp);
|
||||
|
||||
BaseUDP& operator=(BaseUDP&& udp) noexcept;
|
||||
|
||||
BaseUDP& operator=(const BaseUDP& udp);
|
||||
|
||||
virtual void Release() = 0;
|
||||
|
||||
virtual void Bind(AddrType type, const Str_8& address, UInt_16 port) = 0;
|
||||
|
||||
virtual UInt_64 Send(AddrType type, const Str_8& address, UInt_16 port, const Byte* data, UInt_64 size) = 0;
|
||||
|
||||
virtual UInt_64 Receive(AddrType* type, Str_8* address, UInt_16* port, Byte* data, UInt_64 size) = 0;
|
||||
|
||||
bool IsBound() const;
|
||||
|
||||
virtual void SetBlocking(bool blocking) = 0;
|
||||
|
||||
virtual bool IsBlocking() const = 0;
|
||||
|
||||
AddrType GetLocalAddressType() const;
|
||||
|
||||
Str_8 GetLocalAddress() const;
|
||||
|
||||
UInt_16 GetLocalPort() const;
|
||||
|
||||
virtual bool IsValid() const = 0;
|
||||
};
|
||||
}
|
14
include/ehs/io/socket/DNS.h
Normal file
14
include/ehs/io/socket/DNS.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include "ehs/EHS.h"
|
||||
#include "ehs/Str.h"
|
||||
#include "Socket.h"
|
||||
|
||||
namespace ehs
|
||||
{
|
||||
class DNS
|
||||
{
|
||||
public:
|
||||
static Str_8 Resolve(const AddrType addrType, const Str_8& hostName);
|
||||
};
|
||||
}
|
164
include/ehs/io/socket/Request.h
Normal file
164
include/ehs/io/socket/Request.h
Normal 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);
|
||||
|
||||
};
|
||||
}
|
127
include/ehs/io/socket/Response.h
Normal file
127
include/ehs/io/socket/Response.h
Normal 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);
|
||||
|
||||
};
|
||||
}
|
56
include/ehs/io/socket/SSL.h
Normal file
56
include/ehs/io/socket/SSL.h
Normal 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();
|
||||
};
|
||||
}
|
51
include/ehs/io/socket/Socket.h
Normal file
51
include/ehs/io/socket/Socket.h
Normal 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
|
||||
}
|
7
include/ehs/io/socket/TCP.h
Normal file
7
include/ehs/io/socket/TCP.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef EHS_OS_WINDOWS
|
||||
#include "TCP_W32.h"
|
||||
#else
|
||||
#include "TCP_BSD.h"
|
||||
#endif
|
94
include/ehs/io/socket/TCP_BSD.h
Normal file
94
include/ehs/io/socket/TCP_BSD.h
Normal 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(const 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, unsigned short 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, const unsigned short 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* const buffer, const 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* const buffer, const UInt_32 size) override;
|
||||
|
||||
/// Sets whether or not receiving data blocks the next task.
|
||||
/// @param [in] blocking Whether or not to block.
|
||||
void SetBlocking(const 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, unsigned short port);
|
||||
|
||||
void Bind_v4(const Str_8& address, unsigned short port);
|
||||
|
||||
void Connect_v6(const Str_8& address, unsigned short port);
|
||||
|
||||
void Connect_v4(const Str_8& address, unsigned short port);
|
||||
};
|
||||
}
|
94
include/ehs/io/socket/TCP_W32.h
Normal file
94
include/ehs/io/socket/TCP_W32.h
Normal 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(const 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, unsigned short 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, const unsigned short 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* const buffer, const 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* const buffer, const UInt_32 size) override;
|
||||
|
||||
/// Sets whether or not receiving data blocks the next task.
|
||||
/// @param [in] blocking Whether or not to block.
|
||||
void SetBlocking(const 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, unsigned short port);
|
||||
|
||||
void Bind_v4(const Str_8& address, unsigned short port);
|
||||
|
||||
void Connect_v6(const Str_8& address, unsigned short port);
|
||||
|
||||
void Connect_v4(const Str_8& address, unsigned short port);
|
||||
};
|
||||
}
|
7
include/ehs/io/socket/UDP.h
Normal file
7
include/ehs/io/socket/UDP.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef EHS_OS_WINDOWS
|
||||
#include "UDP_W32.h"
|
||||
#else
|
||||
#include "UDP_BSD.h"
|
||||
#endif
|
82
include/ehs/io/socket/UDP_BSD.h
Normal file
82
include/ehs/io/socket/UDP_BSD.h
Normal 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);
|
||||
};
|
||||
}
|
82
include/ehs/io/socket/UDP_W32.h
Normal file
82
include/ehs/io/socket/UDP_W32.h
Normal 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(const Str_8& address, const 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(const Str_8& addr, const UInt_16 port, const Byte* const data, const 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(Str_8* const addr, UInt_16* const port, Byte* const data, const UInt_64 size) override;
|
||||
|
||||
/// Sets whether or not receiving data blocks the next task.
|
||||
/// @param [in] blocking Whether or not to block.
|
||||
void SetBlocking(const 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, const UInt_16 port);
|
||||
|
||||
void Bind_v4(const Str_8& address, const UInt_16 port);
|
||||
|
||||
UInt_64 Send_v6(const Str_8& addr, const UInt_16 port, const Byte* const data, const UInt_64 size);
|
||||
|
||||
UInt_64 Send_v4(const Str_8& addr, const UInt_16 port, const Byte* const data, const UInt_64 size);
|
||||
};
|
||||
}
|
113
include/ehs/io/socket/rest/Spotify.h
Normal file
113
include/ehs/io/socket/rest/Spotify.h
Normal 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();
|
||||
};
|
||||
}
|
39
include/ehs/io/socket/rest/Twitch.h
Normal file
39
include/ehs/io/socket/rest/Twitch.h
Normal 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;
|
||||
};
|
||||
}
|
53
include/ehs/io/socket/rest/TwitchChat.h
Normal file
53
include/ehs/io/socket/rest/TwitchChat.h
Normal 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;
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user