Renamed HLAPI to VoidNet_HL and VoidNet to VoidNet_LL

This commit is contained in:
TheDoctor
2019-10-15 19:16:52 +01:00
parent 0b48708667
commit b671953f40
52 changed files with 0 additions and 0 deletions

View File

@ -0,0 +1,100 @@
// https://github.com/mfichman/http
#pragma once
#include <map>
namespace std::net
{
class Cookie
{
public:
Cookie(const std::string& text);
Cookie() : m_httpOnly(false), m_secure(false)
{
}
const std::string& GetName() const
{
return m_name;
}
const std::string& GetValue() const
{
return m_value;
}
const std::string& GetPath() const
{
return m_path;
}
bool IsHttpOnly() const
{
return m_httpOnly;
}
bool IsSecure() const
{
return m_secure;
}
void SetName(const std::string& name)
{
m_name = name;
}
void SetValue(const std::string& value)
{
m_value = value;
}
void SetPath(const std::string& path)
{
m_path = path;
}
void SetHttpOnly(bool httpOnly)
{
m_httpOnly = httpOnly;
}
void SetSecure(bool secure)
{
m_secure = secure;
}
private:
std::string m_name;
std::string m_value;
std::string m_path;
bool m_httpOnly;
bool m_secure;
};
class Cookies
{
public:
const Cookie operator[](const std::string &name) const;
std::map<std::string, Cookie>::const_iterator begin() const
{
return m_cookie.begin();
}
std::map<std::string, Cookie>::const_iterator end() const
{
return m_cookie.end();
}
void SetCookie(Cookie const& cookie);
static const std::string HOST;
static const std::string CONTENT_LENGTH;
static const std::string ACCEPT_ENCODING;
static const std::string CONNECTION;
private:
std::map<std::string, Cookie> m_cookie;
};
}

View File

@ -0,0 +1,171 @@
#pragma once
#undef DELETE
namespace std::net
{
enum class SocketParam
{
CanRead,
CanWrite,
HasError,
};
enum class SocketReturn
{
Yes,
No,
EncounteredError,
};
enum class SocketErrors
{
SE_SOCKET_ERROR = -1,
SE_NO_ERROR,
SE_EINTR,
SE_EBADF,
SE_EACCES,
SE_EFAULT,
SE_EINVAL,
SE_EMFILE,
SE_EWOULDBLOCK,
SE_EINPROGRESS,
SE_EALREADY,
SE_ENOTSOCK,
SE_EDESTADDRREQ,
SE_EMSGSIZE,
SE_EPROTOTYPE,
SE_ENOPROTOOPT,
SE_EPROTONOSUPPORT,
SE_ESOCKTNOSUPPORT,
SE_EOPNOTSUPP,
SE_EPFNOSUPPORT,
SE_EAFNOSUPPORT,
SE_EADDRINUSE,
SE_EADDRNOTAVAIL,
SE_ENETDOWN,
SE_ENETUNREACH,
SE_ENETRESET,
SE_ECONNABORTED,
SE_ECONNRESET,
SE_ENOBUFS,
SE_EISCONN,
SE_ENOTCONN,
SE_ESHUTDOWN,
SE_ETOOMANYREFS,
SE_ETIMEDOUT,
SE_ECONNREFUSED,
SE_ELOOP,
SE_ENAMETOOLONG,
SE_EHOSTDOWN,
SE_EHOSTUNREACH,
SE_ENOTEMPTY,
SE_EPROCLIM,
SE_EUSERS,
SE_EDQUOT,
SE_ESTALE,
SE_EREMOTE,
SE_EDISCON,
SE_SYSNOTREADY,
SE_VERNOTSUPPORTED,
SE_NOTINITIALISED,
SE_HOST_NOT_FOUND,
SE_TRY_AGAIN,
SE_NO_RECOVERY,
SE_NO_DATA,
SE_UDP_ERR_PORT_UNREACH,
SE_ADDRFAMILY,
SE_SYSTEM,
SE_NODEV,
SE_GET_LAST_ERROR_CODE,
};
enum class SocketType
{
Unknown = -1,
Datagram = 2,
Streaming = 1,
};
enum class SocketProtocol
{
IPv4 = 2, // AF_INET
IPv6 = 23 // AF_INET6
};
enum class SocketReceiveFlags
{
None = 0,
Peek = 2,
WaitAll = 0x100,
};
enum class SocketWaitConditions
{
WaitForRead,
WaitForWrite,
WaitForReadOrWrite,
};
enum class SocketConnectionState
{
NotConnected,
Connected,
ConnectionError,
};
enum class HttpStatus
{
INVALID_CODE = 0,
CONTINUE = 100,
SWITCHING_PROTOCOLS = 101,
OK = 200,
CREATED = 201,
ACCEPTED = 202,
NON_AUTHORITATIVE_INFO = 203,
NO_CONTENT = 204,
RESET_CONTENT = 205,
PARTIAL_CONTENT = 206,
MULTIPLE_CHOICES = 300,
MOVED_PERMANENTLY = 301,
FOUND = 302,
SEE_OTHER = 303,
NOT_MODIFIED = 304,
USE_PROXY = 305,
TEMPORARY_REDIRECT = 307,
BAD_REQUEST = 400,
UNAUTHORIZED = 401,
PAYMENT_REQUIRED = 402,
FORBIDDEN = 403,
NOT_FOUND = 404,
METHOD_NOT_ALLOWED = 405,
NOT_ACCEPTABLE = 406,
PROXY_AUTHENTICATION_REQUIRED = 407,
REQUEST_TIMEOUT = 408,
CONFLICT = 409,
GONE = 410,
LENGTH_REQUIRED = 411,
PRECONDITION_FAILED = 412,
REQUEST_ENTITY_TOO_LARGE = 413,
UNSUPPORTED_MEDIA_TYPE = 415,
REQUESTED_RANGE_NOT_SATISFIABLE = 416,
EXPECTATION_FAILED = 417,
INTERNAL_SERVER_ERROR = 500,
NOT_IMPLEMENTED = 501,
BAD_GATEWAY = 502,
SERVICE_UNAVAILABLE = 503,
GATEWAY_TIMEOUT = 504,
VERSION_NOT_SUPPORTED = 505,
};
enum class Method
{
GET,
HEAD,
POST,
PUT,
DELETE,
TRACE,
CONNECT,
};
}

View File

@ -0,0 +1,33 @@
// https://github.com/mfichman/http
#pragma once
#include <map>
namespace std::net
{
class Headers
{
public:
const std::string operator[](const std::string &name) const;
std::multimap<std::string, std::string>::const_iterator begin() const
{
return m_header.begin();
}
std::multimap<std::string, std::string>::const_iterator end() const
{
return m_header.end();
}
void AddHeader(std::string const& name, std::string const& value);
static std::string const HOST;
static std::string const CONTENT_LENGTH;
static std::string const ACCEPT_ENCODING;
static std::string const CONNECTION;
private:
std::multimap<std::string, std::string> m_header;
};
}

View File

@ -0,0 +1,20 @@
// https://github.com/mfichman/http
#pragma once
#include "VoidNet/Response.hpp"
#include "VoidNet/Request.hpp"
namespace std::net
{
class Http
{
public:
static Response Get(std::string const& path, std::string const& data = "");
static Response Post(std::string const& path, std::string const& data = "");
private:
static Response Send(Request const& request);
static std::string Str(Request const& request);
};
}

View File

@ -0,0 +1,108 @@
#pragma once
#include <chrono>
#include <istream>
#include <ostream>
#include <string>
#include "VoidNet/Net.hpp"
namespace std::net
{
class IPAddress
{
public:
inline IPAddress(const IPAddress &addr, uint16_t port)
: m_address(addr.ToInteger())
, m_valid(true)
, m_port(port)
{
}
inline IPAddress()
: m_address(0)
, m_valid(false)
, m_port(DEFAULT_SERVER_PORT)
{
}
inline IPAddress(const std::string& address, uint16_t port = DEFAULT_SERVER_PORT)
: m_address(0)
, m_valid(false)
, m_port(port)
{
Resolve(address);
}
inline IPAddress(const char* address, uint16_t port = DEFAULT_SERVER_PORT)
: m_address(0)
, m_valid(false)
, m_port(port)
{
Resolve(address);
}
inline IPAddress(uint8_t byte0, uint8_t byte1, uint8_t byte2, uint8_t byte3, uint16_t port = DEFAULT_SERVER_PORT)
: m_address(htonl((byte0 << 24) | (byte1 << 16) | (byte2 << 8) | byte3))
, m_valid(true)
, m_port(port)
{
}
inline explicit IPAddress(uint32_t address, uint16_t port = DEFAULT_SERVER_PORT)
: m_address(htonl(address))
, m_valid(true)
, m_port(port)
{
}
std::string ToString() const;
inline uint32_t ToInteger() const { return ntohl(m_address); }
inline uint16_t GetPort() const { return m_port; }
static const IPAddress None;
static const IPAddress Any;
static const IPAddress LocalHost;
static const IPAddress Broadcast;
inline sockaddr_in ToCAddr() const
{
sockaddr_in addr;
std::memset(&addr, 0, sizeof(addr));
addr.sin_addr.s_addr = htonl(ToInteger());
addr.sin_family = AF_INET;
addr.sin_port = htons(GetPort());
return addr;
}
private:
friend bool operator <(const IPAddress& left, const IPAddress& right);
void Resolve(const std::string& address);
private:
uint32_t m_address;
bool m_valid;
uint16_t m_port;
};
inline bool operator ==(const IPAddress& left, const IPAddress& right) { return !(left < right) && !(right < left); }
inline bool operator !=(const IPAddress& left, const IPAddress& right) { return !(left == right); }
inline bool operator <(const IPAddress& left, const IPAddress& right) { return std::make_pair(left.m_valid, left.m_address) < std::make_pair(right.m_valid, right.m_address); }
inline bool operator >(const IPAddress& left, const IPAddress& right) { return right < left; }
inline bool operator <=(const IPAddress& left, const IPAddress& right) { return !(right < left); }
inline bool operator >=(const IPAddress& left, const IPAddress& right) { return !(left < right); }
inline std::istream& operator >>(std::istream& stream, IPAddress& address)
{
std::string str;
stream >> str;
address = IPAddress(str);
return stream;
}
inline std::ostream& operator <<(std::ostream& stream, const IPAddress& address) { return stream << address.ToString(); }
}

View File

@ -0,0 +1,74 @@
#pragma once
#include <string>
#include <chrono>
#include <memory>
#include "VoidNet/Net.hpp"
#include "VoidNet/IPAddress.hpp"
#include "VoidNet/Enums.hpp"
namespace std::net
{
class Socket;
class ISocket
{
public:
inline ISocket()
: m_socketType(SocketType::Unknown)
, m_protocol(SocketProtocol::IPv4)
{
}
inline ISocket(SocketType InSocketType, SocketProtocol protocol = SocketProtocol::IPv4)
: m_socketType(InSocketType)
, m_protocol(protocol)
{
}
inline virtual ~ISocket()
{
}
virtual bool Close() = 0;
virtual bool Bind(const IPAddress &addr) = 0;
virtual bool Connect(const IPAddress& addr) = 0;
virtual bool Listen() = 0;
virtual bool WaitForPendingConnection(bool& hasPendingConnection, std::chrono::milliseconds t) = 0;
virtual bool HasPendingData(uint32_t& pendingDataSize) = 0;
virtual std::unique_ptr<Socket> Accept() = 0;
virtual bool SendTo(const uint8_t* data, int32_t count, int32_t& sent, const IPAddress& addrDest) = 0;
virtual bool Send(const uint8_t* data, int32_t count, int32_t& sent) = 0;
virtual bool RecvFrom(uint8_t* data, int32_t size, int32_t& read, IPAddress& srcAddr, SocketReceiveFlags flags = SocketReceiveFlags::None) = 0;
virtual bool Recv(uint8_t* data, int32_t size, int32_t& read, SocketReceiveFlags flags = SocketReceiveFlags::None) = 0;
virtual bool Wait(SocketWaitConditions cond, std::chrono::milliseconds t) = 0;
virtual SocketConnectionState GetConnectionState() = 0;
virtual void GetAddress(IPAddress& outAddr) = 0;
virtual bool GetPeerAddress(IPAddress& outAddr) = 0;
virtual bool SetNonBlocking(bool isNonBlocking = true) = 0;
virtual bool JoinMulticastGroup(const IPAddress& addrStr) = 0;
virtual bool LeaveMulticastGroup(const IPAddress& addrStr) = 0;
virtual bool SetMulticastLoopback(bool loopback) = 0;
virtual bool SetMulticastTtl(uint8_t timeToLive) = 0;
virtual bool SetReuseAddr(bool allowReuse = true) = 0;
virtual bool SetLinger(bool shouldLinger = true, int32_t t = 0) = 0;
virtual bool SetSendBufferSize(int32_t size, int32_t& newSize) = 0;
virtual bool SetReceiveBufferSize(int32_t size, int32_t& newSize) = 0;
virtual uint32_t GetPort() = 0;
inline SocketType GetSocketType() const
{
return m_socketType;
}
inline SocketProtocol GetSocketProtocol() const
{
return m_protocol;
}
private:
const SocketType m_socketType;
const SocketProtocol m_protocol;
};
}

View File

@ -0,0 +1,40 @@
#pragma once
#ifdef _MSC_VER
#include <winsock2.h>
#include <ws2tcpip.h>
namespace std::net
{
namespace priv
{
static WSADATA WsaData;
static bool Initialized;
}
inline static bool Initialize()
{
if (priv::Initialized) return true;
return (priv::Initialized = WSAStartup(MAKEWORD(2, 2), &priv::WsaData)) == 0;
}
inline static void Cleanup()
{
if (priv::Initialized)
{
WSACleanup();
priv::Initialized = false;
}
}
}
#else
namespace std::net
{
inline static bool Initialize() { return true; }
inline static void Cleanup() { }
}
#endif

View File

@ -0,0 +1,38 @@
#pragma once
#include <stdint.h>
#ifdef _MSC_VER
#include <winsock2.h>
#include <ws2tcpip.h>
#undef SendMessage
#undef GetMessage
#undef SetPort
#undef GetPort
#undef max
#undef min
#define poll WSAPoll
#define ioctl ioctlsocket
#else
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/ioctl.h>
#define SOCKET_ERROR -1
#define NO_ERROR 0
#define INVALID_SOCKET NO_ERROR
#define SOCKET int
#define closesocket close
#endif
#define DEFAULT_SERVER_PORT 61250

View File

@ -0,0 +1,67 @@
// https://github.com/mfichman/http
#pragma once
#include <string>
namespace std::net
{
template <typename T>
class ParseResult
{
public:
T value;
char const* ch;
};
template <typename F>
static ParseResult<std::string> ParseUntil(char const* str, F func)
{
ParseResult<std::string> result{};
char const* ch = str;
for (; *ch && !func(*ch); ++ch)
{
}
result.value = std::string(str, ch - str);
result.ch = ch;
return result;
}
template <typename F>
static inline ParseResult<std::string> ParseWhile(char const* str, F func)
{
ParseResult<std::string> result{};
char const* ch = str;
for (; *ch && func(*ch); ++ch)
{
}
result.value = std::string(str, ch - str);
result.ch = ch;
return result;
}
static inline ParseResult<std::string> ParseToken(char const* str)
{
auto token = ParseUntil(str, isspace);
token.ch = ParseWhile(token.ch, isspace).ch;
return token;
}
static inline ParseResult<std::string> parseCrLf(char const* str)
{
auto cr = ParseUntil(str, [](char ch) { return ch == '\r'; });
if (*cr.ch == '\r')
cr.ch++;
return ParseWhile(cr.ch, [](char ch)
{
return isspace(ch) && ch != '\r';
});
}
static inline ParseResult<std::string> ParseWhitespace(char const* str)
{
return ParseWhile(str, isspace);
}
}

View File

@ -0,0 +1,52 @@
// https://github.com/mfichman/http
#pragma once
#include "VoidNet/Uri.hpp"
#include "VoidNet/Headers.hpp"
#include "VoidNet/Enums.hpp"
namespace std::net
{
class Request
{
public:
Method GetMethod() const
{
return m_method;
}
const Uri& GetUri() const
{
return m_uri;
}
const std::string& GetPath() const
{
return m_uri.GetPath();
}
const std::string& GetData() const
{
return m_data;
}
const std::string GetHeaderElement(const std::string& name) const;
const Headers& GetHeaders() const
{
return m_headers;
}
void SetMethod(Method method);
void SetUri(const Uri& path);
void SetData(const std::string& data);
void AddHeader(const std::string& name, const std::string& value);
private:
Method m_method = Method::GET;
Uri m_uri;
std::string m_data;
Headers m_headers;
};
}

View File

@ -0,0 +1,43 @@
// https://github.com/mfichman/http
#pragma once
#include "VoidNet/Headers.hpp"
#include "VoidNet/Cookies.hpp"
#include "VoidNet/Enums.hpp"
#include <string>
namespace std::net
{
class Response
{
public:
Response(const std::string& text);
Response() {};
HttpStatus GetStatus() const
{
return m_status;
}
const std::string& GetData() const
{
return m_data;
}
const std::string GetHeader(const std::string& name) const;
const Cookie GetCookie(const std::string& name) const;
void SetStatus(HttpStatus status);
void SetData(const std::string& data);
void SetHeader(const std::string& name, const std::string& value);
void SetCookie(const Cookie& cookie);
private:
HttpStatus m_status = HttpStatus::INVALID_CODE;
std::string m_data;
Headers m_headers;
Cookies m_cookies;
};
}

View File

@ -0,0 +1,43 @@
#pragma once
#include "VoidNet/Socket.hpp"
//#include <openssl/rand.h>
//#include <openssl/ssl.h>
//#include <openssl/err.h>
namespace std::net
{
class SecureSocket
{
public:
SecureSocket();
bool Connect(const IPAddress& addr);
bool Close() const;
bool HasPendingData(uint32_t& pendingDataSize) const;
bool Send(uint8_t* data, int32_t count, int32_t &sent, int flags = 0); // Execute 1 write() syscall
bool Recv(uint8_t* data, int32_t count, int32_t &read, int flags = 0); // Execte 1 read() syscall
bool Wait(SocketWaitConditions cond, std::chrono::milliseconds t) const;
SocketConnectionState GetConnectionState() const;
void GetAddress(IPAddress& outAddr) const;
int32_t GetPort() const;
void UseCertificateFile(std::string const& path);
void UsePrivateKeyFile(std::string const& path);
private:
bool SendRaw(uint8_t* buf, size_t len, int flags = 0);
bool SendFromBio(int flags = 0);
bool RecvToBio(int flags = 0);
void HandleReturn(size_t ret);
std::unique_ptr<Socket> m_socket;
//SSL_CTX* m_context;
//SSL* m_conn;
//BIO* m_in;
//BIO* m_out;
bool m_eof;
};
}

View File

@ -0,0 +1,84 @@
#pragma once
#include "VoidNet/ISocket.hpp"
#include "VoidNet/Enums.hpp"
namespace std::net
{
class Socket : public ISocket
{
public:
inline Socket(SocketType socketType, SocketProtocol protocol = SocketProtocol::IPv4)
: ISocket(socketType, protocol)
{
init();
}
inline Socket(SOCKET newSocket, SocketType socketType = SocketType::Streaming, SocketProtocol protocol = SocketProtocol::IPv4)
: ISocket(socketType, protocol)
, m_socket(newSocket)
{
init();
}
virtual ~Socket() { Close(); }
public:
// ISocket overrides
virtual bool Close() override;
virtual bool Bind(const IPAddress &addr) override;
virtual bool Connect(const IPAddress& addr) override;
inline virtual bool Listen() override { return listen(m_socket, SOMAXCONN) == 0; }
virtual bool WaitForPendingConnection(bool& hasPendingConnection, std::chrono::milliseconds t) override;
virtual bool HasPendingData(uint32_t& pendingDataSize) override;
virtual std::unique_ptr<Socket> Accept() override;
virtual bool SendTo(const uint8_t* data, int32_t count, int32_t& sent, const IPAddress& addrDest) override;
virtual bool Send(const uint8_t* data, int32_t count, int32_t& sent) override;
virtual bool RecvFrom(uint8_t* data, int32_t size, int32_t& read, IPAddress& srcAddr, SocketReceiveFlags flags = SocketReceiveFlags::None) override;
virtual bool Recv(uint8_t* data, int32_t size, int32_t& read, SocketReceiveFlags flags = SocketReceiveFlags::None) override;
virtual bool Wait(SocketWaitConditions cond, std::chrono::milliseconds t) override;
virtual SocketConnectionState GetConnectionState() override;
virtual void GetAddress(IPAddress& outAddr) override;
virtual bool GetPeerAddress(IPAddress& outAddr) override;
virtual bool SetNonBlocking(bool isNonBlocking = true) override;
virtual bool JoinMulticastGroup(const IPAddress& addrStr) override;
virtual bool LeaveMulticastGroup(const IPAddress& addrStr) override;
inline virtual bool SetMulticastLoopback(bool loopback) override { return (setsockopt(m_socket, IPPROTO_IP, IP_MULTICAST_LOOP, (char*)&loopback, sizeof(loopback)) == 0); }
inline virtual bool SetMulticastTtl(uint8_t timeToLive) override { return (setsockopt(m_socket, IPPROTO_IP, IP_MULTICAST_TTL, (char*)&timeToLive, sizeof(timeToLive)) == 0); }
inline virtual bool SetReuseAddr(bool allowReuse = true) override
{
int param = allowReuse ? 1 : 0;
return setsockopt(m_socket, SOL_SOCKET, SO_REUSEADDR, (char*)&param, sizeof(param)) == 0;
}
virtual bool SetLinger(bool shouldLinger = true, int32_t t = 0) override;
virtual bool SetSendBufferSize(int32_t size, int32_t& newSize) override;
virtual bool SetReceiveBufferSize(int32_t size, int32_t& newSize) override;
virtual uint32_t GetPort() override;
SOCKET GetNativeSocket()
{
return m_socket;
}
private:
void init();
virtual SocketReturn HasState(SocketParam state, std::chrono::milliseconds t = std::chrono::milliseconds(0));
virtual SocketErrors TranslateErrorCode(int32_t code);
virtual int TranslateFlags(SocketReceiveFlags flags);
inline void UpdateActivity()
{
m_lastActivityTime = std::chrono::system_clock::now().time_since_epoch().count();
}
private:
SOCKET m_socket = INVALID_SOCKET;
long long m_lastActivityTime = 0;
};
}

View File

@ -0,0 +1,33 @@
#pragma once
#include "VoidNet/Socket.hpp"
namespace std::net
{
class TcpConnectionHandler;
}
namespace std::net
{
class TcpClient
{
friend class std::net::TcpConnectionHandler;
public:
TcpClient(Socket *soc);
TcpClient(SocketProtocol protocol = SocketProtocol::IPv4);
bool Connect(const IPAddress& addrStr);
bool Close() const;
bool HasPendingData(uint32_t& pendingDataSize) const;
bool Send(const uint8_t* data, int32_t count, int32_t& sent) const;
bool Recv(uint8_t* data, int32_t size, int32_t& read, SocketReceiveFlags flags = SocketReceiveFlags::None) const;
bool Wait(SocketWaitConditions cond, std::chrono::milliseconds t) const;
SocketConnectionState GetConnectionState() const;
void GetAddress(IPAddress& outAddr) const;
int32_t GetPort() const;
private:
std::unique_ptr<Socket> m_socket;
};
}

View File

@ -0,0 +1,32 @@
#pragma once
#include <thread>
#include <queue>
#include "VoidNet/Socket.hpp"
namespace std::net
{
class TcpConnectionHandler;
}
namespace std::net
{
class TcpClient;
class TcpListener
{
friend class std::net::TcpConnectionHandler;
public:
TcpListener(uint16_t port, std::chrono::milliseconds inSleepTime = std::chrono::milliseconds(0));
TcpListener(Socket *InSocket, std::chrono::milliseconds inSleepTime = std::chrono::milliseconds(0));
TcpClient *AcceptClient();
private:
std::chrono::milliseconds m_sleepTime;
std::unique_ptr<Socket> m_socket;
uint16_t m_port = 0;
};
}

View File

@ -0,0 +1,112 @@
#pragma once
#include "VoidNet/Enums.hpp"
#include "VoidNet/IPAddress.hpp"
namespace std::net
{
class Socket;
class TcpClient;
class TcpListener;
class TcpSocketBuilder
{
public:
inline TcpSocketBuilder()
: m_blocking(false)
, m_bound(false)
, m_boundAddr(IPAddress::Any, 0)
, m_linger(false)
, m_lingerTimeout(0)
, m_listen(false)
, m_receiveBufferSize(0)
, m_reusable(false)
, m_sendBufferSize(0)
, m_socketProtocol(SocketProtocol::IPv4)
{
}
inline TcpSocketBuilder AsBlocking()
{
m_blocking = true;
return *this;
}
inline TcpSocketBuilder AsNonBlocking()
{
m_blocking = false;
return *this;
}
inline TcpSocketBuilder AsReusable()
{
m_reusable = true;
return *this;
}
inline TcpSocketBuilder Bind(const IPAddress &addr)
{
m_boundAddr = addr;
m_bound = true;
return *this;
}
inline TcpSocketBuilder Lingering(int32_t Timeout)
{
m_linger = true;
m_lingerTimeout = Timeout;
return *this;
}
inline TcpSocketBuilder Listening()
{
m_listen = true;
return *this;
}
inline TcpSocketBuilder WithReceiveBufferSize(int32_t SizeInBytes)
{
m_receiveBufferSize = SizeInBytes;
return *this;
}
inline TcpSocketBuilder WithSendBufferSize(int32_t SizeInBytes)
{
m_sendBufferSize = SizeInBytes;
return *this;
}
inline TcpSocketBuilder Protocol(SocketProtocol prot)
{
m_socketProtocol = prot;
return *this;
}
public:
std::unique_ptr<Socket> Build() const;
std::unique_ptr<TcpClient> BuildClient() const;
std::unique_ptr<TcpListener> BuildListener() const;
private:
bool m_blocking;
bool m_bound;
IPAddress m_boundAddr;
bool m_linger;
int32_t m_lingerTimeout;
bool m_listen;
int32_t m_receiveBufferSize;
bool m_reusable;
int32_t m_sendBufferSize;
SocketProtocol m_socketProtocol;
};
}

View File

@ -0,0 +1,29 @@
#pragma once
#include "VoidNet/Socket.hpp"
#include <memory>
namespace std::net
{
class UdpSocket
{
public:
UdpSocket(Socket *soc);
UdpSocket(SocketProtocol protocol = SocketProtocol::IPv4);
bool Bind(const IPAddress &addr);
bool SendTo(const uint8_t* data, int32_t count, int32_t& sent, const IPAddress& addrDest);
bool RecvFrom(uint8_t* data, int32_t size, int32_t& read, IPAddress& srcAddr, SocketReceiveFlags flags = SocketReceiveFlags::None);
bool GetPeerAddress(IPAddress& outAddr);
bool JoinMulticastGroup(const IPAddress& addrStr);
bool LeaveMulticastGroup(const IPAddress& addrStr);
bool SetMulticastLoopback(bool loopback);
bool SetMulticastTtl(uint8_t timeToLive);
uint32_t GetPort();
bool SetReuseAddr(bool allowReuse = true);
private:
std::unique_ptr<Socket> m_socket;
};
}

View File

@ -0,0 +1,152 @@
#pragma once
#include "VoidNet/UdpSocket.hpp"
#include "VoidNet/IPAddress.hpp"
#include <vector>
namespace std::net
{
class UdpSocketBuilder
{
public:
UdpSocketBuilder()
: m_blocking(false)
, m_bound(false)
, m_boundEndpoint(IPAddress::Any, 0)
, m_multicastLoopback(false)
, m_multicastTtl(1)
, m_receiveBufferSize(0)
, m_reusable(false)
, m_sendBufferSize(0)
{ }
public:
UdpSocketBuilder AsBlocking()
{
m_blocking = true;
return *this;
}
UdpSocketBuilder AsNonBlocking()
{
m_blocking = false;
return *this;
}
UdpSocketBuilder AsReusable()
{
m_reusable = true;
return *this;
}
UdpSocketBuilder BoundToAddress(const IPAddress& addr)
{
m_boundEndpoint = IPAddress(addr);
m_bound = true;
return *this;
}
UdpSocketBuilder BoundToPort(uint16_t port)
{
m_boundEndpoint = IPAddress(m_boundEndpoint.ToInteger(), port);
m_bound = true;
return *this;
}
UdpSocketBuilder JoinedToGroup(const IPAddress& group_addr)
{
m_joinedGroups.emplace_back(group_addr);
return *this;
}
UdpSocketBuilder WithMulticastLoopback()
{
m_multicastLoopback = true;
return *this;
}
UdpSocketBuilder WithMulticastTtl(uint8_t time_to_live)
{
m_multicastTtl = time_to_live;
return *this;
}
UdpSocketBuilder WithReceiveBufferSize(uint32_t size)
{
m_receiveBufferSize = size;
return *this;
}
UdpSocketBuilder WithSendBufferSize(uint32_t size)
{
m_sendBufferSize = size;
return *this;
}
public:
std::unique_ptr<UdpSocket> Build() const
{
std::unique_ptr<Socket> soc = std::make_unique<Socket>(SocketType::Datagram);
if (soc)
{
bool Error =
!soc->SetNonBlocking(!m_blocking) ||
!soc->SetReuseAddr(m_reusable);
if (!Error)
Error = m_bound && !soc->Bind(m_boundEndpoint);
if (!Error)
Error = !soc->SetMulticastLoopback(m_multicastLoopback) || !soc->SetMulticastTtl(m_multicastTtl);
if (!Error)
{
for (const auto& Group : m_joinedGroups)
{
if (!soc->JoinMulticastGroup(IPAddress(Group, 0)))
{
Error = true;
break;
}
}
}
if (!Error)
{
int32_t out_new_size;
if (m_receiveBufferSize > 0)
soc->SetReceiveBufferSize(m_receiveBufferSize, out_new_size);
if (m_sendBufferSize > 0)
soc->SetSendBufferSize(m_sendBufferSize, out_new_size);
}
if (Error)
throw inl::RuntimeException("Couldnt create socket"); // make parameter a string depending on the error
return std::make_unique<UdpSocket>();
}
return std::unique_ptr<UdpSocket>(nullptr);
}
private:
bool m_blocking;
bool m_bound;
IPAddress m_boundEndpoint;
std::vector<IPAddress> m_joinedGroups;
bool m_multicastLoopback;
uint8_t m_multicastTtl;
uint32_t m_receiveBufferSize;
bool m_reusable;
uint32_t m_sendBufferSize;
};
}

View File

@ -0,0 +1,78 @@
// https://github.com/mfichman/http
#pragma once
#include <string>
namespace std::net
{
class Authority
{
public:
Authority(const std::string& user, const std::string& host, uint16_t port);
Authority();
const std::string& GetUser() const
{
return m_user;
}
const std::string& GetHost() const
{
return m_host;
}
uint16_t GetPort() const
{
return m_port;
}
void SetUser(const std::string& user);
void SetHost(const std::string& host);
void SetPort(uint16_t port);
private:
std::string m_user;
std::string m_host;
uint16_t m_port;
};
class Uri {
public:
Uri(const char* value);
Uri(const std::string& value);
Uri();
const std::string& GetScheme() const
{
return m_scheme;
}
const Authority& GetAuthority() const
{
return m_authority;
}
const std::string& GetPath() const
{
return m_path;
}
const std::string& GetHost() const
{
return m_authority.GetHost();
}
uint16_t GetPort() const
{
return m_authority.GetPort();
}
void SetScheme(const std::string& scheme);
void SetAuthority(const Authority& authority);
void SetPath(const std::string& path);
private:
std::string m_scheme;
Authority m_authority;
std::string m_path;
};
}

View File

@ -0,0 +1,12 @@
#pragma once
#include <vector>
#include <sstream>
#include "VoidNet/Net.hpp"
namespace std::net
{
static std::vector<std::string> Split(const std::string& str, const std::string& delimiter);
static sockaddr_in CreateAddress(uint32_t address, uint16_t port);
}