GridManager now works on net objects
This commit is contained in:
parent
67507a9a39
commit
ceafbf8bb6
20 changed files with 214 additions and 222 deletions
|
|
@ -1,4 +1,5 @@
|
|||
#pragma once
|
||||
#include "Helpers/NetworkingObjects/NetObjPtr.decl.hpp"
|
||||
#include "NetObjPtr.hpp"
|
||||
|
||||
namespace NetworkingObjects {
|
||||
|
|
@ -27,4 +28,10 @@ namespace NetworkingObjects {
|
|||
},
|
||||
});
|
||||
}
|
||||
|
||||
template <typename T> void generic_serialized_class_send_update_to_all(const NetworkingObjects::NetObjPtr<T>& o) {
|
||||
o.send_update_to_all(RELIABLE_COMMAND_CHANNEL, [](const NetworkingObjects::NetObjPtr<T>& o, cereal::PortableBinaryOutputArchive& a) {
|
||||
a(*o);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -69,6 +69,7 @@ namespace NetworkingObjects {
|
|||
}
|
||||
|
||||
template <typename T> friend class NetObjPtr;
|
||||
template <typename T> friend class NetObjWeakPtr;
|
||||
|
||||
struct SingleObjectData {
|
||||
NetTypeIDType netTypeID;
|
||||
|
|
|
|||
|
|
@ -56,6 +56,7 @@ namespace NetworkingObjects {
|
|||
static uint32_t erase(const NetObjPtr<NetObjOrderedList<T>>& l, const NetObjPtr<T>& p) {
|
||||
return l->erase_by_ptr(l, p);
|
||||
}
|
||||
virtual bool contains(const NetObjPtr<T>& p) const = 0;
|
||||
virtual uint32_t size() const = 0;
|
||||
virtual const NetObjPtr<T>& at(uint32_t index) const = 0;
|
||||
virtual bool empty() const = 0;
|
||||
|
|
@ -83,6 +84,9 @@ namespace NetworkingObjects {
|
|||
template <typename T> class NetObjOrderedListServer : public NetObjOrderedList<T> {
|
||||
public:
|
||||
NetObjOrderedListServer() {}
|
||||
virtual bool contains(const NetObjPtr<T>& p) const override {
|
||||
return idToDataMap.contains(p.get_net_id());
|
||||
}
|
||||
virtual uint32_t size() const override {
|
||||
return data.size();
|
||||
}
|
||||
|
|
@ -159,8 +163,11 @@ namespace NetworkingObjects {
|
|||
uint32_t constructedSize;
|
||||
a(constructedSize);
|
||||
data.clear();
|
||||
for(uint32_t i = 0; i < constructedSize; i++)
|
||||
idToDataMap.clear();
|
||||
for(uint32_t i = 0; i < constructedSize; i++) {
|
||||
data.emplace_back(std::make_shared<NetObjOrderedListObjectInfo<T>>(l.get_obj_man()->template read_create_message<T>(a, c), static_cast<uint32_t>(data.size()), false));
|
||||
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
|
||||
// Example: the INSERT_SINGLE command in read_update, if it was inserting another NetObjOrderedList, would first construct the list using read_create_message, then it will call insert() to send it to clients
|
||||
}
|
||||
|
|
@ -190,6 +197,9 @@ namespace NetworkingObjects {
|
|||
template <typename T> class NetObjOrderedListClient : public NetObjOrderedList<T> {
|
||||
public:
|
||||
NetObjOrderedListClient() {}
|
||||
virtual bool contains(const NetObjPtr<T>& p) const override {
|
||||
return clientIdToDataMap.contains(p.get_net_id());
|
||||
}
|
||||
virtual uint32_t size() const override {
|
||||
return clientData.size();
|
||||
}
|
||||
|
|
@ -273,9 +283,14 @@ namespace NetworkingObjects {
|
|||
a(constructedSize);
|
||||
clientData.clear();
|
||||
serverData.clear();
|
||||
for(uint32_t i = 0; i < constructedSize; i++)
|
||||
serverIdToDataMap.clear();
|
||||
clientIdToDataMap.clear();
|
||||
for(uint32_t i = 0; i < constructedSize; i++) {
|
||||
serverData.emplace_back(std::make_shared<NetObjOrderedListObjectInfo<T>>(l.get_obj_man()->template read_create_message<T>(a, nullptr), static_cast<uint32_t>(serverData.size()), false));
|
||||
serverIdToDataMap.emplace(serverData.back()->obj.get_net_id(), serverData.back());
|
||||
}
|
||||
clientData = serverData;
|
||||
clientIdToDataMap = serverIdToDataMap;
|
||||
}
|
||||
virtual void read_update(const NetObjPtr<NetObjOrderedList<T>>& l, cereal::PortableBinaryInputArchive& a, const std::shared_ptr<NetServer::ClientData>&) override {
|
||||
ObjPtrOrderedListCommand_StoC c;
|
||||
|
|
|
|||
|
|
@ -21,10 +21,12 @@ namespace NetworkingObjects {
|
|||
std::strong_ordering operator<=>(const NetObjPtr<T>& rhs) const;
|
||||
void send_client_update(const std::string& channel, std::function<void(const NetObjPtr<T>&, cereal::PortableBinaryOutputArchive&)> sendUpdateFunc) const;
|
||||
void send_server_update_to_all_clients(const std::string& channel, std::function<void(const NetObjPtr<T>&, cereal::PortableBinaryOutputArchive&)> sendUpdateFunc) const;
|
||||
void send_update_to_all(const std::string& channel, std::function<void(const NetObjPtr<T>&, cereal::PortableBinaryOutputArchive&)> sendUpdateFunc) const;
|
||||
void write_create_message(cereal::PortableBinaryOutputArchive& a) const;
|
||||
private:
|
||||
friend class NetObjManager;
|
||||
template <typename S> friend class NetObjPtr;
|
||||
template <typename S> friend class NetObjWeakPtr;
|
||||
|
||||
NetObjPtr(NetObjManager* initObjMan, NetObjID initID, const std::shared_ptr<T>& initPtr);
|
||||
NetObjManager* objMan;
|
||||
|
|
|
|||
|
|
@ -89,6 +89,13 @@ namespace NetworkingObjects {
|
|||
}
|
||||
}
|
||||
|
||||
template <typename T> void NetObjPtr<T>::send_update_to_all(const std::string& channel, std::function<void(const NetObjPtr<T>&, cereal::PortableBinaryOutputArchive&)> sendUpdateFunc) const {
|
||||
if(objMan->is_server())
|
||||
send_server_update_to_all_clients(channel, sendUpdateFunc);
|
||||
else
|
||||
send_client_update(channel, sendUpdateFunc);
|
||||
}
|
||||
|
||||
template <typename T> void NetObjPtr<T>::write_create_message(cereal::PortableBinaryOutputArchive& a) const {
|
||||
a(id);
|
||||
objMan->typeList->get_type_index_data<T>(objMan->isServer).writeConstructorFunc(this->cast<void>(), a);
|
||||
|
|
|
|||
63
include/Helpers/NetworkingObjects/NetObjWeakPtr.hpp
Normal file
63
include/Helpers/NetworkingObjects/NetObjWeakPtr.hpp
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
#pragma once
|
||||
#include "NetObjID.hpp"
|
||||
#include "NetObjManager.hpp"
|
||||
#include "NetObjPtr.decl.hpp"
|
||||
|
||||
namespace NetworkingObjects {
|
||||
template <typename T> class NetObjWeakPtr {
|
||||
public:
|
||||
NetObjWeakPtr():
|
||||
objMan(nullptr),
|
||||
id({0, 0})
|
||||
{}
|
||||
NetObjWeakPtr(const NetObjWeakPtr& other):
|
||||
objMan(other.objMan),
|
||||
id(other.id)
|
||||
{}
|
||||
NetObjWeakPtr(const NetObjPtr<T>& other):
|
||||
objMan(other.objMan),
|
||||
id(other.id)
|
||||
{}
|
||||
NetObjWeakPtr& operator=(const NetObjWeakPtr& other) {
|
||||
objMan = other.objMan;
|
||||
id = other.id;
|
||||
}
|
||||
template <typename S> NetObjWeakPtr<S> cast() const {
|
||||
return NetObjWeakPtr<S>(objMan, id);
|
||||
}
|
||||
bool expired() const {
|
||||
if(!objMan)
|
||||
return true;
|
||||
auto it = objMan->objectData.find(id);
|
||||
return it == objMan->objectData.end();
|
||||
}
|
||||
NetObjPtr<T> lock() const {
|
||||
auto it = objMan->objectData.find(id);
|
||||
if(it == objMan->objectData.end())
|
||||
return NetObjPtr<T>();
|
||||
else
|
||||
return NetObjPtr<T>(objMan, id, std::static_pointer_cast<T>(it->second.p));
|
||||
}
|
||||
NetObjManager* get_obj_man() const {
|
||||
return objMan;
|
||||
}
|
||||
NetObjID get_net_id() const {
|
||||
return id;
|
||||
}
|
||||
bool operator!=(const NetObjWeakPtr<T>& rhs) const {
|
||||
return objMan != rhs.objMan || id != rhs.id;
|
||||
}
|
||||
bool operator==(const NetObjWeakPtr<T>& rhs) const {
|
||||
return objMan == rhs.objMan && id == rhs.id;
|
||||
}
|
||||
private:
|
||||
NetObjWeakPtr(NetObjManager* initObjMan, NetObjID initID):
|
||||
objMan(initObjMan),
|
||||
id(initID)
|
||||
{}
|
||||
|
||||
template <typename S> friend class NetObjPtr;
|
||||
NetObjManager* objMan;
|
||||
NetObjID id;
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue