Started work on network object system

This commit is contained in:
Yousef Khadadeh 2025-12-06 14:16:50 +03:00
parent a2ce579b8b
commit ed4810e6ee
12 changed files with 394 additions and 74 deletions

View 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;
}
}

View 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;
}
};

View 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;
}
}

View 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;
};
}

View 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;
};
}

View 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;
};
}