Renamed HLAPI to VoidNet_HL and VoidNet to VoidNet_LL
This commit is contained in:
33
include/VoidNet_HL/ByteConverter.hpp
Normal file
33
include/VoidNet_HL/ByteConverter.hpp
Normal file
@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
|
||||
// i want to use bitshifting but i red in stackoverflow that casting doesnt generate overhead
|
||||
// now ive hit a wall
|
||||
// casting vs bitshifting
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
|
||||
namespace std
|
||||
{
|
||||
class ByteConverter
|
||||
{
|
||||
public:
|
||||
template<typename T>
|
||||
inline static uint8_t *ToBytes(T value)
|
||||
{
|
||||
uint8_t *data = new uint8_t[sizeof(T)]();
|
||||
memcpy(data, &value, sizeof(T));
|
||||
return data;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline static T FromBytes(uint8_t *data)
|
||||
{
|
||||
if (!data)
|
||||
throw std::invalid_argument("cant have null parameter -> ByteConverter::FromBytes");
|
||||
T value;
|
||||
memcpy(&value, data, sizeof(T));
|
||||
return value;
|
||||
}
|
||||
};
|
||||
}
|
||||
10
include/VoidNet_HL/InternalTags.hpp
Normal file
10
include/VoidNet_HL/InternalTags.hpp
Normal file
@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
enum class InternalTags : uint32_t
|
||||
{
|
||||
Disconnect = 0xFFFFFFFF,
|
||||
Connect = 0xFFFFFFFE,
|
||||
AssignID = 0xFFFFFFFD
|
||||
};
|
||||
12
include/VoidNet_HL/NetworkHeader.hpp
Normal file
12
include/VoidNet_HL/NetworkHeader.hpp
Normal file
@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
class NetworkHeader
|
||||
{
|
||||
public:
|
||||
uint32_t Size;
|
||||
};
|
||||
}
|
||||
96
include/VoidNet_HL/NetworkMessage.hpp
Normal file
96
include/VoidNet_HL/NetworkMessage.hpp
Normal file
@ -0,0 +1,96 @@
|
||||
#pragma once
|
||||
|
||||
#include "HLAPI/NetworkHeader.hpp"
|
||||
#include "HLAPI/ByteConverter.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
enum class DistributionMode : uint8_t
|
||||
{
|
||||
ID,
|
||||
Others,
|
||||
OthersAndServer,
|
||||
All,
|
||||
AllAndServer,
|
||||
Server
|
||||
};
|
||||
|
||||
class NetworkMessage
|
||||
{
|
||||
public:
|
||||
NetworkMessage()
|
||||
{
|
||||
}
|
||||
|
||||
NetworkMessage(uint32_t sender, DistributionMode mode, uint32_t destinationId, uint32_t tag, void *data, uint32_t dataSize)
|
||||
: m_senderID(sender)
|
||||
, m_distributionMode(mode)
|
||||
, m_destinationID(destinationId)
|
||||
, m_tag(tag)
|
||||
, m_data(data)
|
||||
, m_dataSize(dataSize)
|
||||
{
|
||||
}
|
||||
|
||||
uint32_t GetSenderID() const;
|
||||
DistributionMode GetDistributionMode() const;
|
||||
uint32_t GetDestinationID() const;
|
||||
uint32_t GetTag() const;
|
||||
|
||||
private:
|
||||
uint32_t m_senderID = 0;
|
||||
DistributionMode m_distributionMode = DistributionMode::All;
|
||||
uint32_t m_destinationID = 0;
|
||||
uint32_t m_tag = 0;
|
||||
|
||||
void *m_data = 0;
|
||||
uint32_t m_dataSize = 0;
|
||||
|
||||
public:
|
||||
template<typename T>
|
||||
void SetData(T *data)
|
||||
{
|
||||
m_data = data;
|
||||
m_dataSize = sizeof(T);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
uint8_t * 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]();
|
||||
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
|
||||
|
||||
memcpy(bytes + sizeOfNetHeader, sender, 4);
|
||||
bytes[sizeOfNetHeader + 4] = (uint8_t)m_distributionMode;
|
||||
memcpy(bytes + sizeOfNetHeader + 5, destination, 4);
|
||||
memcpy(bytes + sizeOfNetHeader + 9, tag, 4);
|
||||
|
||||
memcpy(bytes + 13 + sizeOfNetHeader, m_data, sizeof(T));
|
||||
|
||||
size = header.Size;
|
||||
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);
|
||||
|
||||
template<typename T>
|
||||
T *GetData() const
|
||||
{
|
||||
return (T*)m_data;
|
||||
}
|
||||
};
|
||||
}
|
||||
16
include/VoidNet_HL/Plugin/Plugin.hpp
Normal file
16
include/VoidNet_HL/Plugin/Plugin.hpp
Normal file
@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include <HLAPI/NetworkMessage.hpp>
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
class Plugin
|
||||
{
|
||||
public:
|
||||
Plugin()
|
||||
{
|
||||
}
|
||||
|
||||
virtual void HandleMessage(const NetworkMessage& msg) = 0;
|
||||
};
|
||||
}
|
||||
29
include/VoidNet_HL/Plugin/PluginManager.hpp
Normal file
29
include/VoidNet_HL/Plugin/PluginManager.hpp
Normal file
@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "HLAPI/Plugin/Plugin.hpp"
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
class PluginManager
|
||||
{
|
||||
public:
|
||||
PluginManager();
|
||||
~PluginManager();
|
||||
|
||||
void HandleMessage(const NetworkMessage& msg)
|
||||
{
|
||||
for (size_t i = 0; i < m_plugins.size(); i++)
|
||||
m_plugins.at(i)->HandleMessage(msg);
|
||||
}
|
||||
|
||||
void AddPlugin(Plugin* p)
|
||||
{
|
||||
m_plugins.emplace_back(p);
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<Plugin*> m_plugins = std::vector<Plugin*>();
|
||||
};
|
||||
}
|
||||
29
include/VoidNet_HL/Server.hpp
Normal file
29
include/VoidNet_HL/Server.hpp
Normal file
@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#include <VoidNet/Net.hpp>
|
||||
#include "HLAPI/ServerConfig.hpp"
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
class TcpServer;
|
||||
class Plugin;
|
||||
|
||||
class Server
|
||||
{
|
||||
public:
|
||||
Server(uint32_t max_connections = 20, uint16_t port = DEFAULT_SERVER_PORT);
|
||||
|
||||
void Start();
|
||||
void Stop();
|
||||
|
||||
void AddPlugin(Plugin* plugin);
|
||||
|
||||
public:
|
||||
ServerConfig Config;
|
||||
|
||||
private:
|
||||
std::shared_ptr<std::net::TcpServer> m_tcpServer;
|
||||
};
|
||||
}
|
||||
10
include/VoidNet_HL/ServerConfig.hpp
Normal file
10
include/VoidNet_HL/ServerConfig.hpp
Normal file
@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
class ServerConfig
|
||||
{
|
||||
public:
|
||||
bool BroadcastConnectionEvents;
|
||||
};
|
||||
}
|
||||
44
include/VoidNet_HL/TcpConnection.hpp
Normal file
44
include/VoidNet_HL/TcpConnection.hpp
Normal file
@ -0,0 +1,44 @@
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
|
||||
#include "VoidNet/TcpClient.hpp"
|
||||
|
||||
#include "HLAPI/NetworkMessage.hpp"
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
class TcpConnection
|
||||
{
|
||||
public:
|
||||
friend class TcpConnectionHandler;
|
||||
|
||||
TcpConnection();
|
||||
TcpConnection(TcpClient *client);
|
||||
|
||||
std::shared_ptr<TcpClient> GetClient();
|
||||
uint32_t GetID();
|
||||
|
||||
bool Connect(IPAddress addr);
|
||||
|
||||
template<typename T>
|
||||
void SendMessage(DistributionMode mode, uint32_t destinationId, uint32_t tag, void *data)
|
||||
{
|
||||
NetworkMessage msg(m_id, mode, destinationId, tag, data, sizeof(T));
|
||||
sendMessage(msg);
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
private:
|
||||
bool sendMessage(const NetworkMessage &msg);
|
||||
|
||||
std::shared_ptr<TcpClient> m_client;
|
||||
uint32_t m_id;
|
||||
};
|
||||
}
|
||||
66
include/VoidNet_HL/TcpConnectionHandler.hpp
Normal file
66
include/VoidNet_HL/TcpConnectionHandler.hpp
Normal file
@ -0,0 +1,66 @@
|
||||
#pragma once
|
||||
|
||||
#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;
|
||||
}
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
class TcpConnectionHandler
|
||||
{
|
||||
friend class std::net::Server;
|
||||
|
||||
public:
|
||||
TcpConnectionHandler(std::shared_ptr<TcpListener> listener_ptr);
|
||||
~TcpConnectionHandler();
|
||||
|
||||
void Start();
|
||||
void Stop();
|
||||
void AddClient(std::shared_ptr<TcpConnection> &c);
|
||||
void SetMaxConnections(uint32_t max_connections);
|
||||
|
||||
void HandlePluginMessage(const NetworkMessage& message);
|
||||
|
||||
uint32_t GetAvailableID();
|
||||
|
||||
private:
|
||||
void HandleReceiveMsgAndConns();
|
||||
void HandleMessage(const NetworkMessage &msg);
|
||||
|
||||
void HandleReceiveMsgAndConnsThreaded();
|
||||
|
||||
private:
|
||||
std::vector<std::shared_ptr<TcpConnection>> m_list;
|
||||
std::mutex m_listMutex;
|
||||
|
||||
uint32_t m_maxConnections = 0;
|
||||
|
||||
std::thread m_receiveThread;
|
||||
std::thread m_sendThread;
|
||||
|
||||
std::atomic_bool m_run;
|
||||
|
||||
std::shared_ptr<MessageQueue> m_queue;
|
||||
|
||||
std::shared_ptr<TcpListener> m_listenerPtr;
|
||||
|
||||
std::shared_ptr<PluginManager> m_pluginManager;
|
||||
|
||||
std::vector<pollfd> m_pollFds;
|
||||
|
||||
ServerConfig m_config;
|
||||
};
|
||||
}
|
||||
31
include/VoidNet_HL/TcpServer.hpp
Normal file
31
include/VoidNet_HL/TcpServer.hpp
Normal file
@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include <atomic>
|
||||
|
||||
#include "VoidNet/TcpListener.hpp"
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
class Server;
|
||||
class TcpConnectionHandler;
|
||||
|
||||
class TcpServer
|
||||
{
|
||||
friend class std::net::Server;
|
||||
|
||||
public:
|
||||
TcpServer(uint32_t max_connections, uint16_t port = DEFAULT_SERVER_PORT);
|
||||
|
||||
void Start();
|
||||
void Stop();
|
||||
|
||||
private:
|
||||
std::shared_ptr<TcpListener> listener;
|
||||
uint32_t m_maxConnections;
|
||||
uint16_t m_port;
|
||||
|
||||
std::atomic_bool m_run;
|
||||
|
||||
std::shared_ptr<std::net::TcpConnectionHandler> m_connectionHandler;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user