First commit.

This commit is contained in:
2023-12-17 03:29:08 -08:00
commit 09ced8e899
255 changed files with 45001 additions and 0 deletions

112
include/IO/Socket/BaseTCP.h Normal file
View File

@@ -0,0 +1,112 @@
#pragma once
#include "../../EHS.h"
#include "../../Str.h"
#include "Request.h"
#include "Response.h"
#include "Socket.h"
namespace lwe
{
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);
};
}

View File

@@ -0,0 +1,54 @@
#pragma once
#include "../../EHS.h"
#include "../../Str.h"
#include "Socket.h"
namespace lwe
{
class BaseUDP
{
protected:
AddrType addrType;
Str_8 address;
UInt_16 port;
bool bound;
public:
virtual ~BaseUDP() = default;
BaseUDP();
BaseUDP(const AddrType addrType);
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(const Str_8& address, const UInt_16 port) = 0;
virtual UInt_64 Send(const Str_8& addr, const UInt_16 port, const Byte* const data, const UInt_64 size) = 0;
virtual UInt_64 Receive(Str_8* const addr, UInt_16* const port, Byte* const data, const UInt_64 size) = 0;
bool IsBound() const;
virtual void SetBlocking(const bool blocking) = 0;
virtual bool IsBlocking() const = 0;
AddrType GetAddressType() const;
Str_8 GetLocalAddress() const;
UInt_16 GetLocalPort() const;
virtual bool IsValid() const = 0;
};
}

194
include/IO/Socket/Comms.h Normal file
View File

@@ -0,0 +1,194 @@
#pragma once
#include "../../EHS.h"
#include "../../Log.h"
#include "../../BaseObj.h"
#include "../../Serializer.h"
#include "../../Vector.h"
#include "../../Array.h"
#include "Socket.h"
#include "UDP.h"
#include "Utils.h"
namespace lwe
{
class CommsSystem;
class Endpoint;
class Comms : public BaseObj
{
private:
static const Version ver;
static const UInt_64 internalSys;
static const UInt_64 connectOp;
static const UInt_64 connectedOp;
static const UInt_64 rejectedOp;
static const UInt_64 disconnectOp;
static const UInt_64 disconnectedOp;
static const UInt_64 statusUpdateOp;
static const UInt_64 pingOp;
static const UInt_64 pongOp;
static const UInt_64 latencyOp;
static const UInt_64 receivedOp;
Socket hdl;
AddrType type;
Str_8 address;
UInt_16 port;
bool bound;
Version appVer;
EndDisp disposition;
bool dropPackets;
Str_8 id;
UInt_32 hashId;
Byte* buffer;
UInt_32 bufferSize;
Array<CommsSystem*> systems;
Vector<Endpoint*> endpoints;
UInt_32 maxEndpoints;
UInt_64 lastTSC;
float delta;
float maxTimeout;
float resendRate;
bool (*connectedCb)(Comms*, Endpoint*);
void (*activeCb)(Comms*, Endpoint*);
void (*disconnectedCb)(Comms*, Endpoint*);
public:
~Comms() override;
Comms();
Comms(const Version& ver, const EndDisp disposition, const Str_8& id, const UInt_64 maxEndpoints);
Comms(const Comms& sock);
Comms& operator=(const Comms& sock);
void Initialize();
void UnInitialize();
void Bind(const Str_8& newAddress, const UInt_16 newPort);
void Connect(const Str_8& address, const UInt_16 port);
bool Disconnect(const EndDisp disposition, const UInt_64 hashId, const Str_8& msg);
bool Disconnect(const EndDisp disposition, const Str_8& id, const Str_8& msg);
void Broadcast(const EndDisp disposition, const Status status, const bool deltaLocked, const bool encrypted,
const bool ensure, const UInt_64 sysHashId, const UInt_64 opHashId,
const Serializer<>& payload);
void Broadcast(const EndDisp disposition, const Status status, const bool deltaLocked, const bool encrypted,
const bool ensure, const Str_8& sysId, const Str_8& opId,
const Serializer<>& payload);
void Poll();
bool IsInitialized() const;
void SetAddressType(const AddrType newType);
AddrType GetAddressType() const;
Str_8 GetAddress() const;
UInt_16 GetPort() const;
bool IsBound() const;
Version GetVersion() const;
Version GetAppVersion() const;
EndDisp GetDisposition() const;
void EnableDropPackets(const bool enable);
bool IsDropPacketsEnabled() const;
Str_8 GetId() const;
UInt_64 GetHashId() const;
bool HasSystem(const UInt_64 hashId) const;
bool HasSystem(const Str_8& id) const;
bool AddSystem(CommsSystem* sys);
CommsSystem* GetSystem(const UInt_64 hashId);
CommsSystem* GetSystem(const Str_8& id);
bool HasEndpoint(const EndDisp disposition, const Status status, const UInt_64 hashId) const;
bool HasEndpoint(const EndDisp disposition, const Status status, const Str_8& id) const;
bool HasEndpoint(const EndDisp disposition, const UInt_64 hashId) const;
bool HasEndpoint(const EndDisp disposition, const Str_8& id) const;
bool HasEndpoint(const Str_8& address, const UInt_16 port) const;
Endpoint* GetEndpoint(const EndDisp disposition, const Status status, const UInt_64 hashId);
Endpoint* GetEndpoint(const EndDisp disposition, const Status status, const Str_8& id);
Endpoint* GetEndpoint(const EndDisp disposition, const UInt_64 hashId);
Endpoint* GetEndpoint(const EndDisp disposition, const Str_8& id);
Endpoint* GetEndpoint(const Str_8& address, const UInt_16 port);
Array<Endpoint*> GetEndpoints(const EndDisp disposition, const Status status);
Array<Endpoint*> GetEndpoints(const EndDisp disposition);
UInt_64 GetEndpointsCount(const EndDisp disposition, const Status status);
UInt_64 GetEndpointsCount(const EndDisp disposition);
UInt_64 GetMaxEndpoints() const;
void SetBlocking(const bool blocking);
bool IsBlocking() const;
void SetMaxTimeout(const float seconds);
float GetMaxTimeout() const;
void SetResendRate(const float seconds);
float GetResendRate() const;
void SetConnectedCb(bool (*newCb)(Comms*, Endpoint*));
void SetActiveCb(void (*newCb)(Comms*, Endpoint*));
void SetDisconnectedCb(void (*newCb)(Comms*, Endpoint*));
private:
void UpdateQueue(UInt_64 active);
void UpdateQueue();
bool RemoveEndpoint(const EndDisp disposition, const UInt_64 hashId);
bool RemoveEndpoint(const Str_8& address, const UInt_16 port);
bool RemoveEndpoint(const Endpoint* const end);
void PollEndpoints(Vector<Endpoint*>& endpoints);
void Bind_v6(const Str_8& address, const UInt_16 port);
void Bind_v4(const Str_8& address, const UInt_16 port);
UInt_16 Receive(Str_8* address, UInt_16* port, Byte* const data, const UInt_16 size);
};
}

View File

@@ -0,0 +1,42 @@
#pragma once
#include "../../EHS.h"
#include "../../Str.h"
#include "../../Array.h"
#include "../../Serializer.h"
namespace lwe
{
class Endpoint;
class Operation;
class Comms;
class CommsSystem
{
private:
Str_8 id;
UInt_64 hashId;
Array<Operation*> ops;
public:
~CommsSystem();
CommsSystem();
CommsSystem(const Str_8& id);
CommsSystem(const CommsSystem& sys);
CommsSystem& operator=(const CommsSystem& sys);
Str_8 GetId() const;
UInt_64 GetHashId() const;
bool HasOperation(const UInt_64 hashId);
bool AddOperation(Operation* op);
void Execute(Comms* comms, Endpoint* endpoint, const UInt_64 hashId, Serializer<>& payload);
};
}

14
include/IO/Socket/DNS.h Normal file
View File

@@ -0,0 +1,14 @@
#pragma once
#include "../../EHS.h"
#include "../../Str.h"
#include "Socket.h"
namespace lwe
{
class DNS
{
public:
static Str_8 Resolve(const AddrType addrType, const Str_8& hostName);
};
}

View File

@@ -0,0 +1,137 @@
#pragma once
#include "../../EHS.h"
#include "../../Str.h"
#include "../../BaseObj.h"
#include "../../Vector.h"
#include "../../Serializer.h"
#include "../../IO/Socket/Socket.h"
#include "Utils.h"
#include "Fragments.h"
namespace lwe
{
class Comms;
class Endpoint : public BaseObj
{
private:
Socket hdl;
EndDisp disposition;
Status status;
Architecture arch;
Str_8 id;
UInt_64 hashId;
UInt_64 nextSendId;
Vector<Insurance> sent;
UInt_64 nextRecvId;
Vector<Fragments> received;
Str_8 address;
UInt_16 port;
float deltaDuration;
float deltaRate;
float timeout;
float lastPing;
float oldLatency;
float latency;
UInt_64 queueSlot;
public:
Endpoint();
Endpoint(const Socket hdl, const EndDisp disposition, const Architecture arch, const Str_8& id,
const AddrType& type, const Str_8& address, const UInt_16 port);
Endpoint(const Socket hdl, const AddrType& type, const Str_8& address, const UInt_16 port);
Endpoint(const Endpoint& end);
Endpoint& operator=(const Endpoint& end);
void Poll(const float delta);
EndDisp GetDisposition() const;
void SetStatus(const Status newStatus);
Status GetStatus() const;
Architecture GetArchitecture() const;
Str_8 GetId() const;
UInt_64 GetHashId() const;
UInt_64 GetNextSendId() const;
/// Sends data to the remote endpoint.
/// @param [in] deltaLocked Whether or not to match the remote endpoint's delta time to prevent overloading the client. This will drop data if delta time does not match.
/// @param [in] encrypted Whether or not to encrypt this data before sending to the remote endpoint.
/// @param [in] ensure Whether or not to ensure the data was received by the remote endpoint.
/// @param [in] sys The system hash id to execute an operation from.
/// @param [in] op The operation hash id in the system to execute.
/// @param [in] payload Additional parameters and data to send to the remote endpoint.
void Send(const bool deltaLocked, const bool encrypted, const bool ensure, const UInt_64 sys,
const UInt_64 op, const Serializer<>& payload);
/// Sends data to the remote endpoint.
/// @param [in] deltaLocked Whether or not to match the remote endpoint's delta time to prevent overloading the client. This will drop data if delta time does not match.
/// @param [in] encrypted Whether or not to encrypt this data before sending to the remote endpoint.
/// @param [in] ensure Whether or not to ensure the data was received by the remote endpoint.
/// @param [in] sys The system string id to execute an operation from.
/// @param [in] op The operation string id in the system to execute.
/// @param [in] payload Additional parameters and data to send to the remote endpoint.
void Send(const bool deltaLocked, const bool encrypted, const bool ensure, const Str_8& sys,
const Str_8& op, const Serializer<>& payload);
void RemoveInsurance(const UInt_64 msgId, const UInt_64 fragment);
UInt_64 GetNextRecvId() const;
void AddReceived(const Header& header, const Serializer<>& payload);
Vector<Fragments> GetReceived() const;
Vector<Fragments>* GetReceived();
Str_8 GetAddress() const;
UInt_16 GetPort() const;
void SetDeltaRate(const float newDeltaRate);
float GetDeltaRate() const;
float GetTimeout() const;
float GetLastPing() const;
void Ping(const float delta);
void Pong(const float delta);
void SendLatency();
void SetLatency(const float newLatency);
float GetLatency() const;
void SetQueueSlot(const UInt_64 slot);
UInt_64 GetQueueSlot() const;
private:
Fragments FragmentData(const Header& header, const Serializer<>& data);
void Send(const Header& header, const Serializer<>& payload);
bool SortingNeeded() const;
void SortReceived();
UInt_16 Send_v6(const Serializer<>& payload);
UInt_16 Send_v4(const Serializer<>& payload);
};
}

View File

@@ -0,0 +1,42 @@
#pragma once
#include "../../EHS.h"
#include "../../Serializer.h"
#include "Utils.h"
namespace lwe
{
class Fragments
{
private:
Header header;
Serializer<>* data;
UInt_64 size;
public:
~Fragments();
Fragments();
Fragments(const Header& header, const Serializer<>& payload);
Fragments(const Header& header, const UInt_64 size);
Fragments(const Fragments& frags);
Fragments& operator=(const Fragments& frags);
operator const Serializer<>* () const;
operator Serializer<>* ();
Header GetHeader() const;
UInt_64 Size() const;
bool IsComplete() const;
Packet Combine() const;
};
}

View File

@@ -0,0 +1,36 @@
#pragma once
#include "../../EHS.h"
#include "../../Str.h"
#include "../../Serializer.h"
namespace lwe
{
class CommsSystem;
class Comms;
class Endpoint;
class Operation
{
private:
Str_8 id;
UInt_64 hashId;
public:
virtual ~Operation() = default;
Operation();
Operation(const Str_8& id);
Operation(const Operation& cmd);
Operation& operator=(const Operation& cmd);
virtual void Process(Comms* comms, Endpoint* endpoint, CommsSystem* sys, Serializer<>& payload);
Str_8 GetId() const;
UInt_64 GetHashId() const;
};
}

164
include/IO/Socket/Request.h Normal file
View File

@@ -0,0 +1,164 @@
#pragma once
#include "../../EHS.h"
#include "../../Vector.h"
#include "../../Str.h"
#include "../../Json/Json.h"
#include "Socket.h"
namespace lwe
{
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.h"
#include "../../Vector.h"
#include "../../Str.h"
#include "../../Json/Json.h"
#include "Socket.h"
namespace lwe
{
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,113 @@
#pragma once
#include "../../../EHS.h"
#include "../../../Str.h"
#include "../../../Array.h"
#include "../SSL.h"
namespace lwe
{
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.h"
#include "../../../Str.h"
#include "../SSL.h"
namespace lwe
{
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.h"
#include "../../../Str.h"
#include "../TCP.h"
namespace lwe
{
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;
};
}

56
include/IO/Socket/SSL.h Normal file
View File

@@ -0,0 +1,56 @@
#pragma once
#include "../../EHS.h"
#include "../../Str.h"
#include "TCP.h"
#include "Request.h"
#include "Response.h"
typedef struct ssl_ctx_st SSL_CTX;
typedef struct ssl_st SSL;
namespace lwe
{
/// 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 LWE_IPV4_HEADER
#define LWE_IPV4_HEADER 60
#endif
#ifndef LWE_IPV6_HEADER
#define LWE_IPV6_HEADER 40
#endif
#ifndef LWE_UDP_HEADER
#define LWE_UDP_HEADER 8
#endif
#ifndef LWE_IPV4_UDP_PAYLOAD
#define LWE_IPV4_UDP_PAYLOAD (LWE_UINT_16_MAX - LWE_IPV4_HEADER - LWE_UDP_HEADER)
#endif
#ifndef LWE_IPV6_UDP_PAYLOAD
#define LWE_IPV6_UDP_PAYLOAD (LWE_UINT_16_MAX - LWE_IPV6_HEADER - LWE_UDP_HEADER)
#endif
namespace lwe
{
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(LWE_OS_WINDOWS)
typedef UInt_64 Socket;
#define LWE_INVALID_SOCKET LWE_UINT_64_MAX
#elif defined(LWE_OS_LINUX)
typedef SInt_32 Socket;
#define LWE_INVALID_SOCKET (SInt_32)0xffffffff
#endif
}

7
include/IO/Socket/TCP.h Normal file
View File

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

View File

@@ -0,0 +1,94 @@
#pragma once
#include "../../EHS.h"
#include "../../Str.h"
#include "../../Log.h"
#include "Socket.h"
#include "BaseTCP.h"
namespace lwe
{
/// 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);
};
}

View File

@@ -0,0 +1,94 @@
#pragma once
#include "../../EHS.h"
#include "../../Str.h"
#include "../../Log.h"
#include "Socket.h"
#include "BaseTCP.h"
namespace lwe
{
/// 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/IO/Socket/UDP.h Normal file
View File

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

View File

@@ -0,0 +1,82 @@
#pragma once
#include "../../EHS.h"
#include "../../Str.h"
#include "BaseUDP.h"
namespace lwe
{
/// 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);
};
}

View File

@@ -0,0 +1,82 @@
#pragma once
#include "../../EHS.h"
#include "../../Str.h"
#include "BaseUDP.h"
namespace lwe
{
/// 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);
};
}

56
include/IO/Socket/Utils.h Normal file
View File

@@ -0,0 +1,56 @@
#pragma once
#include "../../EHS.h"
#include "../../Serializer.h"
namespace lwe
{
enum class EndDisp : UInt_8
{
UNKNOWN,
SERVICE,
ENDPOINT
};
enum class Status : UInt_8
{
ACTIVE,
PENDING,
IN_LOCAL_QUEUE,
IN_REMOTE_QUEUE,
};
struct Header
{
bool encrypted = true;
UInt_64 id = 0;
UInt_64 fragments = 0;
UInt_64 fragment = 0;
bool ensure = false;
EndDisp disposition = EndDisp::UNKNOWN;
UInt_64 endpointId = 0;
UInt_64 system = 0;
UInt_64 op = 0;
};
struct Packet
{
Header header;
Serializer<> payload;
};
struct Insurance
{
Header header;
Serializer<> payload;
float lastResend;
};
}
#ifndef COMMS_IPV4_PAYLOAD
#define COMMS_IPV4_PAYLOAD (LWE_IPV4_UDP_PAYLOAD - (UInt_16)sizeof(Header))
#endif
#ifndef COMMS_IPV6_PAYLOAD
#define COMMS_IPV6_PAYLOAD (LWE_IPV6_UDP_PAYLOAD - (UInt_16)sizeof(Header))
#endif