Update
Integrated std::byte added to C++17 recently
This commit is contained in:
@ -4,8 +4,7 @@
|
|||||||
// now ive hit a wall
|
// now ive hit a wall
|
||||||
// casting vs bitshifting
|
// casting vs bitshifting
|
||||||
|
|
||||||
#include <cstdint>
|
#include "VoidNet_LL/Net.hpp"
|
||||||
#include <cstring>
|
|
||||||
|
|
||||||
namespace std
|
namespace std
|
||||||
{
|
{
|
||||||
@ -13,15 +12,15 @@ namespace std
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
template<typename T>
|
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));
|
memcpy(data, &value, sizeof(T));
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
inline static T FromBytes(uint8_t *data)
|
inline static T FromBytes(byte *data)
|
||||||
{
|
{
|
||||||
if (!data)
|
if (!data)
|
||||||
throw std::invalid_argument("cant have null parameter -> ByteConverter::FromBytes");
|
throw std::invalid_argument("cant have null parameter -> ByteConverter::FromBytes");
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <cstdint>
|
|
||||||
|
|
||||||
namespace std::net
|
namespace std::net
|
||||||
{
|
{
|
||||||
|
|||||||
@ -1,10 +1,8 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "HLAPI/NetworkHeader.hpp"
|
#include "VoidNet_LL/Net.hpp"
|
||||||
#include "HLAPI/ByteConverter.hpp"
|
#include "VoidNet_HL/NetworkHeader.hpp"
|
||||||
|
#include "VoidNet_HL/ByteConverter.hpp"
|
||||||
#include <cstdint>
|
|
||||||
#include <cstring>
|
|
||||||
|
|
||||||
namespace std::net
|
namespace std::net
|
||||||
{
|
{
|
||||||
@ -54,26 +52,26 @@ namespace std::net
|
|||||||
void SetData(T *data)
|
void SetData(T *data)
|
||||||
{
|
{
|
||||||
m_data = data;
|
m_data = data;
|
||||||
m_dataSize = sizeof(T);
|
m_dataSize = sizeof(*data);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
uint8_t * SerializeData(uint32_t &size) const
|
byte * SerializeData(uint32_t &size) const
|
||||||
{
|
{
|
||||||
int32_t sizeOfNetHeader = sizeof(NetworkHeader);
|
int32_t sizeOfNetHeader = sizeof(NetworkHeader);
|
||||||
|
|
||||||
NetworkHeader header;
|
NetworkHeader header;
|
||||||
header.Size = 13 + sizeOfNetHeader + sizeof(T);
|
header.Size = 13 + sizeOfNetHeader + sizeof(T);
|
||||||
|
|
||||||
uint8_t *bytes = new uint8_t[header.Size]();
|
byte *bytes = new byte[header.Size]();
|
||||||
memcpy(bytes, &header, sizeOfNetHeader);
|
memcpy(bytes, &header, sizeOfNetHeader);
|
||||||
|
|
||||||
uint8_t *sender = ByteConverter::ToBytes<uint32_t>(m_senderID); // 4
|
byte *sender = ByteConverter::ToBytes<uint32_t>(m_senderID); // 4
|
||||||
uint8_t *destination = ByteConverter::ToBytes<uint32_t>(m_destinationID); // 4
|
byte *destination = ByteConverter::ToBytes<uint32_t>(m_destinationID); // 4
|
||||||
uint8_t *tag = ByteConverter::ToBytes<uint32_t>(m_tag); // 4
|
byte *tag = ByteConverter::ToBytes<uint32_t>(m_tag); // 4
|
||||||
|
|
||||||
memcpy(bytes + sizeOfNetHeader, sender, 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 + 5, destination, 4);
|
||||||
memcpy(bytes + sizeOfNetHeader + 9, tag, 4);
|
memcpy(bytes + sizeOfNetHeader + 9, tag, 4);
|
||||||
|
|
||||||
@ -83,9 +81,9 @@ namespace std::net
|
|||||||
return bytes;
|
return bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t *SerializeData(uint32_t &size) const;
|
byte *SerializeData(uint32_t &size) const;
|
||||||
void Deserialize(uint8_t *data, uint32_t size);
|
void Deserialize(byte *data, uint32_t size);
|
||||||
void DeserializeWithoutHeader(uint8_t* data, uint32_t size);
|
void DeserializeWithoutHeader(byte* data, uint32_t size);
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
T *GetData() const
|
T *GetData() const
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <HLAPI/NetworkMessage.hpp>
|
#include <VoidNet_HL/NetworkMessage.hpp>
|
||||||
|
|
||||||
namespace std::net
|
namespace std::net
|
||||||
{
|
{
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "HLAPI/Plugin/Plugin.hpp"
|
#include "VoidNet_HL/Plugin/Plugin.hpp"
|
||||||
|
|
||||||
namespace std::net
|
namespace std::net
|
||||||
{
|
{
|
||||||
@ -24,6 +24,6 @@ namespace std::net
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<Plugin*> m_plugins = std::vector<Plugin*>();
|
vector<Plugin*> m_plugins;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -1,7 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <VoidNet/Net.hpp>
|
#include <VoidNet_LL/Net.hpp>
|
||||||
#include "HLAPI/ServerConfig.hpp"
|
#include "VoidNet_HL/ServerConfig.hpp"
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
@ -24,6 +24,6 @@ namespace std::net
|
|||||||
ServerConfig Config;
|
ServerConfig Config;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::shared_ptr<std::net::TcpServer> m_tcpServer;
|
shared_ptr<TcpServer> m_tcpServer;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -2,9 +2,9 @@
|
|||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
|
||||||
#include "VoidNet/TcpClient.hpp"
|
#include "VoidNet_LL/TcpClient.hpp"
|
||||||
|
|
||||||
#include "HLAPI/NetworkMessage.hpp"
|
#include "VoidNet_HL/NetworkMessage.hpp"
|
||||||
|
|
||||||
namespace std::net
|
namespace std::net
|
||||||
{
|
{
|
||||||
@ -16,7 +16,7 @@ namespace std::net
|
|||||||
TcpConnection();
|
TcpConnection();
|
||||||
TcpConnection(TcpClient *client);
|
TcpConnection(TcpClient *client);
|
||||||
|
|
||||||
std::shared_ptr<TcpClient> GetClient();
|
shared_ptr<TcpClient> GetClient();
|
||||||
uint32_t GetID();
|
uint32_t GetID();
|
||||||
|
|
||||||
bool Connect(IPAddress addr);
|
bool Connect(IPAddress addr);
|
||||||
@ -30,15 +30,15 @@ namespace std::net
|
|||||||
|
|
||||||
void ReceiveData();
|
void ReceiveData();
|
||||||
|
|
||||||
std::function<void(uint32_t, DistributionMode, uint32_t, uint32_t, void*)> DataReceivedEvent;
|
function<void(uint32_t, DistributionMode, uint32_t, uint32_t, void*)> DataReceivedEvent;
|
||||||
std::function<void(std::string)> DisconnectedEvent;
|
function<void(string)> DisconnectedEvent;
|
||||||
std::function<void(uint32_t, void*)> NewConnectionEvent;
|
function<void(uint32_t, void*)> NewConnectionEvent;
|
||||||
std::function<void()> OnConnectionEvent;
|
function<void()> OnConnectionEvent;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool sendMessage(const NetworkMessage &msg);
|
bool sendMessage(const NetworkMessage &msg);
|
||||||
|
|
||||||
std::shared_ptr<TcpClient> m_client;
|
shared_ptr<TcpClient> m_client;
|
||||||
uint32_t m_id;
|
uint32_t m_id;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -1,17 +1,15 @@
|
|||||||
#pragma once
|
#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 <mutex>
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <queue>
|
|
||||||
|
|
||||||
#include <VoidNet/TcpListener.hpp>
|
|
||||||
#include <HLAPI/TcpConnection.hpp>
|
|
||||||
#include <HLAPI/Plugin/PluginManager.hpp>
|
|
||||||
#include <HLAPI/ServerConfig.hpp>
|
|
||||||
|
|
||||||
namespace std::net
|
namespace std::net
|
||||||
{
|
{
|
||||||
class MessageQueue;
|
|
||||||
class TcpConnection;
|
class TcpConnection;
|
||||||
class Server;
|
class Server;
|
||||||
class TcpListener;
|
class TcpListener;
|
||||||
@ -21,15 +19,15 @@ namespace std::net
|
|||||||
{
|
{
|
||||||
class TcpConnectionHandler
|
class TcpConnectionHandler
|
||||||
{
|
{
|
||||||
friend class std::net::Server;
|
friend class Server;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
TcpConnectionHandler(std::shared_ptr<TcpListener> listener_ptr);
|
TcpConnectionHandler(shared_ptr<TcpListener> listener_ptr);
|
||||||
~TcpConnectionHandler();
|
~TcpConnectionHandler();
|
||||||
|
|
||||||
void Start();
|
void Start();
|
||||||
void Stop();
|
void Stop();
|
||||||
void AddClient(std::shared_ptr<TcpConnection> &c);
|
void AddClient(shared_ptr<TcpConnection> &c);
|
||||||
void SetMaxConnections(uint32_t max_connections);
|
void SetMaxConnections(uint32_t max_connections);
|
||||||
|
|
||||||
void HandlePluginMessage(const NetworkMessage& message);
|
void HandlePluginMessage(const NetworkMessage& message);
|
||||||
@ -43,23 +41,21 @@ namespace std::net
|
|||||||
void HandleReceiveMsgAndConnsThreaded();
|
void HandleReceiveMsgAndConnsThreaded();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<std::shared_ptr<TcpConnection>> m_list;
|
vector<shared_ptr<TcpConnection>> m_list;
|
||||||
std::mutex m_listMutex;
|
mutex m_listMutex;
|
||||||
|
|
||||||
uint32_t m_maxConnections = 0;
|
uint32_t m_maxConnections = 0;
|
||||||
|
|
||||||
std::thread m_receiveThread;
|
thread m_receiveThread;
|
||||||
std::thread m_sendThread;
|
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;
|
vector<pollfd> m_pollFds;
|
||||||
|
|
||||||
std::vector<pollfd> m_pollFds;
|
|
||||||
|
|
||||||
ServerConfig m_config;
|
ServerConfig m_config;
|
||||||
};
|
};
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
|
|
||||||
#include "VoidNet/TcpListener.hpp"
|
#include "VoidNet_LL/TcpListener.hpp"
|
||||||
|
|
||||||
namespace std::net
|
namespace std::net
|
||||||
{
|
{
|
||||||
@ -11,7 +11,7 @@ namespace std::net
|
|||||||
|
|
||||||
class TcpServer
|
class TcpServer
|
||||||
{
|
{
|
||||||
friend class std::net::Server;
|
friend class Server;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
TcpServer(uint32_t max_connections, uint16_t port = DEFAULT_SERVER_PORT);
|
TcpServer(uint32_t max_connections, uint16_t port = DEFAULT_SERVER_PORT);
|
||||||
@ -20,12 +20,12 @@ namespace std::net
|
|||||||
void Stop();
|
void Stop();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::shared_ptr<TcpListener> listener;
|
shared_ptr<TcpListener> listener;
|
||||||
uint32_t m_maxConnections;
|
uint32_t m_maxConnections;
|
||||||
uint16_t m_port;
|
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
|
class Cookie
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Cookie(const std::string& text);
|
Cookie(const string& text);
|
||||||
|
|
||||||
Cookie() : m_httpOnly(false), m_secure(false)
|
Cookie() : m_httpOnly(false), m_secure(false)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string& GetName() const
|
const string& GetName() const
|
||||||
{
|
{
|
||||||
return m_name;
|
return m_name;
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string& GetValue() const
|
const string& GetValue() const
|
||||||
{
|
{
|
||||||
return m_value;
|
return m_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string& GetPath() const
|
const string& GetPath() const
|
||||||
{
|
{
|
||||||
return m_path;
|
return m_path;
|
||||||
}
|
}
|
||||||
@ -40,17 +40,17 @@ namespace std::net
|
|||||||
return m_secure;
|
return m_secure;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SetName(const std::string& name)
|
void SetName(const string& name)
|
||||||
{
|
{
|
||||||
m_name = name;
|
m_name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SetValue(const std::string& value)
|
void SetValue(const string& value)
|
||||||
{
|
{
|
||||||
m_value = value;
|
m_value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SetPath(const std::string& path)
|
void SetPath(const string& path)
|
||||||
{
|
{
|
||||||
m_path = path;
|
m_path = path;
|
||||||
}
|
}
|
||||||
@ -66,9 +66,9 @@ namespace std::net
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string m_name;
|
string m_name;
|
||||||
std::string m_value;
|
string m_value;
|
||||||
std::string m_path;
|
string m_path;
|
||||||
bool m_httpOnly;
|
bool m_httpOnly;
|
||||||
bool m_secure;
|
bool m_secure;
|
||||||
};
|
};
|
||||||
@ -76,25 +76,25 @@ namespace std::net
|
|||||||
class Cookies
|
class Cookies
|
||||||
{
|
{
|
||||||
public:
|
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();
|
return m_cookie.begin();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::map<std::string, Cookie>::const_iterator end() const
|
map<string, Cookie>::const_iterator end() const
|
||||||
{
|
{
|
||||||
return m_cookie.end();
|
return m_cookie.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
void SetCookie(Cookie const& cookie);
|
void SetCookie(Cookie const& cookie);
|
||||||
|
|
||||||
static const std::string HOST;
|
static const string HOST;
|
||||||
static const std::string CONTENT_LENGTH;
|
static const string CONTENT_LENGTH;
|
||||||
static const std::string ACCEPT_ENCODING;
|
static const string ACCEPT_ENCODING;
|
||||||
static const std::string CONNECTION;
|
static const string CONNECTION;
|
||||||
private:
|
private:
|
||||||
std::map<std::string, Cookie> m_cookie;
|
map<string, Cookie> m_cookie;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -1,7 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#undef DELETE
|
|
||||||
|
|
||||||
namespace std::net
|
namespace std::net
|
||||||
{
|
{
|
||||||
enum class SocketParam
|
enum class SocketParam
|
||||||
|
|||||||
@ -9,25 +9,25 @@ namespace std::net
|
|||||||
class Headers
|
class Headers
|
||||||
{
|
{
|
||||||
public:
|
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();
|
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();
|
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 string const HOST;
|
||||||
static std::string const CONTENT_LENGTH;
|
static string const CONTENT_LENGTH;
|
||||||
static std::string const ACCEPT_ENCODING;
|
static string const ACCEPT_ENCODING;
|
||||||
static std::string const CONNECTION;
|
static string const CONNECTION;
|
||||||
private:
|
private:
|
||||||
std::multimap<std::string, std::string> m_header;
|
multimap<string, string> m_header;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -2,19 +2,19 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "VoidNet/Response.hpp"
|
#include "VoidNet_LL/Response.hpp"
|
||||||
#include "VoidNet/Request.hpp"
|
#include "VoidNet_LL/Request.hpp"
|
||||||
|
|
||||||
namespace std::net
|
namespace std::net
|
||||||
{
|
{
|
||||||
class Http
|
class Http
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static Response Get(std::string const& path, std::string const& data = "");
|
static Response Get(string const& path, string const& data = "");
|
||||||
static Response Post(std::string const& path, std::string const& data = "");
|
static Response Post(string const& path, string const& data = "");
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static Response Send(Request const& request);
|
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 <ostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "VoidNet/Net.hpp"
|
#include "VoidNet_LL/Net.hpp"
|
||||||
|
|
||||||
namespace std::net
|
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_address(0)
|
||||||
, m_valid(false)
|
, m_valid(false)
|
||||||
, m_port(port)
|
, 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 uint32_t ToInteger() const { return ntohl(m_address); }
|
||||||
inline uint16_t GetPort() const { return m_port; }
|
inline uint16_t GetPort() const { return m_port; }
|
||||||
|
|
||||||
@ -68,7 +68,7 @@ namespace std::net
|
|||||||
inline sockaddr_in ToCAddr() const
|
inline sockaddr_in ToCAddr() const
|
||||||
{
|
{
|
||||||
sockaddr_in addr;
|
sockaddr_in addr;
|
||||||
std::memset(&addr, 0, sizeof(addr));
|
memset(&addr, 0, sizeof(addr));
|
||||||
addr.sin_addr.s_addr = htonl(ToInteger());
|
addr.sin_addr.s_addr = htonl(ToInteger());
|
||||||
addr.sin_family = AF_INET;
|
addr.sin_family = AF_INET;
|
||||||
addr.sin_port = htons(GetPort());
|
addr.sin_port = htons(GetPort());
|
||||||
@ -80,7 +80,7 @@ namespace std::net
|
|||||||
|
|
||||||
friend bool operator <(const IPAddress& left, const IPAddress& right);
|
friend bool operator <(const IPAddress& left, const IPAddress& right);
|
||||||
|
|
||||||
void Resolve(const std::string& address);
|
void Resolve(const string& address);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
uint32_t m_address;
|
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) && !(right < left); }
|
||||||
inline bool operator !=(const IPAddress& left, const IPAddress& right) { return !(left == right); }
|
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 !(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 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;
|
stream >> str;
|
||||||
address = IPAddress(str);
|
address = IPAddress(str);
|
||||||
|
|
||||||
return stream;
|
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 <chrono>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
#include "VoidNet/Net.hpp"
|
#include "VoidNet_LL/Net.hpp"
|
||||||
#include "VoidNet/IPAddress.hpp"
|
#include "VoidNet_LL/IPAddress.hpp"
|
||||||
#include "VoidNet/Enums.hpp"
|
#include "VoidNet_LL/Enums.hpp"
|
||||||
|
|
||||||
namespace std::net
|
namespace std::net
|
||||||
{
|
{
|
||||||
@ -35,14 +35,14 @@ namespace std::net
|
|||||||
virtual bool Bind(const IPAddress &addr) = 0;
|
virtual bool Bind(const IPAddress &addr) = 0;
|
||||||
virtual bool Connect(const IPAddress& addr) = 0;
|
virtual bool Connect(const IPAddress& addr) = 0;
|
||||||
virtual bool Listen() = 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 bool HasPendingData(uint32_t& pendingDataSize) = 0;
|
||||||
virtual std::unique_ptr<Socket> Accept() = 0;
|
virtual unique_ptr<Socket> Accept() = 0;
|
||||||
virtual bool SendTo(const uint8_t* data, int32_t count, int32_t& sent, const IPAddress& addrDest) = 0;
|
virtual bool SendTo(const byte* 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 Send(const byte* 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 RecvFrom(byte* 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 Recv(byte* data, int32_t size, int32_t& read, SocketReceiveFlags flags = SocketReceiveFlags::None) = 0;
|
||||||
virtual bool Wait(SocketWaitConditions cond, std::chrono::milliseconds t) = 0;
|
virtual bool Wait(SocketWaitConditions cond, chrono::milliseconds t) = 0;
|
||||||
virtual SocketConnectionState GetConnectionState() = 0;
|
virtual SocketConnectionState GetConnectionState() = 0;
|
||||||
virtual void GetAddress(IPAddress& outAddr) = 0;
|
virtual void GetAddress(IPAddress& outAddr) = 0;
|
||||||
virtual bool GetPeerAddress(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 JoinMulticastGroup(const IPAddress& addrStr) = 0;
|
||||||
virtual bool LeaveMulticastGroup(const IPAddress& addrStr) = 0;
|
virtual bool LeaveMulticastGroup(const IPAddress& addrStr) = 0;
|
||||||
virtual bool SetMulticastLoopback(bool loopback) = 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 SetReuseAddr(bool allowReuse = true) = 0;
|
||||||
virtual bool SetLinger(bool shouldLinger = true, int32_t t = 0) = 0;
|
virtual bool SetLinger(bool shouldLinger = true, int32_t t = 0) = 0;
|
||||||
virtual bool SetSendBufferSize(int32_t size, int32_t& newSize) = 0;
|
virtual bool SetSendBufferSize(int32_t size, int32_t& newSize) = 0;
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <cstdint>
|
||||||
|
#include <cstddef>
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
#include <winsock2.h>
|
#include <winsock2.h>
|
||||||
@ -11,6 +12,7 @@
|
|||||||
#undef GetPort
|
#undef GetPort
|
||||||
#undef max
|
#undef max
|
||||||
#undef min
|
#undef min
|
||||||
|
#undef DELETE
|
||||||
|
|
||||||
#define poll WSAPoll
|
#define poll WSAPoll
|
||||||
#define ioctl ioctlsocket
|
#define ioctl ioctlsocket
|
||||||
|
|||||||
@ -15,41 +15,41 @@ namespace std::net
|
|||||||
};
|
};
|
||||||
|
|
||||||
template <typename F>
|
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;
|
char const* ch = str;
|
||||||
for (; *ch && !func(*ch); ++ch)
|
for (; *ch && !func(*ch); ++ch)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
result.value = std::string(str, ch - str);
|
result.value = string(str, ch - str);
|
||||||
result.ch = ch;
|
result.ch = ch;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename F>
|
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;
|
char const* ch = str;
|
||||||
for (; *ch && func(*ch); ++ch)
|
for (; *ch && func(*ch); ++ch)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
result.value = std::string(str, ch - str);
|
result.value = string(str, ch - str);
|
||||||
result.ch = ch;
|
result.ch = ch;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline ParseResult<std::string> ParseToken(char const* str)
|
static inline ParseResult<string> ParseToken(char const* str)
|
||||||
{
|
{
|
||||||
auto token = ParseUntil(str, isspace);
|
auto token = ParseUntil(str, isspace);
|
||||||
token.ch = ParseWhile(token.ch, isspace).ch;
|
token.ch = ParseWhile(token.ch, isspace).ch;
|
||||||
return token;
|
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'; });
|
auto cr = ParseUntil(str, [](char ch) { return ch == '\r'; });
|
||||||
if (*cr.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);
|
return ParseWhile(str, isspace);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,9 +2,9 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "VoidNet/Uri.hpp"
|
#include "VoidNet_LL/Uri.hpp"
|
||||||
#include "VoidNet/Headers.hpp"
|
#include "VoidNet_LL/Headers.hpp"
|
||||||
#include "VoidNet/Enums.hpp"
|
#include "VoidNet_LL/Enums.hpp"
|
||||||
|
|
||||||
namespace std::net
|
namespace std::net
|
||||||
{
|
{
|
||||||
@ -21,17 +21,17 @@ namespace std::net
|
|||||||
return m_uri;
|
return m_uri;
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string& GetPath() const
|
const string& GetPath() const
|
||||||
{
|
{
|
||||||
return m_uri.GetPath();
|
return m_uri.GetPath();
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string& GetData() const
|
const string& GetData() const
|
||||||
{
|
{
|
||||||
return m_data;
|
return m_data;
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string GetHeaderElement(const std::string& name) const;
|
const string GetHeaderElement(const string& name) const;
|
||||||
|
|
||||||
const Headers& GetHeaders() const
|
const Headers& GetHeaders() const
|
||||||
{
|
{
|
||||||
@ -40,13 +40,13 @@ namespace std::net
|
|||||||
|
|
||||||
void SetMethod(Method method);
|
void SetMethod(Method method);
|
||||||
void SetUri(const Uri& path);
|
void SetUri(const Uri& path);
|
||||||
void SetData(const std::string& data);
|
void SetData(const string& data);
|
||||||
void AddHeader(const std::string& name, const std::string& value);
|
void AddHeader(const string& name, const string& value);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Method m_method = Method::GET;
|
Method m_method = Method::GET;
|
||||||
Uri m_uri;
|
Uri m_uri;
|
||||||
std::string m_data;
|
string m_data;
|
||||||
Headers m_headers;
|
Headers m_headers;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -2,9 +2,9 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "VoidNet/Headers.hpp"
|
#include "VoidNet_LL/Headers.hpp"
|
||||||
#include "VoidNet/Cookies.hpp"
|
#include "VoidNet_LL/Cookies.hpp"
|
||||||
#include "VoidNet/Enums.hpp"
|
#include "VoidNet_LL/Enums.hpp"
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
@ -13,7 +13,7 @@ namespace std::net
|
|||||||
class Response
|
class Response
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Response(const std::string& text);
|
Response(const string& text);
|
||||||
Response() {};
|
Response() {};
|
||||||
|
|
||||||
HttpStatus GetStatus() const
|
HttpStatus GetStatus() const
|
||||||
@ -21,22 +21,22 @@ namespace std::net
|
|||||||
return m_status;
|
return m_status;
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string& GetData() const
|
const string& GetData() const
|
||||||
{
|
{
|
||||||
return m_data;
|
return m_data;
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string GetHeader(const std::string& name) const;
|
const string GetHeader(const string& name) const;
|
||||||
const Cookie GetCookie(const std::string& name) const;
|
const Cookie GetCookie(const string& name) const;
|
||||||
|
|
||||||
void SetStatus(HttpStatus status);
|
void SetStatus(HttpStatus status);
|
||||||
void SetData(const std::string& data);
|
void SetData(const string& data);
|
||||||
void SetHeader(const std::string& name, const std::string& value);
|
void SetHeader(const string& name, const string& value);
|
||||||
void SetCookie(const Cookie& cookie);
|
void SetCookie(const Cookie& cookie);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
HttpStatus m_status = HttpStatus::INVALID_CODE;
|
HttpStatus m_status = HttpStatus::INVALID_CODE;
|
||||||
std::string m_data;
|
string m_data;
|
||||||
Headers m_headers;
|
Headers m_headers;
|
||||||
Cookies m_cookies;
|
Cookies m_cookies;
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "VoidNet/Socket.hpp"
|
#include "VoidNet_LL/Socket.hpp"
|
||||||
|
|
||||||
//#include <openssl/rand.h>
|
//#include <openssl/rand.h>
|
||||||
//#include <openssl/ssl.h>
|
//#include <openssl/ssl.h>
|
||||||
@ -16,23 +16,23 @@ namespace std::net
|
|||||||
bool Connect(const IPAddress& addr);
|
bool Connect(const IPAddress& addr);
|
||||||
bool Close() const;
|
bool Close() const;
|
||||||
bool HasPendingData(uint32_t& pendingDataSize) 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 Send(byte* 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 Recv(byte* data, int32_t count, int32_t &read, int flags = 0); // Execte 1 read() syscall
|
||||||
bool Wait(SocketWaitConditions cond, std::chrono::milliseconds t) const;
|
bool Wait(SocketWaitConditions cond, chrono::milliseconds t) const;
|
||||||
SocketConnectionState GetConnectionState() const;
|
SocketConnectionState GetConnectionState() const;
|
||||||
void GetAddress(IPAddress& outAddr) const;
|
void GetAddress(IPAddress& outAddr) const;
|
||||||
int32_t GetPort() const;
|
int32_t GetPort() const;
|
||||||
|
|
||||||
void UseCertificateFile(std::string const& path);
|
void UseCertificateFile(string const& path);
|
||||||
void UsePrivateKeyFile(std::string const& path);
|
void UsePrivateKeyFile(string const& path);
|
||||||
|
|
||||||
private:
|
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 SendFromBio(int flags = 0);
|
||||||
bool RecvToBio(int flags = 0);
|
bool RecvToBio(int flags = 0);
|
||||||
void HandleReturn(size_t ret);
|
void HandleReturn(size_t ret);
|
||||||
|
|
||||||
std::unique_ptr<Socket> m_socket;
|
unique_ptr<Socket> m_socket;
|
||||||
|
|
||||||
//SSL_CTX* m_context;
|
//SSL_CTX* m_context;
|
||||||
//SSL* m_conn;
|
//SSL* m_conn;
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "VoidNet/ISocket.hpp"
|
#include "VoidNet_LL/ISocket.hpp"
|
||||||
#include "VoidNet/Enums.hpp"
|
#include "VoidNet_LL/Enums.hpp"
|
||||||
|
|
||||||
namespace std::net
|
namespace std::net
|
||||||
{
|
{
|
||||||
@ -31,14 +31,14 @@ namespace std::net
|
|||||||
virtual bool Bind(const IPAddress &addr) override;
|
virtual bool Bind(const IPAddress &addr) override;
|
||||||
virtual bool Connect(const IPAddress& addr) override;
|
virtual bool Connect(const IPAddress& addr) override;
|
||||||
inline virtual bool Listen() override { return listen(m_socket, SOMAXCONN) == 0; }
|
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 bool HasPendingData(uint32_t& pendingDataSize) override;
|
||||||
virtual std::unique_ptr<Socket> Accept() override;
|
virtual unique_ptr<Socket> Accept() override;
|
||||||
virtual bool SendTo(const uint8_t* data, int32_t count, int32_t& sent, const IPAddress& addrDest) override;
|
virtual bool SendTo(const byte* 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 Send(const byte* 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 RecvFrom(byte* 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 Recv(byte* data, int32_t size, int32_t& read, SocketReceiveFlags flags = SocketReceiveFlags::None) override;
|
||||||
virtual bool Wait(SocketWaitConditions cond, std::chrono::milliseconds t) override;
|
virtual bool Wait(SocketWaitConditions cond, chrono::milliseconds t) override;
|
||||||
virtual SocketConnectionState GetConnectionState() override;
|
virtual SocketConnectionState GetConnectionState() override;
|
||||||
virtual void GetAddress(IPAddress& outAddr) override;
|
virtual void GetAddress(IPAddress& outAddr) override;
|
||||||
virtual bool GetPeerAddress(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 JoinMulticastGroup(const IPAddress& addrStr) override;
|
||||||
virtual bool LeaveMulticastGroup(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 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
|
inline virtual bool SetReuseAddr(bool allowReuse = true) override
|
||||||
{
|
{
|
||||||
int param = allowReuse ? 1 : 0;
|
int param = allowReuse ? 1 : 0;
|
||||||
@ -67,13 +67,13 @@ namespace std::net
|
|||||||
private:
|
private:
|
||||||
void init();
|
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 SocketErrors TranslateErrorCode(int32_t code);
|
||||||
virtual int TranslateFlags(SocketReceiveFlags flags);
|
virtual int TranslateFlags(SocketReceiveFlags flags);
|
||||||
|
|
||||||
inline void UpdateActivity()
|
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:
|
private:
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "VoidNet/Socket.hpp"
|
#include "VoidNet_LL/Socket.hpp"
|
||||||
|
|
||||||
namespace std::net
|
namespace std::net
|
||||||
{
|
{
|
||||||
@ -11,7 +11,7 @@ namespace std::net
|
|||||||
{
|
{
|
||||||
class TcpClient
|
class TcpClient
|
||||||
{
|
{
|
||||||
friend class std::net::TcpConnectionHandler;
|
friend class TcpConnectionHandler;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
TcpClient(Socket *soc);
|
TcpClient(Socket *soc);
|
||||||
@ -20,14 +20,14 @@ namespace std::net
|
|||||||
bool Connect(const IPAddress& addrStr);
|
bool Connect(const IPAddress& addrStr);
|
||||||
bool Close() const;
|
bool Close() const;
|
||||||
bool HasPendingData(uint32_t& pendingDataSize) const;
|
bool HasPendingData(uint32_t& pendingDataSize) const;
|
||||||
bool Send(const uint8_t* data, int32_t count, int32_t& sent) const;
|
bool Send(const byte* 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 Recv(byte* data, int32_t size, int32_t& read, SocketReceiveFlags flags = SocketReceiveFlags::None) const;
|
||||||
bool Wait(SocketWaitConditions cond, std::chrono::milliseconds t) const;
|
bool Wait(SocketWaitConditions cond, chrono::milliseconds t) const;
|
||||||
SocketConnectionState GetConnectionState() const;
|
SocketConnectionState GetConnectionState() const;
|
||||||
void GetAddress(IPAddress& outAddr) const;
|
void GetAddress(IPAddress& outAddr) const;
|
||||||
int32_t GetPort() const;
|
int32_t GetPort() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<Socket> m_socket;
|
unique_ptr<Socket> m_socket;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -3,7 +3,7 @@
|
|||||||
#include <thread>
|
#include <thread>
|
||||||
#include <queue>
|
#include <queue>
|
||||||
|
|
||||||
#include "VoidNet/Socket.hpp"
|
#include "VoidNet_LL/Socket.hpp"
|
||||||
|
|
||||||
namespace std::net
|
namespace std::net
|
||||||
{
|
{
|
||||||
@ -16,17 +16,17 @@ namespace std::net
|
|||||||
|
|
||||||
class TcpListener
|
class TcpListener
|
||||||
{
|
{
|
||||||
friend class std::net::TcpConnectionHandler;
|
friend class TcpConnectionHandler;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
TcpListener(uint16_t port, std::chrono::milliseconds inSleepTime = std::chrono::milliseconds(0));
|
TcpListener(uint16_t port, chrono::milliseconds inSleepTime = chrono::milliseconds(0));
|
||||||
TcpListener(Socket *InSocket, std::chrono::milliseconds inSleepTime = std::chrono::milliseconds(0));
|
TcpListener(Socket *InSocket, chrono::milliseconds inSleepTime = chrono::milliseconds(0));
|
||||||
|
|
||||||
TcpClient *AcceptClient();
|
TcpClient *AcceptClient();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::chrono::milliseconds m_sleepTime;
|
chrono::milliseconds m_sleepTime;
|
||||||
std::unique_ptr<Socket> m_socket;
|
unique_ptr<Socket> m_socket;
|
||||||
uint16_t m_port = 0;
|
uint16_t m_port = 0;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -1,7 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "VoidNet/Enums.hpp"
|
#include "VoidNet_LL/Enums.hpp"
|
||||||
#include "VoidNet/IPAddress.hpp"
|
#include "VoidNet_LL/IPAddress.hpp"
|
||||||
|
|
||||||
namespace std::net
|
namespace std::net
|
||||||
{
|
{
|
||||||
@ -92,9 +92,9 @@ namespace std::net
|
|||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
std::unique_ptr<Socket> Build() const;
|
unique_ptr<Socket> Build() const;
|
||||||
std::unique_ptr<TcpClient> BuildClient() const;
|
unique_ptr<TcpClient> BuildClient() const;
|
||||||
std::unique_ptr<TcpListener> BuildListener() const;
|
unique_ptr<TcpListener> BuildListener() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool m_blocking;
|
bool m_blocking;
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "VoidNet/Socket.hpp"
|
#include "VoidNet_LL/Socket.hpp"
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
@ -13,17 +13,17 @@ namespace std::net
|
|||||||
UdpSocket(SocketProtocol protocol = SocketProtocol::IPv4);
|
UdpSocket(SocketProtocol protocol = SocketProtocol::IPv4);
|
||||||
|
|
||||||
bool Bind(const IPAddress &addr);
|
bool Bind(const IPAddress &addr);
|
||||||
bool SendTo(const uint8_t* data, int32_t count, int32_t& sent, const IPAddress& addrDest);
|
bool SendTo(const byte* 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 RecvFrom(byte* data, int32_t size, int32_t& read, IPAddress& srcAddr, SocketReceiveFlags flags = SocketReceiveFlags::None);
|
||||||
bool GetPeerAddress(IPAddress& outAddr);
|
bool GetPeerAddress(IPAddress& outAddr);
|
||||||
bool JoinMulticastGroup(const IPAddress& addrStr);
|
bool JoinMulticastGroup(const IPAddress& addrStr);
|
||||||
bool LeaveMulticastGroup(const IPAddress& addrStr);
|
bool LeaveMulticastGroup(const IPAddress& addrStr);
|
||||||
bool SetMulticastLoopback(bool loopback);
|
bool SetMulticastLoopback(bool loopback);
|
||||||
bool SetMulticastTtl(uint8_t timeToLive);
|
bool SetMulticastTtl(byte timeToLive);
|
||||||
uint32_t GetPort();
|
uint32_t GetPort();
|
||||||
bool SetReuseAddr(bool allowReuse = true);
|
bool SetReuseAddr(bool allowReuse = true);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<Socket> m_socket;
|
unique_ptr<Socket> m_socket;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -1,7 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "VoidNet/UdpSocket.hpp"
|
#include "VoidNet_LL/UdpSocket.hpp"
|
||||||
#include "VoidNet/IPAddress.hpp"
|
#include "VoidNet_LL/IPAddress.hpp"
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@ -73,7 +73,7 @@ namespace std::net
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
UdpSocketBuilder WithMulticastTtl(uint8_t time_to_live)
|
UdpSocketBuilder WithMulticastTtl(byte time_to_live)
|
||||||
{
|
{
|
||||||
m_multicastTtl = time_to_live;
|
m_multicastTtl = time_to_live;
|
||||||
|
|
||||||
@ -95,9 +95,9 @@ namespace std::net
|
|||||||
}
|
}
|
||||||
|
|
||||||
public:
|
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)
|
if (soc)
|
||||||
{
|
{
|
||||||
@ -132,19 +132,19 @@ namespace std::net
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (Error)
|
if (Error)
|
||||||
throw inl::RuntimeException("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 std::make_unique<UdpSocket>();
|
return make_unique<UdpSocket>();
|
||||||
}
|
}
|
||||||
return std::unique_ptr<UdpSocket>(nullptr);
|
return unique_ptr<UdpSocket>(nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool m_blocking;
|
bool m_blocking;
|
||||||
bool m_bound;
|
bool m_bound;
|
||||||
IPAddress m_boundEndpoint;
|
IPAddress m_boundEndpoint;
|
||||||
std::vector<IPAddress> m_joinedGroups;
|
vector<IPAddress> m_joinedGroups;
|
||||||
bool m_multicastLoopback;
|
bool m_multicastLoopback;
|
||||||
uint8_t m_multicastTtl;
|
byte m_multicastTtl;
|
||||||
uint32_t m_receiveBufferSize;
|
uint32_t m_receiveBufferSize;
|
||||||
bool m_reusable;
|
bool m_reusable;
|
||||||
uint32_t m_sendBufferSize;
|
uint32_t m_sendBufferSize;
|
||||||
|
|||||||
@ -9,15 +9,15 @@ namespace std::net
|
|||||||
class Authority
|
class Authority
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Authority(const std::string& user, const std::string& host, uint16_t port);
|
Authority(const string& user, const string& host, uint16_t port);
|
||||||
Authority();
|
Authority();
|
||||||
|
|
||||||
const std::string& GetUser() const
|
const string& GetUser() const
|
||||||
{
|
{
|
||||||
return m_user;
|
return m_user;
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string& GetHost() const
|
const string& GetHost() const
|
||||||
{
|
{
|
||||||
return m_host;
|
return m_host;
|
||||||
}
|
}
|
||||||
@ -27,22 +27,22 @@ namespace std::net
|
|||||||
return m_port;
|
return m_port;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SetUser(const std::string& user);
|
void SetUser(const string& user);
|
||||||
void SetHost(const std::string& host);
|
void SetHost(const string& host);
|
||||||
void SetPort(uint16_t port);
|
void SetPort(uint16_t port);
|
||||||
private:
|
private:
|
||||||
std::string m_user;
|
string m_user;
|
||||||
std::string m_host;
|
string m_host;
|
||||||
uint16_t m_port;
|
uint16_t m_port;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Uri {
|
class Uri {
|
||||||
public:
|
public:
|
||||||
Uri(const char* value);
|
Uri(const char* value);
|
||||||
Uri(const std::string& value);
|
Uri(const string& value);
|
||||||
Uri();
|
Uri();
|
||||||
|
|
||||||
const std::string& GetScheme() const
|
const string& GetScheme() const
|
||||||
{
|
{
|
||||||
return m_scheme;
|
return m_scheme;
|
||||||
}
|
}
|
||||||
@ -52,12 +52,12 @@ namespace std::net
|
|||||||
return m_authority;
|
return m_authority;
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string& GetPath() const
|
const string& GetPath() const
|
||||||
{
|
{
|
||||||
return m_path;
|
return m_path;
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string& GetHost() const
|
const string& GetHost() const
|
||||||
{
|
{
|
||||||
return m_authority.GetHost();
|
return m_authority.GetHost();
|
||||||
}
|
}
|
||||||
@ -67,12 +67,12 @@ namespace std::net
|
|||||||
return m_authority.GetPort();
|
return m_authority.GetPort();
|
||||||
}
|
}
|
||||||
|
|
||||||
void SetScheme(const std::string& scheme);
|
void SetScheme(const string& scheme);
|
||||||
void SetAuthority(const Authority& authority);
|
void SetAuthority(const Authority& authority);
|
||||||
void SetPath(const std::string& path);
|
void SetPath(const string& path);
|
||||||
private:
|
private:
|
||||||
std::string m_scheme;
|
string m_scheme;
|
||||||
Authority m_authority;
|
Authority m_authority;
|
||||||
std::string m_path;
|
string m_path;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -3,10 +3,10 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
#include "VoidNet/Net.hpp"
|
#include "VoidNet_LL/Net.hpp"
|
||||||
|
|
||||||
namespace std::net
|
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);
|
static sockaddr_in CreateAddress(uint32_t address, uint16_t port);
|
||||||
}
|
}
|
||||||
@ -1,5 +1,5 @@
|
|||||||
#include "HLAPI/NetworkMessage.hpp"
|
#include "VoidNet_HL/NetworkMessage.hpp"
|
||||||
#include <cassert>
|
#include <VoidNet_HL\NetworkHeader.hpp>
|
||||||
|
|
||||||
namespace std::net
|
namespace std::net
|
||||||
{
|
{
|
||||||
@ -23,22 +23,22 @@ namespace std::net
|
|||||||
return m_tag;
|
return m_tag;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t *NetworkMessage::SerializeData(uint32_t &size) const
|
byte *NetworkMessage::SerializeData(uint32_t &size) const
|
||||||
{
|
{
|
||||||
int32_t sizeOfNetHeader = sizeof(NetworkHeader);
|
int32_t sizeOfNetHeader = sizeof(NetworkHeader);
|
||||||
|
|
||||||
NetworkHeader header;
|
NetworkHeader header;
|
||||||
header.Size = 13 + sizeOfNetHeader + m_dataSize;
|
header.Size = 13 + sizeOfNetHeader + m_dataSize;
|
||||||
|
|
||||||
uint8_t *bytes = new uint8_t[header.Size]();
|
byte *bytes = new byte[header.Size]();
|
||||||
memcpy(bytes, &header, sizeOfNetHeader);
|
memcpy(bytes, &header, sizeOfNetHeader);
|
||||||
|
|
||||||
uint8_t *sender = ByteConverter::ToBytes<uint32_t>(m_senderID); // 4
|
byte *sender = ByteConverter::ToBytes<uint32_t>(m_senderID); // 4
|
||||||
uint8_t *destination = ByteConverter::ToBytes<uint32_t>(m_destinationID); // 4
|
byte *destination = ByteConverter::ToBytes<uint32_t>(m_destinationID); // 4
|
||||||
uint8_t *tag = ByteConverter::ToBytes<uint32_t>(m_tag); // 4
|
byte *tag = ByteConverter::ToBytes<uint32_t>(m_tag); // 4
|
||||||
|
|
||||||
memcpy(bytes + sizeOfNetHeader, sender, 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 + 5, destination, 4);
|
||||||
memcpy(bytes + sizeOfNetHeader + 9, tag, 4);
|
memcpy(bytes + sizeOfNetHeader + 9, tag, 4);
|
||||||
|
|
||||||
@ -48,7 +48,7 @@ namespace std::net
|
|||||||
return bytes;
|
return bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
void NetworkMessage::Deserialize(uint8_t *data, uint32_t size)
|
void NetworkMessage::Deserialize(byte *data, uint32_t size)
|
||||||
{
|
{
|
||||||
NetworkHeader header;
|
NetworkHeader header;
|
||||||
memcpy(&(header), data, sizeof(NetworkHeader));
|
memcpy(&(header), data, sizeof(NetworkHeader));
|
||||||
@ -57,7 +57,7 @@ namespace std::net
|
|||||||
|
|
||||||
DeserializeWithoutHeader(data, header.Size);
|
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);
|
memcpy(&(m_senderID), data, 4);
|
||||||
m_distributionMode = (DistributionMode)data[4];
|
m_distributionMode = (DistributionMode)data[4];
|
||||||
|
|||||||
@ -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()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
}
|
||||||
@ -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 "VoidNet_HL/TcpServer.hpp"
|
||||||
#include "HLAPI/TcpConnectionHandler.hpp"
|
#include "VoidNet_HL/TcpConnectionHandler.hpp"
|
||||||
//#include "UdpServer.hpp
|
//#include "UdpServer.hpp
|
||||||
|
|
||||||
namespace std::net
|
namespace std::net
|
||||||
@ -11,7 +11,7 @@ namespace std::net
|
|||||||
Server::Server(uint32_t max_connections, uint16_t port) :
|
Server::Server(uint32_t max_connections, uint16_t port) :
|
||||||
Config(ServerConfig())
|
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;
|
m_tcpServer->m_connectionHandler->m_config = Config;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
#include "HLAPI/TcpConnection.hpp"
|
#include "VoidNet_HL/TcpConnection.hpp"
|
||||||
|
|
||||||
#include "HLAPI/InternalTags.hpp"
|
#include "VoidNet_HL/InternalTags.hpp"
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
@ -40,7 +40,7 @@ namespace std::net
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<TcpClient> TcpConnection::GetClient()
|
shared_ptr<TcpClient> TcpConnection::GetClient()
|
||||||
{
|
{
|
||||||
return m_client;
|
return m_client;
|
||||||
}
|
}
|
||||||
@ -58,7 +58,7 @@ namespace std::net
|
|||||||
bool TcpConnection::sendMessage(const NetworkMessage & msg)
|
bool TcpConnection::sendMessage(const NetworkMessage & msg)
|
||||||
{
|
{
|
||||||
uint32_t size;
|
uint32_t size;
|
||||||
uint8_t *data = msg.SerializeData(size);
|
byte *data = msg.SerializeData(size);
|
||||||
int32_t sent;
|
int32_t sent;
|
||||||
return m_client->Send(data, size, sent);
|
return m_client->Send(data, size, sent);
|
||||||
}
|
}
|
||||||
@ -68,9 +68,9 @@ namespace std::net
|
|||||||
uint32_t data_size;
|
uint32_t data_size;
|
||||||
while (m_client->HasPendingData(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;
|
int32_t read;
|
||||||
m_client->Recv(bytes, data_size, read);
|
m_client->Recv(bytes, data_size, read);
|
||||||
@ -80,7 +80,7 @@ namespace std::net
|
|||||||
if (message.GetTag() == (uint32_t)InternalTags::Disconnect)
|
if (message.GetTag() == (uint32_t)InternalTags::Disconnect)
|
||||||
{
|
{
|
||||||
if (DisconnectedEvent)
|
if (DisconnectedEvent)
|
||||||
DisconnectedEvent(*(message.GetData<std::string>()));
|
DisconnectedEvent(*(message.GetData<string>()));
|
||||||
}
|
}
|
||||||
else if (message.GetTag() == (uint32_t)InternalTags::Connect)
|
else if (message.GetTag() == (uint32_t)InternalTags::Connect)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -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 "VoidNet_HL/NetworkMessage.hpp"
|
||||||
#include "HLAPI/TcpConnection.hpp"
|
#include "VoidNet_HL/TcpConnection.hpp"
|
||||||
#include "VoidNet/TcpListener.hpp"
|
#include "VoidNet_LL/TcpListener.hpp"
|
||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
|
||||||
namespace std::net
|
namespace std::net
|
||||||
{
|
{
|
||||||
TcpConnectionHandler::TcpConnectionHandler(std::shared_ptr<TcpListener> listener_ptr)
|
TcpConnectionHandler::TcpConnectionHandler(shared_ptr<TcpListener> listener_ptr)
|
||||||
: m_run(false)
|
: m_run(false)
|
||||||
, m_listenerPtr(listener_ptr)
|
, m_listenerPtr(listener_ptr)
|
||||||
, m_pluginManager(new PluginManager())
|
, m_pluginManager(new PluginManager())
|
||||||
@ -31,7 +31,7 @@ namespace std::net
|
|||||||
master_fd.events = POLLRDNORM;
|
master_fd.events = POLLRDNORM;
|
||||||
m_pollFds.emplace_back(master_fd);
|
m_pollFds.emplace_back(master_fd);
|
||||||
|
|
||||||
std::thread receive_thread(&TcpConnectionHandler::HandleReceiveMsgAndConnsThreaded, this);
|
thread receive_thread(&TcpConnectionHandler::HandleReceiveMsgAndConnsThreaded, this);
|
||||||
m_receiveThread.swap(receive_thread);
|
m_receiveThread.swap(receive_thread);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,16 +40,16 @@ namespace std::net
|
|||||||
m_run.exchange(false);
|
m_run.exchange(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TcpConnectionHandler::AddClient(std::shared_ptr<TcpConnection> &c)
|
void TcpConnectionHandler::AddClient(shared_ptr<TcpConnection> &c)
|
||||||
{
|
{
|
||||||
uint32_t id = GetAvailableID();
|
uint32_t id = GetAvailableID();
|
||||||
if (id == -1)
|
if (id == -1)
|
||||||
{
|
{
|
||||||
std::shared_ptr<TcpClient> client = c->GetClient();
|
shared_ptr<TcpClient> client = c->GetClient();
|
||||||
std::string reason("Server is full");
|
string reason("Server is full");
|
||||||
NetworkMessage msg(0, DistributionMode::ID, 0, (uint32_t)InternalTags::Disconnect, &reason, sizeof(reason));
|
NetworkMessage msg(0, DistributionMode::ID, 0, (uint32_t)InternalTags::Disconnect, &reason, sizeof(reason));
|
||||||
uint32_t size;
|
uint32_t size;
|
||||||
uint8_t* msgArr = msg.SerializeData(size);
|
byte* msgArr = msg.SerializeData(size);
|
||||||
int32_t sent;
|
int32_t sent;
|
||||||
client->Send(msgArr, size, sent);
|
client->Send(msgArr, size, sent);
|
||||||
client->Close();
|
client->Close();
|
||||||
@ -132,7 +132,7 @@ namespace std::net
|
|||||||
TcpClient *c = m_listenerPtr->AcceptClient();
|
TcpClient *c = m_listenerPtr->AcceptClient();
|
||||||
if (c)
|
if (c)
|
||||||
{
|
{
|
||||||
std::shared_ptr<TcpConnection> connection = std::make_shared<TcpConnection>(c);
|
shared_ptr<TcpConnection> connection = make_shared<TcpConnection>(c);
|
||||||
AddClient(connection);
|
AddClient(connection);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -141,14 +141,14 @@ namespace std::net
|
|||||||
{
|
{
|
||||||
SOCKET c = m_pollFds.at(i).fd;
|
SOCKET c = m_pollFds.at(i).fd;
|
||||||
|
|
||||||
uint8_t* header = new uint8_t[sizeof(NetworkHeader)]();
|
byte* header = new byte[sizeof(NetworkHeader)]();
|
||||||
|
|
||||||
int32_t read;
|
int32_t read;
|
||||||
if ((read = recv(c, (char*)header, sizeof(NetworkHeader), 0)) != sizeof(NetworkHeader))
|
if ((read = recv(c, (char*)header, sizeof(NetworkHeader), 0)) != sizeof(NetworkHeader))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
NetworkHeader net_header(*(NetworkHeader*)(header));
|
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);
|
read = recv(c, (char*)buffer, net_header.Size - 4, 0);
|
||||||
if ((read) == net_header.Size - 4)
|
if ((read) == net_header.Size - 4)
|
||||||
@ -180,7 +180,7 @@ namespace std::net
|
|||||||
m_listMutex.lock();
|
m_listMutex.lock();
|
||||||
for (int i = 0; i < m_list.size(); i++)
|
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->GetID() != msg.GetSenderID())
|
||||||
{
|
{
|
||||||
if (!c->sendMessage(msg))
|
if (!c->sendMessage(msg))
|
||||||
@ -198,7 +198,7 @@ namespace std::net
|
|||||||
m_listMutex.lock();
|
m_listMutex.lock();
|
||||||
for (int i = 0; i < m_list.size(); i++)
|
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->GetID() != msg.GetSenderID())
|
||||||
{
|
{
|
||||||
if (!c->sendMessage(msg))
|
if (!c->sendMessage(msg))
|
||||||
@ -214,7 +214,7 @@ namespace std::net
|
|||||||
m_listMutex.lock();
|
m_listMutex.lock();
|
||||||
for (int i = 0; i < m_list.size(); i++)
|
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->GetID() == msg.GetSenderID())
|
||||||
{
|
{
|
||||||
if (!c->sendMessage(msg))
|
if (!c->sendMessage(msg))
|
||||||
@ -230,7 +230,7 @@ namespace std::net
|
|||||||
m_listMutex.lock();
|
m_listMutex.lock();
|
||||||
for (int i = 0; i < m_list.size(); i++)
|
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))
|
if (!c->sendMessage(msg))
|
||||||
{
|
{
|
||||||
@ -246,7 +246,7 @@ namespace std::net
|
|||||||
m_listMutex.lock();
|
m_listMutex.lock();
|
||||||
for (int i = 0; i < m_list.size(); i++)
|
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))
|
if (!c->sendMessage(msg))
|
||||||
{
|
{
|
||||||
|
|||||||
@ -1,9 +1,9 @@
|
|||||||
#include "HLAPI/TcpServer.hpp"
|
#include "VoidNet_HL/TcpServer.hpp"
|
||||||
|
|
||||||
#include "HLAPI/TcpConnection.hpp"
|
#include "VoidNet_HL/TcpConnection.hpp"
|
||||||
#include "VoidNet/TcpSocketBuilder.hpp"
|
#include "VoidNet_LL/TcpSocketBuilder.hpp"
|
||||||
#include "VoidNet/TcpClient.hpp"
|
#include "VoidNet_LL/TcpClient.hpp"
|
||||||
#include "HLAPI/TcpConnectionHandler.hpp"
|
#include "VoidNet_HL/TcpConnectionHandler.hpp"
|
||||||
|
|
||||||
namespace std::net
|
namespace std::net
|
||||||
{
|
{
|
||||||
@ -13,10 +13,10 @@ namespace std::net
|
|||||||
, m_run(false)
|
, m_run(false)
|
||||||
{
|
{
|
||||||
if (port == 0)
|
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());
|
listener = shared_ptr<TcpListener>(TcpSocketBuilder().AsReusable().Bind(IPAddress(0, 0, 0, 0, port)).Listening().BuildListener().release());
|
||||||
m_connectionHandler = std::make_shared<std::net::TcpConnectionHandler>(listener);
|
m_connectionHandler = make_shared<TcpConnectionHandler>(listener);
|
||||||
m_connectionHandler->SetMaxConnections(max_connections);
|
m_connectionHandler->SetMaxConnections(max_connections);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,13 +1,13 @@
|
|||||||
#include "VoidNet/Init.hpp"
|
#include "VoidNet_LL/Init.hpp"
|
||||||
#include "HLAPI/Server.hpp"
|
#include "VoidNet_HL/Server.hpp"
|
||||||
#include "HLAPI/ByteConverter.hpp"
|
#include "VoidNet_HL/ByteConverter.hpp"
|
||||||
#include "VoidNet/TcpClient.hpp"
|
#include "VoidNet_LL/TcpClient.hpp"
|
||||||
#include "HLAPI/InternalTags.hpp"
|
#include "VoidNet_HL/InternalTags.hpp"
|
||||||
#include "HLAPI/NetworkMessage.hpp"
|
#include "VoidNet_HL/NetworkMessage.hpp"
|
||||||
#include "HLAPI/Plugin/Plugin.hpp"
|
#include "VoidNet_HL/Plugin/Plugin.hpp"
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <HLAPI\TcpConnection.hpp>
|
#include <VoidNet_HL\TcpConnection.hpp>
|
||||||
|
|
||||||
class Plugin : public std::net::Plugin
|
class Plugin : public std::net::Plugin
|
||||||
{
|
{
|
||||||
@ -20,7 +20,7 @@ class Plugin : public std::net::Plugin
|
|||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
std::net::Initialize();
|
std::net::Initialize();
|
||||||
std::net::Server server(0);
|
std::net::Server server(1);
|
||||||
server.AddPlugin(new Plugin());
|
server.AddPlugin(new Plugin());
|
||||||
server.Start();
|
server.Start();
|
||||||
|
|
||||||
|
|||||||
@ -1,13 +1,13 @@
|
|||||||
// https://github.com/mfichman/http
|
// https://github.com/mfichman/http
|
||||||
|
|
||||||
#include "VoidNet/Cookies.hpp"
|
#include "VoidNet_LL/Cookies.hpp"
|
||||||
#include "VoidNet/Parse.hpp"
|
#include "VoidNet_LL/Parse.hpp"
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
|
||||||
namespace std::net
|
namespace std::net
|
||||||
{
|
{
|
||||||
ParseResult<std::string> ParseName(const char* str)
|
ParseResult<string> ParseName(const char* str)
|
||||||
{
|
{
|
||||||
return ParseUntil(str, [](char ch)
|
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)
|
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)
|
if (*str)
|
||||||
{
|
{
|
||||||
@ -32,7 +32,7 @@ namespace std::net
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
auto result = ParseResult<std::string>{};
|
auto result = ParseResult<string>{};
|
||||||
result.ch = str;
|
result.ch = str;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@ -66,12 +66,12 @@ namespace std::net
|
|||||||
return cookie;
|
return cookie;
|
||||||
}
|
}
|
||||||
|
|
||||||
Cookie::Cookie(const std::string& text)
|
Cookie::Cookie(const string& text)
|
||||||
{
|
{
|
||||||
*this = ParseCookie(text.c_str());
|
*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);
|
auto i = m_cookie.find(name);
|
||||||
return (i == m_cookie.end()) ? Cookie() : i->second;
|
return (i == m_cookie.end()) ? Cookie() : i->second;
|
||||||
|
|||||||
@ -1,21 +1,21 @@
|
|||||||
// https://github.com/mfichman/http
|
// https://github.com/mfichman/http
|
||||||
|
|
||||||
#include "VoidNet/Headers.hpp"
|
#include "VoidNet_LL/Headers.hpp"
|
||||||
|
|
||||||
namespace std::net
|
namespace std::net
|
||||||
{
|
{
|
||||||
std::string const Headers::HOST("Host");
|
string const Headers::HOST("Host");
|
||||||
std::string const Headers::CONTENT_LENGTH("Content-Length");
|
string const Headers::CONTENT_LENGTH("Content-Length");
|
||||||
std::string const Headers::ACCEPT_ENCODING("Accept-Encoding");
|
string const Headers::ACCEPT_ENCODING("Accept-Encoding");
|
||||||
std::string const Headers::CONNECTION("Connection");
|
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);
|
auto i = m_header.find(name);
|
||||||
return (i == m_header.end()) ? "" : i->second;
|
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);
|
m_header.emplace(name, value);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,13 +1,13 @@
|
|||||||
// https://github.com/mfichman/http
|
// https://github.com/mfichman/http
|
||||||
|
|
||||||
#include "VoidNet/Http.hpp"
|
#include "VoidNet_LL/Http.hpp"
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
#include "VoidNet/Socket.hpp"
|
#include "VoidNet_LL/Socket.hpp"
|
||||||
#include "VoidNet/SecureSocket.hpp"
|
#include "VoidNet_LL/SecureSocket.hpp"
|
||||||
|
|
||||||
#undef DELETE
|
#undef DELETE
|
||||||
|
|
||||||
@ -16,11 +16,11 @@ namespace std::net
|
|||||||
Response Http::Send(Request const& request)
|
Response Http::Send(Request const& request)
|
||||||
{
|
{
|
||||||
// Send an HTTP request. Auto-fill the content-length headers.
|
// Send an HTTP request. Auto-fill the content-length headers.
|
||||||
std::string string = Str(request);
|
string string = Str(request);
|
||||||
|
|
||||||
uint16_t port = 0;
|
uint16_t port = 0;
|
||||||
std::unique_ptr<Socket> socket;
|
unique_ptr<Socket> socket;
|
||||||
std::unique_ptr<SecureSocket> secure_socket;
|
unique_ptr<SecureSocket> secure_socket;
|
||||||
bool secure = false;
|
bool secure = false;
|
||||||
if (request.GetUri().GetScheme() == "https")
|
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));
|
secure ? secure_socket->Connect(IPAddress(request.GetUri().GetHost(), port)) : socket->Connect(IPAddress(request.GetUri().GetHost(), port));
|
||||||
int32_t sent;
|
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
|
vector<char> buffer(16384); // 16 KiB
|
||||||
std::stringstream ss;
|
stringstream ss;
|
||||||
|
|
||||||
int32_t read;
|
int32_t read;
|
||||||
do
|
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);
|
ss.write(&buffer[0], read);
|
||||||
}
|
}
|
||||||
while (read > 0);
|
while (read > 0);
|
||||||
@ -58,7 +58,7 @@ namespace std::net
|
|||||||
return Response(ss.str());
|
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
|
// Shortcut for simple GET requests
|
||||||
Request request;
|
Request request;
|
||||||
@ -68,7 +68,7 @@ namespace std::net
|
|||||||
return Send(request);
|
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
|
// Shortcut for simple POST requests
|
||||||
Request request;
|
Request request;
|
||||||
@ -78,7 +78,7 @@ namespace std::net
|
|||||||
return Send(request);
|
return Send(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::string str_impl(Method method) {
|
static string str_impl(Method method) {
|
||||||
switch (method)
|
switch (method)
|
||||||
{
|
{
|
||||||
case Method::GET:
|
case Method::GET:
|
||||||
@ -101,10 +101,10 @@ namespace std::net
|
|||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Http::Str(Request const& request)
|
string Http::Str(Request const& request)
|
||||||
{
|
{
|
||||||
// Serialize a request to a string
|
// Serialize a request to a string
|
||||||
std::stringstream ss;
|
stringstream ss;
|
||||||
auto path = request.GetPath().empty() ? "/" : request.GetPath();
|
auto path = request.GetPath().empty() ? "/" : request.GetPath();
|
||||||
ss << str_impl(request.GetMethod()) << ' ' << path << " HTTP/1.1\n";
|
ss << str_impl(request.GetMethod()) << ' ' << path << " HTTP/1.1\n";
|
||||||
ss << Headers::HOST << ": " << request.GetUri().GetHost() << "\n";
|
ss << Headers::HOST << ": " << request.GetUri().GetHost() << "\n";
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
#include "VoidNet/IPAddress.hpp"
|
#include "VoidNet_LL/IPAddress.hpp"
|
||||||
|
|
||||||
namespace std::net
|
namespace std::net
|
||||||
{
|
{
|
||||||
@ -7,7 +7,7 @@ namespace std::net
|
|||||||
const IPAddress IPAddress::LocalHost(127, 0, 0, 1);
|
const IPAddress IPAddress::LocalHost(127, 0, 0, 1);
|
||||||
const IPAddress IPAddress::Broadcast(255, 255, 255, 255);
|
const IPAddress IPAddress::Broadcast(255, 255, 255, 255);
|
||||||
|
|
||||||
std::string IPAddress::ToString() const
|
string IPAddress::ToString() const
|
||||||
{
|
{
|
||||||
in_addr address;
|
in_addr address;
|
||||||
address.s_addr = m_address;
|
address.s_addr = m_address;
|
||||||
@ -15,7 +15,7 @@ namespace std::net
|
|||||||
return inet_ntoa(address);
|
return inet_ntoa(address);
|
||||||
}
|
}
|
||||||
|
|
||||||
void IPAddress::Resolve(const std::string& address)
|
void IPAddress::Resolve(const string& address)
|
||||||
{
|
{
|
||||||
m_address = 0;
|
m_address = 0;
|
||||||
m_valid = false;
|
m_valid = false;
|
||||||
@ -43,7 +43,7 @@ namespace std::net
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
addrinfo hints;
|
addrinfo hints;
|
||||||
std::memset(&hints, 0, sizeof(hints));
|
memset(&hints, 0, sizeof(hints));
|
||||||
hints.ai_family = AF_INET;
|
hints.ai_family = AF_INET;
|
||||||
addrinfo* result = NULL;
|
addrinfo* result = NULL;
|
||||||
if (getaddrinfo(address.c_str(), NULL, &hints, &result) == 0)
|
if (getaddrinfo(address.c_str(), NULL, &hints, &result) == 0)
|
||||||
|
|||||||
@ -1,10 +1,10 @@
|
|||||||
// https://github.com/mfichman/http
|
// https://github.com/mfichman/http
|
||||||
|
|
||||||
#include "VoidNet/Request.hpp"
|
#include "VoidNet_LL/Request.hpp"
|
||||||
|
|
||||||
namespace std::net
|
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];
|
return m_headers[name];
|
||||||
}
|
}
|
||||||
@ -19,12 +19,12 @@ namespace std::net
|
|||||||
m_uri = uri;
|
m_uri = uri;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Request::SetData(const std::string& data)
|
void Request::SetData(const string& data)
|
||||||
{
|
{
|
||||||
m_data = 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);
|
m_headers.AddHeader(name, value);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
// https://github.com/mfichman/http
|
// https://github.com/mfichman/http
|
||||||
|
|
||||||
#include "VoidNet/Response.hpp"
|
#include "VoidNet_LL/Response.hpp"
|
||||||
#include "VoidNet/Parse.hpp"
|
#include "VoidNet_LL/Parse.hpp"
|
||||||
|
|
||||||
namespace std::net
|
namespace std::net
|
||||||
{
|
{
|
||||||
@ -13,7 +13,7 @@ namespace std::net
|
|||||||
|
|
||||||
auto code = ParseToken(str);
|
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;
|
result.ch = code.ch;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@ -30,7 +30,7 @@ namespace std::net
|
|||||||
|
|
||||||
auto response = Response();
|
auto response = Response();
|
||||||
if (version.value != "HTTP/1.1")
|
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;
|
auto ch = parseCrLf(message.ch).ch;
|
||||||
while (*ch != '\0' && *ch != '\r')
|
while (*ch != '\0' && *ch != '\r')
|
||||||
@ -60,17 +60,17 @@ namespace std::net
|
|||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
Response::Response(const std::string& response)
|
Response::Response(const string& response)
|
||||||
{
|
{
|
||||||
*this = ParseResponse(response.c_str());
|
*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];
|
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];
|
return m_cookies[name];
|
||||||
}
|
}
|
||||||
@ -80,12 +80,12 @@ namespace std::net
|
|||||||
m_status = status;
|
m_status = status;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Response::SetData(const std::string& data)
|
void Response::SetData(const string& data)
|
||||||
{
|
{
|
||||||
m_data = 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);
|
m_headers.AddHeader(name, value);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
#include "VoidNet/SecureSocket.hpp"
|
#include "VoidNet_LL/SecureSocket.hpp"
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
|
||||||
@ -7,7 +7,7 @@ namespace inl::net::sockets
|
|||||||
/*SecureSocket::SecureSocket()
|
/*SecureSocket::SecureSocket()
|
||||||
: m_context(0), m_conn(0), m_eof(false)
|
: 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
|
// Intitialize the SSL client-side socket
|
||||||
SSL_library_init();
|
SSL_library_init();
|
||||||
@ -46,7 +46,7 @@ namespace inl::net::sockets
|
|||||||
return m_socket->HasPendingData(pendingDataSize);
|
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);
|
sent = SSL_write(m_conn, data, count);
|
||||||
SendFromBio(); // Write data if available
|
SendFromBio(); // Write data if available
|
||||||
@ -58,7 +58,7 @@ namespace inl::net::sockets
|
|||||||
return sent > 0;
|
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;
|
int32_t sent;
|
||||||
return m_socket->Send(buf, len, sent) && sent == len;
|
return m_socket->Send(buf, len, sent) && sent == len;
|
||||||
@ -67,7 +67,7 @@ namespace inl::net::sockets
|
|||||||
|
|
||||||
bool SecureSocket::SendFromBio(int flags)
|
bool SecureSocket::SendFromBio(int flags)
|
||||||
{
|
{
|
||||||
uint8_t buf[4096];
|
byte buf[4096];
|
||||||
size_t pending = BIO_ctrl_pending(m_out);
|
size_t pending = BIO_ctrl_pending(m_out);
|
||||||
if (!pending)
|
if (!pending)
|
||||||
return true;
|
return true;
|
||||||
@ -81,7 +81,7 @@ namespace inl::net::sockets
|
|||||||
|
|
||||||
bool SecureSocket::RecvToBio(int flags)
|
bool SecureSocket::RecvToBio(int flags)
|
||||||
{
|
{
|
||||||
uint8_t buf[4096];
|
byte buf[4096];
|
||||||
size_t bytes = m_socket->Recv(buf, sizeof(buf), flags);
|
size_t bytes = m_socket->Recv(buf, sizeof(buf), flags);
|
||||||
if (bytes > 0)
|
if (bytes > 0)
|
||||||
{
|
{
|
||||||
@ -111,7 +111,7 @@ namespace inl::net::sockets
|
|||||||
assert(!"unexpected error");
|
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);
|
read = SSL_read(m_conn, data, count);
|
||||||
if (read < 0)
|
if (read < 0)
|
||||||
@ -123,7 +123,7 @@ namespace inl::net::sockets
|
|||||||
return read > 0;
|
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);
|
return m_socket->Wait(cond, t);
|
||||||
}
|
}
|
||||||
@ -143,7 +143,7 @@ namespace inl::net::sockets
|
|||||||
return m_socket->GetPort();
|
return m_socket->GetPort();
|
||||||
}
|
}
|
||||||
|
|
||||||
void SecureSocket::UseCertificateFile(std::string const & path)
|
void SecureSocket::UseCertificateFile(string const & path)
|
||||||
{
|
{
|
||||||
if (!m_context)
|
if (!m_context)
|
||||||
assert(!"not initialized yet");
|
assert(!"not initialized yet");
|
||||||
@ -151,7 +151,7 @@ namespace inl::net::sockets
|
|||||||
throw inl::RuntimeException();
|
throw inl::RuntimeException();
|
||||||
}
|
}
|
||||||
|
|
||||||
void SecureSocket::UsePrivateKeyFile(std::string const & path)
|
void SecureSocket::UsePrivateKeyFile(string const & path)
|
||||||
{
|
{
|
||||||
if (!m_context)
|
if (!m_context)
|
||||||
assert(!"not initialized yet");
|
assert(!"not initialized yet");
|
||||||
|
|||||||
@ -1,19 +1,19 @@
|
|||||||
#include "VoidNet/Socket.hpp"
|
#include "VoidNet_LL/Socket.hpp"
|
||||||
#include "VoidNet/IPAddress.hpp"
|
#include "VoidNet_LL/IPAddress.hpp"
|
||||||
|
|
||||||
namespace std::net
|
namespace std::net
|
||||||
{
|
{
|
||||||
void Socket::init()
|
void Socket::init()
|
||||||
{
|
{
|
||||||
if (GetSocketType() == SocketType::Unknown)
|
if (GetSocketType() == SocketType::Unknown)
|
||||||
throw std::invalid_argument("Unknown socket type");
|
throw invalid_argument("Unknown socket type");
|
||||||
|
|
||||||
if (m_socket == INVALID_SOCKET)
|
if (m_socket == INVALID_SOCKET)
|
||||||
{
|
{
|
||||||
m_socket = socket(AF_INET, (int)GetSocketType(), 0);
|
m_socket = socket(AF_INET, (int)GetSocketType(), 0);
|
||||||
|
|
||||||
if (m_socket == INVALID_SOCKET)
|
if (m_socket == INVALID_SOCKET)
|
||||||
throw std::runtime_error("Couldnt create socket");
|
throw runtime_error("Couldnt create socket");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (GetSocketType() == SocketType::Streaming)
|
if (GetSocketType() == SocketType::Streaming)
|
||||||
@ -62,7 +62,7 @@ namespace std::net
|
|||||||
return ((Error == SocketErrors::SE_NO_ERROR) || (Error == SocketErrors::SE_EWOULDBLOCK));
|
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;
|
bool hasSucceeded = false;
|
||||||
hasPendingConnection = false;
|
hasPendingConnection = false;
|
||||||
@ -95,42 +95,42 @@ namespace std::net
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<Socket> Socket::Accept()
|
unique_ptr<Socket> Socket::Accept()
|
||||||
{
|
{
|
||||||
SOCKET newSocket = accept(m_socket, nullptr, nullptr);
|
SOCKET newSocket = accept(m_socket, nullptr, nullptr);
|
||||||
|
|
||||||
if (newSocket != INVALID_SOCKET)
|
if (newSocket != INVALID_SOCKET)
|
||||||
{
|
{
|
||||||
return std::make_unique<Socket>(newSocket, GetSocketType());
|
return make_unique<Socket>(newSocket, GetSocketType());
|
||||||
}
|
}
|
||||||
|
|
||||||
return nullptr;
|
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();
|
sockaddr_in addr = addrDest.ToCAddr();
|
||||||
sent = sendto(m_socket, (const char*)data, count, 0, (sockaddr*)&addr, sizeof(sockaddr_in));
|
sent = sendto(m_socket, (const char*)data, count, 0, (sockaddr*)&addr, sizeof(sockaddr_in));
|
||||||
|
|
||||||
bool result = sent >= 0;
|
bool result = sent >= 0;
|
||||||
if (result)
|
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;
|
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);
|
sent = send(m_socket, (const char*)data, count, 0);
|
||||||
|
|
||||||
bool result = sent != SOCKET_ERROR;
|
bool result = sent != SOCKET_ERROR;
|
||||||
if (result)
|
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;
|
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);
|
socklen_t len = sizeof(sockaddr_in);
|
||||||
sockaddr_in addr = srcAddr.ToCAddr();
|
sockaddr_in addr = srcAddr.ToCAddr();
|
||||||
@ -146,12 +146,12 @@ namespace std::net
|
|||||||
return false;
|
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;
|
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);
|
const int translatedFlags = TranslateFlags(flags);
|
||||||
read = recv(m_socket, (char*)data, size, translatedFlags);
|
read = recv(m_socket, (char*)data, size, translatedFlags);
|
||||||
@ -164,12 +164,12 @@ namespace std::net
|
|||||||
return false;
|
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;
|
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))
|
if ((cond == SocketWaitConditions::WaitForRead) || (cond == SocketWaitConditions::WaitForReadOrWrite))
|
||||||
{
|
{
|
||||||
@ -192,7 +192,7 @@ namespace std::net
|
|||||||
|
|
||||||
if (HasState(SocketParam::HasError) == SocketReturn::No)
|
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 writeState = HasState(SocketParam::CanWrite);
|
||||||
SocketReturn readState = HasState(SocketParam::CanRead);
|
SocketReturn readState = HasState(SocketParam::CanRead);
|
||||||
@ -200,7 +200,7 @@ namespace std::net
|
|||||||
if (writeState == SocketReturn::Yes || readState == SocketReturn::Yes)
|
if (writeState == SocketReturn::Yes || readState == SocketReturn::Yes)
|
||||||
{
|
{
|
||||||
currentState = SocketConnectionState::Connected;
|
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)
|
else if (writeState == SocketReturn::No && readState == SocketReturn::No)
|
||||||
currentState = SocketConnectionState::NotConnected;
|
currentState = SocketConnectionState::NotConnected;
|
||||||
@ -240,7 +240,7 @@ namespace std::net
|
|||||||
bool Socket::SetNonBlocking(bool isNonBlocking)
|
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
|
#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;
|
return false;
|
||||||
#else
|
#else
|
||||||
|
|
||||||
@ -311,11 +311,11 @@ namespace std::net
|
|||||||
sockaddr_in addr;
|
sockaddr_in addr;
|
||||||
socklen_t size = sizeof(sockaddr_in);
|
socklen_t size = sizeof(sockaddr_in);
|
||||||
if (getsockname(m_socket, (sockaddr*)&addr, &size) != 0)
|
if (getsockname(m_socket, (sockaddr*)&addr, &size) != 0)
|
||||||
throw std::runtime_error("Invalid port");
|
throw runtime_error("Invalid port");
|
||||||
return ntohs(addr.sin_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;
|
timeval time;
|
||||||
time.tv_sec = t.count();
|
time.tv_sec = t.count();
|
||||||
|
|||||||
@ -1,11 +1,11 @@
|
|||||||
#include "VoidNet/TcpClient.hpp"
|
#include "VoidNet_LL/TcpClient.hpp"
|
||||||
#include "VoidNet/TcpSocketBuilder.hpp"
|
#include "VoidNet_LL/TcpSocketBuilder.hpp"
|
||||||
|
|
||||||
namespace std::net
|
namespace std::net
|
||||||
{
|
{
|
||||||
TcpClient::TcpClient(Socket *soc)
|
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)
|
TcpClient::TcpClient(SocketProtocol protocol)
|
||||||
@ -28,17 +28,17 @@ namespace std::net
|
|||||||
return m_socket->HasPendingData(pendingDataSize);
|
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);
|
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);
|
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);
|
return m_socket->Wait(cond, t);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,21 +1,21 @@
|
|||||||
#include "VoidNet/TcpListener.hpp"
|
#include "VoidNet_LL/TcpListener.hpp"
|
||||||
#include "VoidNet/TcpSocketBuilder.hpp"
|
#include "VoidNet_LL/TcpSocketBuilder.hpp"
|
||||||
#include "VoidNet/Socket.hpp"
|
#include "VoidNet_LL/Socket.hpp"
|
||||||
#include "VoidNet/TcpClient.hpp"
|
#include "VoidNet_LL/TcpClient.hpp"
|
||||||
|
|
||||||
namespace std::net
|
namespace std::net
|
||||||
{
|
{
|
||||||
TcpListener::TcpListener(uint16_t port, std::chrono::milliseconds inSleepTime)
|
TcpListener::TcpListener(uint16_t port, chrono::milliseconds inSleepTime)
|
||||||
: m_port(port)
|
: m_port(port)
|
||||||
, m_sleepTime(inSleepTime)
|
, m_sleepTime(inSleepTime)
|
||||||
{
|
{
|
||||||
m_socket = TcpSocketBuilder().AsNonBlocking().AsReusable().Bind(IPAddress(0, 0, 0, 0, port)).Listening().Build();
|
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_sleepTime(inSleepTime)
|
||||||
{
|
{
|
||||||
m_socket = std::unique_ptr<Socket>(InSocket);
|
m_socket = unique_ptr<Socket>(InSocket);
|
||||||
}
|
}
|
||||||
|
|
||||||
TcpClient *TcpListener::AcceptClient()
|
TcpClient *TcpListener::AcceptClient()
|
||||||
@ -26,9 +26,9 @@ namespace std::net
|
|||||||
if (m_socket == nullptr)
|
if (m_socket == nullptr)
|
||||||
return 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;
|
bool pending = false;
|
||||||
|
|
||||||
@ -36,7 +36,7 @@ namespace std::net
|
|||||||
{
|
{
|
||||||
if (pending)
|
if (pending)
|
||||||
{
|
{
|
||||||
std::unique_ptr<Socket> connectionSocket = m_socket->Accept();
|
unique_ptr<Socket> connectionSocket = m_socket->Accept();
|
||||||
|
|
||||||
if (connectionSocket != nullptr)
|
if (connectionSocket != nullptr)
|
||||||
{
|
{
|
||||||
@ -45,7 +45,7 @@ namespace std::net
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(m_sleepTime));
|
this_thread::sleep_for(chrono::milliseconds(m_sleepTime));
|
||||||
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,14 +1,14 @@
|
|||||||
#include "VoidNet/TcpSocketBuilder.hpp"
|
#include "VoidNet_LL/TcpSocketBuilder.hpp"
|
||||||
|
|
||||||
#include "VoidNet/Socket.hpp"
|
#include "VoidNet_LL/Socket.hpp"
|
||||||
#include "VoidNet/TcpClient.hpp"
|
#include "VoidNet_LL/TcpClient.hpp"
|
||||||
#include "VoidNet/TcpListener.hpp"
|
#include "VoidNet_LL/TcpListener.hpp"
|
||||||
|
|
||||||
namespace std::net
|
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)
|
if (socket != nullptr)
|
||||||
{
|
{
|
||||||
@ -32,21 +32,19 @@ namespace std::net
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (Error)
|
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;
|
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>(Build().release());
|
||||||
return std::make_unique<TcpClient>(socket.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>(Build().release());
|
||||||
return std::make_unique<TcpListener>(socket.release());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,15 +1,15 @@
|
|||||||
#include "VoidNet/UdpSocket.hpp"
|
#include "VoidNet_LL/UdpSocket.hpp"
|
||||||
|
|
||||||
namespace std::net
|
namespace std::net
|
||||||
{
|
{
|
||||||
UdpSocket::UdpSocket(Socket * soc)
|
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)
|
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)
|
bool UdpSocket::Bind(const IPAddress & addr)
|
||||||
@ -17,12 +17,12 @@ namespace std::net
|
|||||||
return m_socket->Bind(addr);
|
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);
|
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);
|
return m_socket->RecvFrom(data, size, read, srcAddr, flags);
|
||||||
}
|
}
|
||||||
@ -47,7 +47,7 @@ namespace std::net
|
|||||||
return m_socket->SetMulticastLoopback(loopback);
|
return m_socket->SetMulticastLoopback(loopback);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool UdpSocket::SetMulticastTtl(uint8_t timeToLive)
|
bool UdpSocket::SetMulticastTtl(byte timeToLive)
|
||||||
{
|
{
|
||||||
return m_socket->SetMulticastTtl(timeToLive);
|
return m_socket->SetMulticastTtl(timeToLive);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
// https://github.com/mfichman/http
|
// https://github.com/mfichman/http
|
||||||
|
|
||||||
#include "VoidNet/Uri.hpp"
|
#include "VoidNet_LL/Uri.hpp"
|
||||||
#include "VoidNet/Parse.hpp"
|
#include "VoidNet_LL/Parse.hpp"
|
||||||
|
|
||||||
namespace std::net
|
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)
|
auto result = ParseWhile(str, [](char ch)
|
||||||
{
|
{
|
||||||
@ -31,7 +31,7 @@ namespace std::net
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
static ParseResult<std::string> ParseUser(char const* str)
|
static ParseResult<string> ParseUser(char const* str)
|
||||||
{
|
{
|
||||||
auto result = ParseWhile(str, [](char ch)
|
auto result = ParseWhile(str, [](char ch)
|
||||||
{
|
{
|
||||||
@ -47,7 +47,7 @@ namespace std::net
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
static ParseResult<std::string> ParseHost(char const* str)
|
static ParseResult<string> ParseHost(char const* str)
|
||||||
{
|
{
|
||||||
return ParseWhile(str, [](char ch)
|
return ParseWhile(str, [](char ch)
|
||||||
{
|
{
|
||||||
@ -99,10 +99,10 @@ namespace std::net
|
|||||||
return result;
|
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
|
// 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;
|
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_user = user;
|
||||||
m_host = host;
|
m_host = host;
|
||||||
@ -143,12 +143,12 @@ namespace std::net
|
|||||||
m_port = 0;
|
m_port = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Authority::SetUser(std::string const& user)
|
void Authority::SetUser(string const& user)
|
||||||
{
|
{
|
||||||
m_user = user;
|
m_user = user;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Authority::SetHost(std::string const& host)
|
void Authority::SetHost(string const& host)
|
||||||
{
|
{
|
||||||
m_host = host;
|
m_host = host;
|
||||||
}
|
}
|
||||||
@ -163,7 +163,7 @@ namespace std::net
|
|||||||
*this = parseUri(value);
|
*this = parseUri(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
Uri::Uri(const std::string& value)
|
Uri::Uri(const string& value)
|
||||||
{
|
{
|
||||||
*this = parseUri(value.c_str());
|
*this = parseUri(value.c_str());
|
||||||
}
|
}
|
||||||
@ -171,7 +171,7 @@ namespace std::net
|
|||||||
Uri::Uri() {
|
Uri::Uri() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Uri::SetScheme(const std::string& scheme)
|
void Uri::SetScheme(const string& scheme)
|
||||||
{
|
{
|
||||||
m_scheme = scheme;
|
m_scheme = scheme;
|
||||||
}
|
}
|
||||||
@ -181,7 +181,7 @@ namespace std::net
|
|||||||
m_authority = authority;
|
m_authority = authority;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Uri::SetPath(const std::string& path)
|
void Uri::SetPath(const string& path)
|
||||||
{
|
{
|
||||||
m_path = path;
|
m_path = path;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,29 +1,32 @@
|
|||||||
#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)
|
||||||
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);
|
|
||||||
|
|
||||||
while (std::string::npos != pos || std::string::npos != lastPos)
|
|
||||||
{
|
{
|
||||||
splited.push_back(str.substr(lastPos, pos - lastPos));
|
vector<string> splited;
|
||||||
lastPos = str.find_first_not_of(delimiter, pos);
|
if (str.empty() && delimiter.empty())
|
||||||
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 (string::npos != pos || string::npos != lastPos)
|
||||||
|
{
|
||||||
|
splited.push_back(str.substr(lastPos, pos - lastPos));
|
||||||
|
lastPos = str.find_first_not_of(delimiter, pos);
|
||||||
|
pos = str.find_first_of(delimiter, lastPos);
|
||||||
|
}
|
||||||
|
return splited;
|
||||||
}
|
}
|
||||||
return splited;
|
|
||||||
}
|
|
||||||
|
|
||||||
sockaddr_in std::net::CreateAddress(uint32_t address, uint16_t port)
|
sockaddr_in CreateAddress(uint32_t address, uint16_t port)
|
||||||
{
|
{
|
||||||
sockaddr_in addr;
|
sockaddr_in addr;
|
||||||
std::memset(&addr, 0, sizeof(addr));
|
memset(&addr, 0, sizeof(addr));
|
||||||
addr.sin_addr.s_addr = htonl(address);
|
addr.sin_addr.s_addr = htonl(address);
|
||||||
addr.sin_family = AF_INET;
|
addr.sin_family = AF_INET;
|
||||||
addr.sin_port = htons(port);
|
addr.sin_port = htons(port);
|
||||||
|
|
||||||
return addr;
|
return addr;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user