diff --git a/include/Helpers/NetworkingObjects/NetObjGenericSerializedClass.hpp b/include/Helpers/NetworkingObjects/NetObjGenericSerializedClass.hpp index 3fbc143..2001849 100644 --- a/include/Helpers/NetworkingObjects/NetObjGenericSerializedClass.hpp +++ b/include/Helpers/NetworkingObjects/NetObjGenericSerializedClass.hpp @@ -12,7 +12,7 @@ namespace NetworkingObjects { a(*o); } - template void default_serialize_read_constructor_func(NetObjManager*, const std::shared_ptr& o, cereal::PortableBinaryInputArchive& a, const std::shared_ptr&) { + template void default_serialize_read_constructor_func(const NetObjTemporaryPtr& o, cereal::PortableBinaryInputArchive& a, const std::shared_ptr&) { a(*o); } @@ -32,7 +32,7 @@ namespace NetworkingObjects { }); } - template void generic_serialized_class_send_update_to_all(const NetworkingObjects::NetObjTemporaryPtr& o) { + template void generic_serialized_class_send_update_to_all(const NetObjTemporaryPtr& o) { o.send_update_to_all(RELIABLE_COMMAND_CHANNEL, default_serialize_write_func); } } diff --git a/include/Helpers/NetworkingObjects/NetObjManager.hpp b/include/Helpers/NetworkingObjects/NetObjManager.hpp index a92f6c4..738c4bb 100644 --- a/include/Helpers/NetworkingObjects/NetObjManager.hpp +++ b/include/Helpers/NetworkingObjects/NetObjManager.hpp @@ -30,8 +30,8 @@ namespace NetworkingObjects { auto it = objectData.find(id); if(it != objectData.end()) throw std::runtime_error("[NetObjManager::read_create_message] Attempted to create an object with a used ID"); - NetObjOwnerPtr newPtr = emplace_raw_ptr(id, typeList->get_type_index_data(isServer).allocatorFunc()); - typeList->get_type_index_data(isServer).readConstructorFunc(newPtr, a, clientReceivedFrom); + NetObjOwnerPtr newPtr = emplace_raw_ptr(id, static_cast(typeList->get_type_index_data(isServer).allocatorFunc())); + typeList->get_type_index_data(isServer).readConstructorFunc(NetObjTemporaryPtr(newPtr).template cast(), a, clientReceivedFrom); return newPtr; } template NetObjTemporaryPtr read_get_obj_ref_from_message(cereal::PortableBinaryInputArchive& a) { @@ -39,11 +39,11 @@ namespace NetworkingObjects { a(id); auto it = objectData.find(id); if(it == objectData.end()) - return NetObjPtr(); - return NetObjPtr(this, id, std::static_pointer_cast(it->second.p)); + return NetObjTemporaryPtr(); + return NetObjTemporaryPtr(this, id, std::static_pointer_cast(it->second.p)); } template NetObjOwnerPtr make_obj() { - return emplace_raw_ptr(NetObjID::random_gen(), typeList->get_type_index_data(isServer).allocatorFunc()); + return emplace_raw_ptr(NetObjID::random_gen(), static_cast(typeList->get_type_index_data(isServer).allocatorFunc())); } // Don't use this function unless you're sure that class T isn't a base class template NetObjOwnerPtr make_obj_direct(Args&&... items) { @@ -56,7 +56,7 @@ namespace NetworkingObjects { auto it = objectData.find(id); if(it == objectData.end()) throw std::runtime_error("[NetObjManager::get_obj_from_id] ID doesn't exist"); - return NetObjPtr(this, id, std::static_pointer_cast(it->second.p)); + return NetObjOwnerPtr(this, id, static_cast(it->second.p)); } private: template NetObjOwnerPtr emplace_raw_ptr(NetObjID id, T* rawPtr) { @@ -121,8 +121,8 @@ namespace NetworkingObjects { } template static void write_create_message(const NetObjTemporaryPtr& ptr, cereal::PortableBinaryOutputArchive& a) { - a(ptr.get_obj_man()->id); - ptr.get_obj_man()->typeList->template get_type_index_data(ptr.get_obj_man()->isServer).writeConstructorFunc(ptr, a); + a(ptr.get_net_id()); + ptr.get_obj_man()->typeList->template get_type_index_data(ptr.get_obj_man()->isServer).writeConstructorFunc(ptr.template cast(), a); } template friend class NetObjOwnerPtr; diff --git a/include/Helpers/NetworkingObjects/NetObjOrderedList.hpp b/include/Helpers/NetworkingObjects/NetObjOrderedList.hpp index dfdb2ca..8c86080 100644 --- a/include/Helpers/NetworkingObjects/NetObjOrderedList.hpp +++ b/include/Helpers/NetworkingObjects/NetObjOrderedList.hpp @@ -68,7 +68,7 @@ namespace NetworkingObjects { virtual uint32_t erase_by_ptr(const NetObjTemporaryPtr>& l, const NetObjTemporaryPtr& p) = 0; virtual void write_constructor(const NetObjTemporaryPtr>& l, cereal::PortableBinaryOutputArchive& a) = 0; virtual void read_update(const NetObjTemporaryPtr>& l, cereal::PortableBinaryInputArchive& a, const std::shared_ptr&) = 0; - virtual void read_constructor(NetObjManager* objMan, const std::shared_ptr>& l, cereal::PortableBinaryInputArchive& a, const std::shared_ptr&) = 0; + virtual void read_constructor(const NetObjTemporaryPtr>& l, cereal::PortableBinaryInputArchive& a, const std::shared_ptr&) = 0; private: template friend void register_ordered_list_class(NetObjManagerTypeList& t); static void write_constructor_func(const NetObjTemporaryPtr>& l, cereal::PortableBinaryOutputArchive& a) { @@ -144,13 +144,13 @@ namespace NetworkingObjects { for(size_t i = 0; i < data.size(); i++) data[i]->obj.write_create_message(a); } - virtual void read_constructor(NetObjManager* objMan, const std::shared_ptr>& l, cereal::PortableBinaryInputArchive& a, const std::shared_ptr& c) override { + virtual void read_constructor(const NetObjTemporaryPtr>& l, cereal::PortableBinaryInputArchive& a, const std::shared_ptr& c) override { uint32_t constructedSize; a(constructedSize); data.clear(); idToDataMap.clear(); for(uint32_t i = 0; i < constructedSize; i++) { - data.emplace_back(std::make_shared>(objMan->template read_create_message(a, c), static_cast(data.size()))); + data.emplace_back(std::make_shared>(l.get_obj_man()->template read_create_message(a, c), static_cast(data.size()))); idToDataMap.emplace(data.back()->obj.get_net_id(), data.back()); } // Dont send the object data to all clients in the constructor, as the function that called read_create_message is also the one that will decide where to send the data of the constructed object @@ -239,15 +239,15 @@ namespace NetworkingObjects { for(size_t i = 0; i < clientData.size(); i++) clientData[i]->obj.write_create_message(a); } - virtual void read_constructor(NetObjManager* objMan, const std::shared_ptr>& l, cereal::PortableBinaryInputArchive& a, const std::shared_ptr&) override { + virtual void read_constructor(const NetObjTemporaryPtr>& l, cereal::PortableBinaryInputArchive& a, const std::shared_ptr&) override { uint32_t constructedSize; a(constructedSize); clientData.clear(); serverData.clear(); clientIdToDataMap.clear(); for(uint32_t i = 0; i < constructedSize; i++) { - clientData.emplace_back(std::make_shared>(objMan->template read_create_message(a, nullptr), static_cast(serverData.size()))); - clientIdToDataMap.emplace_back(clientData.back()->obj.get_net_id(), serverData.back()); + clientData.emplace_back(std::make_shared>(l.get_obj_man()->template read_create_message(a, nullptr), static_cast(serverData.size()))); + clientIdToDataMap.emplace(clientData.back()->obj.get_net_id(), clientData.back()); serverData.emplace_back(clientData.back()->obj.get_net_id()); } } @@ -261,7 +261,7 @@ namespace NetworkingObjects { a(newServerPos, newObjID); serverData.insert(serverData.begin() + newServerPos, newObjID); clientJustInsertedSyncIgnore.erase(newObjID); - sync(); + sync(std::unordered_map>()); break; } case ObjPtrOrderedListCommand_StoC::INSERT_SINGLE_CONSTRUCT: { @@ -270,7 +270,10 @@ namespace NetworkingObjects { auto objInfoToInsert = std::make_shared>(l.get_obj_man()->template read_create_message(a, nullptr), 0); serverData.insert(serverData.begin() + newServerPos, objInfoToInsert->obj.get_net_id()); clientJustInsertedSyncIgnore.erase(objInfoToInsert->obj.get_net_id()); - sync(); + + std::unordered_map> serverNewObjects; + serverNewObjects.emplace(objInfoToInsert->obj.get_net_id(), objInfoToInsert); + sync(serverNewObjects); break; } case ObjPtrOrderedListCommand_StoC::ERASE_SINGLE: { diff --git a/include/Helpers/NetworkingObjects/NetObjOwnerPtr.decl.hpp b/include/Helpers/NetworkingObjects/NetObjOwnerPtr.decl.hpp index bcb3f52..4ec9440 100644 --- a/include/Helpers/NetworkingObjects/NetObjOwnerPtr.decl.hpp +++ b/include/Helpers/NetworkingObjects/NetObjOwnerPtr.decl.hpp @@ -30,6 +30,7 @@ namespace NetworkingObjects { void write_create_message(cereal::PortableBinaryOutputArchive& a) const; private: friend class NetObjManager; + template friend class NetObjWeakPtr; template friend class NetObjTemporaryPtr; NetObjOwnerPtr(NetObjManager* initObjMan, NetObjID initID, T* initRawPtr); diff --git a/include/Helpers/NetworkingObjects/NetObjOwnerPtr.impl.hpp b/include/Helpers/NetworkingObjects/NetObjOwnerPtr.impl.hpp index 2c1e5d2..f6e5319 100644 --- a/include/Helpers/NetworkingObjects/NetObjOwnerPtr.impl.hpp +++ b/include/Helpers/NetworkingObjects/NetObjOwnerPtr.impl.hpp @@ -30,6 +30,7 @@ namespace NetworkingObjects { rawPtr = other.rawPtr; other.objMan = nullptr; other.rawPtr = nullptr; + return *this; } template NetObjOwnerPtr::~NetObjOwnerPtr() { @@ -69,26 +70,26 @@ namespace NetworkingObjects { } template void NetObjOwnerPtr::send_client_update(const std::string& channel, std::function&, cereal::PortableBinaryOutputArchive&)> sendUpdateFunc) const { - NetObjManager::send_client_update(*this, channel, sendUpdateFunc); + NetObjManager::send_client_update(NetObjTemporaryPtr(*this), channel, sendUpdateFunc); } template void NetObjOwnerPtr::send_server_update_to_all_clients(const std::string& channel, std::function&, cereal::PortableBinaryOutputArchive&)> sendUpdateFunc) const { - NetObjManager::send_server_update_to_all_clients(*this, channel, sendUpdateFunc); + NetObjManager::send_server_update_to_all_clients(NetObjTemporaryPtr(*this), channel, sendUpdateFunc); } template void NetObjOwnerPtr::send_server_update_to_client(const std::shared_ptr& clientToSendTo, const std::string& channel, std::function&, cereal::PortableBinaryOutputArchive&)> sendUpdateFunc) const { - NetObjManager::send_server_update_to_client(*this, clientToSendTo, channel, sendUpdateFunc); + NetObjManager::send_server_update_to_client(NetObjTemporaryPtr(*this), clientToSendTo, channel, sendUpdateFunc); } template void NetObjOwnerPtr::send_server_update_to_all_clients_except(const std::shared_ptr& clientToNotSendTo, const std::string& channel, std::function&, cereal::PortableBinaryOutputArchive&)> sendUpdateFunc) const { - NetObjManager::send_server_update_to_all_clients_except(*this, clientToNotSendTo, channel, sendUpdateFunc); + NetObjManager::send_server_update_to_all_clients_except(NetObjTemporaryPtr(*this), clientToNotSendTo, channel, sendUpdateFunc); } template void NetObjOwnerPtr::send_update_to_all(const std::string& channel, std::function&, cereal::PortableBinaryOutputArchive&)> sendUpdateFunc) const { - NetObjManager::send_update_to_all(*this, channel, sendUpdateFunc); + NetObjManager::send_update_to_all(NetObjTemporaryPtr(*this), channel, sendUpdateFunc); } template void NetObjOwnerPtr::write_create_message(cereal::PortableBinaryOutputArchive& a) const { - NetObjManager::write_create_message(*this, a); + NetObjManager::write_create_message(NetObjTemporaryPtr(*this), a); } template NetObjOwnerPtr::NetObjOwnerPtr(NetObjManager* initObjMan, NetObjID initID, T* initRawPtr): diff --git a/include/Helpers/NetworkingObjects/NetObjTemporaryPtr.decl.hpp b/include/Helpers/NetworkingObjects/NetObjTemporaryPtr.decl.hpp index 4bfa5f0..c7f44db 100644 --- a/include/Helpers/NetworkingObjects/NetObjTemporaryPtr.decl.hpp +++ b/include/Helpers/NetworkingObjects/NetObjTemporaryPtr.decl.hpp @@ -30,6 +30,7 @@ namespace NetworkingObjects { private: friend class NetObjManager; template friend class NetObjWeakPtr; + template friend class NetObjTemporaryPtr; NetObjTemporaryPtr(NetObjManager* initObjMan, NetObjID initID, T* initRawPtr); NetObjManager* objMan; diff --git a/include/Helpers/NetworkingObjects/NetObjTemporaryPtr.impl.hpp b/include/Helpers/NetworkingObjects/NetObjTemporaryPtr.impl.hpp index f0a8ad4..4822f2d 100644 --- a/include/Helpers/NetworkingObjects/NetObjTemporaryPtr.impl.hpp +++ b/include/Helpers/NetworkingObjects/NetObjTemporaryPtr.impl.hpp @@ -31,7 +31,7 @@ namespace NetworkingObjects { return *this; } template template NetObjTemporaryPtr NetObjTemporaryPtr::cast() const { - return NetObjPtr(objMan, id, static_cast(rawPtr)); + return NetObjTemporaryPtr(objMan, id, static_cast(rawPtr)); } template NetObjManager* NetObjTemporaryPtr::get_obj_man() const { diff --git a/include/Helpers/NetworkingObjects/NetObjWeakPtr.hpp b/include/Helpers/NetworkingObjects/NetObjWeakPtr.hpp index e6145de..b8f068f 100644 --- a/include/Helpers/NetworkingObjects/NetObjWeakPtr.hpp +++ b/include/Helpers/NetworkingObjects/NetObjWeakPtr.hpp @@ -27,14 +27,17 @@ namespace NetworkingObjects { NetObjWeakPtr& operator=(const NetObjOwnerPtr& other) { objMan = other.objMan; id = other.id; + return *this; } NetObjWeakPtr& operator=(const NetObjTemporaryPtr& other) { objMan = other.objMan; id = other.id; + return *this; } NetObjWeakPtr& operator=(const NetObjWeakPtr& other) { objMan = other.objMan; id = other.id; + return *this; } template NetObjWeakPtr cast() const { return NetObjWeakPtr(objMan, id);