Check all futures

This commit is contained in:
Lion Kortlepel 2021-10-01 03:27:24 +02:00
parent 932fbe2b2f
commit 243e96d503
No known key found for this signature in database
GPG Key ID: 4322FF2B4C71259B
4 changed files with 28 additions and 18 deletions

View File

@ -83,6 +83,7 @@ public:
void SetServer(TServer* Server) { mServer = Server; } void SetServer(TServer* Server) { mServer = Server; }
static void WaitForAll(std::vector<std::shared_ptr<TLuaResult>>& Results); static void WaitForAll(std::vector<std::shared_ptr<TLuaResult>>& Results);
void IgnoreIfNotError(const std::vector<std::shared_ptr<TLuaResult> >& Results);
[[nodiscard]] std::shared_ptr<TLuaResult> EnqueueScript(TLuaStateId StateID, const TLuaChunk& Script); [[nodiscard]] std::shared_ptr<TLuaResult> EnqueueScript(TLuaStateId StateID, const TLuaChunk& Script);
[[nodiscard]] std::shared_ptr<TLuaResult> EnqueueFunctionCall(TLuaStateId StateID, const std::string& FunctionName, const std::vector<TLuaArgTypes>& Args); [[nodiscard]] std::shared_ptr<TLuaResult> EnqueueFunctionCall(TLuaStateId StateID, const std::string& FunctionName, const std::vector<TLuaArgTypes>& Args);
void EnsureStateExists(TLuaStateId StateId, const std::string& Name, bool DontCallOnInit = false); void EnsureStateExists(TLuaStateId StateId, const std::string& Name, bool DontCallOnInit = false);
@ -174,6 +175,8 @@ private:
std::recursive_mutex mLuaEventsMutex; std::recursive_mutex mLuaEventsMutex;
std::vector<TimedEvent> mTimedEvents; std::vector<TimedEvent> mTimedEvents;
std::recursive_mutex mTimedEventsMutex; std::recursive_mutex mTimedEventsMutex;
std::queue<std::shared_ptr<TLuaResult>> mResultsToCheck;
std::recursive_mutex mResultsToCheckMutex;
}; };
//std::any TriggerLuaEvent(const std::string& Event, bool local, TLuaPlugin* Caller, std::shared_ptr<TLuaArg> arg, bool Wait); //std::any TriggerLuaEvent(const std::string& Event, bool local, TLuaPlugin* Caller, std::shared_ptr<TLuaArg> arg, bool Wait);

View File

@ -45,15 +45,14 @@ void TLuaEngine::operator()() {
beammp_lua_error("Calling \"onInit\" on \"" + Future->StateId + "\" failed: " + Future->ErrorMessage); beammp_lua_error("Calling \"onInit\" on \"" + Future->StateId + "\" failed: " + Future->ErrorMessage);
} }
} }
std::queue<std::shared_ptr<TLuaResult>> ResultsToCheck;
std::recursive_mutex ResultsToCheckMutex; auto ResultCheckThread = std::thread([&] {
std::thread ResultCheckThread([&] {
while (!mShutdown) { while (!mShutdown) {
std::this_thread::sleep_for(std::chrono::milliseconds(10)); std::this_thread::sleep_for(std::chrono::milliseconds(100));
std::unique_lock Lock(ResultsToCheckMutex); std::unique_lock Lock(mResultsToCheckMutex);
if (!ResultsToCheck.empty()) { if (!mResultsToCheck.empty()) {
auto Res = ResultsToCheck.front(); auto Res = mResultsToCheck.front();
ResultsToCheck.pop(); mResultsToCheck.pop();
Lock.unlock(); Lock.unlock();
size_t Waited = 0; size_t Waited = 0;
@ -81,10 +80,10 @@ void TLuaEngine::operator()() {
Timer.Reset(); Timer.Reset();
auto Handlers = GetEventHandlersForState(Timer.EventName, Timer.StateId); auto Handlers = GetEventHandlersForState(Timer.EventName, Timer.StateId);
std::unique_lock StateLock(mLuaStatesMutex); std::unique_lock StateLock(mLuaStatesMutex);
//std::unique_lock Lock2(ResultsToCheckMutex); std::unique_lock Lock2(mResultsToCheckMutex);
for (auto& Handler : Handlers) { for (auto& Handler : Handlers) {
auto Res = mLuaStates[Timer.StateId]->EnqueueFunctionCall(Handler, {}); auto Res = mLuaStates[Timer.StateId]->EnqueueFunctionCall(Handler, {});
//ResultsToCheck.push(Res); mResultsToCheck.push(Res);
} }
} }
} }
@ -143,6 +142,14 @@ void TLuaEngine::WaitForAll(std::vector<std::shared_ptr<TLuaResult>>& Results) {
} }
} }
// run this on the error checking thread
void TLuaEngine::IgnoreIfNotError(const std::vector<std::shared_ptr<TLuaResult>>& Results) {
std::unique_lock Lock2(mResultsToCheckMutex);
for (const auto& Result : Results) {
mResultsToCheck.push(Result);
}
}
std::shared_ptr<TLuaResult> TLuaEngine::EnqueueScript(TLuaStateId StateID, const TLuaChunk& Script) { std::shared_ptr<TLuaResult> TLuaEngine::EnqueueScript(TLuaStateId StateID, const TLuaChunk& Script) {
std::unique_lock Lock(mLuaStatesMutex); std::unique_lock Lock(mLuaStatesMutex);
return mLuaStates.at(StateID)->EnqueueScript(Script); return mLuaStates.at(StateID)->EnqueueScript(Script);

View File

@ -571,7 +571,7 @@ void TNetwork::OnDisconnect(const std::weak_ptr<TClient>& ClientPtr, bool kicked
SendToAll(&c, Packet, false, true); SendToAll(&c, Packet, false, true);
Packet.clear(); Packet.clear();
auto Futures = LuaAPI::MP::Engine->TriggerEvent("onPlayerDisconnect", "", c.GetID()); auto Futures = LuaAPI::MP::Engine->TriggerEvent("onPlayerDisconnect", "", c.GetID());
beammp_ignore(Futures); LuaAPI::MP::Engine->IgnoreIfNotError(Futures);
if (c.GetTCPSock()) if (c.GetTCPSock())
CloseSocketProper(c.GetTCPSock()); CloseSocketProper(c.GetTCPSock());
if (c.GetDownSock()) if (c.GetDownSock())
@ -605,13 +605,13 @@ void TNetwork::OnConnect(const std::weak_ptr<TClient>& c) {
auto LockedClient = c.lock(); auto LockedClient = c.lock();
LockedClient->SetID(OpenID()); LockedClient->SetID(OpenID());
beammp_info("Assigned ID " + std::to_string(LockedClient->GetID()) + " to " + LockedClient->GetName()); beammp_info("Assigned ID " + std::to_string(LockedClient->GetID()) + " to " + LockedClient->GetName());
beammp_ignore(LuaAPI::MP::Engine->TriggerEvent("onPlayerConnecting", "", LockedClient->GetID())); LuaAPI::MP::Engine->IgnoreIfNotError(LuaAPI::MP::Engine->TriggerEvent("onPlayerConnecting", "", LockedClient->GetID()));
SyncResources(*LockedClient); SyncResources(*LockedClient);
if (LockedClient->GetStatus() < 0) if (LockedClient->GetStatus() < 0)
return; return;
(void)Respond(*LockedClient, "M" + Application::Settings.MapName, true); //Send the Map on connect (void)Respond(*LockedClient, "M" + Application::Settings.MapName, true); //Send the Map on connect
beammp_info(LockedClient->GetName() + " : Connected"); beammp_info(LockedClient->GetName() + " : Connected");
beammp_ignore(LuaAPI::MP::Engine->TriggerEvent("onPlayerJoining", "", LockedClient->GetID())); LuaAPI::MP::Engine->IgnoreIfNotError(LuaAPI::MP::Engine->TriggerEvent("onPlayerJoining", "", LockedClient->GetID()));
} }
void TNetwork::SyncResources(TClient& c) { void TNetwork::SyncResources(TClient& c) {
@ -810,7 +810,7 @@ bool TNetwork::SyncClient(const std::weak_ptr<TClient>& c) {
// ignore error // ignore error
(void)SendToAll(LockedClient.get(), ("JWelcome ") + LockedClient->GetName() + "!", false, true); (void)SendToAll(LockedClient.get(), ("JWelcome ") + LockedClient->GetName() + "!", false, true);
beammp_ignore(LuaAPI::MP::Engine->TriggerEvent("onPlayerJoin", "", LockedClient->GetID())); LuaAPI::MP::Engine->IgnoreIfNotError(LuaAPI::MP::Engine->TriggerEvent("onPlayerJoin", "", LockedClient->GetID()));
LockedClient->SetIsSyncing(true); LockedClient->SetIsSyncing(true);
bool Return = false; bool Return = false;
bool res = true; bool res = true;

View File

@ -162,7 +162,7 @@ void TServer::HandleEvent(TClient& c, const std::string& Data) {
Name = t; Name = t;
break; break;
case 2: case 2:
beammp_ignore(LuaAPI::MP::Engine->TriggerEvent(Name, "", c.GetID(), t)); LuaAPI::MP::Engine->IgnoreIfNotError(LuaAPI::MP::Engine->TriggerEvent(Name, "", c.GetID(), t));
break; break;
default: default:
break; break;
@ -282,7 +282,7 @@ void TServer::ParseVehicle(TClient& c, const std::string& Pckt, TNetwork& Networ
c.SetUnicycleID(-1); c.SetUnicycleID(-1);
} }
Network.SendToAll(nullptr, Packet, true, true); Network.SendToAll(nullptr, Packet, true, true);
beammp_ignore(LuaAPI::MP::Engine->TriggerEvent("onVehicleDeleted", "", c.GetID(), VID)); LuaAPI::MP::Engine->IgnoreIfNotError(LuaAPI::MP::Engine->TriggerEvent("onVehicleDeleted", "", c.GetID(), VID));
c.DeleteCar(VID); c.DeleteCar(VID);
beammp_debug(c.GetName() + (" deleted car with ID ") + std::to_string(VID)); beammp_debug(c.GetName() + (" deleted car with ID ") + std::to_string(VID));
} }
@ -300,7 +300,7 @@ void TServer::ParseVehicle(TClient& c, const std::string& Pckt, TNetwork& Networ
if (PID != -1 && VID != -1 && PID == c.GetID()) { if (PID != -1 && VID != -1 && PID == c.GetID()) {
Data = Data.substr(Data.find('{')); Data = Data.substr(Data.find('{'));
beammp_ignore(LuaAPI::MP::Engine->TriggerEvent("onVehicleReset", "", c.GetID(), VID, Data)); LuaAPI::MP::Engine->IgnoreIfNotError(LuaAPI::MP::Engine->TriggerEvent("onVehicleReset", "", c.GetID(), VID, Data));
Network.SendToAll(&c, Packet, false, true); Network.SendToAll(&c, Packet, false, true);
} }
return; return;