Automatically finalize any delayed update objects
This commit is contained in:
parent
8defbe7569
commit
66d372d810
4 changed files with 38 additions and 7 deletions
|
|
@ -1,9 +1,17 @@
|
||||||
#include "DelayUpdateSerializedClassManager.hpp"
|
#include "DelayUpdateSerializedClassManager.hpp"
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
namespace NetworkingObjects {
|
namespace NetworkingObjects {
|
||||||
void DelayUpdateSerializedClassManager::update() {
|
void DelayUpdateSerializedClassManager::update(NetworkingObjects::NetObjManager& netObjMan) {
|
||||||
|
for(auto& [objID, updatingObj] : updatingObjs) {
|
||||||
|
if(updatingObj.lastTemporaryUpdateTimePoint.has_value() && (std::chrono::steady_clock::now() - updatingObj.lastTemporaryUpdateTimePoint.value()) >= std::chrono::milliseconds(300)) {
|
||||||
|
updatingObj.sendFinalUpdate();
|
||||||
|
std::cout << "yay!" << std::endl;
|
||||||
|
updatingObj.lastTemporaryUpdateTimePoint = std::nullopt;
|
||||||
|
}
|
||||||
|
}
|
||||||
std::erase_if(updatingObjs, [](auto& updatingPair) {
|
std::erase_if(updatingObjs, [](auto& updatingPair) {
|
||||||
if((std::chrono::steady_clock::now() - updatingPair.second.lastUpdateTimePoint) >= std::chrono::seconds(3)) {
|
if((std::chrono::steady_clock::now() - updatingPair.second.lastUpdateTimePoint) >= std::chrono::seconds(3) && !updatingPair.second.lastTemporaryUpdateTimePoint.has_value()) {
|
||||||
updatingPair.second.setDataAfterTimeout();
|
updatingPair.second.setDataAfterTimeout();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -60,12 +60,12 @@ namespace NetworkingObjects {
|
||||||
// TODO: When finalUpdate = false, maybe add a small delay between updates instead of sending them every frame?
|
// TODO: When finalUpdate = false, maybe add a small delay between updates instead of sending them every frame?
|
||||||
o.send_update_to_all(finalUpdate ? RELIABLE_COMMAND_CHANNEL : UNRELIABLE_COMMAND_CHANNEL, [&](const NetObjTemporaryPtr<T>& o, cereal::PortableBinaryOutputArchive& a) {
|
o.send_update_to_all(finalUpdate ? RELIABLE_COMMAND_CHANNEL : UNRELIABLE_COMMAND_CHANNEL, [&](const NetObjTemporaryPtr<T>& o, cereal::PortableBinaryOutputArchive& a) {
|
||||||
if(o.get_obj_man()->is_server())
|
if(o.get_obj_man()->is_server())
|
||||||
server_write_func(o, a);
|
server_write_func(o, a, finalUpdate);
|
||||||
else
|
else
|
||||||
client_write_update_func(o, a, finalUpdate);
|
client_write_update_func(o, a, finalUpdate);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
void update();
|
void update(NetworkingObjects::NetObjManager& netObjMan);
|
||||||
private:
|
private:
|
||||||
template <typename T> void client_write_update_func(const NetObjTemporaryPtr<T>& o, cereal::PortableBinaryOutputArchive& a, bool finalUpdate) {
|
template <typename T> void client_write_update_func(const NetObjTemporaryPtr<T>& o, cereal::PortableBinaryOutputArchive& a, bool finalUpdate) {
|
||||||
a(finalUpdate);
|
a(finalUpdate);
|
||||||
|
|
@ -84,11 +84,22 @@ namespace NetworkingObjects {
|
||||||
typeIndexFuncs[std::type_index(typeid(T))].postUpdateFunc(static_cast<void*>(oLock.get()));
|
typeIndexFuncs[std::type_index(typeid(T))].postUpdateFunc(static_cast<void*>(oLock.get()));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
toAdd.sendFinalUpdate = [&typeIndexFuncs = typeIndexFuncs, oWeak = NetObjWeakPtr<T>(o)]() {
|
||||||
|
NetObjTemporaryPtr<T> oLock = oWeak.lock();
|
||||||
|
if(oLock) {
|
||||||
|
oLock.send_update_to_all(RELIABLE_COMMAND_CHANNEL, [&typeIndexFuncs = typeIndexFuncs](const NetObjTemporaryPtr<T>& o, cereal::PortableBinaryOutputArchive& a) {
|
||||||
|
a(true); // It's the final update (sent over reliable channel)
|
||||||
|
typeIndexFuncs[std::type_index(typeid(T))].writeUpdateFunc(static_cast<const void*>(o.get()), a);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
toAdd.lastTemporaryUpdateTimePoint = finalUpdate ? std::nullopt : std::optional<std::chrono::steady_clock::time_point>(std::chrono::steady_clock::now());
|
||||||
updatingObjs.emplace(o.get_net_id(), toAdd);
|
updatingObjs.emplace(o.get_net_id(), toAdd);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
auto& [netID, updatingObjData] = *it;
|
auto& [netID, updatingObjData] = *it;
|
||||||
updatingObjData.lastUpdateTimePoint = std::chrono::steady_clock::now();
|
updatingObjData.lastUpdateTimePoint = std::chrono::steady_clock::now();
|
||||||
|
updatingObjData.lastTemporaryUpdateTimePoint = finalUpdate ? std::nullopt : std::optional<std::chrono::steady_clock::time_point>(std::chrono::steady_clock::now());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -105,7 +116,7 @@ namespace NetworkingObjects {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T> void server_write_func(const NetObjTemporaryPtr<T>& o, cereal::PortableBinaryOutputArchive& a) {
|
template <typename T> void server_write_func(const NetObjTemporaryPtr<T>& o, cereal::PortableBinaryOutputArchive& a, bool finalUpdate) {
|
||||||
typeIndexFuncs[std::type_index(typeid(T))].writeUpdateFunc(static_cast<const void*>(o.get()), a);
|
typeIndexFuncs[std::type_index(typeid(T))].writeUpdateFunc(static_cast<const void*>(o.get()), a);
|
||||||
auto it = updatingObjs.find(o.get_net_id());
|
auto it = updatingObjs.find(o.get_net_id());
|
||||||
|
|
||||||
|
|
@ -124,11 +135,21 @@ namespace NetworkingObjects {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
toAdd.sendFinalUpdate = [&typeIndexFuncs = typeIndexFuncs, oWeak = NetObjWeakPtr<T>(o)]() {
|
||||||
|
NetObjTemporaryPtr<T> oLock = oWeak.lock();
|
||||||
|
if(oLock) {
|
||||||
|
oLock.send_update_to_all(RELIABLE_COMMAND_CHANNEL, [&typeIndexFuncs = typeIndexFuncs](const NetObjTemporaryPtr<T>& o, cereal::PortableBinaryOutputArchive& a) {
|
||||||
|
typeIndexFuncs[std::type_index(typeid(T))].writeUpdateFunc(static_cast<const void*>(o.get()), a);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
toAdd.lastTemporaryUpdateTimePoint = finalUpdate ? std::nullopt : std::optional<std::chrono::steady_clock::time_point>(std::chrono::steady_clock::now());
|
||||||
updatingObjs.emplace(o.get_net_id(), toAdd);
|
updatingObjs.emplace(o.get_net_id(), toAdd);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
auto& [netID, updatingObjData] = *it;
|
auto& [netID, updatingObjData] = *it;
|
||||||
updatingObjData.lastUpdateTimePoint = std::chrono::steady_clock::now();
|
updatingObjData.lastUpdateTimePoint = std::chrono::steady_clock::now();
|
||||||
|
updatingObjData.lastTemporaryUpdateTimePoint = finalUpdate ? std::nullopt : std::optional<std::chrono::steady_clock::time_point>(std::chrono::steady_clock::now());
|
||||||
typeIndexFuncs[std::type_index(typeid(T))].assignmentFunc(static_cast<void*>(updatingObjs[o.get_net_id()].objToUpdateCurrentData.get()), static_cast<const void*>(o.get()));
|
typeIndexFuncs[std::type_index(typeid(T))].assignmentFunc(static_cast<void*>(updatingObjs[o.get_net_id()].objToUpdateCurrentData.get()), static_cast<const void*>(o.get()));
|
||||||
*updatingObjData.doLastAssignment = false;
|
*updatingObjData.doLastAssignment = false;
|
||||||
}
|
}
|
||||||
|
|
@ -158,6 +179,8 @@ namespace NetworkingObjects {
|
||||||
std::shared_ptr<bool> doLastAssignment;
|
std::shared_ptr<bool> doLastAssignment;
|
||||||
std::chrono::steady_clock::time_point lastUpdateTimePoint;
|
std::chrono::steady_clock::time_point lastUpdateTimePoint;
|
||||||
std::function<void()> setDataAfterTimeout;
|
std::function<void()> setDataAfterTimeout;
|
||||||
|
std::optional<std::chrono::steady_clock::time_point> lastTemporaryUpdateTimePoint;
|
||||||
|
std::function<void()> sendFinalUpdate;
|
||||||
};
|
};
|
||||||
std::unordered_map<NetObjID, UpdatingObject> updatingObjs;
|
std::unordered_map<NetObjID, UpdatingObject> updatingObjs;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,7 @@ const SkColor4f& CanvasTheme::get_tool_front_color() const {
|
||||||
void CanvasTheme::set_back_color(const Vector3f& newBackColor) {
|
void CanvasTheme::set_back_color(const Vector3f& newBackColor) {
|
||||||
if(backColor && newBackColor != backColor->c) {
|
if(backColor && newBackColor != backColor->c) {
|
||||||
backColor->c = newBackColor;
|
backColor->c = newBackColor;
|
||||||
world.delayedUpdateObjectManager.send_update_to_all<BackColor>(backColor, true);
|
world.delayedUpdateObjectManager.send_update_to_all<BackColor>(backColor, false);
|
||||||
set_tool_front_color(world.drawProg);
|
set_tool_front_color(world.drawProg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -217,7 +217,7 @@ void World::focus_update() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!clientStillConnecting) {
|
if(!clientStillConnecting) {
|
||||||
delayedUpdateObjectManager.update();
|
delayedUpdateObjectManager.update(netObjMan);
|
||||||
constexpr float SECONDS_TO_SEND_CAMERA_DATA = 0.5f;
|
constexpr float SECONDS_TO_SEND_CAMERA_DATA = 0.5f;
|
||||||
timeToSendCameraData.update_time_since();
|
timeToSendCameraData.update_time_since();
|
||||||
if(timeToSendCameraData.get_time_since() > SECONDS_TO_SEND_CAMERA_DATA) {
|
if(timeToSendCameraData.get_time_since() > SECONDS_TO_SEND_CAMERA_DATA) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue