Started work on network object system
This commit is contained in:
parent
a2ce579b8b
commit
ed4810e6ee
12 changed files with 394 additions and 74 deletions
|
|
@ -103,6 +103,8 @@ set(sources "src/main.cpp"
|
||||||
"include/Helpers/Networking/NetServer.cpp"
|
"include/Helpers/Networking/NetServer.cpp"
|
||||||
"include/Helpers/Networking/NetClient.cpp"
|
"include/Helpers/Networking/NetClient.cpp"
|
||||||
"include/Helpers/Networking/ByteStream.cpp"
|
"include/Helpers/Networking/ByteStream.cpp"
|
||||||
|
"include/Helpers/NetworkingObjects/NetObjIDGenerator.cpp"
|
||||||
|
"include/Helpers/NetworkingObjects/NetObjManager.cpp"
|
||||||
"include/Helpers/Hashes.cpp"
|
"include/Helpers/Hashes.cpp"
|
||||||
"include/Helpers/BezierEasing.cpp"
|
"include/Helpers/BezierEasing.cpp"
|
||||||
"include/Helpers/Random.cpp"
|
"include/Helpers/Random.cpp"
|
||||||
|
|
|
||||||
|
|
@ -93,6 +93,27 @@ void NetClient::parse_received_messages() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void NetClient::send_string_stream_to_server(const std::string& channel, const std::shared_ptr<std::stringstream>& ss) {
|
||||||
|
if(isDisconnected)
|
||||||
|
return;
|
||||||
|
|
||||||
|
auto& messageQueue = messageQueues[channel];
|
||||||
|
|
||||||
|
if(channel == UNRELIABLE_COMMAND_CHANNEL) {
|
||||||
|
if(ss->view().length() <= NetLibrary::MAX_UNRELIABLE_MESSAGE_SIZE) // Drop unreliable messages that are too big
|
||||||
|
messageQueue.emplace(NetLibrary::calc_order_for_queued_message(channel, nextMessageOrderToSend), ss);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
std::vector<std::shared_ptr<std::stringstream>> fragmentedMessage = fragment_message(ss->view(), NetLibrary::FRAGMENT_MESSAGE_STRIDE);
|
||||||
|
if(fragmentedMessage.empty())
|
||||||
|
messageQueue.emplace(NetLibrary::calc_order_for_queued_message(channel, nextMessageOrderToSend), ss);
|
||||||
|
else {
|
||||||
|
for(auto& ss2 : fragmentedMessage)
|
||||||
|
messageQueue.emplace(NetLibrary::calc_order_for_queued_message(channel, nextMessageOrderToSend), ss2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
NetLibrary::DownloadProgress NetClient::get_progress_into_fragmented_message(const std::string& channel) const {
|
NetLibrary::DownloadProgress NetClient::get_progress_into_fragmented_message(const std::string& channel) const {
|
||||||
auto it = pfm.find(channel);
|
auto it = pfm.find(channel);
|
||||||
if(it == pfm.end())
|
if(it == pfm.end())
|
||||||
|
|
|
||||||
|
|
@ -25,23 +25,9 @@ class NetClient : public std::enable_shared_from_this<NetClient> {
|
||||||
cereal::PortableBinaryOutputArchive m(*ss);
|
cereal::PortableBinaryOutputArchive m(*ss);
|
||||||
(m(items), ...);
|
(m(items), ...);
|
||||||
}
|
}
|
||||||
|
send_string_stream_to_server(channel, ss);
|
||||||
auto& messageQueue = messageQueues[channel];
|
|
||||||
|
|
||||||
if(channel == UNRELIABLE_COMMAND_CHANNEL) {
|
|
||||||
if(ss->view().length() <= NetLibrary::MAX_UNRELIABLE_MESSAGE_SIZE) // Drop unreliable messages that are too big
|
|
||||||
messageQueue.emplace(NetLibrary::calc_order_for_queued_message(channel, nextMessageOrderToSend), ss);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
std::vector<std::shared_ptr<std::stringstream>> fragmentedMessage = fragment_message(ss->view(), NetLibrary::FRAGMENT_MESSAGE_STRIDE);
|
|
||||||
if(fragmentedMessage.empty())
|
|
||||||
messageQueue.emplace(NetLibrary::calc_order_for_queued_message(channel, nextMessageOrderToSend), ss);
|
|
||||||
else {
|
|
||||||
for(auto& ss2 : fragmentedMessage)
|
|
||||||
messageQueue.emplace(NetLibrary::calc_order_for_queued_message(channel, nextMessageOrderToSend), ss2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
void send_string_stream_to_server(const std::string& channel, const std::shared_ptr<std::stringstream>& ss);
|
||||||
void update();
|
void update();
|
||||||
void add_recv_callback(MessageCommandType commandID, const NetClientRecvCallback& callback);
|
void add_recv_callback(MessageCommandType commandID, const NetClientRecvCallback& callback);
|
||||||
bool is_disconnected() const;
|
bool is_disconnected() const;
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@ template <class T> std::weak_ptr<T> make_weak_ptr(std::shared_ptr<T> ptr) { retu
|
||||||
#define MAIN_NETLIBRARY_CHANNEL "main00005" // NOTE: Change this value whenever netcode is incompatible with the previous version
|
#define MAIN_NETLIBRARY_CHANNEL "main00005" // NOTE: Change this value whenever netcode is incompatible with the previous version
|
||||||
|
|
||||||
typedef uint8_t MessageCommandType;
|
typedef uint8_t MessageCommandType;
|
||||||
|
typedef uint8_t NetTypeIDType;
|
||||||
|
|
||||||
class NetLibrary {
|
class NetLibrary {
|
||||||
public:
|
public:
|
||||||
|
|
|
||||||
|
|
@ -93,6 +93,66 @@ void NetServer::client_connected(std::shared_ptr<rtc::PeerConnection> connection
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void NetServer::send_string_stream_to_client(const std::shared_ptr<ClientData>& client, const std::string& channel, const std::shared_ptr<std::stringstream>& ss) {
|
||||||
|
if(!client)
|
||||||
|
return;
|
||||||
|
|
||||||
|
auto& messageQueue = client->messageQueues[channel];
|
||||||
|
if(channel == UNRELIABLE_COMMAND_CHANNEL) {
|
||||||
|
if(ss->view().length() <= NetLibrary::MAX_UNRELIABLE_MESSAGE_SIZE) // Drop unreliable messages that are too big
|
||||||
|
messageQueue.emplace(NetLibrary::calc_order_for_queued_message(channel, client->nextMessageOrderToSend), ss);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
std::vector<std::shared_ptr<std::stringstream>> fragmentedMessage = fragment_message(ss->view(), NetLibrary::FRAGMENT_MESSAGE_STRIDE);
|
||||||
|
if(fragmentedMessage.empty())
|
||||||
|
messageQueue.emplace(NetLibrary::calc_order_for_queued_message(channel, client->nextMessageOrderToSend), ss);
|
||||||
|
else {
|
||||||
|
for(auto& ss2 : fragmentedMessage)
|
||||||
|
messageQueue.emplace(NetLibrary::calc_order_for_queued_message(channel, client->nextMessageOrderToSend), ss2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void NetServer::send_string_stream_to_all_clients(const std::string& channel, const std::shared_ptr<std::stringstream>& ss) {
|
||||||
|
send_string_stream_to_client_if([&](const std::shared_ptr<ClientData>& c) {
|
||||||
|
return true;
|
||||||
|
}, channel, ss);
|
||||||
|
}
|
||||||
|
|
||||||
|
void NetServer::send_string_stream_to_all_clients_except(const std::shared_ptr<ClientData>& client, const std::string& channel, const std::shared_ptr<std::stringstream>& ss) {
|
||||||
|
send_string_stream_to_client_if([&](const std::shared_ptr<ClientData>& c) {
|
||||||
|
return c != client;
|
||||||
|
}, channel, ss);
|
||||||
|
}
|
||||||
|
|
||||||
|
void NetServer::send_string_stream_to_client_if(std::function<bool(const std::shared_ptr<ClientData>&)> clientChecker, const std::string& channel, const std::shared_ptr<std::stringstream>& ss) {
|
||||||
|
if(channel == UNRELIABLE_COMMAND_CHANNEL) {
|
||||||
|
if(ss->view().length() <= NetLibrary::MAX_UNRELIABLE_MESSAGE_SIZE) { // Drop unreliable messages that are too big
|
||||||
|
for(auto& client : clients) {
|
||||||
|
if(client && clientChecker(client)) {
|
||||||
|
auto& messageQueue = client->messageQueues[channel];
|
||||||
|
messageQueue.emplace(NetLibrary::calc_order_for_queued_message(channel, client->nextMessageOrderToSend), ss);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
std::vector<std::shared_ptr<std::stringstream>> fragmentedMessage = fragment_message(ss->view(), NetLibrary::FRAGMENT_MESSAGE_STRIDE);
|
||||||
|
|
||||||
|
for(auto& client : clients) {
|
||||||
|
if(client && clientChecker(client)) {
|
||||||
|
auto& messageQueue = client->messageQueues[channel];
|
||||||
|
if(fragmentedMessage.empty())
|
||||||
|
messageQueue.emplace(NetLibrary::calc_order_for_queued_message(channel, client->nextMessageOrderToSend), ss);
|
||||||
|
else {
|
||||||
|
for(auto& ss2 : fragmentedMessage)
|
||||||
|
messageQueue.emplace(NetLibrary::calc_order_for_queued_message(channel, client->nextMessageOrderToSend), ss2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void NetServer::ClientData::send_queued_messages(NetServer& server) {
|
void NetServer::ClientData::send_queued_messages(NetServer& server) {
|
||||||
for(auto& [channelName, messageQueue] : messageQueues) {
|
for(auto& [channelName, messageQueue] : messageQueues) {
|
||||||
bool addMessageOrder = NetLibrary::is_ordered_channel(channelName);
|
bool addMessageOrder = NetLibrary::is_ordered_channel(channelName);
|
||||||
|
|
|
||||||
|
|
@ -54,75 +54,32 @@ class NetServer : public std::enable_shared_from_this<NetServer> {
|
||||||
NetServer(const std::string& serverLocalID);
|
NetServer(const std::string& serverLocalID);
|
||||||
~NetServer();
|
~NetServer();
|
||||||
void update();
|
void update();
|
||||||
template <typename... Args> void send_items_to_client(std::shared_ptr<ClientData> client, const std::string& channel, Args&&... items) {
|
template <typename... Args> void send_items_to_client(const std::shared_ptr<ClientData>& client, const std::string& channel, Args&&... items) {
|
||||||
if(!client)
|
send_string_stream_to_client(client, channel, get_string_stream_from_items(items...));
|
||||||
return;
|
|
||||||
|
|
||||||
auto ss(std::make_shared<std::stringstream>());
|
|
||||||
{
|
|
||||||
cereal::PortableBinaryOutputArchive m(*ss);
|
|
||||||
(m(items), ...);
|
|
||||||
}
|
|
||||||
|
|
||||||
auto& messageQueue = client->messageQueues[channel];
|
|
||||||
if(channel == UNRELIABLE_COMMAND_CHANNEL) {
|
|
||||||
if(ss->view().length() <= NetLibrary::MAX_UNRELIABLE_MESSAGE_SIZE) // Drop unreliable messages that are too big
|
|
||||||
messageQueue.emplace(NetLibrary::calc_order_for_queued_message(channel, client->nextMessageOrderToSend), ss);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
std::vector<std::shared_ptr<std::stringstream>> fragmentedMessage = fragment_message(ss->view(), NetLibrary::FRAGMENT_MESSAGE_STRIDE);
|
|
||||||
if(fragmentedMessage.empty())
|
|
||||||
messageQueue.emplace(NetLibrary::calc_order_for_queued_message(channel, client->nextMessageOrderToSend), ss);
|
|
||||||
else {
|
|
||||||
for(auto& ss2 : fragmentedMessage)
|
|
||||||
messageQueue.emplace(NetLibrary::calc_order_for_queued_message(channel, client->nextMessageOrderToSend), ss2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
template <typename... Args> void send_items_to_all_clients(const std::string& channel, Args&&... items) {
|
template <typename... Args> void send_items_to_all_clients(const std::string& channel, Args&&... items) {
|
||||||
send_items_to_client_if([&](std::shared_ptr<ClientData> c) {
|
send_string_stream_to_all_clients(channel, get_string_stream_from_items(items...));
|
||||||
return true;
|
|
||||||
}, channel, items...);
|
|
||||||
}
|
}
|
||||||
template <typename... Args> void send_items_to_all_clients_except(std::shared_ptr<ClientData> client, const std::string& channel, Args&&... items) {
|
template <typename... Args> void send_items_to_all_clients_except(const std::shared_ptr<ClientData>& client, const std::string& channel, Args&&... items) {
|
||||||
send_items_to_client_if([&](std::shared_ptr<ClientData> c) {
|
send_string_stream_to_all_clients_except(client, channel, get_string_stream_from_items(items...));
|
||||||
return c != client;
|
|
||||||
}, channel, items...);
|
|
||||||
}
|
}
|
||||||
template <typename... Args> void send_items_to_client_if(std::function<bool(std::shared_ptr<ClientData>)> clientChecker, const std::string& channel, Args&&... items) {
|
template <typename... Args> void send_items_to_client_if(std::function<bool(const std::shared_ptr<ClientData>&)> clientChecker, const std::string& channel, Args&&... items) {
|
||||||
|
send_string_stream_to_client_if(clientChecker, channel, get_string_stream_from_items(items...));
|
||||||
|
}
|
||||||
|
template <typename... Args> std::shared_ptr<std::stringstream> get_string_stream_from_items(Args&&... items) {
|
||||||
auto ss(std::make_shared<std::stringstream>());
|
auto ss(std::make_shared<std::stringstream>());
|
||||||
{
|
{
|
||||||
cereal::PortableBinaryOutputArchive m(*ss);
|
cereal::PortableBinaryOutputArchive m(*ss);
|
||||||
(m(items), ...);
|
(m(items), ...);
|
||||||
}
|
}
|
||||||
|
return ss;
|
||||||
if(channel == UNRELIABLE_COMMAND_CHANNEL) {
|
|
||||||
if(ss->view().length() <= NetLibrary::MAX_UNRELIABLE_MESSAGE_SIZE) { // Drop unreliable messages that are too big
|
|
||||||
for(auto& client : clients) {
|
|
||||||
if(client && clientChecker(client)) {
|
|
||||||
auto& messageQueue = client->messageQueues[channel];
|
|
||||||
messageQueue.emplace(NetLibrary::calc_order_for_queued_message(channel, client->nextMessageOrderToSend), ss);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
std::vector<std::shared_ptr<std::stringstream>> fragmentedMessage = fragment_message(ss->view(), NetLibrary::FRAGMENT_MESSAGE_STRIDE);
|
|
||||||
|
|
||||||
for(auto& client : clients) {
|
|
||||||
if(client && clientChecker(client)) {
|
|
||||||
auto& messageQueue = client->messageQueues[channel];
|
|
||||||
if(fragmentedMessage.empty())
|
|
||||||
messageQueue.emplace(NetLibrary::calc_order_for_queued_message(channel, client->nextMessageOrderToSend), ss);
|
|
||||||
else {
|
|
||||||
for(auto& ss2 : fragmentedMessage)
|
|
||||||
messageQueue.emplace(NetLibrary::calc_order_for_queued_message(channel, client->nextMessageOrderToSend), ss2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void send_string_stream_to_client(const std::shared_ptr<ClientData>& client, const std::string& channel, const std::shared_ptr<std::stringstream>& ss);
|
||||||
|
void send_string_stream_to_all_clients(const std::string& channel, const std::shared_ptr<std::stringstream>& ss);
|
||||||
|
void send_string_stream_to_all_clients_except(const std::shared_ptr<ClientData>& client, const std::string& channel, const std::shared_ptr<std::stringstream>& ss);
|
||||||
|
void send_string_stream_to_client_if(std::function<bool(const std::shared_ptr<ClientData>&)> clientChecker, const std::string& channel, const std::shared_ptr<std::stringstream>& ss);
|
||||||
|
|
||||||
bool is_disconnected() const;
|
bool is_disconnected() const;
|
||||||
|
|
||||||
void add_recv_callback(MessageCommandType commandID, const NetServerRecvCallback& callback);
|
void add_recv_callback(MessageCommandType commandID, const NetServerRecvCallback& callback);
|
||||||
|
|
|
||||||
14
include/Helpers/NetworkingObjects/NetObjIDGenerator.cpp
Normal file
14
include/Helpers/NetworkingObjects/NetObjIDGenerator.cpp
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
#include "NetObjIDGenerator.hpp"
|
||||||
|
|
||||||
|
namespace NetworkingObjects {
|
||||||
|
|
||||||
|
void NetObjIDGenerator::set_starting_id(NetObjID c) {
|
||||||
|
currentID = c;
|
||||||
|
}
|
||||||
|
|
||||||
|
NetObjID NetObjIDGenerator::gen() {
|
||||||
|
currentID.clientID++;
|
||||||
|
return currentID;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
35
include/Helpers/NetworkingObjects/NetObjIDGenerator.hpp
Normal file
35
include/Helpers/NetworkingObjects/NetObjIDGenerator.hpp
Normal file
|
|
@ -0,0 +1,35 @@
|
||||||
|
#pragma once
|
||||||
|
#include <cstdint>
|
||||||
|
#include "../Hashes.hpp"
|
||||||
|
|
||||||
|
namespace NetworkingObjects {
|
||||||
|
|
||||||
|
struct NetObjID {
|
||||||
|
typedef uint16_t ClientPortion;
|
||||||
|
typedef uint64_t ObjectPortion;
|
||||||
|
ClientPortion clientID = 0;
|
||||||
|
ObjectPortion objID = 0;
|
||||||
|
bool operator==(const NetObjID&) const = default;
|
||||||
|
template <typename Archive> void serialize(Archive& a) {
|
||||||
|
a(clientID, objID);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class NetObjIDGenerator {
|
||||||
|
public:
|
||||||
|
void set_starting_id(NetObjID c);
|
||||||
|
NetObjID gen();
|
||||||
|
private:
|
||||||
|
NetObjID currentID;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
template <> struct std::hash<NetworkingObjects::NetObjID> {
|
||||||
|
size_t operator()(const NetworkingObjects::NetObjID& v) const
|
||||||
|
{
|
||||||
|
uint64_t h = 0;
|
||||||
|
hash_combine(h, v.clientID, v.objID);
|
||||||
|
return h;
|
||||||
|
}
|
||||||
|
};
|
||||||
26
include/Helpers/NetworkingObjects/NetObjManager.cpp
Normal file
26
include/Helpers/NetworkingObjects/NetObjManager.cpp
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
#include "NetObjManager.hpp"
|
||||||
|
|
||||||
|
namespace NetworkingObjects {
|
||||||
|
NetObjManager::NetObjManager(std::shared_ptr<NetObjIDGenerator> initIDGen, std::shared_ptr<NetObjManagerTypeList> initTypeList):
|
||||||
|
idGen(initIDGen),
|
||||||
|
typeList(initTypeList),
|
||||||
|
nextTypeID(0)
|
||||||
|
{}
|
||||||
|
void NetObjManager::read_update_message(cereal::PortableBinaryInputArchive& a) {
|
||||||
|
NetObjID id;
|
||||||
|
a(id);
|
||||||
|
auto it = objectData.find(id);
|
||||||
|
if(it == objectData.end())
|
||||||
|
return;
|
||||||
|
auto& netTypeIDData = typeList->netTypeIDData[it->second.netTypeID];
|
||||||
|
netTypeIDData.readUpdateFunc(it->second.p.get(), a);
|
||||||
|
}
|
||||||
|
void NetObjManager::set_client(std::shared_ptr<NetClient> initClient, MessageCommandType initUpdateCommandID) {
|
||||||
|
client = initClient;
|
||||||
|
updateCommandID = initUpdateCommandID;
|
||||||
|
}
|
||||||
|
void NetObjManager::set_server(std::shared_ptr<NetServer> initServer, MessageCommandType initUpdateCommandID) {
|
||||||
|
server = initServer;
|
||||||
|
updateCommandID = initUpdateCommandID;
|
||||||
|
}
|
||||||
|
}
|
||||||
81
include/Helpers/NetworkingObjects/NetObjManager.hpp
Normal file
81
include/Helpers/NetworkingObjects/NetObjManager.hpp
Normal file
|
|
@ -0,0 +1,81 @@
|
||||||
|
#pragma once
|
||||||
|
#include <compare>
|
||||||
|
#include <memory>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <functional>
|
||||||
|
#include <cereal/archives/portable_binary.hpp>
|
||||||
|
#include <chrono>
|
||||||
|
#include <vector>
|
||||||
|
#include "../Networking/NetClient.hpp"
|
||||||
|
#include "../Networking/NetServer.hpp"
|
||||||
|
#include "../Networking/NetLibrary.hpp"
|
||||||
|
#include "NetObjIDGenerator.hpp"
|
||||||
|
#include "NetObjManagerTypeList.hpp"
|
||||||
|
|
||||||
|
namespace NetworkingObjects {
|
||||||
|
template <typename T> class NetObjPtr;
|
||||||
|
|
||||||
|
class NetObjManager {
|
||||||
|
public:
|
||||||
|
NetObjManager(std::shared_ptr<NetObjIDGenerator> initIDGen, std::shared_ptr<NetObjManagerTypeList> initTypeList);
|
||||||
|
void read_update_message(cereal::PortableBinaryInputArchive& a);
|
||||||
|
void set_client(std::shared_ptr<NetClient> initClient, MessageCommandType initUpdateCommandID);
|
||||||
|
void set_server(std::shared_ptr<NetServer> initServer, MessageCommandType initUpdateCommandID);
|
||||||
|
|
||||||
|
template <typename T> NetObjPtr<T> read_create_message(cereal::PortableBinaryInputArchive& a) {
|
||||||
|
NetObjID id;
|
||||||
|
NetTypeIDType typeID;
|
||||||
|
a(id, typeID);
|
||||||
|
auto& netTypeIDData = typeList->netTypeIDData[typeID];
|
||||||
|
std::shared_ptr<void> sharedPtr = netTypeIDData.allocatorFunc();
|
||||||
|
auto [it, placed] = objectData.emplace(id, typeID, sharedPtr);
|
||||||
|
if(!placed)
|
||||||
|
throw std::runtime_error("[NetObjManager::read_create_message] ID Collision on object creation");
|
||||||
|
netTypeIDData.readConstructorFunc(sharedPtr.get(), a);
|
||||||
|
return NetObjPtr<T>(id, std::static_pointer_cast<T>(sharedPtr));
|
||||||
|
}
|
||||||
|
template <typename T> NetObjPtr<T> read_get_obj_from_message(cereal::PortableBinaryInputArchive& a) {
|
||||||
|
NetObjID id;
|
||||||
|
a(id);
|
||||||
|
auto it = objectData.find(id);
|
||||||
|
if(it == objectData.end())
|
||||||
|
return NetObjPtr<T>();
|
||||||
|
return NetObjPtr<T>(id, std::static_pointer_cast<T>(it->second.p));
|
||||||
|
}
|
||||||
|
template <typename T, typename... Args> NetObjPtr<T> make_obj(Args&&... items) {
|
||||||
|
auto sharedPtr = std::make_shared<T>(items...);
|
||||||
|
NetObjID newID = idGen->gen();
|
||||||
|
if(!objectData.emplace(newID, typeList->typeIndexData[std::type_index(typeid(T))].netTypeID, sharedPtr).second)
|
||||||
|
throw std::runtime_error("[NetObjManager::make_obj] ID Collision");
|
||||||
|
return NetObjPtr<T>(newID, sharedPtr);
|
||||||
|
}
|
||||||
|
template <typename T> NetObjPtr<T> obj_from_ptr(T* p) {
|
||||||
|
std::shared_ptr<T> sharedPtr(p);
|
||||||
|
NetObjID newID = idGen->gen();
|
||||||
|
if(!objectData.emplace(newID, typeList->typeIndexData[std::type_index(typeid(T))].netTypeID, sharedPtr).second)
|
||||||
|
throw std::runtime_error("[NetObjManager::obj_from_ptr] ID Collision");
|
||||||
|
return NetObjPtr<T>(newID, sharedPtr);
|
||||||
|
}
|
||||||
|
template <typename T> NetObjPtr<T> get_obj_from_id(NetObjID id) {
|
||||||
|
auto it = objectData.find(id);
|
||||||
|
if(it == objectData.end())
|
||||||
|
throw std::runtime_error("[NetObjManager::get_obj_from_id] ID doesn't exist");
|
||||||
|
return NetObjPtr<T>(id, std::static_pointer_cast<T>(it->second.p));
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
template <typename T> friend class NetObjPtr;
|
||||||
|
|
||||||
|
struct SingleObjectData {
|
||||||
|
NetTypeIDType netTypeID;
|
||||||
|
std::shared_ptr<void> p;
|
||||||
|
std::optional<std::chrono::time_point<std::chrono::steady_clock>> timeSinceNoReference;
|
||||||
|
};
|
||||||
|
std::shared_ptr<NetClient> client;
|
||||||
|
std::shared_ptr<NetServer> server;
|
||||||
|
MessageCommandType updateCommandID;
|
||||||
|
std::unordered_map<NetObjID, SingleObjectData> objectData;
|
||||||
|
std::shared_ptr<NetObjIDGenerator> idGen;
|
||||||
|
std::shared_ptr<NetObjManagerTypeList> typeList;
|
||||||
|
NetTypeIDType nextTypeID;
|
||||||
|
};
|
||||||
|
}
|
||||||
44
include/Helpers/NetworkingObjects/NetObjManagerTypeList.hpp
Normal file
44
include/Helpers/NetworkingObjects/NetObjManagerTypeList.hpp
Normal file
|
|
@ -0,0 +1,44 @@
|
||||||
|
#pragma once
|
||||||
|
#include "../Networking/NetLibrary.hpp"
|
||||||
|
#include <cereal/archives/portable_binary.hpp>
|
||||||
|
|
||||||
|
namespace NetworkingObjects {
|
||||||
|
|
||||||
|
class NetObjManagerTypeList {
|
||||||
|
public:
|
||||||
|
template <typename T> void register_class(NetTypeIDType netTypeID, std::function<void(T&, cereal::PortableBinaryOutputArchive&)> writeConstructorFunc, std::function<void(T&, cereal::PortableBinaryInputArchive&)> readConstructorFunc, std::function<void(T&, cereal::PortableBinaryInputArchive&)> readUpdateFunc) {
|
||||||
|
netTypeIDData.emplace(netTypeID, {
|
||||||
|
.readConstructorFunc = [readConstructorFunc](void* obj, cereal::PortableBinaryInputArchive& message) {
|
||||||
|
readConstructorFunc(static_cast<T*>(obj), message);
|
||||||
|
},
|
||||||
|
.readUpdateFunc = [readUpdateFunc](void* obj, cereal::PortableBinaryInputArchive& message) {
|
||||||
|
readUpdateFunc(static_cast<T*>(obj), message);
|
||||||
|
},
|
||||||
|
.allocatorFunc = []() { return std::make_shared<T>(); }
|
||||||
|
});
|
||||||
|
typeIndexData.emplace(std::type_index(typeid(T)), {
|
||||||
|
.netTypeID = netTypeID,
|
||||||
|
.writeConstructorFunc = [writeConstructorFunc](void* obj, cereal::PortableBinaryInputArchive& message) {
|
||||||
|
writeConstructorFunc(static_cast<T*>(obj), message);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
friend class NetObjManager;
|
||||||
|
template <typename T> friend class NetObjPtr;
|
||||||
|
|
||||||
|
struct TypeIndexContainer {
|
||||||
|
NetTypeIDType netTypeID;
|
||||||
|
std::function<void(void*, cereal::PortableBinaryOutputArchive&)> writeConstructorFunc;
|
||||||
|
};
|
||||||
|
std::unordered_map<std::type_index, TypeIndexContainer> typeIndexData;
|
||||||
|
|
||||||
|
struct NetTypeIDContainer {
|
||||||
|
std::function<void(void*, cereal::PortableBinaryInputArchive&)> readConstructorFunc;
|
||||||
|
std::function<void(void*, cereal::PortableBinaryInputArchive&)> readUpdateFunc;
|
||||||
|
std::function<std::shared_ptr<void>()> allocatorFunc;
|
||||||
|
};
|
||||||
|
std::unordered_map<NetTypeIDType, NetTypeIDContainer> netTypeIDData;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
93
include/Helpers/NetworkingObjects/NetObjPtr.hpp
Normal file
93
include/Helpers/NetworkingObjects/NetObjPtr.hpp
Normal file
|
|
@ -0,0 +1,93 @@
|
||||||
|
#pragma once
|
||||||
|
#include "NetObjIDGenerator.hpp"
|
||||||
|
#include <compare>
|
||||||
|
#include <string>
|
||||||
|
#include <functional>
|
||||||
|
#include <cereal/archives/portable_binary.hpp>
|
||||||
|
#include "NetObjManager.hpp"
|
||||||
|
#include "NetObjManagerTypeList.hpp"
|
||||||
|
|
||||||
|
namespace NetworkingObjects {
|
||||||
|
template <typename T> class NetObjPtr {
|
||||||
|
public:
|
||||||
|
NetObjPtr():
|
||||||
|
objMan(nullptr),
|
||||||
|
sharedPtr(nullptr)
|
||||||
|
{}
|
||||||
|
NetObjPtr(const NetObjPtr& other):
|
||||||
|
objMan(other.objMan),
|
||||||
|
id(other.id),
|
||||||
|
sharedPtr(other.sharedPtr)
|
||||||
|
{}
|
||||||
|
NetObjPtr& operator=(const NetObjPtr& other) {
|
||||||
|
if(this != &other) {
|
||||||
|
if(sharedPtr && sharedPtr.unique())
|
||||||
|
objMan->objectData.erase(id);
|
||||||
|
objMan = other.objMan;
|
||||||
|
id = other.id;
|
||||||
|
sharedPtr = other.sharedPtr;
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
~NetObjPtr() {
|
||||||
|
if(sharedPtr && sharedPtr.unique())
|
||||||
|
objMan->objectData.erase(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
NetObjID get_net_id() const {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
T* get() const {
|
||||||
|
return sharedPtr.get();
|
||||||
|
}
|
||||||
|
T& operator*() const {
|
||||||
|
return *sharedPtr;
|
||||||
|
}
|
||||||
|
operator bool() const {
|
||||||
|
return sharedPtr != nullptr;
|
||||||
|
}
|
||||||
|
std::strong_ordering operator<=>(const NetObjPtr<T>& rhs) const {
|
||||||
|
return sharedPtr <=> rhs.sharedPtr;
|
||||||
|
}
|
||||||
|
void send_client_update(const std::string& channel, std::function<void(T&, cereal::PortableBinaryOutputArchive&)> sendUpdateFunc) const {
|
||||||
|
if(objMan->client && !objMan->client->is_disconnected()) {
|
||||||
|
auto ss(std::make_shared<std::stringstream>());
|
||||||
|
{
|
||||||
|
cereal::PortableBinaryOutputArchive m(*ss);
|
||||||
|
m(objMan->updateCommandID, id);
|
||||||
|
sendUpdateFunc(*this, m);
|
||||||
|
}
|
||||||
|
objMan->client->send_string_stream_to_server(channel, ss);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void send_server_update_to_all_clients(const std::string& channel, std::function<void(T&, cereal::PortableBinaryOutputArchive&)> sendUpdateFunc) const {
|
||||||
|
if(objMan->server && !objMan->server->is_disconnected()) {
|
||||||
|
auto ss(std::make_shared<std::stringstream>());
|
||||||
|
{
|
||||||
|
cereal::PortableBinaryOutputArchive m(*ss);
|
||||||
|
m(objMan->updateCommandID, id);
|
||||||
|
sendUpdateFunc(*this, m);
|
||||||
|
}
|
||||||
|
objMan->server->send_string_stream_to_all_clients(channel, ss);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void write_create_message(cereal::PortableBinaryOutputArchive& a) const {
|
||||||
|
auto& typeIndexData = objMan->typeList->typeIndexData[std::type_index(typeid(T))];
|
||||||
|
a(id, typeIndexData.netTypeID);
|
||||||
|
typeIndexData.writeConstructorFunc(static_cast<void*>(this), a);
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
friend class NetObjManager;
|
||||||
|
|
||||||
|
NetObjPtr(NetObjManager* initObjMan, NetObjID initID, const std::shared_ptr<T>& initPtr):
|
||||||
|
objMan(initObjMan),
|
||||||
|
id(initID),
|
||||||
|
sharedPtr(initPtr)
|
||||||
|
{}
|
||||||
|
|
||||||
|
NetObjManager* objMan;
|
||||||
|
NetObjID id;
|
||||||
|
std::shared_ptr<T> sharedPtr;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue