Update
Integrated std::byte added to C++17 recently
This commit is contained in:
@ -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");
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
|
||||
@ -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
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <HLAPI/NetworkMessage.hpp>
|
||||
#include <VoidNet_HL/NetworkMessage.hpp>
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
|
||||
@ -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;
|
||||
};
|
||||
}
|
||||
@ -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;
|
||||
};
|
||||
}
|
||||
@ -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;
|
||||
};
|
||||
}
|
||||
@ -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;
|
||||
};
|
||||
|
||||
@ -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;
|
||||
};
|
||||
}
|
||||
@ -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;
|
||||
};
|
||||
}
|
||||
@ -1,7 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#undef DELETE
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
enum class SocketParam
|
||||
|
||||
@ -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;
|
||||
};
|
||||
}
|
||||
@ -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);
|
||||
};
|
||||
}
|
||||
@ -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(); }
|
||||
}
|
||||
@ -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;
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
@ -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;
|
||||
};
|
||||
}
|
||||
@ -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;
|
||||
};
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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:
|
||||
|
||||
@ -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;
|
||||
};
|
||||
}
|
||||
@ -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;
|
||||
};
|
||||
}
|
||||
@ -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;
|
||||
|
||||
@ -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;
|
||||
};
|
||||
}
|
||||
@ -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;
|
||||
|
||||
@ -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;
|
||||
};
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
Reference in New Issue
Block a user