Update
Integrated std::byte added to C++17 recently
This commit is contained in:
@ -4,8 +4,7 @@
|
||||
// now ive hit a wall
|
||||
// casting vs bitshifting
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include "VoidNet_LL/Net.hpp"
|
||||
|
||||
namespace std
|
||||
{
|
||||
@ -13,15 +12,15 @@ namespace std
|
||||
{
|
||||
public:
|
||||
template<typename T>
|
||||
inline static uint8_t *ToBytes(T value)
|
||||
inline static byte *ToBytes(T value)
|
||||
{
|
||||
uint8_t *data = new uint8_t[sizeof(T)]();
|
||||
byte *data = new byte[sizeof(T)]();
|
||||
memcpy(data, &value, sizeof(T));
|
||||
return data;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline static T FromBytes(uint8_t *data)
|
||||
inline static T FromBytes(byte *data)
|
||||
{
|
||||
if (!data)
|
||||
throw std::invalid_argument("cant have null parameter -> ByteConverter::FromBytes");
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
|
||||
@ -1,10 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include "HLAPI/NetworkHeader.hpp"
|
||||
#include "HLAPI/ByteConverter.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include "VoidNet_LL/Net.hpp"
|
||||
#include "VoidNet_HL/NetworkHeader.hpp"
|
||||
#include "VoidNet_HL/ByteConverter.hpp"
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
@ -54,26 +52,26 @@ namespace std::net
|
||||
void SetData(T *data)
|
||||
{
|
||||
m_data = data;
|
||||
m_dataSize = sizeof(T);
|
||||
m_dataSize = sizeof(*data);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
uint8_t * SerializeData(uint32_t &size) const
|
||||
byte * SerializeData(uint32_t &size) const
|
||||
{
|
||||
int32_t sizeOfNetHeader = sizeof(NetworkHeader);
|
||||
|
||||
NetworkHeader header;
|
||||
header.Size = 13 + sizeOfNetHeader + sizeof(T);
|
||||
|
||||
uint8_t *bytes = new uint8_t[header.Size]();
|
||||
byte *bytes = new byte[header.Size]();
|
||||
memcpy(bytes, &header, sizeOfNetHeader);
|
||||
|
||||
uint8_t *sender = ByteConverter::ToBytes<uint32_t>(m_senderID); // 4
|
||||
uint8_t *destination = ByteConverter::ToBytes<uint32_t>(m_destinationID); // 4
|
||||
uint8_t *tag = ByteConverter::ToBytes<uint32_t>(m_tag); // 4
|
||||
byte *sender = ByteConverter::ToBytes<uint32_t>(m_senderID); // 4
|
||||
byte *destination = ByteConverter::ToBytes<uint32_t>(m_destinationID); // 4
|
||||
byte *tag = ByteConverter::ToBytes<uint32_t>(m_tag); // 4
|
||||
|
||||
memcpy(bytes + sizeOfNetHeader, sender, 4);
|
||||
bytes[sizeOfNetHeader + 4] = (uint8_t)m_distributionMode;
|
||||
bytes[sizeOfNetHeader + 4] = (byte)m_distributionMode;
|
||||
memcpy(bytes + sizeOfNetHeader + 5, destination, 4);
|
||||
memcpy(bytes + sizeOfNetHeader + 9, tag, 4);
|
||||
|
||||
@ -83,9 +81,9 @@ namespace std::net
|
||||
return bytes;
|
||||
}
|
||||
|
||||
uint8_t *SerializeData(uint32_t &size) const;
|
||||
void Deserialize(uint8_t *data, uint32_t size);
|
||||
void DeserializeWithoutHeader(uint8_t* data, uint32_t size);
|
||||
byte *SerializeData(uint32_t &size) const;
|
||||
void Deserialize(byte *data, uint32_t size);
|
||||
void DeserializeWithoutHeader(byte* data, uint32_t size);
|
||||
|
||||
template<typename T>
|
||||
T *GetData() const
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <HLAPI/NetworkMessage.hpp>
|
||||
#include <VoidNet_HL/NetworkMessage.hpp>
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "HLAPI/Plugin/Plugin.hpp"
|
||||
#include "VoidNet_HL/Plugin/Plugin.hpp"
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
@ -24,6 +24,6 @@ namespace std::net
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<Plugin*> m_plugins = std::vector<Plugin*>();
|
||||
vector<Plugin*> m_plugins;
|
||||
};
|
||||
}
|
||||
@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <VoidNet/Net.hpp>
|
||||
#include "HLAPI/ServerConfig.hpp"
|
||||
#include <VoidNet_LL/Net.hpp>
|
||||
#include "VoidNet_HL/ServerConfig.hpp"
|
||||
|
||||
#include <memory>
|
||||
|
||||
@ -24,6 +24,6 @@ namespace std::net
|
||||
ServerConfig Config;
|
||||
|
||||
private:
|
||||
std::shared_ptr<std::net::TcpServer> m_tcpServer;
|
||||
shared_ptr<TcpServer> m_tcpServer;
|
||||
};
|
||||
}
|
||||
@ -2,9 +2,9 @@
|
||||
|
||||
#include <functional>
|
||||
|
||||
#include "VoidNet/TcpClient.hpp"
|
||||
#include "VoidNet_LL/TcpClient.hpp"
|
||||
|
||||
#include "HLAPI/NetworkMessage.hpp"
|
||||
#include "VoidNet_HL/NetworkMessage.hpp"
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
@ -16,7 +16,7 @@ namespace std::net
|
||||
TcpConnection();
|
||||
TcpConnection(TcpClient *client);
|
||||
|
||||
std::shared_ptr<TcpClient> GetClient();
|
||||
shared_ptr<TcpClient> GetClient();
|
||||
uint32_t GetID();
|
||||
|
||||
bool Connect(IPAddress addr);
|
||||
@ -30,15 +30,15 @@ namespace std::net
|
||||
|
||||
void ReceiveData();
|
||||
|
||||
std::function<void(uint32_t, DistributionMode, uint32_t, uint32_t, void*)> DataReceivedEvent;
|
||||
std::function<void(std::string)> DisconnectedEvent;
|
||||
std::function<void(uint32_t, void*)> NewConnectionEvent;
|
||||
std::function<void()> OnConnectionEvent;
|
||||
function<void(uint32_t, DistributionMode, uint32_t, uint32_t, void*)> DataReceivedEvent;
|
||||
function<void(string)> DisconnectedEvent;
|
||||
function<void(uint32_t, void*)> NewConnectionEvent;
|
||||
function<void()> OnConnectionEvent;
|
||||
|
||||
private:
|
||||
bool sendMessage(const NetworkMessage &msg);
|
||||
|
||||
std::shared_ptr<TcpClient> m_client;
|
||||
shared_ptr<TcpClient> m_client;
|
||||
uint32_t m_id;
|
||||
};
|
||||
}
|
||||
@ -1,17 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include <VoidNet_LL/TcpListener.hpp>
|
||||
#include <VoidNet_HL/TcpConnection.hpp>
|
||||
#include <VoidNet_HL/Plugin/PluginManager.hpp>
|
||||
#include <VoidNet_HL/ServerConfig.hpp>
|
||||
|
||||
#include <mutex>
|
||||
#include <atomic>
|
||||
#include <queue>
|
||||
|
||||
#include <VoidNet/TcpListener.hpp>
|
||||
#include <HLAPI/TcpConnection.hpp>
|
||||
#include <HLAPI/Plugin/PluginManager.hpp>
|
||||
#include <HLAPI/ServerConfig.hpp>
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
class MessageQueue;
|
||||
class TcpConnection;
|
||||
class Server;
|
||||
class TcpListener;
|
||||
@ -21,15 +19,15 @@ namespace std::net
|
||||
{
|
||||
class TcpConnectionHandler
|
||||
{
|
||||
friend class std::net::Server;
|
||||
friend class Server;
|
||||
|
||||
public:
|
||||
TcpConnectionHandler(std::shared_ptr<TcpListener> listener_ptr);
|
||||
TcpConnectionHandler(shared_ptr<TcpListener> listener_ptr);
|
||||
~TcpConnectionHandler();
|
||||
|
||||
void Start();
|
||||
void Stop();
|
||||
void AddClient(std::shared_ptr<TcpConnection> &c);
|
||||
void AddClient(shared_ptr<TcpConnection> &c);
|
||||
void SetMaxConnections(uint32_t max_connections);
|
||||
|
||||
void HandlePluginMessage(const NetworkMessage& message);
|
||||
@ -43,23 +41,21 @@ namespace std::net
|
||||
void HandleReceiveMsgAndConnsThreaded();
|
||||
|
||||
private:
|
||||
std::vector<std::shared_ptr<TcpConnection>> m_list;
|
||||
std::mutex m_listMutex;
|
||||
vector<shared_ptr<TcpConnection>> m_list;
|
||||
mutex m_listMutex;
|
||||
|
||||
uint32_t m_maxConnections = 0;
|
||||
|
||||
std::thread m_receiveThread;
|
||||
std::thread m_sendThread;
|
||||
thread m_receiveThread;
|
||||
thread m_sendThread;
|
||||
|
||||
std::atomic_bool m_run;
|
||||
atomic_bool m_run;
|
||||
|
||||
std::shared_ptr<MessageQueue> m_queue;
|
||||
shared_ptr<TcpListener> m_listenerPtr;
|
||||
|
||||
std::shared_ptr<TcpListener> m_listenerPtr;
|
||||
shared_ptr<PluginManager> m_pluginManager;
|
||||
|
||||
std::shared_ptr<PluginManager> m_pluginManager;
|
||||
|
||||
std::vector<pollfd> m_pollFds;
|
||||
vector<pollfd> m_pollFds;
|
||||
|
||||
ServerConfig m_config;
|
||||
};
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
#include <atomic>
|
||||
|
||||
#include "VoidNet/TcpListener.hpp"
|
||||
#include "VoidNet_LL/TcpListener.hpp"
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
@ -11,7 +11,7 @@ namespace std::net
|
||||
|
||||
class TcpServer
|
||||
{
|
||||
friend class std::net::Server;
|
||||
friend class Server;
|
||||
|
||||
public:
|
||||
TcpServer(uint32_t max_connections, uint16_t port = DEFAULT_SERVER_PORT);
|
||||
@ -20,12 +20,12 @@ namespace std::net
|
||||
void Stop();
|
||||
|
||||
private:
|
||||
std::shared_ptr<TcpListener> listener;
|
||||
shared_ptr<TcpListener> listener;
|
||||
uint32_t m_maxConnections;
|
||||
uint16_t m_port;
|
||||
|
||||
std::atomic_bool m_run;
|
||||
atomic_bool m_run;
|
||||
|
||||
std::shared_ptr<std::net::TcpConnectionHandler> m_connectionHandler;
|
||||
shared_ptr<TcpConnectionHandler> m_connectionHandler;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user