Integrated std::byte added to C++17 recently
This commit is contained in:
TheDoctor
2019-10-15 21:03:24 +01:00
parent 8fbda17074
commit 5ab59a6fb8
49 changed files with 424 additions and 427 deletions

View File

@ -4,8 +4,7 @@
// now ive hit a wall
// casting vs bitshifting
#include <cstdint>
#include <cstring>
#include "VoidNet_LL/Net.hpp"
namespace std
{
@ -13,15 +12,15 @@ namespace std
{
public:
template<typename T>
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<typename T>
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");

View File

@ -1,6 +1,6 @@
#pragma once
#include <cstdint>
namespace std::net
{

View File

@ -1,10 +1,8 @@
#pragma once
#include "HLAPI/NetworkHeader.hpp"
#include "HLAPI/ByteConverter.hpp"
#include <cstdint>
#include <cstring>
#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<typename T>
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<uint32_t>(m_senderID); // 4
uint8_t *destination = ByteConverter::ToBytes<uint32_t>(m_destinationID); // 4
uint8_t *tag = ByteConverter::ToBytes<uint32_t>(m_tag); // 4
byte *sender = ByteConverter::ToBytes<uint32_t>(m_senderID); // 4
byte *destination = ByteConverter::ToBytes<uint32_t>(m_destinationID); // 4
byte *tag = ByteConverter::ToBytes<uint32_t>(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<typename T>
T *GetData() const

View File

@ -1,6 +1,6 @@
#pragma once
#include <HLAPI/NetworkMessage.hpp>
#include <VoidNet_HL/NetworkMessage.hpp>
namespace std::net
{

View File

@ -2,7 +2,7 @@
#include <vector>
#include "HLAPI/Plugin/Plugin.hpp"
#include "VoidNet_HL/Plugin/Plugin.hpp"
namespace std::net
{
@ -24,6 +24,6 @@ namespace std::net
}
private:
std::vector<Plugin*> m_plugins = std::vector<Plugin*>();
vector<Plugin*> m_plugins;
};
}

View File

@ -1,7 +1,7 @@
#pragma once
#include <VoidNet/Net.hpp>
#include "HLAPI/ServerConfig.hpp"
#include <VoidNet_LL/Net.hpp>
#include "VoidNet_HL/ServerConfig.hpp"
#include <memory>
@ -24,6 +24,6 @@ namespace std::net
ServerConfig Config;
private:
std::shared_ptr<std::net::TcpServer> m_tcpServer;
shared_ptr<TcpServer> m_tcpServer;
};
}

View File

@ -2,9 +2,9 @@
#include <functional>
#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<TcpClient> GetClient();
shared_ptr<TcpClient> GetClient();
uint32_t GetID();
bool Connect(IPAddress addr);
@ -30,15 +30,15 @@ namespace std::net
void ReceiveData();
std::function<void(uint32_t, DistributionMode, uint32_t, uint32_t, void*)> DataReceivedEvent;
std::function<void(std::string)> DisconnectedEvent;
std::function<void(uint32_t, void*)> NewConnectionEvent;
std::function<void()> OnConnectionEvent;
function<void(uint32_t, DistributionMode, uint32_t, uint32_t, void*)> DataReceivedEvent;
function<void(string)> DisconnectedEvent;
function<void(uint32_t, void*)> NewConnectionEvent;
function<void()> OnConnectionEvent;
private:
bool sendMessage(const NetworkMessage &msg);
std::shared_ptr<TcpClient> m_client;
shared_ptr<TcpClient> m_client;
uint32_t m_id;
};
}

View File

@ -1,17 +1,15 @@
#pragma once
#include <VoidNet_LL/TcpListener.hpp>
#include <VoidNet_HL/TcpConnection.hpp>
#include <VoidNet_HL/Plugin/PluginManager.hpp>
#include <VoidNet_HL/ServerConfig.hpp>
#include <mutex>
#include <atomic>
#include <queue>
#include <VoidNet/TcpListener.hpp>
#include <HLAPI/TcpConnection.hpp>
#include <HLAPI/Plugin/PluginManager.hpp>
#include <HLAPI/ServerConfig.hpp>
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<TcpListener> listener_ptr);
TcpConnectionHandler(shared_ptr<TcpListener> listener_ptr);
~TcpConnectionHandler();
void Start();
void Stop();
void AddClient(std::shared_ptr<TcpConnection> &c);
void AddClient(shared_ptr<TcpConnection> &c);
void SetMaxConnections(uint32_t max_connections);
void HandlePluginMessage(const NetworkMessage& message);
@ -43,23 +41,21 @@ namespace std::net
void HandleReceiveMsgAndConnsThreaded();
private:
std::vector<std::shared_ptr<TcpConnection>> m_list;
std::mutex m_listMutex;
vector<shared_ptr<TcpConnection>> 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<MessageQueue> m_queue;
shared_ptr<TcpListener> m_listenerPtr;
std::shared_ptr<TcpListener> m_listenerPtr;
shared_ptr<PluginManager> m_pluginManager;
std::shared_ptr<PluginManager> m_pluginManager;
std::vector<pollfd> m_pollFds;
vector<pollfd> m_pollFds;
ServerConfig m_config;
};

View File

@ -2,7 +2,7 @@
#include <atomic>
#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<TcpListener> listener;
shared_ptr<TcpListener> listener;
uint32_t m_maxConnections;
uint16_t m_port;
std::atomic_bool m_run;
atomic_bool m_run;
std::shared_ptr<std::net::TcpConnectionHandler> m_connectionHandler;
shared_ptr<TcpConnectionHandler> m_connectionHandler;
};
}

View File

@ -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<std::string, Cookie>::const_iterator begin() const
map<string, Cookie>::const_iterator begin() const
{
return m_cookie.begin();
}
std::map<std::string, Cookie>::const_iterator end() const
map<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;
static const string HOST;
static const string CONTENT_LENGTH;
static const string ACCEPT_ENCODING;
static const string CONNECTION;
private:
std::map<std::string, Cookie> m_cookie;
map<string, Cookie> m_cookie;
};
}

View File

@ -1,7 +1,5 @@
#pragma once
#undef DELETE
namespace std::net
{
enum class SocketParam

View File

@ -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<std::string, std::string>::const_iterator begin() const
multimap<string, string>::const_iterator begin() const
{
return m_header.begin();
}
std::multimap<std::string, std::string>::const_iterator end() const
multimap<string, string>::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<std::string, std::string> m_header;
multimap<string, string> m_header;
};
}

View File

@ -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);
};
}

View File

@ -5,7 +5,7 @@
#include <ostream>
#include <string>
#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(); }
}

View File

@ -4,9 +4,9 @@
#include <chrono>
#include <memory>
#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<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 unique_ptr<Socket> 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;

View File

@ -1,6 +1,7 @@
#pragma once
#include <stdint.h>
#include <cstdint>
#include <cstddef>
#ifdef _MSC_VER
#include <winsock2.h>
@ -11,6 +12,7 @@
#undef GetPort
#undef max
#undef min
#undef DELETE
#define poll WSAPoll
#define ioctl ioctlsocket

View File

@ -15,41 +15,41 @@ namespace std::net
};
template <typename F>
static ParseResult<std::string> ParseUntil(char const* str, F func)
static ParseResult<string> ParseUntil(char const* str, F func)
{
ParseResult<std::string> result{};
ParseResult<string> 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 <typename F>
static inline ParseResult<std::string> ParseWhile(char const* str, F func)
static inline ParseResult<string> ParseWhile(char const* str, F func)
{
ParseResult<std::string> result{};
ParseResult<string> 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<std::string> ParseToken(char const* str)
static inline ParseResult<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)
static inline ParseResult<string> 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<std::string> ParseWhitespace(char const* str)
static inline ParseResult<string> ParseWhitespace(char const* str)
{
return ParseWhile(str, isspace);
}

View File

@ -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;
};
}

View File

@ -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 <string>
@ -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;
};

View File

@ -1,6 +1,6 @@
#pragma once
#include "VoidNet/Socket.hpp"
#include "VoidNet_LL/Socket.hpp"
//#include <openssl/rand.h>
//#include <openssl/ssl.h>
@ -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<Socket> m_socket;
unique_ptr<Socket> m_socket;
//SSL_CTX* m_context;
//SSL* m_conn;

View File

@ -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<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 unique_ptr<Socket> 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:

View File

@ -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<Socket> m_socket;
unique_ptr<Socket> m_socket;
};
}

View File

@ -3,7 +3,7 @@
#include <thread>
#include <queue>
#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<Socket> m_socket;
chrono::milliseconds m_sleepTime;
unique_ptr<Socket> m_socket;
uint16_t m_port = 0;
};
}

View File

@ -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<Socket> Build() const;
std::unique_ptr<TcpClient> BuildClient() const;
std::unique_ptr<TcpListener> BuildListener() const;
unique_ptr<Socket> Build() const;
unique_ptr<TcpClient> BuildClient() const;
unique_ptr<TcpListener> BuildListener() const;
private:
bool m_blocking;

View File

@ -1,6 +1,6 @@
#pragma once
#include "VoidNet/Socket.hpp"
#include "VoidNet_LL/Socket.hpp"
#include <memory>
@ -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<Socket> m_socket;
unique_ptr<Socket> m_socket;
};
}

View File

@ -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 <vector>
@ -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<UdpSocket> Build() const
unique_ptr<UdpSocket> Build() const
{
std::unique_ptr<Socket> soc = std::make_unique<Socket>(SocketType::Datagram);
unique_ptr<Socket> soc = make_unique<Socket>(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<UdpSocket>();
throw runtime_error("Couldnt create socket"); // make parameter a string depending on the error
return make_unique<UdpSocket>();
}
return std::unique_ptr<UdpSocket>(nullptr);
return unique_ptr<UdpSocket>(nullptr);
}
private:
bool m_blocking;
bool m_bound;
IPAddress m_boundEndpoint;
std::vector<IPAddress> m_joinedGroups;
vector<IPAddress> m_joinedGroups;
bool m_multicastLoopback;
uint8_t m_multicastTtl;
byte m_multicastTtl;
uint32_t m_receiveBufferSize;
bool m_reusable;
uint32_t m_sendBufferSize;

View File

@ -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;
};
}

View File

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

View File

@ -1,5 +1,5 @@
#include "HLAPI/NetworkMessage.hpp"
#include <cassert>
#include "VoidNet_HL/NetworkMessage.hpp"
#include <VoidNet_HL\NetworkHeader.hpp>
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<uint32_t>(m_senderID); // 4
uint8_t *destination = ByteConverter::ToBytes<uint32_t>(m_destinationID); // 4
uint8_t *tag = ByteConverter::ToBytes<uint32_t>(m_tag); // 4
byte *sender = ByteConverter::ToBytes<uint32_t>(m_senderID); // 4
byte *destination = ByteConverter::ToBytes<uint32_t>(m_destinationID); // 4
byte *tag = ByteConverter::ToBytes<uint32_t>(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];

View File

@ -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()
{
}
}

View File

@ -1,9 +1,9 @@
#include "HLAPI/Server.hpp"
#include "VoidNet_HL/Server.hpp"
#include <HLAPI/Plugin/Plugin.hpp>
#include <VoidNet_HL/Plugin/Plugin.hpp>
#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<std::net::TcpServer>(max_connections, port);
m_tcpServer = make_shared<TcpServer>(max_connections, port);
m_tcpServer->m_connectionHandler->m_config = Config;
}

View File

@ -1,6 +1,6 @@
#include "HLAPI/TcpConnection.hpp"
#include "VoidNet_HL/TcpConnection.hpp"
#include "HLAPI/InternalTags.hpp"
#include "VoidNet_HL/InternalTags.hpp"
#include <iostream>
@ -40,7 +40,7 @@ namespace std::net
{
}
std::shared_ptr<TcpClient> TcpConnection::GetClient()
shared_ptr<TcpClient> 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<std::string>()));
DisconnectedEvent(*(message.GetData<string>()));
}
else if (message.GetTag() == (uint32_t)InternalTags::Connect)
{

View File

@ -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 <chrono>
namespace std::net
{
TcpConnectionHandler::TcpConnectionHandler(std::shared_ptr<TcpListener> listener_ptr)
TcpConnectionHandler::TcpConnectionHandler(shared_ptr<TcpListener> 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<TcpConnection> &c)
void TcpConnectionHandler::AddClient(shared_ptr<TcpConnection> &c)
{
uint32_t id = GetAvailableID();
if (id == -1)
{
std::shared_ptr<TcpClient> client = c->GetClient();
std::string reason("Server is full");
shared_ptr<TcpClient> 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<TcpConnection> connection = std::make_shared<TcpConnection>(c);
shared_ptr<TcpConnection> connection = make_shared<TcpConnection>(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<TcpConnection> c = m_list.at(i);
shared_ptr<TcpConnection> 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<TcpConnection> c = m_list.at(i);
shared_ptr<TcpConnection> 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<TcpConnection> c = m_list.at(i);
shared_ptr<TcpConnection> 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<TcpConnection> c = m_list.at(i);
shared_ptr<TcpConnection> 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<TcpConnection> c = m_list.at(i);
shared_ptr<TcpConnection> c = m_list.at(i);
if (!c->sendMessage(msg))
{

View File

@ -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<TcpListener>(TcpSocketBuilder().AsReusable().Bind(IPAddress(0, 0, 0, 0, port)).Listening().BuildListener().release());
m_connectionHandler = std::make_shared<std::net::TcpConnectionHandler>(listener);
listener = shared_ptr<TcpListener>(TcpSocketBuilder().AsReusable().Bind(IPAddress(0, 0, 0, 0, port)).Listening().BuildListener().release());
m_connectionHandler = make_shared<TcpConnectionHandler>(listener);
m_connectionHandler->SetMaxConnections(max_connections);
}

View File

@ -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 <iostream>
#include <HLAPI\TcpConnection.hpp>
#include <VoidNet_HL\TcpConnection.hpp>
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();

View File

@ -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 <cassert>
namespace std::net
{
ParseResult<std::string> ParseName(const char* str)
ParseResult<string> ParseName(const char* str)
{
return ParseUntil(str, [](char ch)
{
@ -15,7 +15,7 @@ namespace std::net
});
}
ParseResult<std::string> ParseValue(const char* str)
ParseResult<string> ParseValue(const char* str)
{
return ParseUntil(str, [](char ch)
{
@ -23,7 +23,7 @@ namespace std::net
});
}
ParseResult<std::string> ParseSeparator(const char* str)
ParseResult<string> ParseSeparator(const char* str)
{
if (*str)
{
@ -32,7 +32,7 @@ namespace std::net
}
else
{
auto result = ParseResult<std::string>{};
auto result = ParseResult<string>{};
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;

View File

@ -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);
}

View File

@ -1,13 +1,13 @@
// https://github.com/mfichman/http
#include "VoidNet/Http.hpp"
#include "VoidNet_LL/Http.hpp"
#include <cassert>
#include <vector>
#include <sstream>
#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> socket;
std::unique_ptr<SecureSocket> secure_socket;
unique_ptr<Socket> socket;
unique_ptr<SecureSocket> 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<char> buffer(16384); // 16 KiB
std::stringstream ss;
vector<char> 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";

View File

@ -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)

View File

@ -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);
}

View File

@ -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);
}

View File

@ -1,4 +1,4 @@
#include "VoidNet/SecureSocket.hpp"
#include "VoidNet_LL/SecureSocket.hpp"
#include <cassert>
@ -7,7 +7,7 @@ namespace inl::net::sockets
/*SecureSocket::SecureSocket()
: m_context(0), m_conn(0), m_eof(false)
{
m_socket = std::make_unique<Socket>(SocketType::Streaming);
m_socket = make_unique<Socket>(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");

View File

@ -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> Socket::Accept()
unique_ptr<Socket> Socket::Accept()
{
SOCKET newSocket = accept(m_socket, nullptr, nullptr);
if (newSocket != INVALID_SOCKET)
{
return std::make_unique<Socket>(newSocket, GetSocketType());
return make_unique<Socket>(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();

View File

@ -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<Socket>(soc); // will this work
m_socket = unique_ptr<Socket>(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);
}

View File

@ -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<Socket>(InSocket);
m_socket = unique_ptr<Socket>(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<Socket> connectionSocket = m_socket->Accept();
unique_ptr<Socket> 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;
}

View File

@ -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<Socket> TcpSocketBuilder::Build() const
unique_ptr<Socket> TcpSocketBuilder::Build() const
{
std::unique_ptr<Socket> socket = std::make_unique<Socket>(SocketType::Streaming, m_socketProtocol);
unique_ptr<Socket> socket = make_unique<Socket>(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<TcpClient> TcpSocketBuilder::BuildClient() const
unique_ptr<TcpClient> TcpSocketBuilder::BuildClient() const
{
std::unique_ptr<Socket> socket = Build();
return std::make_unique<TcpClient>(socket.release());
return std::make_unique<TcpClient>(Build().release());
}
std::unique_ptr<TcpListener> TcpSocketBuilder::BuildListener() const
unique_ptr<TcpListener> TcpSocketBuilder::BuildListener() const
{
std::unique_ptr<Socket> socket = Build();
return std::make_unique<TcpListener>(socket.release());
return std::make_unique<TcpListener>(Build().release());
}
}

View File

@ -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<Socket>(soc); // will this work
m_socket = unique_ptr<Socket>(soc); // will this work
}
UdpSocket::UdpSocket(SocketProtocol protocol)
{
m_socket = std::make_unique<Socket>(SocketType::Datagram, protocol);
m_socket = make_unique<Socket>(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);
}

View File

@ -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<std::string> ParseScheme(char const* str)
static ParseResult<string> ParseScheme(char const* str)
{
auto result = ParseWhile(str, [](char ch)
{
@ -31,7 +31,7 @@ namespace std::net
return result;
}
static ParseResult<std::string> ParseUser(char const* str)
static ParseResult<string> ParseUser(char const* str)
{
auto result = ParseWhile(str, [](char ch)
{
@ -47,7 +47,7 @@ namespace std::net
return result;
}
static ParseResult<std::string> ParseHost(char const* str)
static ParseResult<string> ParseHost(char const* str)
{
return ParseWhile(str, [](char ch)
{
@ -99,10 +99,10 @@ namespace std::net
return result;
}
static ParseResult<std::string> ParsePath(char const* str)
static ParseResult<string> ParsePath(char const* str)
{
// Return query/frag as part of path for now
ParseResult<std::string> result = ParseWhile(str, [](char ch)
ParseResult<string> 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;
}

View File

@ -1,14 +1,16 @@
#include "VoidNet/Util.hpp"
#include "VoidNet_LL/Util.hpp"
std::vector<std::string> std::net::Split(const std::string& str, const std::string& delimiter)
namespace std::net
{
std::vector<std::string> splited;
vector<string> Split(const string& str, const string& delimiter)
{
vector<string> splited;
if (str.empty() && delimiter.empty())
return std::vector<std::string>();
std::string::size_type lastPos = str.find_first_not_of(delimiter, 0);
std::string::size_type pos = str.find_first_of(delimiter, lastPos);
return vector<string>();
string::size_type lastPos = str.find_first_not_of(delimiter, 0);
string::size_type pos = str.find_first_of(delimiter, lastPos);
while (std::string::npos != pos || std::string::npos != lastPos)
while (string::npos != pos || string::npos != lastPos)
{
splited.push_back(str.substr(lastPos, pos - lastPos));
lastPos = str.find_first_not_of(delimiter, pos);
@ -17,13 +19,14 @@ std::vector<std::string> std::net::Split(const std::string& str, const std::stri
return splited;
}
sockaddr_in std::net::CreateAddress(uint32_t address, uint16_t port)
sockaddr_in CreateAddress(uint32_t address, uint16_t port)
{
sockaddr_in addr;
std::memset(&addr, 0, sizeof(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;
}
}