From 5ab59a6fb8ce1d3ba5287076067b8af8e2cf7d65 Mon Sep 17 00:00:00 2001 From: TheDoctor Date: Tue, 15 Oct 2019 21:03:24 +0100 Subject: [PATCH] Update Integrated std::byte added to C++17 recently --- include/VoidNet_HL/ByteConverter.hpp | 9 ++-- include/VoidNet_HL/NetworkHeader.hpp | 2 +- include/VoidNet_HL/NetworkMessage.hpp | 28 ++++++------ include/VoidNet_HL/Plugin/Plugin.hpp | 2 +- include/VoidNet_HL/Plugin/PluginManager.hpp | 4 +- include/VoidNet_HL/Server.hpp | 6 +-- include/VoidNet_HL/TcpConnection.hpp | 16 +++---- include/VoidNet_HL/TcpConnectionHandler.hpp | 36 +++++++-------- include/VoidNet_HL/TcpServer.hpp | 10 ++--- include/VoidNet_LL/Cookies.hpp | 36 +++++++-------- include/VoidNet_LL/Enums.hpp | 2 - include/VoidNet_LL/Headers.hpp | 18 ++++---- include/VoidNet_LL/Http.hpp | 10 ++--- include/VoidNet_LL/IPAddress.hpp | 18 ++++---- include/VoidNet_LL/ISocket.hpp | 22 ++++----- include/VoidNet_LL/Net.hpp | 4 +- include/VoidNet_LL/Parse.hpp | 18 ++++---- include/VoidNet_LL/Request.hpp | 18 ++++---- include/VoidNet_LL/Response.hpp | 20 ++++----- include/VoidNet_LL/SecureSocket.hpp | 16 +++---- include/VoidNet_LL/Socket.hpp | 24 +++++----- include/VoidNet_LL/TcpClient.hpp | 12 ++--- include/VoidNet_LL/TcpListener.hpp | 12 ++--- include/VoidNet_LL/TcpSocketBuilder.hpp | 10 ++--- include/VoidNet_LL/UdpSocket.hpp | 10 ++--- include/VoidNet_LL/UdpSocketBuilder.hpp | 20 ++++----- include/VoidNet_LL/Uri.hpp | 30 ++++++------- include/VoidNet_LL/Util.hpp | 4 +- src/VoidNet_HL/NetworkMessage.cpp | 20 ++++----- src/VoidNet_HL/Plugin/PluginManager.cpp | 17 ++++--- src/VoidNet_HL/Server.cpp | 10 ++--- src/VoidNet_HL/TcpConnection.cpp | 14 +++--- src/VoidNet_HL/TcpConnectionHandler.cpp | 38 ++++++++-------- src/VoidNet_HL/TcpServer.cpp | 16 +++---- src/VoidNet_HL/main.cpp | 18 ++++---- src/VoidNet_LL/Cookies.cpp | 16 +++---- src/VoidNet_LL/Headers.cpp | 14 +++--- src/VoidNet_LL/Http.cpp | 30 ++++++------- src/VoidNet_LL/IPAddress.cpp | 8 ++-- src/VoidNet_LL/Request.cpp | 8 ++-- src/VoidNet_LL/Response.cpp | 18 ++++---- src/VoidNet_LL/SecureSocket.cpp | 20 ++++----- src/VoidNet_LL/Socket.cpp | 42 +++++++++--------- src/VoidNet_LL/TcpClient.cpp | 12 ++--- src/VoidNet_LL/TcpListener.cpp | 22 ++++----- src/VoidNet_LL/TcpSocketBuilder.cpp | 24 +++++----- src/VoidNet_LL/UdpSocket.cpp | 12 ++--- src/VoidNet_LL/Uri.cpp | 26 +++++------ src/VoidNet_LL/Util.cpp | 49 +++++++++++---------- 49 files changed, 424 insertions(+), 427 deletions(-) diff --git a/include/VoidNet_HL/ByteConverter.hpp b/include/VoidNet_HL/ByteConverter.hpp index 1957da2..9ff4e8a 100644 --- a/include/VoidNet_HL/ByteConverter.hpp +++ b/include/VoidNet_HL/ByteConverter.hpp @@ -4,8 +4,7 @@ // now ive hit a wall // casting vs bitshifting -#include -#include +#include "VoidNet_LL/Net.hpp" namespace std { @@ -13,15 +12,15 @@ namespace std { public: template - inline static uint8_t *ToBytes(T value) + inline static byte *ToBytes(T value) { - uint8_t *data = new uint8_t[sizeof(T)](); + byte *data = new byte[sizeof(T)](); memcpy(data, &value, sizeof(T)); return data; } template - inline static T FromBytes(uint8_t *data) + inline static T FromBytes(byte *data) { if (!data) throw std::invalid_argument("cant have null parameter -> ByteConverter::FromBytes"); diff --git a/include/VoidNet_HL/NetworkHeader.hpp b/include/VoidNet_HL/NetworkHeader.hpp index f7ea01d..b9edf57 100644 --- a/include/VoidNet_HL/NetworkHeader.hpp +++ b/include/VoidNet_HL/NetworkHeader.hpp @@ -1,6 +1,6 @@ #pragma once -#include + namespace std::net { diff --git a/include/VoidNet_HL/NetworkMessage.hpp b/include/VoidNet_HL/NetworkMessage.hpp index 1a2bd63..dcfda91 100644 --- a/include/VoidNet_HL/NetworkMessage.hpp +++ b/include/VoidNet_HL/NetworkMessage.hpp @@ -1,10 +1,8 @@ #pragma once -#include "HLAPI/NetworkHeader.hpp" -#include "HLAPI/ByteConverter.hpp" - -#include -#include +#include "VoidNet_LL/Net.hpp" +#include "VoidNet_HL/NetworkHeader.hpp" +#include "VoidNet_HL/ByteConverter.hpp" namespace std::net { @@ -54,26 +52,26 @@ namespace std::net void SetData(T *data) { m_data = data; - m_dataSize = sizeof(T); + m_dataSize = sizeof(*data); } template - uint8_t * SerializeData(uint32_t &size) const + byte * SerializeData(uint32_t &size) const { int32_t sizeOfNetHeader = sizeof(NetworkHeader); NetworkHeader header; header.Size = 13 + sizeOfNetHeader + sizeof(T); - uint8_t *bytes = new uint8_t[header.Size](); + byte *bytes = new byte[header.Size](); memcpy(bytes, &header, sizeOfNetHeader); - uint8_t *sender = ByteConverter::ToBytes(m_senderID); // 4 - uint8_t *destination = ByteConverter::ToBytes(m_destinationID); // 4 - uint8_t *tag = ByteConverter::ToBytes(m_tag); // 4 + byte *sender = ByteConverter::ToBytes(m_senderID); // 4 + byte *destination = ByteConverter::ToBytes(m_destinationID); // 4 + byte *tag = ByteConverter::ToBytes(m_tag); // 4 memcpy(bytes + sizeOfNetHeader, sender, 4); - bytes[sizeOfNetHeader + 4] = (uint8_t)m_distributionMode; + bytes[sizeOfNetHeader + 4] = (byte)m_distributionMode; memcpy(bytes + sizeOfNetHeader + 5, destination, 4); memcpy(bytes + sizeOfNetHeader + 9, tag, 4); @@ -83,9 +81,9 @@ namespace std::net return bytes; } - uint8_t *SerializeData(uint32_t &size) const; - void Deserialize(uint8_t *data, uint32_t size); - void DeserializeWithoutHeader(uint8_t* data, uint32_t size); + byte *SerializeData(uint32_t &size) const; + void Deserialize(byte *data, uint32_t size); + void DeserializeWithoutHeader(byte* data, uint32_t size); template T *GetData() const diff --git a/include/VoidNet_HL/Plugin/Plugin.hpp b/include/VoidNet_HL/Plugin/Plugin.hpp index b82a54a..bd69568 100644 --- a/include/VoidNet_HL/Plugin/Plugin.hpp +++ b/include/VoidNet_HL/Plugin/Plugin.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include namespace std::net { diff --git a/include/VoidNet_HL/Plugin/PluginManager.hpp b/include/VoidNet_HL/Plugin/PluginManager.hpp index add5254..c2ddb6b 100644 --- a/include/VoidNet_HL/Plugin/PluginManager.hpp +++ b/include/VoidNet_HL/Plugin/PluginManager.hpp @@ -2,7 +2,7 @@ #include -#include "HLAPI/Plugin/Plugin.hpp" +#include "VoidNet_HL/Plugin/Plugin.hpp" namespace std::net { @@ -24,6 +24,6 @@ namespace std::net } private: - std::vector m_plugins = std::vector(); + vector m_plugins; }; } \ No newline at end of file diff --git a/include/VoidNet_HL/Server.hpp b/include/VoidNet_HL/Server.hpp index 4a137fd..2f63c0f 100644 --- a/include/VoidNet_HL/Server.hpp +++ b/include/VoidNet_HL/Server.hpp @@ -1,7 +1,7 @@ #pragma once -#include -#include "HLAPI/ServerConfig.hpp" +#include +#include "VoidNet_HL/ServerConfig.hpp" #include @@ -24,6 +24,6 @@ namespace std::net ServerConfig Config; private: - std::shared_ptr m_tcpServer; + shared_ptr m_tcpServer; }; } \ No newline at end of file diff --git a/include/VoidNet_HL/TcpConnection.hpp b/include/VoidNet_HL/TcpConnection.hpp index 5569a26..cd6928e 100644 --- a/include/VoidNet_HL/TcpConnection.hpp +++ b/include/VoidNet_HL/TcpConnection.hpp @@ -2,9 +2,9 @@ #include -#include "VoidNet/TcpClient.hpp" +#include "VoidNet_LL/TcpClient.hpp" -#include "HLAPI/NetworkMessage.hpp" +#include "VoidNet_HL/NetworkMessage.hpp" namespace std::net { @@ -16,7 +16,7 @@ namespace std::net TcpConnection(); TcpConnection(TcpClient *client); - std::shared_ptr GetClient(); + shared_ptr GetClient(); uint32_t GetID(); bool Connect(IPAddress addr); @@ -30,15 +30,15 @@ namespace std::net void ReceiveData(); - std::function DataReceivedEvent; - std::function DisconnectedEvent; - std::function NewConnectionEvent; - std::function OnConnectionEvent; + function DataReceivedEvent; + function DisconnectedEvent; + function NewConnectionEvent; + function OnConnectionEvent; private: bool sendMessage(const NetworkMessage &msg); - std::shared_ptr m_client; + shared_ptr m_client; uint32_t m_id; }; } \ No newline at end of file diff --git a/include/VoidNet_HL/TcpConnectionHandler.hpp b/include/VoidNet_HL/TcpConnectionHandler.hpp index 8a3ac8b..ab8f31b 100644 --- a/include/VoidNet_HL/TcpConnectionHandler.hpp +++ b/include/VoidNet_HL/TcpConnectionHandler.hpp @@ -1,17 +1,15 @@ #pragma once +#include +#include +#include +#include + #include #include -#include - -#include -#include -#include -#include namespace std::net { - class MessageQueue; class TcpConnection; class Server; class TcpListener; @@ -21,15 +19,15 @@ namespace std::net { class TcpConnectionHandler { - friend class std::net::Server; + friend class Server; public: - TcpConnectionHandler(std::shared_ptr listener_ptr); + TcpConnectionHandler(shared_ptr listener_ptr); ~TcpConnectionHandler(); void Start(); void Stop(); - void AddClient(std::shared_ptr &c); + void AddClient(shared_ptr &c); void SetMaxConnections(uint32_t max_connections); void HandlePluginMessage(const NetworkMessage& message); @@ -43,23 +41,21 @@ namespace std::net void HandleReceiveMsgAndConnsThreaded(); private: - std::vector> m_list; - std::mutex m_listMutex; + vector> m_list; + mutex m_listMutex; uint32_t m_maxConnections = 0; - std::thread m_receiveThread; - std::thread m_sendThread; + thread m_receiveThread; + thread m_sendThread; - std::atomic_bool m_run; + atomic_bool m_run; - std::shared_ptr m_queue; + shared_ptr m_listenerPtr; - std::shared_ptr m_listenerPtr; + shared_ptr m_pluginManager; - std::shared_ptr m_pluginManager; - - std::vector m_pollFds; + vector m_pollFds; ServerConfig m_config; }; diff --git a/include/VoidNet_HL/TcpServer.hpp b/include/VoidNet_HL/TcpServer.hpp index 798400a..8b49aaa 100644 --- a/include/VoidNet_HL/TcpServer.hpp +++ b/include/VoidNet_HL/TcpServer.hpp @@ -2,7 +2,7 @@ #include -#include "VoidNet/TcpListener.hpp" +#include "VoidNet_LL/TcpListener.hpp" namespace std::net { @@ -11,7 +11,7 @@ namespace std::net class TcpServer { - friend class std::net::Server; + friend class Server; public: TcpServer(uint32_t max_connections, uint16_t port = DEFAULT_SERVER_PORT); @@ -20,12 +20,12 @@ namespace std::net void Stop(); private: - std::shared_ptr listener; + shared_ptr listener; uint32_t m_maxConnections; uint16_t m_port; - std::atomic_bool m_run; + atomic_bool m_run; - std::shared_ptr m_connectionHandler; + shared_ptr m_connectionHandler; }; } \ No newline at end of file diff --git a/include/VoidNet_LL/Cookies.hpp b/include/VoidNet_LL/Cookies.hpp index 0f61249..ab3b8ad 100644 --- a/include/VoidNet_LL/Cookies.hpp +++ b/include/VoidNet_LL/Cookies.hpp @@ -9,23 +9,23 @@ namespace std::net class Cookie { public: - Cookie(const std::string& text); + Cookie(const string& text); Cookie() : m_httpOnly(false), m_secure(false) { } - const std::string& GetName() const + const string& GetName() const { return m_name; } - const std::string& GetValue() const + const string& GetValue() const { return m_value; } - const std::string& GetPath() const + const string& GetPath() const { return m_path; } @@ -40,17 +40,17 @@ namespace std::net return m_secure; } - void SetName(const std::string& name) + void SetName(const string& name) { m_name = name; } - void SetValue(const std::string& value) + void SetValue(const string& value) { m_value = value; } - void SetPath(const std::string& path) + void SetPath(const string& path) { m_path = path; } @@ -66,9 +66,9 @@ namespace std::net } private: - std::string m_name; - std::string m_value; - std::string m_path; + string m_name; + string m_value; + string m_path; bool m_httpOnly; bool m_secure; }; @@ -76,25 +76,25 @@ namespace std::net class Cookies { public: - const Cookie operator[](const std::string &name) const; + const Cookie operator[](const string &name) const; - std::map::const_iterator begin() const + map::const_iterator begin() const { return m_cookie.begin(); } - std::map::const_iterator end() const + map::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; + static const string HOST; + static const string CONTENT_LENGTH; + static const string ACCEPT_ENCODING; + static const string CONNECTION; private: - std::map m_cookie; + map m_cookie; }; } \ No newline at end of file diff --git a/include/VoidNet_LL/Enums.hpp b/include/VoidNet_LL/Enums.hpp index ef9b48d..7675fed 100644 --- a/include/VoidNet_LL/Enums.hpp +++ b/include/VoidNet_LL/Enums.hpp @@ -1,7 +1,5 @@ #pragma once -#undef DELETE - namespace std::net { enum class SocketParam diff --git a/include/VoidNet_LL/Headers.hpp b/include/VoidNet_LL/Headers.hpp index 1400b6a..c9337af 100644 --- a/include/VoidNet_LL/Headers.hpp +++ b/include/VoidNet_LL/Headers.hpp @@ -9,25 +9,25 @@ namespace std::net class Headers { public: - const std::string operator[](const std::string &name) const; + const string operator[](const string &name) const; - std::multimap::const_iterator begin() const + multimap::const_iterator begin() const { return m_header.begin(); } - std::multimap::const_iterator end() const + multimap::const_iterator end() const { return m_header.end(); } - void AddHeader(std::string const& name, std::string const& value); + void AddHeader(string const& name, 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; + static string const HOST; + static string const CONTENT_LENGTH; + static string const ACCEPT_ENCODING; + static string const CONNECTION; private: - std::multimap m_header; + multimap m_header; }; } \ No newline at end of file diff --git a/include/VoidNet_LL/Http.hpp b/include/VoidNet_LL/Http.hpp index 89f6b78..dfda402 100644 --- a/include/VoidNet_LL/Http.hpp +++ b/include/VoidNet_LL/Http.hpp @@ -2,19 +2,19 @@ #pragma once -#include "VoidNet/Response.hpp" -#include "VoidNet/Request.hpp" +#include "VoidNet_LL/Response.hpp" +#include "VoidNet_LL/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 = ""); + static Response Get(string const& path, string const& data = ""); + static Response Post(string const& path, string const& data = ""); private: static Response Send(Request const& request); - static std::string Str(Request const& request); + static string Str(Request const& request); }; } \ No newline at end of file diff --git a/include/VoidNet_LL/IPAddress.hpp b/include/VoidNet_LL/IPAddress.hpp index b9798cd..3413b46 100644 --- a/include/VoidNet_LL/IPAddress.hpp +++ b/include/VoidNet_LL/IPAddress.hpp @@ -5,7 +5,7 @@ #include #include -#include "VoidNet/Net.hpp" +#include "VoidNet_LL/Net.hpp" namespace std::net { @@ -26,7 +26,7 @@ namespace std::net { } - inline IPAddress(const std::string& address, uint16_t port = DEFAULT_SERVER_PORT) + inline IPAddress(const string& address, uint16_t port = DEFAULT_SERVER_PORT) : m_address(0) , m_valid(false) , m_port(port) @@ -56,7 +56,7 @@ namespace std::net { } - std::string ToString() const; + string ToString() const; inline uint32_t ToInteger() const { return ntohl(m_address); } inline uint16_t GetPort() const { return m_port; } @@ -68,7 +68,7 @@ namespace std::net inline sockaddr_in ToCAddr() const { sockaddr_in addr; - std::memset(&addr, 0, sizeof(addr)); + memset(&addr, 0, sizeof(addr)); addr.sin_addr.s_addr = htonl(ToInteger()); addr.sin_family = AF_INET; addr.sin_port = htons(GetPort()); @@ -80,7 +80,7 @@ namespace std::net friend bool operator <(const IPAddress& left, const IPAddress& right); - void Resolve(const std::string& address); + void Resolve(const string& address); private: uint32_t m_address; @@ -90,19 +90,19 @@ namespace std::net 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 make_pair(left.m_valid, left.m_address) < 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) + inline istream& operator >>(istream& stream, IPAddress& address) { - std::string str; + string str; stream >> str; address = IPAddress(str); return stream; } - inline std::ostream& operator <<(std::ostream& stream, const IPAddress& address) { return stream << address.ToString(); } + inline ostream& operator <<(ostream& stream, const IPAddress& address) { return stream << address.ToString(); } } \ No newline at end of file diff --git a/include/VoidNet_LL/ISocket.hpp b/include/VoidNet_LL/ISocket.hpp index 08a2188..e2328b9 100644 --- a/include/VoidNet_LL/ISocket.hpp +++ b/include/VoidNet_LL/ISocket.hpp @@ -4,9 +4,9 @@ #include #include -#include "VoidNet/Net.hpp" -#include "VoidNet/IPAddress.hpp" -#include "VoidNet/Enums.hpp" +#include "VoidNet_LL/Net.hpp" +#include "VoidNet_LL/IPAddress.hpp" +#include "VoidNet_LL/Enums.hpp" namespace std::net { @@ -35,14 +35,14 @@ namespace std::net 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 WaitForPendingConnection(bool& hasPendingConnection, chrono::milliseconds t) = 0; virtual bool HasPendingData(uint32_t& pendingDataSize) = 0; - virtual std::unique_ptr 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 unique_ptr Accept() = 0; + virtual bool SendTo(const byte* data, int32_t count, int32_t& sent, const IPAddress& addrDest) = 0; + virtual bool Send(const byte* data, int32_t count, int32_t& sent) = 0; + virtual bool RecvFrom(byte* data, int32_t size, int32_t& read, IPAddress& srcAddr, SocketReceiveFlags flags = SocketReceiveFlags::None) = 0; + virtual bool Recv(byte* data, int32_t size, int32_t& read, SocketReceiveFlags flags = SocketReceiveFlags::None) = 0; + virtual bool Wait(SocketWaitConditions cond, chrono::milliseconds t) = 0; virtual SocketConnectionState GetConnectionState() = 0; virtual void GetAddress(IPAddress& outAddr) = 0; virtual bool GetPeerAddress(IPAddress& outAddr) = 0; @@ -50,7 +50,7 @@ namespace std::net 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 SetMulticastTtl(byte 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; diff --git a/include/VoidNet_LL/Net.hpp b/include/VoidNet_LL/Net.hpp index e759cd2..40ff59a 100644 --- a/include/VoidNet_LL/Net.hpp +++ b/include/VoidNet_LL/Net.hpp @@ -1,6 +1,7 @@ #pragma once -#include +#include +#include #ifdef _MSC_VER #include @@ -11,6 +12,7 @@ #undef GetPort #undef max #undef min + #undef DELETE #define poll WSAPoll #define ioctl ioctlsocket diff --git a/include/VoidNet_LL/Parse.hpp b/include/VoidNet_LL/Parse.hpp index a223772..7d652e9 100644 --- a/include/VoidNet_LL/Parse.hpp +++ b/include/VoidNet_LL/Parse.hpp @@ -15,41 +15,41 @@ namespace std::net }; template - static ParseResult ParseUntil(char const* str, F func) + static ParseResult ParseUntil(char const* str, F func) { - ParseResult result{}; + ParseResult result{}; char const* ch = str; for (; *ch && !func(*ch); ++ch) { } - result.value = std::string(str, ch - str); + result.value = string(str, ch - str); result.ch = ch; return result; } template - static inline ParseResult ParseWhile(char const* str, F func) + static inline ParseResult ParseWhile(char const* str, F func) { - ParseResult result{}; + ParseResult result{}; char const* ch = str; for (; *ch && func(*ch); ++ch) { } - result.value = std::string(str, ch - str); + result.value = string(str, ch - str); result.ch = ch; return result; } - static inline ParseResult ParseToken(char const* str) + static inline ParseResult ParseToken(char const* str) { auto token = ParseUntil(str, isspace); token.ch = ParseWhile(token.ch, isspace).ch; return token; } - static inline ParseResult parseCrLf(char const* str) + static inline ParseResult parseCrLf(char const* str) { auto cr = ParseUntil(str, [](char ch) { return ch == '\r'; }); if (*cr.ch == '\r') @@ -60,7 +60,7 @@ namespace std::net }); } - static inline ParseResult ParseWhitespace(char const* str) + static inline ParseResult ParseWhitespace(char const* str) { return ParseWhile(str, isspace); } diff --git a/include/VoidNet_LL/Request.hpp b/include/VoidNet_LL/Request.hpp index f31fae9..4e50e40 100644 --- a/include/VoidNet_LL/Request.hpp +++ b/include/VoidNet_LL/Request.hpp @@ -2,9 +2,9 @@ #pragma once -#include "VoidNet/Uri.hpp" -#include "VoidNet/Headers.hpp" -#include "VoidNet/Enums.hpp" +#include "VoidNet_LL/Uri.hpp" +#include "VoidNet_LL/Headers.hpp" +#include "VoidNet_LL/Enums.hpp" namespace std::net { @@ -21,17 +21,17 @@ namespace std::net return m_uri; } - const std::string& GetPath() const + const string& GetPath() const { return m_uri.GetPath(); } - const std::string& GetData() const + const string& GetData() const { return m_data; } - const std::string GetHeaderElement(const std::string& name) const; + const string GetHeaderElement(const string& name) const; const Headers& GetHeaders() const { @@ -40,13 +40,13 @@ namespace std::net 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); + void SetData(const string& data); + void AddHeader(const string& name, const string& value); private: Method m_method = Method::GET; Uri m_uri; - std::string m_data; + string m_data; Headers m_headers; }; } \ No newline at end of file diff --git a/include/VoidNet_LL/Response.hpp b/include/VoidNet_LL/Response.hpp index e757ed3..81306f4 100644 --- a/include/VoidNet_LL/Response.hpp +++ b/include/VoidNet_LL/Response.hpp @@ -2,9 +2,9 @@ #pragma once -#include "VoidNet/Headers.hpp" -#include "VoidNet/Cookies.hpp" -#include "VoidNet/Enums.hpp" +#include "VoidNet_LL/Headers.hpp" +#include "VoidNet_LL/Cookies.hpp" +#include "VoidNet_LL/Enums.hpp" #include @@ -13,7 +13,7 @@ namespace std::net class Response { public: - Response(const std::string& text); + Response(const string& text); Response() {}; HttpStatus GetStatus() const @@ -21,22 +21,22 @@ namespace std::net return m_status; } - const std::string& GetData() const + const string& GetData() const { return m_data; } - const std::string GetHeader(const std::string& name) const; - const Cookie GetCookie(const std::string& name) const; + const string GetHeader(const string& name) const; + const Cookie GetCookie(const string& name) const; void SetStatus(HttpStatus status); - void SetData(const std::string& data); - void SetHeader(const std::string& name, const std::string& value); + void SetData(const string& data); + void SetHeader(const string& name, const string& value); void SetCookie(const Cookie& cookie); private: HttpStatus m_status = HttpStatus::INVALID_CODE; - std::string m_data; + string m_data; Headers m_headers; Cookies m_cookies; }; diff --git a/include/VoidNet_LL/SecureSocket.hpp b/include/VoidNet_LL/SecureSocket.hpp index fdf95b2..791ddda 100644 --- a/include/VoidNet_LL/SecureSocket.hpp +++ b/include/VoidNet_LL/SecureSocket.hpp @@ -1,6 +1,6 @@ #pragma once -#include "VoidNet/Socket.hpp" +#include "VoidNet_LL/Socket.hpp" //#include //#include @@ -16,23 +16,23 @@ namespace std::net 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; + bool Send(byte* data, int32_t count, int32_t &sent, int flags = 0); // Execute 1 write() syscall + bool Recv(byte* data, int32_t count, int32_t &read, int flags = 0); // Execte 1 read() syscall + bool Wait(SocketWaitConditions cond, 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); + void UseCertificateFile(string const& path); + void UsePrivateKeyFile(string const& path); private: - bool SendRaw(uint8_t* buf, size_t len, int flags = 0); + bool SendRaw(byte* 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 m_socket; + unique_ptr m_socket; //SSL_CTX* m_context; //SSL* m_conn; diff --git a/include/VoidNet_LL/Socket.hpp b/include/VoidNet_LL/Socket.hpp index 39871d2..a38aacb 100644 --- a/include/VoidNet_LL/Socket.hpp +++ b/include/VoidNet_LL/Socket.hpp @@ -1,7 +1,7 @@ #pragma once -#include "VoidNet/ISocket.hpp" -#include "VoidNet/Enums.hpp" +#include "VoidNet_LL/ISocket.hpp" +#include "VoidNet_LL/Enums.hpp" namespace std::net { @@ -31,14 +31,14 @@ namespace std::net 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 WaitForPendingConnection(bool& hasPendingConnection, chrono::milliseconds t) override; virtual bool HasPendingData(uint32_t& pendingDataSize) override; - virtual std::unique_ptr 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 unique_ptr Accept() override; + virtual bool SendTo(const byte* data, int32_t count, int32_t& sent, const IPAddress& addrDest) override; + virtual bool Send(const byte* data, int32_t count, int32_t& sent) override; + virtual bool RecvFrom(byte* data, int32_t size, int32_t& read, IPAddress& srcAddr, SocketReceiveFlags flags = SocketReceiveFlags::None) override; + virtual bool Recv(byte* data, int32_t size, int32_t& read, SocketReceiveFlags flags = SocketReceiveFlags::None) override; + virtual bool Wait(SocketWaitConditions cond, chrono::milliseconds t) override; virtual SocketConnectionState GetConnectionState() override; virtual void GetAddress(IPAddress& outAddr) override; virtual bool GetPeerAddress(IPAddress& outAddr) override; @@ -47,7 +47,7 @@ namespace std::net 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 SetMulticastTtl(byte 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; @@ -67,13 +67,13 @@ namespace std::net private: void init(); - virtual SocketReturn HasState(SocketParam state, std::chrono::milliseconds t = std::chrono::milliseconds(0)); + virtual SocketReturn HasState(SocketParam state, chrono::milliseconds t = 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(); + m_lastActivityTime = chrono::system_clock::now().time_since_epoch().count(); } private: diff --git a/include/VoidNet_LL/TcpClient.hpp b/include/VoidNet_LL/TcpClient.hpp index 7f297c1..2f21095 100644 --- a/include/VoidNet_LL/TcpClient.hpp +++ b/include/VoidNet_LL/TcpClient.hpp @@ -1,6 +1,6 @@ #pragma once -#include "VoidNet/Socket.hpp" +#include "VoidNet_LL/Socket.hpp" namespace std::net { @@ -11,7 +11,7 @@ namespace std::net { class TcpClient { - friend class std::net::TcpConnectionHandler; + friend class TcpConnectionHandler; public: TcpClient(Socket *soc); @@ -20,14 +20,14 @@ namespace std::net 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; + bool Send(const byte* data, int32_t count, int32_t& sent) const; + bool Recv(byte* data, int32_t size, int32_t& read, SocketReceiveFlags flags = SocketReceiveFlags::None) const; + bool Wait(SocketWaitConditions cond, chrono::milliseconds t) const; SocketConnectionState GetConnectionState() const; void GetAddress(IPAddress& outAddr) const; int32_t GetPort() const; private: - std::unique_ptr m_socket; + unique_ptr m_socket; }; } \ No newline at end of file diff --git a/include/VoidNet_LL/TcpListener.hpp b/include/VoidNet_LL/TcpListener.hpp index dd32e9b..243243a 100644 --- a/include/VoidNet_LL/TcpListener.hpp +++ b/include/VoidNet_LL/TcpListener.hpp @@ -3,7 +3,7 @@ #include #include -#include "VoidNet/Socket.hpp" +#include "VoidNet_LL/Socket.hpp" namespace std::net { @@ -16,17 +16,17 @@ namespace std::net class TcpListener { - friend class std::net::TcpConnectionHandler; + friend class 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)); + TcpListener(uint16_t port, chrono::milliseconds inSleepTime = chrono::milliseconds(0)); + TcpListener(Socket *InSocket, chrono::milliseconds inSleepTime = chrono::milliseconds(0)); TcpClient *AcceptClient(); private: - std::chrono::milliseconds m_sleepTime; - std::unique_ptr m_socket; + chrono::milliseconds m_sleepTime; + unique_ptr m_socket; uint16_t m_port = 0; }; } \ No newline at end of file diff --git a/include/VoidNet_LL/TcpSocketBuilder.hpp b/include/VoidNet_LL/TcpSocketBuilder.hpp index cbffd95..870b811 100644 --- a/include/VoidNet_LL/TcpSocketBuilder.hpp +++ b/include/VoidNet_LL/TcpSocketBuilder.hpp @@ -1,7 +1,7 @@ #pragma once -#include "VoidNet/Enums.hpp" -#include "VoidNet/IPAddress.hpp" +#include "VoidNet_LL/Enums.hpp" +#include "VoidNet_LL/IPAddress.hpp" namespace std::net { @@ -92,9 +92,9 @@ namespace std::net } public: - std::unique_ptr Build() const; - std::unique_ptr BuildClient() const; - std::unique_ptr BuildListener() const; + unique_ptr Build() const; + unique_ptr BuildClient() const; + unique_ptr BuildListener() const; private: bool m_blocking; diff --git a/include/VoidNet_LL/UdpSocket.hpp b/include/VoidNet_LL/UdpSocket.hpp index c40418e..e735732 100644 --- a/include/VoidNet_LL/UdpSocket.hpp +++ b/include/VoidNet_LL/UdpSocket.hpp @@ -1,6 +1,6 @@ #pragma once -#include "VoidNet/Socket.hpp" +#include "VoidNet_LL/Socket.hpp" #include @@ -13,17 +13,17 @@ namespace std::net 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 SendTo(const byte* data, int32_t count, int32_t& sent, const IPAddress& addrDest); + bool RecvFrom(byte* 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); + bool SetMulticastTtl(byte timeToLive); uint32_t GetPort(); bool SetReuseAddr(bool allowReuse = true); private: - std::unique_ptr m_socket; + unique_ptr m_socket; }; } \ No newline at end of file diff --git a/include/VoidNet_LL/UdpSocketBuilder.hpp b/include/VoidNet_LL/UdpSocketBuilder.hpp index 156ceb1..885de63 100644 --- a/include/VoidNet_LL/UdpSocketBuilder.hpp +++ b/include/VoidNet_LL/UdpSocketBuilder.hpp @@ -1,7 +1,7 @@ #pragma once -#include "VoidNet/UdpSocket.hpp" -#include "VoidNet/IPAddress.hpp" +#include "VoidNet_LL/UdpSocket.hpp" +#include "VoidNet_LL/IPAddress.hpp" #include @@ -73,7 +73,7 @@ namespace std::net return *this; } - UdpSocketBuilder WithMulticastTtl(uint8_t time_to_live) + UdpSocketBuilder WithMulticastTtl(byte time_to_live) { m_multicastTtl = time_to_live; @@ -95,9 +95,9 @@ namespace std::net } public: - std::unique_ptr Build() const + unique_ptr Build() const { - std::unique_ptr soc = std::make_unique(SocketType::Datagram); + unique_ptr soc = make_unique(SocketType::Datagram); if (soc) { @@ -132,19 +132,19 @@ namespace std::net } if (Error) - throw inl::RuntimeException("Couldnt create socket"); // make parameter a string depending on the error - return std::make_unique(); + throw runtime_error("Couldnt create socket"); // make parameter a string depending on the error + return make_unique(); } - return std::unique_ptr(nullptr); + return unique_ptr(nullptr); } private: bool m_blocking; bool m_bound; IPAddress m_boundEndpoint; - std::vector m_joinedGroups; + vector m_joinedGroups; bool m_multicastLoopback; - uint8_t m_multicastTtl; + byte m_multicastTtl; uint32_t m_receiveBufferSize; bool m_reusable; uint32_t m_sendBufferSize; diff --git a/include/VoidNet_LL/Uri.hpp b/include/VoidNet_LL/Uri.hpp index 90a7126..3df53a7 100644 --- a/include/VoidNet_LL/Uri.hpp +++ b/include/VoidNet_LL/Uri.hpp @@ -9,15 +9,15 @@ namespace std::net class Authority { public: - Authority(const std::string& user, const std::string& host, uint16_t port); + Authority(const string& user, const string& host, uint16_t port); Authority(); - const std::string& GetUser() const + const string& GetUser() const { return m_user; } - const std::string& GetHost() const + const string& GetHost() const { return m_host; } @@ -27,22 +27,22 @@ namespace std::net return m_port; } - void SetUser(const std::string& user); - void SetHost(const std::string& host); + void SetUser(const string& user); + void SetHost(const string& host); void SetPort(uint16_t port); private: - std::string m_user; - std::string m_host; + string m_user; + string m_host; uint16_t m_port; }; class Uri { public: Uri(const char* value); - Uri(const std::string& value); + Uri(const string& value); Uri(); - const std::string& GetScheme() const + const string& GetScheme() const { return m_scheme; } @@ -52,12 +52,12 @@ namespace std::net return m_authority; } - const std::string& GetPath() const + const string& GetPath() const { return m_path; } - const std::string& GetHost() const + const string& GetHost() const { return m_authority.GetHost(); } @@ -67,12 +67,12 @@ namespace std::net return m_authority.GetPort(); } - void SetScheme(const std::string& scheme); + void SetScheme(const string& scheme); void SetAuthority(const Authority& authority); - void SetPath(const std::string& path); + void SetPath(const string& path); private: - std::string m_scheme; + string m_scheme; Authority m_authority; - std::string m_path; + string m_path; }; } \ No newline at end of file diff --git a/include/VoidNet_LL/Util.hpp b/include/VoidNet_LL/Util.hpp index e1fccb4..b81b590 100644 --- a/include/VoidNet_LL/Util.hpp +++ b/include/VoidNet_LL/Util.hpp @@ -3,10 +3,10 @@ #include #include -#include "VoidNet/Net.hpp" +#include "VoidNet_LL/Net.hpp" namespace std::net { - static std::vector Split(const std::string& str, const std::string& delimiter); + static vector Split(const string& str, const string& delimiter); static sockaddr_in CreateAddress(uint32_t address, uint16_t port); } \ No newline at end of file diff --git a/src/VoidNet_HL/NetworkMessage.cpp b/src/VoidNet_HL/NetworkMessage.cpp index 24ff5b5..aa5ba2b 100644 --- a/src/VoidNet_HL/NetworkMessage.cpp +++ b/src/VoidNet_HL/NetworkMessage.cpp @@ -1,5 +1,5 @@ -#include "HLAPI/NetworkMessage.hpp" -#include +#include "VoidNet_HL/NetworkMessage.hpp" +#include namespace std::net { @@ -23,22 +23,22 @@ namespace std::net return m_tag; } - uint8_t *NetworkMessage::SerializeData(uint32_t &size) const + byte *NetworkMessage::SerializeData(uint32_t &size) const { int32_t sizeOfNetHeader = sizeof(NetworkHeader); NetworkHeader header; header.Size = 13 + sizeOfNetHeader + m_dataSize; - uint8_t *bytes = new uint8_t[header.Size](); + byte *bytes = new byte[header.Size](); memcpy(bytes, &header, sizeOfNetHeader); - uint8_t *sender = ByteConverter::ToBytes(m_senderID); // 4 - uint8_t *destination = ByteConverter::ToBytes(m_destinationID); // 4 - uint8_t *tag = ByteConverter::ToBytes(m_tag); // 4 + byte *sender = ByteConverter::ToBytes(m_senderID); // 4 + byte *destination = ByteConverter::ToBytes(m_destinationID); // 4 + byte *tag = ByteConverter::ToBytes(m_tag); // 4 memcpy(bytes + sizeOfNetHeader, sender, 4); - bytes[sizeOfNetHeader + 4] = (uint8_t)m_distributionMode; + bytes[sizeOfNetHeader + 4] = (byte)m_distributionMode; memcpy(bytes + sizeOfNetHeader + 5, destination, 4); memcpy(bytes + sizeOfNetHeader + 9, tag, 4); @@ -48,7 +48,7 @@ namespace std::net return bytes; } - void NetworkMessage::Deserialize(uint8_t *data, uint32_t size) + void NetworkMessage::Deserialize(byte *data, uint32_t size) { NetworkHeader header; memcpy(&(header), data, sizeof(NetworkHeader)); @@ -57,7 +57,7 @@ namespace std::net DeserializeWithoutHeader(data, header.Size); } - void NetworkMessage::DeserializeWithoutHeader(uint8_t* data, uint32_t size) + void NetworkMessage::DeserializeWithoutHeader(byte* data, uint32_t size) { memcpy(&(m_senderID), data, 4); m_distributionMode = (DistributionMode)data[4]; diff --git a/src/VoidNet_HL/Plugin/PluginManager.cpp b/src/VoidNet_HL/Plugin/PluginManager.cpp index e310f25..298349c 100644 --- a/src/VoidNet_HL/Plugin/PluginManager.cpp +++ b/src/VoidNet_HL/Plugin/PluginManager.cpp @@ -1,11 +1,14 @@ -#include "HLAPI/Plugin/PluginManager.hpp" +#include "VoidNet_HL/Plugin/PluginManager.hpp" -#include "HLAPI/Plugin/Plugin.hpp" +#include "VoidNet_HL/Plugin/Plugin.hpp" -std::net::PluginManager::PluginManager() +namespace std::net { -} + PluginManager::PluginManager() + { + } -std::net::PluginManager::~PluginManager() -{ -} + PluginManager::~PluginManager() + { + } +} \ No newline at end of file diff --git a/src/VoidNet_HL/Server.cpp b/src/VoidNet_HL/Server.cpp index e510ae6..7545e23 100644 --- a/src/VoidNet_HL/Server.cpp +++ b/src/VoidNet_HL/Server.cpp @@ -1,9 +1,9 @@ -#include "HLAPI/Server.hpp" +#include "VoidNet_HL/Server.hpp" -#include +#include -#include "HLAPI/TcpServer.hpp" -#include "HLAPI/TcpConnectionHandler.hpp" +#include "VoidNet_HL/TcpServer.hpp" +#include "VoidNet_HL/TcpConnectionHandler.hpp" //#include "UdpServer.hpp namespace std::net @@ -11,7 +11,7 @@ namespace std::net Server::Server(uint32_t max_connections, uint16_t port) : Config(ServerConfig()) { - m_tcpServer = std::make_shared(max_connections, port); + m_tcpServer = make_shared(max_connections, port); m_tcpServer->m_connectionHandler->m_config = Config; } diff --git a/src/VoidNet_HL/TcpConnection.cpp b/src/VoidNet_HL/TcpConnection.cpp index 8a30a40..012f21e 100644 --- a/src/VoidNet_HL/TcpConnection.cpp +++ b/src/VoidNet_HL/TcpConnection.cpp @@ -1,6 +1,6 @@ -#include "HLAPI/TcpConnection.hpp" +#include "VoidNet_HL/TcpConnection.hpp" -#include "HLAPI/InternalTags.hpp" +#include "VoidNet_HL/InternalTags.hpp" #include @@ -40,7 +40,7 @@ namespace std::net { } - std::shared_ptr TcpConnection::GetClient() + shared_ptr TcpConnection::GetClient() { return m_client; } @@ -58,7 +58,7 @@ namespace std::net bool TcpConnection::sendMessage(const NetworkMessage & msg) { uint32_t size; - uint8_t *data = msg.SerializeData(size); + byte *data = msg.SerializeData(size); int32_t sent; return m_client->Send(data, size, sent); } @@ -68,9 +68,9 @@ namespace std::net uint32_t data_size; while (m_client->HasPendingData(data_size)) { - std::net::NetworkMessage message; + NetworkMessage message; - uint8_t* bytes = new uint8_t[data_size](); + byte* bytes = new byte[data_size](); int32_t read; m_client->Recv(bytes, data_size, read); @@ -80,7 +80,7 @@ namespace std::net if (message.GetTag() == (uint32_t)InternalTags::Disconnect) { if (DisconnectedEvent) - DisconnectedEvent(*(message.GetData())); + DisconnectedEvent(*(message.GetData())); } else if (message.GetTag() == (uint32_t)InternalTags::Connect) { diff --git a/src/VoidNet_HL/TcpConnectionHandler.cpp b/src/VoidNet_HL/TcpConnectionHandler.cpp index 8bc5e79..d3e9b7d 100644 --- a/src/VoidNet_HL/TcpConnectionHandler.cpp +++ b/src/VoidNet_HL/TcpConnectionHandler.cpp @@ -1,16 +1,16 @@ -#include "HLAPI/TcpConnectionHandler.hpp" +#include "VoidNet_HL/TcpConnectionHandler.hpp" -#include "HLAPI/InternalTags.hpp" +#include "VoidNet_HL/InternalTags.hpp" -#include "HLAPI/NetworkMessage.hpp" -#include "HLAPI/TcpConnection.hpp" -#include "VoidNet/TcpListener.hpp" +#include "VoidNet_HL/NetworkMessage.hpp" +#include "VoidNet_HL/TcpConnection.hpp" +#include "VoidNet_LL/TcpListener.hpp" #include namespace std::net { - TcpConnectionHandler::TcpConnectionHandler(std::shared_ptr listener_ptr) + TcpConnectionHandler::TcpConnectionHandler(shared_ptr listener_ptr) : m_run(false) , m_listenerPtr(listener_ptr) , m_pluginManager(new PluginManager()) @@ -31,7 +31,7 @@ namespace std::net master_fd.events = POLLRDNORM; m_pollFds.emplace_back(master_fd); - std::thread receive_thread(&TcpConnectionHandler::HandleReceiveMsgAndConnsThreaded, this); + thread receive_thread(&TcpConnectionHandler::HandleReceiveMsgAndConnsThreaded, this); m_receiveThread.swap(receive_thread); } @@ -40,16 +40,16 @@ namespace std::net m_run.exchange(false); } - void TcpConnectionHandler::AddClient(std::shared_ptr &c) + void TcpConnectionHandler::AddClient(shared_ptr &c) { uint32_t id = GetAvailableID(); if (id == -1) { - std::shared_ptr client = c->GetClient(); - std::string reason("Server is full"); + shared_ptr client = c->GetClient(); + string reason("Server is full"); NetworkMessage msg(0, DistributionMode::ID, 0, (uint32_t)InternalTags::Disconnect, &reason, sizeof(reason)); uint32_t size; - uint8_t* msgArr = msg.SerializeData(size); + byte* msgArr = msg.SerializeData(size); int32_t sent; client->Send(msgArr, size, sent); client->Close(); @@ -132,7 +132,7 @@ namespace std::net TcpClient *c = m_listenerPtr->AcceptClient(); if (c) { - std::shared_ptr connection = std::make_shared(c); + shared_ptr connection = make_shared(c); AddClient(connection); break; } @@ -141,14 +141,14 @@ namespace std::net { SOCKET c = m_pollFds.at(i).fd; - uint8_t* header = new uint8_t[sizeof(NetworkHeader)](); + byte* header = new byte[sizeof(NetworkHeader)](); int32_t read; if ((read = recv(c, (char*)header, sizeof(NetworkHeader), 0)) != sizeof(NetworkHeader)) continue; NetworkHeader net_header(*(NetworkHeader*)(header)); - uint8_t *buffer = new uint8_t[net_header.Size](); + byte *buffer = new byte[net_header.Size](); read = recv(c, (char*)buffer, net_header.Size - 4, 0); if ((read) == net_header.Size - 4) @@ -180,7 +180,7 @@ namespace std::net m_listMutex.lock(); for (int i = 0; i < m_list.size(); i++) { - std::shared_ptr c = m_list.at(i); + shared_ptr c = m_list.at(i); if (c->GetID() != msg.GetSenderID()) { if (!c->sendMessage(msg)) @@ -198,7 +198,7 @@ namespace std::net m_listMutex.lock(); for (int i = 0; i < m_list.size(); i++) { - std::shared_ptr c = m_list.at(i); + shared_ptr c = m_list.at(i); if (c->GetID() != msg.GetSenderID()) { if (!c->sendMessage(msg)) @@ -214,7 +214,7 @@ namespace std::net m_listMutex.lock(); for (int i = 0; i < m_list.size(); i++) { - std::shared_ptr c = m_list.at(i); + shared_ptr c = m_list.at(i); if (c->GetID() == msg.GetSenderID()) { if (!c->sendMessage(msg)) @@ -230,7 +230,7 @@ namespace std::net m_listMutex.lock(); for (int i = 0; i < m_list.size(); i++) { - std::shared_ptr c = m_list.at(i); + shared_ptr c = m_list.at(i); if (!c->sendMessage(msg)) { @@ -246,7 +246,7 @@ namespace std::net m_listMutex.lock(); for (int i = 0; i < m_list.size(); i++) { - std::shared_ptr c = m_list.at(i); + shared_ptr c = m_list.at(i); if (!c->sendMessage(msg)) { diff --git a/src/VoidNet_HL/TcpServer.cpp b/src/VoidNet_HL/TcpServer.cpp index e4507f9..d82fe49 100644 --- a/src/VoidNet_HL/TcpServer.cpp +++ b/src/VoidNet_HL/TcpServer.cpp @@ -1,9 +1,9 @@ -#include "HLAPI/TcpServer.hpp" +#include "VoidNet_HL/TcpServer.hpp" -#include "HLAPI/TcpConnection.hpp" -#include "VoidNet/TcpSocketBuilder.hpp" -#include "VoidNet/TcpClient.hpp" -#include "HLAPI/TcpConnectionHandler.hpp" +#include "VoidNet_HL/TcpConnection.hpp" +#include "VoidNet_LL/TcpSocketBuilder.hpp" +#include "VoidNet_LL/TcpClient.hpp" +#include "VoidNet_HL/TcpConnectionHandler.hpp" namespace std::net { @@ -13,10 +13,10 @@ namespace std::net , m_run(false) { if (port == 0) - throw std::invalid_argument("TcpServer::TcpServer()"); + throw invalid_argument("TcpServer::TcpServer()"); - listener = std::shared_ptr(TcpSocketBuilder().AsReusable().Bind(IPAddress(0, 0, 0, 0, port)).Listening().BuildListener().release()); - m_connectionHandler = std::make_shared(listener); + listener = shared_ptr(TcpSocketBuilder().AsReusable().Bind(IPAddress(0, 0, 0, 0, port)).Listening().BuildListener().release()); + m_connectionHandler = make_shared(listener); m_connectionHandler->SetMaxConnections(max_connections); } diff --git a/src/VoidNet_HL/main.cpp b/src/VoidNet_HL/main.cpp index 57c39bf..879dea5 100644 --- a/src/VoidNet_HL/main.cpp +++ b/src/VoidNet_HL/main.cpp @@ -1,13 +1,13 @@ -#include "VoidNet/Init.hpp" -#include "HLAPI/Server.hpp" -#include "HLAPI/ByteConverter.hpp" -#include "VoidNet/TcpClient.hpp" -#include "HLAPI/InternalTags.hpp" -#include "HLAPI/NetworkMessage.hpp" -#include "HLAPI/Plugin/Plugin.hpp" +#include "VoidNet_LL/Init.hpp" +#include "VoidNet_HL/Server.hpp" +#include "VoidNet_HL/ByteConverter.hpp" +#include "VoidNet_LL/TcpClient.hpp" +#include "VoidNet_HL/InternalTags.hpp" +#include "VoidNet_HL/NetworkMessage.hpp" +#include "VoidNet_HL/Plugin/Plugin.hpp" #include -#include +#include class Plugin : public std::net::Plugin { @@ -20,7 +20,7 @@ class Plugin : public std::net::Plugin int main() { std::net::Initialize(); - std::net::Server server(0); + std::net::Server server(1); server.AddPlugin(new Plugin()); server.Start(); diff --git a/src/VoidNet_LL/Cookies.cpp b/src/VoidNet_LL/Cookies.cpp index e88f101..ecf80f7 100644 --- a/src/VoidNet_LL/Cookies.cpp +++ b/src/VoidNet_LL/Cookies.cpp @@ -1,13 +1,13 @@ // https://github.com/mfichman/http -#include "VoidNet/Cookies.hpp" -#include "VoidNet/Parse.hpp" +#include "VoidNet_LL/Cookies.hpp" +#include "VoidNet_LL/Parse.hpp" #include namespace std::net { - ParseResult ParseName(const char* str) + ParseResult ParseName(const char* str) { return ParseUntil(str, [](char ch) { @@ -15,7 +15,7 @@ namespace std::net }); } - ParseResult ParseValue(const char* str) + ParseResult ParseValue(const char* str) { return ParseUntil(str, [](char ch) { @@ -23,7 +23,7 @@ namespace std::net }); } - ParseResult ParseSeparator(const char* str) + ParseResult ParseSeparator(const char* str) { if (*str) { @@ -32,7 +32,7 @@ namespace std::net } else { - auto result = ParseResult{}; + auto result = ParseResult{}; result.ch = str; return result; } @@ -66,12 +66,12 @@ namespace std::net return cookie; } - Cookie::Cookie(const std::string& text) + Cookie::Cookie(const string& text) { *this = ParseCookie(text.c_str()); } - const Cookie Cookies::operator[](const std::string & name) const + const Cookie Cookies::operator[](const string & name) const { auto i = m_cookie.find(name); return (i == m_cookie.end()) ? Cookie() : i->second; diff --git a/src/VoidNet_LL/Headers.cpp b/src/VoidNet_LL/Headers.cpp index 2873b69..d850250 100644 --- a/src/VoidNet_LL/Headers.cpp +++ b/src/VoidNet_LL/Headers.cpp @@ -1,21 +1,21 @@ // https://github.com/mfichman/http -#include "VoidNet/Headers.hpp" +#include "VoidNet_LL/Headers.hpp" namespace std::net { - std::string const Headers::HOST("Host"); - std::string const Headers::CONTENT_LENGTH("Content-Length"); - std::string const Headers::ACCEPT_ENCODING("Accept-Encoding"); - std::string const Headers::CONNECTION("Connection"); + string const Headers::HOST("Host"); + string const Headers::CONTENT_LENGTH("Content-Length"); + string const Headers::ACCEPT_ENCODING("Accept-Encoding"); + string const Headers::CONNECTION("Connection"); - const std::string Headers::operator[](const std::string & name) const + const string Headers::operator[](const string & name) const { auto i = m_header.find(name); return (i == m_header.end()) ? "" : i->second; } - void Headers::AddHeader(std::string const& name, std::string const& value) + void Headers::AddHeader(string const& name, string const& value) { m_header.emplace(name, value); } diff --git a/src/VoidNet_LL/Http.cpp b/src/VoidNet_LL/Http.cpp index 390332b..d567c3a 100644 --- a/src/VoidNet_LL/Http.cpp +++ b/src/VoidNet_LL/Http.cpp @@ -1,13 +1,13 @@ // https://github.com/mfichman/http -#include "VoidNet/Http.hpp" +#include "VoidNet_LL/Http.hpp" #include #include #include -#include "VoidNet/Socket.hpp" -#include "VoidNet/SecureSocket.hpp" +#include "VoidNet_LL/Socket.hpp" +#include "VoidNet_LL/SecureSocket.hpp" #undef DELETE @@ -16,11 +16,11 @@ namespace std::net Response Http::Send(Request const& request) { // Send an HTTP request. Auto-fill the content-length headers. - std::string string = Str(request); + string string = Str(request); uint16_t port = 0; - std::unique_ptr socket; - std::unique_ptr secure_socket; + unique_ptr socket; + unique_ptr secure_socket; bool secure = false; if (request.GetUri().GetScheme() == "https") { @@ -41,15 +41,15 @@ namespace std::net secure ? secure_socket->Connect(IPAddress(request.GetUri().GetHost(), port)) : socket->Connect(IPAddress(request.GetUri().GetHost(), port)); int32_t sent; - secure ? secure_socket->Recv((uint8_t*)string.c_str(), string.size(), sent) : socket->Send((uint8_t*)string.c_str(), string.size(), sent); + secure ? secure_socket->Recv((byte*)string.c_str(), string.size(), sent) : socket->Send((byte*)string.c_str(), string.size(), sent); - std::vector buffer(16384); // 16 KiB - std::stringstream ss; + vector buffer(16384); // 16 KiB + stringstream ss; int32_t read; do { - secure ? secure_socket->Recv((uint8_t*)&buffer[0], buffer.size(), read) : socket->Recv((uint8_t*)&buffer[0], buffer.size(), read); + secure ? secure_socket->Recv((byte*)&buffer[0], buffer.size(), read) : socket->Recv((byte*)&buffer[0], buffer.size(), read); ss.write(&buffer[0], read); } while (read > 0); @@ -58,7 +58,7 @@ namespace std::net return Response(ss.str()); } - Response Http::Get(std::string const& path, std::string const& data) + Response Http::Get(string const& path, string const& data) { // Shortcut for simple GET requests Request request; @@ -68,7 +68,7 @@ namespace std::net return Send(request); } - Response Http::Post(std::string const& path, std::string const& data) + Response Http::Post(string const& path, string const& data) { // Shortcut for simple POST requests Request request; @@ -78,7 +78,7 @@ namespace std::net return Send(request); } - static std::string str_impl(Method method) { + static string str_impl(Method method) { switch (method) { case Method::GET: @@ -101,10 +101,10 @@ namespace std::net return ""; } - std::string Http::Str(Request const& request) + string Http::Str(Request const& request) { // Serialize a request to a string - std::stringstream ss; + stringstream ss; auto path = request.GetPath().empty() ? "/" : request.GetPath(); ss << str_impl(request.GetMethod()) << ' ' << path << " HTTP/1.1\n"; ss << Headers::HOST << ": " << request.GetUri().GetHost() << "\n"; diff --git a/src/VoidNet_LL/IPAddress.cpp b/src/VoidNet_LL/IPAddress.cpp index 5e5dd0c..64adf22 100644 --- a/src/VoidNet_LL/IPAddress.cpp +++ b/src/VoidNet_LL/IPAddress.cpp @@ -1,4 +1,4 @@ -#include "VoidNet/IPAddress.hpp" +#include "VoidNet_LL/IPAddress.hpp" namespace std::net { @@ -7,7 +7,7 @@ namespace std::net const IPAddress IPAddress::LocalHost(127, 0, 0, 1); const IPAddress IPAddress::Broadcast(255, 255, 255, 255); - std::string IPAddress::ToString() const + string IPAddress::ToString() const { in_addr address; address.s_addr = m_address; @@ -15,7 +15,7 @@ namespace std::net return inet_ntoa(address); } - void IPAddress::Resolve(const std::string& address) + void IPAddress::Resolve(const string& address) { m_address = 0; m_valid = false; @@ -43,7 +43,7 @@ namespace std::net else { addrinfo hints; - std::memset(&hints, 0, sizeof(hints)); + memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_INET; addrinfo* result = NULL; if (getaddrinfo(address.c_str(), NULL, &hints, &result) == 0) diff --git a/src/VoidNet_LL/Request.cpp b/src/VoidNet_LL/Request.cpp index b3c97cc..6901feb 100644 --- a/src/VoidNet_LL/Request.cpp +++ b/src/VoidNet_LL/Request.cpp @@ -1,10 +1,10 @@ // https://github.com/mfichman/http -#include "VoidNet/Request.hpp" +#include "VoidNet_LL/Request.hpp" namespace std::net { - const std::string Request::GetHeaderElement(const std::string& name) const + const string Request::GetHeaderElement(const string& name) const { return m_headers[name]; } @@ -19,12 +19,12 @@ namespace std::net m_uri = uri; } - void Request::SetData(const std::string& data) + void Request::SetData(const string& data) { m_data = data; } - void Request::AddHeader(const std::string& name, const std::string& value) + void Request::AddHeader(const string& name, const string& value) { m_headers.AddHeader(name, value); } diff --git a/src/VoidNet_LL/Response.cpp b/src/VoidNet_LL/Response.cpp index 7bd9603..15cadba 100644 --- a/src/VoidNet_LL/Response.cpp +++ b/src/VoidNet_LL/Response.cpp @@ -1,7 +1,7 @@ // https://github.com/mfichman/http -#include "VoidNet/Response.hpp" -#include "VoidNet/Parse.hpp" +#include "VoidNet_LL/Response.hpp" +#include "VoidNet_LL/Parse.hpp" namespace std::net { @@ -13,7 +13,7 @@ namespace std::net auto code = ParseToken(str); - result.value = (HttpStatus)std::atoi(code.value.c_str()); + result.value = (HttpStatus)atoi(code.value.c_str()); result.ch = code.ch; return result; } @@ -30,7 +30,7 @@ namespace std::net auto response = Response(); if (version.value != "HTTP/1.1") - throw std::runtime_error("bad HTTP version"); + throw runtime_error("bad HTTP version"); auto ch = parseCrLf(message.ch).ch; while (*ch != '\0' && *ch != '\r') @@ -60,17 +60,17 @@ namespace std::net return response; } - Response::Response(const std::string& response) + Response::Response(const string& response) { *this = ParseResponse(response.c_str()); } - const std::string Response::GetHeader(const std::string& name) const + const string Response::GetHeader(const string& name) const { return m_headers[name]; } - const Cookie Response::GetCookie(const std::string& name) const + const Cookie Response::GetCookie(const string& name) const { return m_cookies[name]; } @@ -80,12 +80,12 @@ namespace std::net m_status = status; } - void Response::SetData(const std::string& data) + void Response::SetData(const string& data) { m_data = data; } - void Response::SetHeader(const std::string& name, const std::string& value) + void Response::SetHeader(const string& name, const string& value) { m_headers.AddHeader(name, value); } diff --git a/src/VoidNet_LL/SecureSocket.cpp b/src/VoidNet_LL/SecureSocket.cpp index 7a7e7a1..88fa2aa 100644 --- a/src/VoidNet_LL/SecureSocket.cpp +++ b/src/VoidNet_LL/SecureSocket.cpp @@ -1,4 +1,4 @@ -#include "VoidNet/SecureSocket.hpp" +#include "VoidNet_LL/SecureSocket.hpp" #include @@ -7,7 +7,7 @@ namespace inl::net::sockets /*SecureSocket::SecureSocket() : m_context(0), m_conn(0), m_eof(false) { - m_socket = std::make_unique(SocketType::Streaming); + m_socket = make_unique(SocketType::Streaming); // Intitialize the SSL client-side socket SSL_library_init(); @@ -46,7 +46,7 @@ namespace inl::net::sockets return m_socket->HasPendingData(pendingDataSize); } - bool SecureSocket::Send(uint8_t* data, int32_t count, int32_t &sent, int flags) + bool SecureSocket::Send(byte* data, int32_t count, int32_t &sent, int flags) { sent = SSL_write(m_conn, data, count); SendFromBio(); // Write data if available @@ -58,7 +58,7 @@ namespace inl::net::sockets return sent > 0; } - bool SecureSocket::SendRaw(uint8_t * buf, size_t len, int flags) + bool SecureSocket::SendRaw(byte * buf, size_t len, int flags) { int32_t sent; return m_socket->Send(buf, len, sent) && sent == len; @@ -67,7 +67,7 @@ namespace inl::net::sockets bool SecureSocket::SendFromBio(int flags) { - uint8_t buf[4096]; + byte buf[4096]; size_t pending = BIO_ctrl_pending(m_out); if (!pending) return true; @@ -81,7 +81,7 @@ namespace inl::net::sockets bool SecureSocket::RecvToBio(int flags) { - uint8_t buf[4096]; + byte buf[4096]; size_t bytes = m_socket->Recv(buf, sizeof(buf), flags); if (bytes > 0) { @@ -111,7 +111,7 @@ namespace inl::net::sockets assert(!"unexpected error"); } - bool SecureSocket::Recv(uint8_t* data, int32_t count, int32_t &read, int flags) + bool SecureSocket::Recv(byte* data, int32_t count, int32_t &read, int flags) { read = SSL_read(m_conn, data, count); if (read < 0) @@ -123,7 +123,7 @@ namespace inl::net::sockets return read > 0; } - bool SecureSocket::Wait(SocketWaitConditions cond, std::chrono::milliseconds t) const + bool SecureSocket::Wait(SocketWaitConditions cond, chrono::milliseconds t) const { return m_socket->Wait(cond, t); } @@ -143,7 +143,7 @@ namespace inl::net::sockets return m_socket->GetPort(); } - void SecureSocket::UseCertificateFile(std::string const & path) + void SecureSocket::UseCertificateFile(string const & path) { if (!m_context) assert(!"not initialized yet"); @@ -151,7 +151,7 @@ namespace inl::net::sockets throw inl::RuntimeException(); } - void SecureSocket::UsePrivateKeyFile(std::string const & path) + void SecureSocket::UsePrivateKeyFile(string const & path) { if (!m_context) assert(!"not initialized yet"); diff --git a/src/VoidNet_LL/Socket.cpp b/src/VoidNet_LL/Socket.cpp index eb3e02c..b4faa71 100644 --- a/src/VoidNet_LL/Socket.cpp +++ b/src/VoidNet_LL/Socket.cpp @@ -1,19 +1,19 @@ -#include "VoidNet/Socket.hpp" -#include "VoidNet/IPAddress.hpp" +#include "VoidNet_LL/Socket.hpp" +#include "VoidNet_LL/IPAddress.hpp" namespace std::net { void Socket::init() { if (GetSocketType() == SocketType::Unknown) - throw std::invalid_argument("Unknown socket type"); + throw invalid_argument("Unknown socket type"); if (m_socket == INVALID_SOCKET) { m_socket = socket(AF_INET, (int)GetSocketType(), 0); if (m_socket == INVALID_SOCKET) - throw std::runtime_error("Couldnt create socket"); + throw runtime_error("Couldnt create socket"); } if (GetSocketType() == SocketType::Streaming) @@ -62,7 +62,7 @@ namespace std::net return ((Error == SocketErrors::SE_NO_ERROR) || (Error == SocketErrors::SE_EWOULDBLOCK)); } - bool Socket::WaitForPendingConnection(bool& hasPendingConnection, std::chrono::milliseconds t) + bool Socket::WaitForPendingConnection(bool& hasPendingConnection, chrono::milliseconds t) { bool hasSucceeded = false; hasPendingConnection = false; @@ -95,42 +95,42 @@ namespace std::net return false; } - std::unique_ptr Socket::Accept() + unique_ptr Socket::Accept() { SOCKET newSocket = accept(m_socket, nullptr, nullptr); if (newSocket != INVALID_SOCKET) { - return std::make_unique(newSocket, GetSocketType()); + return make_unique(newSocket, GetSocketType()); } return nullptr; } - bool Socket::SendTo(const uint8_t* data, int32_t count, int32_t& sent, const IPAddress& addrDest) + bool Socket::SendTo(const byte* data, int32_t count, int32_t& sent, const IPAddress& addrDest) { sockaddr_in addr = addrDest.ToCAddr(); sent = sendto(m_socket, (const char*)data, count, 0, (sockaddr*)&addr, sizeof(sockaddr_in)); bool result = sent >= 0; if (result) - m_lastActivityTime = std::chrono::system_clock::now().time_since_epoch().count(); + m_lastActivityTime = chrono::system_clock::now().time_since_epoch().count(); return result; } - bool Socket::Send(const uint8_t* data, int32_t count, int32_t& sent) + bool Socket::Send(const byte* data, int32_t count, int32_t& sent) { sent = send(m_socket, (const char*)data, count, 0); bool result = sent != SOCKET_ERROR; if (result) - m_lastActivityTime = std::chrono::system_clock::now().time_since_epoch().count(); + m_lastActivityTime = chrono::system_clock::now().time_since_epoch().count(); return result; } - bool Socket::RecvFrom(uint8_t* data, int32_t size, int32_t& read, IPAddress& srcAddr, SocketReceiveFlags flags) + bool Socket::RecvFrom(byte* data, int32_t size, int32_t& read, IPAddress& srcAddr, SocketReceiveFlags flags) { socklen_t len = sizeof(sockaddr_in); sockaddr_in addr = srcAddr.ToCAddr(); @@ -146,12 +146,12 @@ namespace std::net return false; } - m_lastActivityTime = std::chrono::system_clock::now().time_since_epoch().count(); + m_lastActivityTime = chrono::system_clock::now().time_since_epoch().count(); return true; } - bool Socket::Recv(uint8_t* data, int32_t size, int32_t& read, SocketReceiveFlags flags) + bool Socket::Recv(byte* data, int32_t size, int32_t& read, SocketReceiveFlags flags) { const int translatedFlags = TranslateFlags(flags); read = recv(m_socket, (char*)data, size, translatedFlags); @@ -164,12 +164,12 @@ namespace std::net return false; } - m_lastActivityTime = std::chrono::system_clock::now().time_since_epoch().count(); + m_lastActivityTime = chrono::system_clock::now().time_since_epoch().count(); return true; } - bool Socket::Wait(SocketWaitConditions cond, std::chrono::milliseconds t) + bool Socket::Wait(SocketWaitConditions cond, chrono::milliseconds t) { if ((cond == SocketWaitConditions::WaitForRead) || (cond == SocketWaitConditions::WaitForReadOrWrite)) { @@ -192,7 +192,7 @@ namespace std::net if (HasState(SocketParam::HasError) == SocketReturn::No) { - if (std::chrono::system_clock::now().time_since_epoch().count() - m_lastActivityTime > std::chrono::milliseconds(5).count()) + if (chrono::system_clock::now().time_since_epoch().count() - m_lastActivityTime > chrono::milliseconds(5).count()) { SocketReturn writeState = HasState(SocketParam::CanWrite); SocketReturn readState = HasState(SocketParam::CanRead); @@ -200,7 +200,7 @@ namespace std::net if (writeState == SocketReturn::Yes || readState == SocketReturn::Yes) { currentState = SocketConnectionState::Connected; - m_lastActivityTime = std::chrono::system_clock::now().time_since_epoch().count(); + m_lastActivityTime = chrono::system_clock::now().time_since_epoch().count(); } else if (writeState == SocketReturn::No && readState == SocketReturn::No) currentState = SocketConnectionState::NotConnected; @@ -240,7 +240,7 @@ namespace std::net bool Socket::SetNonBlocking(bool isNonBlocking) { #if PLATFORM_HTML5 // if we have more platforms later (html5, android, ios) later we need to do some changes to networking - throw std::exception("Can't have blocking sockets on HTML5"); + throw runtime_error("Can't have blocking sockets on HTML5"); return false; #else @@ -311,11 +311,11 @@ namespace std::net sockaddr_in addr; socklen_t size = sizeof(sockaddr_in); if (getsockname(m_socket, (sockaddr*)&addr, &size) != 0) - throw std::runtime_error("Invalid port"); + throw runtime_error("Invalid port"); return ntohs(addr.sin_port); } - SocketReturn Socket::HasState(SocketParam state, std::chrono::milliseconds t) + SocketReturn Socket::HasState(SocketParam state, chrono::milliseconds t) { timeval time; time.tv_sec = t.count(); diff --git a/src/VoidNet_LL/TcpClient.cpp b/src/VoidNet_LL/TcpClient.cpp index 7b438ff..bc710db 100644 --- a/src/VoidNet_LL/TcpClient.cpp +++ b/src/VoidNet_LL/TcpClient.cpp @@ -1,11 +1,11 @@ -#include "VoidNet/TcpClient.hpp" -#include "VoidNet/TcpSocketBuilder.hpp" +#include "VoidNet_LL/TcpClient.hpp" +#include "VoidNet_LL/TcpSocketBuilder.hpp" namespace std::net { TcpClient::TcpClient(Socket *soc) { - m_socket = std::unique_ptr(soc); // will this work + m_socket = unique_ptr(soc); // will this work } TcpClient::TcpClient(SocketProtocol protocol) @@ -28,17 +28,17 @@ namespace std::net return m_socket->HasPendingData(pendingDataSize); } - bool TcpClient::Send(const uint8_t* data, int32_t count, int32_t& sent) const + bool TcpClient::Send(const byte* data, int32_t count, int32_t& sent) const { return m_socket->Send(data, count, sent); } - bool TcpClient::Recv(uint8_t* data, int32_t size, int32_t& read, SocketReceiveFlags flags) const + bool TcpClient::Recv(byte* data, int32_t size, int32_t& read, SocketReceiveFlags flags) const { return m_socket->Recv(data, size, read, flags); } - bool TcpClient::Wait(SocketWaitConditions cond, std::chrono::milliseconds t) const + bool TcpClient::Wait(SocketWaitConditions cond, chrono::milliseconds t) const { return m_socket->Wait(cond, t); } diff --git a/src/VoidNet_LL/TcpListener.cpp b/src/VoidNet_LL/TcpListener.cpp index 54c4aba..a049273 100644 --- a/src/VoidNet_LL/TcpListener.cpp +++ b/src/VoidNet_LL/TcpListener.cpp @@ -1,21 +1,21 @@ -#include "VoidNet/TcpListener.hpp" -#include "VoidNet/TcpSocketBuilder.hpp" -#include "VoidNet/Socket.hpp" -#include "VoidNet/TcpClient.hpp" +#include "VoidNet_LL/TcpListener.hpp" +#include "VoidNet_LL/TcpSocketBuilder.hpp" +#include "VoidNet_LL/Socket.hpp" +#include "VoidNet_LL/TcpClient.hpp" namespace std::net { - TcpListener::TcpListener(uint16_t port, std::chrono::milliseconds inSleepTime) + TcpListener::TcpListener(uint16_t port, chrono::milliseconds inSleepTime) : m_port(port) , m_sleepTime(inSleepTime) { m_socket = TcpSocketBuilder().AsNonBlocking().AsReusable().Bind(IPAddress(0, 0, 0, 0, port)).Listening().Build(); } - TcpListener::TcpListener(Socket *InSocket, std::chrono::milliseconds inSleepTime) + TcpListener::TcpListener(Socket *InSocket, chrono::milliseconds inSleepTime) : m_sleepTime(inSleepTime) { - m_socket = std::unique_ptr(InSocket); + m_socket = unique_ptr(InSocket); } TcpClient *TcpListener::AcceptClient() @@ -26,9 +26,9 @@ namespace std::net if (m_socket == nullptr) return nullptr; - std::string remoteAddress; + string remoteAddress; - const bool hasZeroSleepTime = (m_sleepTime == std::chrono::milliseconds(0)); + const bool hasZeroSleepTime = (m_sleepTime == chrono::milliseconds(0)); bool pending = false; @@ -36,7 +36,7 @@ namespace std::net { if (pending) { - std::unique_ptr connectionSocket = m_socket->Accept(); + unique_ptr connectionSocket = m_socket->Accept(); if (connectionSocket != nullptr) { @@ -45,7 +45,7 @@ namespace std::net } } else - std::this_thread::sleep_for(std::chrono::milliseconds(m_sleepTime)); + this_thread::sleep_for(chrono::milliseconds(m_sleepTime)); return nullptr; } diff --git a/src/VoidNet_LL/TcpSocketBuilder.cpp b/src/VoidNet_LL/TcpSocketBuilder.cpp index dfa417b..b69c1cb 100644 --- a/src/VoidNet_LL/TcpSocketBuilder.cpp +++ b/src/VoidNet_LL/TcpSocketBuilder.cpp @@ -1,14 +1,14 @@ -#include "VoidNet/TcpSocketBuilder.hpp" +#include "VoidNet_LL/TcpSocketBuilder.hpp" -#include "VoidNet/Socket.hpp" -#include "VoidNet/TcpClient.hpp" -#include "VoidNet/TcpListener.hpp" +#include "VoidNet_LL/Socket.hpp" +#include "VoidNet_LL/TcpClient.hpp" +#include "VoidNet_LL/TcpListener.hpp" namespace std::net { - std::unique_ptr TcpSocketBuilder::Build() const + unique_ptr TcpSocketBuilder::Build() const { - std::unique_ptr socket = std::make_unique(SocketType::Streaming, m_socketProtocol); + unique_ptr socket = make_unique(SocketType::Streaming, m_socketProtocol); if (socket != nullptr) { @@ -32,21 +32,19 @@ namespace std::net } if (Error) - throw std::runtime_error("Couldnt create socket"); // make parameter a string depending on the error + throw runtime_error("Couldnt create socket"); // make parameter a string depending on the error } return socket; } - std::unique_ptr TcpSocketBuilder::BuildClient() const + unique_ptr TcpSocketBuilder::BuildClient() const { - std::unique_ptr socket = Build(); - return std::make_unique(socket.release()); + return std::make_unique(Build().release()); } - std::unique_ptr TcpSocketBuilder::BuildListener() const + unique_ptr TcpSocketBuilder::BuildListener() const { - std::unique_ptr socket = Build(); - return std::make_unique(socket.release()); + return std::make_unique(Build().release()); } } \ No newline at end of file diff --git a/src/VoidNet_LL/UdpSocket.cpp b/src/VoidNet_LL/UdpSocket.cpp index 468c266..0fde8b6 100644 --- a/src/VoidNet_LL/UdpSocket.cpp +++ b/src/VoidNet_LL/UdpSocket.cpp @@ -1,15 +1,15 @@ -#include "VoidNet/UdpSocket.hpp" +#include "VoidNet_LL/UdpSocket.hpp" namespace std::net { UdpSocket::UdpSocket(Socket * soc) { - m_socket = std::unique_ptr(soc); // will this work + m_socket = unique_ptr(soc); // will this work } UdpSocket::UdpSocket(SocketProtocol protocol) { - m_socket = std::make_unique(SocketType::Datagram, protocol); + m_socket = make_unique(SocketType::Datagram, protocol); } bool UdpSocket::Bind(const IPAddress & addr) @@ -17,12 +17,12 @@ namespace std::net return m_socket->Bind(addr); } - bool UdpSocket::SendTo(const uint8_t * data, int32_t count, int32_t & sent, const IPAddress & addrDest) + bool UdpSocket::SendTo(const byte * data, int32_t count, int32_t & sent, const IPAddress & addrDest) { return m_socket->SendTo(data, count, sent, addrDest); } - bool UdpSocket::RecvFrom(uint8_t * data, int32_t size, int32_t & read, IPAddress & srcAddr, SocketReceiveFlags flags) + bool UdpSocket::RecvFrom(byte * data, int32_t size, int32_t & read, IPAddress & srcAddr, SocketReceiveFlags flags) { return m_socket->RecvFrom(data, size, read, srcAddr, flags); } @@ -47,7 +47,7 @@ namespace std::net return m_socket->SetMulticastLoopback(loopback); } - bool UdpSocket::SetMulticastTtl(uint8_t timeToLive) + bool UdpSocket::SetMulticastTtl(byte timeToLive) { return m_socket->SetMulticastTtl(timeToLive); } diff --git a/src/VoidNet_LL/Uri.cpp b/src/VoidNet_LL/Uri.cpp index 743798e..71c4976 100644 --- a/src/VoidNet_LL/Uri.cpp +++ b/src/VoidNet_LL/Uri.cpp @@ -1,7 +1,7 @@ // https://github.com/mfichman/http -#include "VoidNet/Uri.hpp" -#include "VoidNet/Parse.hpp" +#include "VoidNet_LL/Uri.hpp" +#include "VoidNet_LL/Parse.hpp" namespace std::net { @@ -20,7 +20,7 @@ namespace std::net } } - static ParseResult ParseScheme(char const* str) + static ParseResult ParseScheme(char const* str) { auto result = ParseWhile(str, [](char ch) { @@ -31,7 +31,7 @@ namespace std::net return result; } - static ParseResult ParseUser(char const* str) + static ParseResult ParseUser(char const* str) { auto result = ParseWhile(str, [](char ch) { @@ -47,7 +47,7 @@ namespace std::net return result; } - static ParseResult ParseHost(char const* str) + static ParseResult ParseHost(char const* str) { return ParseWhile(str, [](char ch) { @@ -99,10 +99,10 @@ namespace std::net return result; } - static ParseResult ParsePath(char const* str) + static ParseResult ParsePath(char const* str) { // Return query/frag as part of path for now - ParseResult result = ParseWhile(str, [](char ch) + ParseResult result = ParseWhile(str, [](char ch) { return true; }); @@ -131,7 +131,7 @@ namespace std::net } - Authority::Authority(std::string const& user, std::string const& host, uint16_t port) + Authority::Authority(string const& user, string const& host, uint16_t port) { m_user = user; m_host = host; @@ -143,12 +143,12 @@ namespace std::net m_port = 0; } - void Authority::SetUser(std::string const& user) + void Authority::SetUser(string const& user) { m_user = user; } - void Authority::SetHost(std::string const& host) + void Authority::SetHost(string const& host) { m_host = host; } @@ -163,7 +163,7 @@ namespace std::net *this = parseUri(value); } - Uri::Uri(const std::string& value) + Uri::Uri(const string& value) { *this = parseUri(value.c_str()); } @@ -171,7 +171,7 @@ namespace std::net Uri::Uri() { } - void Uri::SetScheme(const std::string& scheme) + void Uri::SetScheme(const string& scheme) { m_scheme = scheme; } @@ -181,7 +181,7 @@ namespace std::net m_authority = authority; } - void Uri::SetPath(const std::string& path) + void Uri::SetPath(const string& path) { m_path = path; } diff --git a/src/VoidNet_LL/Util.cpp b/src/VoidNet_LL/Util.cpp index 516ae95..65e92f1 100644 --- a/src/VoidNet_LL/Util.cpp +++ b/src/VoidNet_LL/Util.cpp @@ -1,29 +1,32 @@ -#include "VoidNet/Util.hpp" +#include "VoidNet_LL/Util.hpp" -std::vector std::net::Split(const std::string& str, const std::string& delimiter) +namespace std::net { - std::vector splited; - if (str.empty() && delimiter.empty()) - return std::vector(); - std::string::size_type lastPos = str.find_first_not_of(delimiter, 0); - std::string::size_type pos = str.find_first_of(delimiter, lastPos); - - while (std::string::npos != pos || std::string::npos != lastPos) + vector Split(const string& str, const string& delimiter) { - splited.push_back(str.substr(lastPos, pos - lastPos)); - lastPos = str.find_first_not_of(delimiter, pos); - pos = str.find_first_of(delimiter, lastPos); + vector splited; + if (str.empty() && delimiter.empty()) + return vector(); + string::size_type lastPos = str.find_first_not_of(delimiter, 0); + string::size_type pos = str.find_first_of(delimiter, lastPos); + + while (string::npos != pos || string::npos != lastPos) + { + splited.push_back(str.substr(lastPos, pos - lastPos)); + lastPos = str.find_first_not_of(delimiter, pos); + pos = str.find_first_of(delimiter, lastPos); + } + return splited; } - return splited; -} -sockaddr_in std::net::CreateAddress(uint32_t address, uint16_t port) -{ - sockaddr_in addr; - std::memset(&addr, 0, sizeof(addr)); - addr.sin_addr.s_addr = htonl(address); - addr.sin_family = AF_INET; - addr.sin_port = htons(port); + sockaddr_in CreateAddress(uint32_t address, uint16_t port) + { + sockaddr_in addr; + memset(&addr, 0, sizeof(addr)); + addr.sin_addr.s_addr = htonl(address); + addr.sin_family = AF_INET; + addr.sin_port = htons(port); - return addr; -} + return addr; + } +} \ No newline at end of file