mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2026-07-17 04:03:48 +00:00
Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 573bd77bbd | |||
| 770e03e3c6 | |||
| 11d4522dc7 | |||
| 830bc47987 | |||
| bec698fbdc | |||
| 60cc835daf | |||
| a85ce18589 |
-12
@@ -468,15 +468,3 @@ cmake-build-debug/include/commandline/Makefile
|
|||||||
*.manifest
|
*.manifest
|
||||||
*.rc
|
*.rc
|
||||||
*.res
|
*.res
|
||||||
changelog-2.0.txt
|
|
||||||
compile_commands.json
|
|
||||||
err.html
|
|
||||||
file.c
|
|
||||||
log.txt
|
|
||||||
nohup.out
|
|
||||||
notes/
|
|
||||||
patch.patch
|
|
||||||
new-backend.txt
|
|
||||||
sanitizer.txt
|
|
||||||
speedcc.fit
|
|
||||||
socket.txt
|
|
||||||
|
|||||||
+1
-1
@@ -42,7 +42,7 @@ public:
|
|||||||
// Causes all threads to finish up and exit gracefull gracefully
|
// Causes all threads to finish up and exit gracefull gracefully
|
||||||
static void GracefullyShutdown();
|
static void GracefullyShutdown();
|
||||||
static TConsole& Console() { return *mConsole; }
|
static TConsole& Console() { return *mConsole; }
|
||||||
static std::string ServerVersion() { return "2.0.2"; }
|
static std::string ServerVersion() { return "2.0.3"; }
|
||||||
static std::string ClientVersion() { return "2.0"; }
|
static std::string ClientVersion() { return "2.0"; }
|
||||||
static std::string PPS() { return mPPS; }
|
static std::string PPS() { return mPPS; }
|
||||||
static void SetPPS(std::string NewPPS) { mPPS = NewPPS; }
|
static void SetPPS(std::string NewPPS) { mPPS = NewPPS; }
|
||||||
|
|||||||
+5
-17
@@ -9,42 +9,30 @@
|
|||||||
#include <optional>
|
#include <optional>
|
||||||
#include <set>
|
#include <set>
|
||||||
|
|
||||||
#include <filesystem>
|
|
||||||
namespace fs = std::filesystem;
|
|
||||||
|
|
||||||
struct TLuaArgs final {
|
|
||||||
std::vector<std::any> Args;
|
|
||||||
};
|
|
||||||
|
|
||||||
class TLuaEngine : public IThreaded {
|
class TLuaEngine : public IThreaded {
|
||||||
public:
|
public:
|
||||||
explicit TLuaEngine(TServer& Server, TNetwork& Network);
|
explicit TLuaEngine(TServer& Server, TNetwork& Network);
|
||||||
|
|
||||||
using TSetOfLuaFile = std::vector<std::shared_ptr<TLuaFile>>;
|
using TSetOfLuaFile = std::set<std::unique_ptr<TLuaFile>>;
|
||||||
|
|
||||||
void operator()() override;
|
void operator()() override;
|
||||||
|
|
||||||
std::any TriggerLuaEvent(const std::string& Event, bool local, std::weak_ptr<TLuaFile> Caller, std::shared_ptr<TLuaArgs> arg, bool Wait);
|
[[nodiscard]] const TSetOfLuaFile& LuaFiles() const { return mLuaFiles; }
|
||||||
|
|
||||||
[[nodiscard]] TServer& Server() { return mServer; }
|
[[nodiscard]] TServer& Server() { return mServer; }
|
||||||
[[nodiscard]] const TServer& Server() const { return mServer; }
|
[[nodiscard]] const TServer& Server() const { return mServer; }
|
||||||
[[nodiscard]] TNetwork& Network() { return mNetwork; }
|
[[nodiscard]] TNetwork& Network() { return mNetwork; }
|
||||||
[[nodiscard]] const TNetwork& Network() const { return mNetwork; }
|
[[nodiscard]] const TNetwork& Network() const { return mNetwork; }
|
||||||
|
|
||||||
void UnregisterScript(std::shared_ptr<TLuaFile> Script);
|
std::optional<std::reference_wrapper<TLuaFile>> GetScript(lua_State* L);
|
||||||
std::shared_ptr<TLuaFile> GetLuaFileOfState(lua_State* L);
|
|
||||||
std::shared_ptr<TLuaFile> InsertNewLuaFile(const fs::path& FileName, const std::string& PluginName);
|
|
||||||
void SendError(lua_State* L, const std::string& msg);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void FolderList(const std::string& Path, bool HotSwap);
|
void FolderList(const std::string& Path, bool HotSwap);
|
||||||
void RegisterFiles(const fs::path& Path, bool HotSwap);
|
void RegisterFiles(const std::string& Path, bool HotSwap);
|
||||||
bool IsNewFile(const std::string& Path);
|
bool NewFile(const std::string& Path);
|
||||||
|
|
||||||
TNetwork& mNetwork;
|
TNetwork& mNetwork;
|
||||||
TServer& mServer;
|
TServer& mServer;
|
||||||
std::string mPath;
|
std::string mPath;
|
||||||
bool mShutdown { false };
|
bool mShutdown { false };
|
||||||
mutable std::mutex mLuaFilesMutex;
|
|
||||||
TSetOfLuaFile mLuaFiles;
|
TSetOfLuaFile mLuaFiles;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,7 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include <unordered_map>
|
|
||||||
|
|
||||||
class TLuaEvent final {
|
|
||||||
};
|
|
||||||
+11
-10
@@ -11,14 +11,15 @@
|
|||||||
|
|
||||||
namespace fs = std::filesystem;
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
|
struct TLuaArg {
|
||||||
|
std::vector<std::any> args;
|
||||||
|
void PushArgs(lua_State* State);
|
||||||
|
};
|
||||||
|
|
||||||
class TLuaEngine;
|
class TLuaEngine;
|
||||||
class TLuaArgs;
|
|
||||||
|
|
||||||
class TLuaFile {
|
class TLuaFile {
|
||||||
public:
|
public:
|
||||||
explicit TLuaFile(TLuaEngine& Engine, bool Console = false);
|
|
||||||
~TLuaFile();
|
|
||||||
|
|
||||||
void RegisterEvent(const std::string& Event, const std::string& FunctionName);
|
void RegisterEvent(const std::string& Event, const std::string& FunctionName);
|
||||||
void UnRegisterEvent(const std::string& Event);
|
void UnRegisterEvent(const std::string& Event);
|
||||||
void SetLastWrite(fs::file_time_type time);
|
void SetLastWrite(fs::file_time_type time);
|
||||||
@@ -29,8 +30,11 @@ public:
|
|||||||
fs::file_time_type GetLastWrite();
|
fs::file_time_type GetLastWrite();
|
||||||
lua_State* GetState();
|
lua_State* GetState();
|
||||||
std::string GetOrigin();
|
std::string GetOrigin();
|
||||||
|
std::mutex Lock;
|
||||||
void Reload();
|
void Reload();
|
||||||
void Init(const std::string& PluginName, const std::string& FileName, fs::file_time_type LastWrote);
|
void Init(const std::string& PluginName, const std::string& FileName, fs::file_time_type LastWrote);
|
||||||
|
explicit TLuaFile(TLuaEngine& Engine, bool Console = false);
|
||||||
|
~TLuaFile();
|
||||||
void SetStopThread(bool StopThread) { mStopThread = StopThread; }
|
void SetStopThread(bool StopThread) { mStopThread = StopThread; }
|
||||||
TLuaEngine& Engine() { return mEngine; }
|
TLuaEngine& Engine() { return mEngine; }
|
||||||
[[nodiscard]] std::string GetPluginName() const;
|
[[nodiscard]] std::string GetPluginName() const;
|
||||||
@@ -39,14 +43,8 @@ public:
|
|||||||
[[nodiscard]] bool GetStopThread() const { return mStopThread; }
|
[[nodiscard]] bool GetStopThread() const { return mStopThread; }
|
||||||
[[nodiscard]] const TLuaEngine& Engine() const { return mEngine; }
|
[[nodiscard]] const TLuaEngine& Engine() const { return mEngine; }
|
||||||
[[nodiscard]] std::string GetRegistered(const std::string& Event) const;
|
[[nodiscard]] std::string GetRegistered(const std::string& Event) const;
|
||||||
void PushArgs(const TLuaArgs& args);
|
|
||||||
|
|
||||||
bool operator==(const TLuaFile& Other) const;
|
|
||||||
|
|
||||||
void Load();
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::mutex Lock;
|
|
||||||
TLuaEngine& mEngine;
|
TLuaEngine& mEngine;
|
||||||
std::set<std::pair<std::string, std::string>> mRegisteredEvents;
|
std::set<std::pair<std::string, std::string>> mRegisteredEvents;
|
||||||
lua_State* mLuaState { nullptr };
|
lua_State* mLuaState { nullptr };
|
||||||
@@ -55,6 +53,9 @@ private:
|
|||||||
std::string mFileName {};
|
std::string mFileName {};
|
||||||
bool mStopThread = false;
|
bool mStopThread = false;
|
||||||
bool mConsole = false;
|
bool mConsole = false;
|
||||||
|
void Load();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
std::any TriggerLuaEvent(const std::string& Event, bool local, TLuaFile* Caller, std::shared_ptr<TLuaArg> arg, bool Wait);
|
||||||
|
|
||||||
#endif // TLUAFILE_H
|
#endif // TLUAFILE_H
|
||||||
|
|||||||
+1
-1
@@ -44,6 +44,6 @@ private:
|
|||||||
void OnDisconnect(const std::weak_ptr<TClient>& ClientPtr, bool kicked);
|
void OnDisconnect(const std::weak_ptr<TClient>& ClientPtr, bool kicked);
|
||||||
void Parse(TClient& c, const std::string& Packet);
|
void Parse(TClient& c, const std::string& Packet);
|
||||||
void SendFile(TClient& c, const std::string& Name);
|
void SendFile(TClient& c, const std::string& Name);
|
||||||
static bool TCPSendRaw(SOCKET C, char* Data, int32_t Size);
|
static bool TCPSendRaw(TClient& C, SOCKET socket, char* Data, int32_t Size);
|
||||||
static void SplitLoad(TClient& c, size_t Sent, size_t Size, bool D, const std::string& Name);
|
static void SplitLoad(TClient& c, size_t Sent, size_t Size, bool D, const std::string& Name);
|
||||||
};
|
};
|
||||||
|
|||||||
+1
-1
Submodule include/commandline updated: 412ece748d...0860266a2a
+30
-203
@@ -1,16 +1,18 @@
|
|||||||
#include "TLuaEngine.h"
|
#include "TLuaEngine.h"
|
||||||
#include "Client.h"
|
|
||||||
#include "CustomAssert.h"
|
|
||||||
#include "TLuaFile.h"
|
#include "TLuaFile.h"
|
||||||
|
|
||||||
#include <lua.hpp>
|
#include <filesystem>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
extern TLuaEngine* TheLuaEngine;
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
|
// necessary as lua relies on global state
|
||||||
|
TLuaEngine* TheEngine;
|
||||||
|
|
||||||
TLuaEngine::TLuaEngine(TServer& Server, TNetwork& Network)
|
TLuaEngine::TLuaEngine(TServer& Server, TNetwork& Network)
|
||||||
: mNetwork(Network)
|
: mNetwork(Network)
|
||||||
, mServer(Server) {
|
, mServer(Server) {
|
||||||
TheLuaEngine = this;
|
TheEngine = this;
|
||||||
if (!fs::exists(Application::Settings.Resource)) {
|
if (!fs::exists(Application::Settings.Resource)) {
|
||||||
fs::create_directory(Application::Settings.Resource);
|
fs::create_directory(Application::Settings.Resource);
|
||||||
}
|
}
|
||||||
@@ -29,27 +31,16 @@ TLuaEngine::TLuaEngine(TServer& Server, TNetwork& Network)
|
|||||||
Start();
|
Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
void TLuaEngine::UnregisterScript(std::shared_ptr<TLuaFile> Script) {
|
|
||||||
std::unique_lock Lock(mLuaFilesMutex);
|
|
||||||
auto Iter = std::find_if(mLuaFiles.begin(), mLuaFiles.end(), [&](const auto& ScriptPtr) {
|
|
||||||
return *Script == *ScriptPtr;
|
|
||||||
});
|
|
||||||
if (Iter != mLuaFiles.end()) {
|
|
||||||
// found!
|
|
||||||
mLuaFiles.erase(Iter);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void TLuaEngine::operator()() {
|
void TLuaEngine::operator()() {
|
||||||
RegisterThread("LuaEngine");
|
RegisterThread("LuaEngine");
|
||||||
info("Lua system online");
|
info("Lua system online");
|
||||||
while (!mShutdown) {
|
while (!mShutdown) {
|
||||||
if (!mLuaFiles.empty()) {
|
if (!mLuaFiles.empty()) {
|
||||||
for (auto& Script : mLuaFiles) {
|
for (auto& Script : mLuaFiles) {
|
||||||
const auto& filename = Script->GetFileName();
|
struct stat Info { };
|
||||||
if (!fs::is_regular_file(filename)) {
|
if (stat(Script->GetFileName().c_str(), &Info) != 0) {
|
||||||
Script->SetStopThread(true);
|
Script->SetStopThread(true);
|
||||||
UnregisterScript(Script);
|
mLuaFiles.erase(Script);
|
||||||
info(("[HOTSWAP] Removed removed script due to delete"));
|
info(("[HOTSWAP] Removed removed script due to delete"));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -66,211 +57,47 @@ void TLuaEngine::operator()() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::any TLuaEngine::TriggerLuaEvent(const std::string& Event, bool local, std::weak_ptr<TLuaFile> Caller, std::shared_ptr<TLuaArgs> arg, bool Wait) {
|
std::optional<std::reference_wrapper<TLuaFile>> TLuaEngine::GetScript(lua_State* L) {
|
||||||
std::any R;
|
|
||||||
std::string Type;
|
|
||||||
int Ret = 0;
|
|
||||||
// TODO: lock
|
|
||||||
for (auto& Script : mLuaFiles) {
|
|
||||||
if (Script->IsRegistered(Event)) {
|
|
||||||
if (local) {
|
|
||||||
if (Script->GetPluginName() == Caller->GetPluginName()) {
|
|
||||||
R = FutureWait(Script.get(), Script->GetRegistered(Event), arg, Wait);
|
|
||||||
Type = R.type().name();
|
|
||||||
if (Type.find("int") != std::string::npos) {
|
|
||||||
if (std::any_cast<int>(R))
|
|
||||||
Ret++;
|
|
||||||
} else if (Event == "onPlayerAuth")
|
|
||||||
return R;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
R = FutureWait(Script.get(), Script->GetRegistered(Event), arg, Wait);
|
|
||||||
Type = R.type().name();
|
|
||||||
if (Type.find("int") != std::string::npos) {
|
|
||||||
if (std::any_cast<int>(R))
|
|
||||||
Ret++;
|
|
||||||
} else if (Event == "onPlayerAuth")
|
|
||||||
return R;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return Ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::shared_ptr<TLuaFile> TLuaEngine::GetLuaFileOfState(lua_State* L) {
|
|
||||||
std::unique_lock Lock(mLuaFilesMutex);
|
|
||||||
for (auto& Script : mLuaFiles) {
|
for (auto& Script : mLuaFiles) {
|
||||||
if (Script->GetState() == L)
|
if (Script->GetState() == L)
|
||||||
return Script;
|
return *Script;
|
||||||
}
|
}
|
||||||
return nullptr;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TLuaEngine::FolderList(const std::string& Path, bool HotSwap) {
|
void TLuaEngine::FolderList(const std::string& Path, bool HotSwap) {
|
||||||
for (const auto& entry : fs::directory_iterator(Path)) {
|
for (const auto& entry : fs::directory_iterator(Path)) {
|
||||||
if (fs::is_directory(entry)) {
|
auto pos = entry.path().filename().string().find('.');
|
||||||
RegisterFiles(entry.path(), HotSwap);
|
if (pos == std::string::npos) {
|
||||||
|
RegisterFiles(entry.path().string(), HotSwap);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void TLuaEngine::RegisterFiles(const fs::path& Path, bool HotSwap) {
|
void TLuaEngine::RegisterFiles(const std::string& Path, bool HotSwap) {
|
||||||
std::string Name = Path.filename();
|
std::string Name = Path.substr(Path.find_last_of('\\') + 1);
|
||||||
if (!HotSwap) {
|
if (!HotSwap)
|
||||||
info(("Loading plugin : ") + Name);
|
info(("Loading plugin : ") + Name);
|
||||||
}
|
|
||||||
for (const auto& entry : fs::directory_iterator(Path)) {
|
for (const auto& entry : fs::directory_iterator(Path)) {
|
||||||
if (entry.is_regular_file() && entry.path().extension() == ".lua") {
|
auto pos = entry.path().string().find((".lua"));
|
||||||
if (!HotSwap || IsNewFile(entry.path().string())) {
|
if (pos != std::string::npos && entry.path().string().length() - pos == 4) {
|
||||||
InsertNewLuaFile(entry.path(), Path.filename());
|
if (!HotSwap || NewFile(entry.path().string())) {
|
||||||
if (HotSwap) {
|
auto FileName = entry.path().string();
|
||||||
info(("[HOTSWAP] Added : ") + entry.path().filename().string());
|
std::unique_ptr<TLuaFile> ScriptToInsert(new TLuaFile(*this));
|
||||||
}
|
auto& Script = *ScriptToInsert;
|
||||||
|
mLuaFiles.insert(std::move(ScriptToInsert));
|
||||||
|
Script.Init(Name, FileName, fs::last_write_time(FileName));
|
||||||
|
if (HotSwap)
|
||||||
|
info(("[HOTSWAP] Added : ") + Script.GetFileName().substr(Script.GetFileName().find('\\')));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool TLuaEngine::IsNewFile(const std::string& Path) {
|
bool TLuaEngine::NewFile(const std::string& Path) {
|
||||||
std::unique_lock Lock(mLuaFilesMutex);
|
|
||||||
for (auto& Script : mLuaFiles) {
|
for (auto& Script : mLuaFiles) {
|
||||||
// TODO: use std::any_of
|
|
||||||
if (Path == Script->GetFileName())
|
if (Path == Script->GetFileName())
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TLuaEngine::SendError(lua_State* L, const std::string& msg) {
|
|
||||||
Assert(L);
|
|
||||||
auto MaybeS = GetLuaFileOfState(L);
|
|
||||||
std::string a;
|
|
||||||
if (!MaybeS) {
|
|
||||||
a = ("_Console");
|
|
||||||
} else {
|
|
||||||
// FIXME: GetFileName already returns the filename. This is redundant.
|
|
||||||
a = fs::path(MaybeS->GetFileName()).filename().string();
|
|
||||||
}
|
|
||||||
warn(a + (" | Incorrect call of ") + msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
// //////////////////////////////////////////////
|
|
||||||
// ////////// LUA *ONLY* BELOW //////////////////
|
|
||||||
// //////////////////////////////////////////////
|
|
||||||
|
|
||||||
std::shared_ptr<TClient> GetClient(TServer& Server, int ID) {
|
|
||||||
std::shared_ptr<TClient> MaybeClient = nullptr;
|
|
||||||
Server.ForEachClient([&](std::weak_ptr<TClient> CPtr) -> bool {
|
|
||||||
ReadLock Lock(Server.GetClientMutex());
|
|
||||||
if (!CPtr.expired()) {
|
|
||||||
auto C = CPtr.lock();
|
|
||||||
if (C->GetID() == ID) {
|
|
||||||
MaybeClient = C;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
});
|
|
||||||
return MaybeClient;
|
|
||||||
}
|
|
||||||
|
|
||||||
int ServerLua_GetIdentifiers(lua_State* L) {
|
|
||||||
Assert(TheLuaEngine);
|
|
||||||
Assert(L);
|
|
||||||
if (lua_isnumber(L, 1)) {
|
|
||||||
// do not store this
|
|
||||||
// FIXME: should this not be lua_tointeger?!
|
|
||||||
auto MaybeClient = GetClient(TheLuaEngine->Server(), int(lua_tonumber(L, 1)));
|
|
||||||
if (MaybeClient) {
|
|
||||||
auto IDs = MaybeClient->GetIdentifiers();
|
|
||||||
if (IDs.empty())
|
|
||||||
return 0;
|
|
||||||
// FIXME: What does this entire thing do? Add comments.
|
|
||||||
lua_newtable(L);
|
|
||||||
for (const std::string& ID : IDs) {
|
|
||||||
lua_pushstring(L, ID.substr(0, ID.find(':')).c_str());
|
|
||||||
lua_pushstring(L, ID.c_str());
|
|
||||||
lua_settable(L, -3);
|
|
||||||
}
|
|
||||||
} else
|
|
||||||
return 0;
|
|
||||||
} else {
|
|
||||||
TheLuaEngine->SendError(L, "lua_GetIdentifiers wrong arguments");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static std::shared_ptr<TLuaArgs> CollectArgs(lua_State* L) {
|
|
||||||
int ArgCount = lua_gettop(L);
|
|
||||||
if (ArgCount == 0) {
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
std::shared_ptr<TLuaArgs> Temp = std::make_shared<TLuaArgs>();
|
|
||||||
for (int C = 2; C <= ArgCount; C++) {
|
|
||||||
if (lua_isstring(L, C)) {
|
|
||||||
Temp->Args.emplace_back(std::string(lua_tostring(L, C)));
|
|
||||||
} else if (lua_isinteger(L, C)) {
|
|
||||||
Temp->Args.emplace_back(int(lua_tointeger(L, C)));
|
|
||||||
} else if (lua_isboolean(L, C)) {
|
|
||||||
Temp->Args.emplace_back(bool(lua_toboolean(L, C)));
|
|
||||||
} else if (lua_isnumber(L, C)) {
|
|
||||||
Temp->Args.emplace_back(float(lua_tonumber(L, C)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return Temp;
|
|
||||||
}
|
|
||||||
|
|
||||||
int ServerLua_TriggerEventG(lua_State* L) {
|
|
||||||
auto MaybeScript = TheLuaEngine->GetLuaFileOfState(L);
|
|
||||||
Assert(!!MaybeScript);
|
|
||||||
if (lua_gettop(L) <= 0) {
|
|
||||||
TheLuaEngine->SendError(L, ("TriggerGlobalEvent not enough arguments"));
|
|
||||||
return 0;
|
|
||||||
} else if (!lua_isstring(L, 1)) {
|
|
||||||
TheLuaEngine->SendError(L, "TriggerGlobalEvent wrong argument [1] need string");
|
|
||||||
}
|
|
||||||
std::string Name = lua_tostring(L, 1);
|
|
||||||
auto Args = CollectArgs(L);
|
|
||||||
TheLuaEngine->TriggerLuaEvent(Name, false, MaybeScript, Args, false);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// LEAVE THIS AT THE **VERY** BOTTOM OF THE FILE
|
|
||||||
|
|
||||||
std::shared_ptr<TLuaFile> TLuaEngine::InsertNewLuaFile(const fs::path& FileName, const std::string& PluginName) {
|
|
||||||
std::shared_ptr<TLuaFile> Script(new TLuaFile(*this));
|
|
||||||
mLuaFiles.push_back(Script);
|
|
||||||
Script->SetPluginName(PluginName);
|
|
||||||
Script->SetFileName(FileName);
|
|
||||||
Script->SetLastWrite(fs::last_write_time(FileName));
|
|
||||||
lua_State* State = Script->GetState();
|
|
||||||
luaL_openlibs(State);
|
|
||||||
lua_register(State, "GetPlayerIdentifiers", ServerLua_GetIdentifiers);
|
|
||||||
lua_register(State, "TriggerGlobalEvent", ServerLua_TriggerEventG);
|
|
||||||
lua_register(State, "TriggerLocalEvent", lua_TriggerEventL);
|
|
||||||
lua_register(State, "TriggerClientEvent", lua_RemoteEvent);
|
|
||||||
lua_register(State, "GetPlayerCount", lua_GetPlayerCount);
|
|
||||||
lua_register(State, "isPlayerConnected", lua_isConnected);
|
|
||||||
lua_register(State, "RegisterEvent", lua_RegisterEvent);
|
|
||||||
lua_register(State, "GetPlayerName", lua_GetPlayerName);
|
|
||||||
lua_register(State, "RemoveVehicle", lua_RemoveVehicle);
|
|
||||||
lua_register(State, "GetPlayerDiscordID", lua_TempFix);
|
|
||||||
lua_register(State, "CreateThread", lua_CreateThread);
|
|
||||||
lua_register(State, "GetPlayerVehicles", lua_GetCars);
|
|
||||||
lua_register(State, "SendChatMessage", lua_sendChat);
|
|
||||||
lua_register(State, "GetPlayers", lua_GetAllPlayers);
|
|
||||||
lua_register(State, "GetPlayerGuest", lua_GetGuest);
|
|
||||||
lua_register(State, "StopThread", lua_StopThread);
|
|
||||||
lua_register(State, "DropPlayer", lua_dropPlayer);
|
|
||||||
lua_register(State, "GetPlayerHWID", lua_HWID);
|
|
||||||
lua_register(State, "exit", lua_ServerExit);
|
|
||||||
lua_register(State, "Sleep", lua_Sleep);
|
|
||||||
lua_register(State, "print", lua_Print);
|
|
||||||
lua_register(State, "Set", lua_Set);
|
|
||||||
if (!Script->IsConsole()) {
|
|
||||||
Reload();
|
|
||||||
}
|
|
||||||
debug("inserted new lua file: " + PluginName + " - " + FileName.string());
|
|
||||||
return Script;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,2 +0,0 @@
|
|||||||
#include "TLuaEvent.h"
|
|
||||||
|
|
||||||
+156
-20
@@ -12,13 +12,41 @@
|
|||||||
// TODO: REWRITE
|
// TODO: REWRITE
|
||||||
|
|
||||||
void SendError(TLuaEngine& Engine, lua_State* L, const std::string& msg);
|
void SendError(TLuaEngine& Engine, lua_State* L, const std::string& msg);
|
||||||
std::any CallFunction(TLuaFile* lua, const std::string& FuncName, std::shared_ptr<TLuaArgs> Arg);
|
std::any CallFunction(TLuaFile* lua, const std::string& FuncName, std::shared_ptr<TLuaArg> Arg);
|
||||||
std::any TriggerLuaEvent(TLuaEngine& Engine, const std::string& Event, bool local, TLuaFile* Caller, std::shared_ptr<TLuaArgs> arg, bool Wait);
|
std::any TriggerLuaEvent(TLuaEngine& Engine, const std::string& Event, bool local, TLuaFile* Caller, std::shared_ptr<TLuaArg> arg, bool Wait);
|
||||||
|
|
||||||
std::any Trigger(TLuaFile* lua, const std::string& R, std::shared_ptr<TLuaArgs> arg) {
|
extern TLuaEngine* TheEngine;
|
||||||
|
|
||||||
|
static TLuaEngine& Engine() {
|
||||||
|
Assert(TheEngine);
|
||||||
|
return *TheEngine;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<TLuaArg> CreateArg(lua_State* L, int T, int S) {
|
||||||
|
if (S > T)
|
||||||
|
return nullptr;
|
||||||
|
std::shared_ptr<TLuaArg> temp(new TLuaArg);
|
||||||
|
for (int C = S; C <= T; C++) {
|
||||||
|
if (lua_isstring(L, C)) {
|
||||||
|
temp->args.emplace_back(std::string(lua_tostring(L, C)));
|
||||||
|
} else if (lua_isinteger(L, C)) {
|
||||||
|
temp->args.emplace_back(int(lua_tointeger(L, C)));
|
||||||
|
} else if (lua_isboolean(L, C)) {
|
||||||
|
temp->args.emplace_back(bool(lua_toboolean(L, C)));
|
||||||
|
} else if (lua_isnumber(L, C)) {
|
||||||
|
temp->args.emplace_back(float(lua_tonumber(L, C)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return temp;
|
||||||
|
}
|
||||||
|
void ClearStack(lua_State* L) {
|
||||||
|
lua_settop(L, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::any Trigger(TLuaFile* lua, const std::string& R, std::shared_ptr<TLuaArg> arg) {
|
||||||
RegisterThread(lua->GetFileName());
|
RegisterThread(lua->GetFileName());
|
||||||
std::lock_guard<std::mutex> lockGuard(lua->Lock);
|
std::lock_guard<std::mutex> lockGuard(lua->Lock);
|
||||||
std::packaged_task<std::any(std::shared_ptr<TLuaArg>)> task([lua, R](std::shared_ptr<TLuaArgs> arg) { return CallFunction(lua, R, arg); });
|
std::packaged_task<std::any(std::shared_ptr<TLuaArg>)> task([lua, R](std::shared_ptr<TLuaArg> arg) { return CallFunction(lua, R, arg); });
|
||||||
std::future<std::any> f1 = task.get_future();
|
std::future<std::any> f1 = task.get_future();
|
||||||
std::thread t(std::move(task), arg);
|
std::thread t(std::move(task), arg);
|
||||||
t.detach();
|
t.detach();
|
||||||
@@ -28,9 +56,9 @@ std::any Trigger(TLuaFile* lua, const std::string& R, std::shared_ptr<TLuaArgs>
|
|||||||
SendError(lua->Engine(), lua->GetState(), R + " took too long to respond");
|
SendError(lua->Engine(), lua->GetState(), R + " took too long to respond");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
std::any FutureWait(TLuaFile* lua, const std::string& R, std::shared_ptr<TLuaArgs> arg, bool Wait) {
|
std::any FutureWait(TLuaFile* lua, const std::string& R, std::shared_ptr<TLuaArg> arg, bool Wait) {
|
||||||
Assert(lua);
|
Assert(lua);
|
||||||
std::packaged_task<std::any(std::shared_ptr<TLuaArg>)> task([lua, R](std::shared_ptr<TLuaArgs> arg) { return Trigger(lua, R, arg); });
|
std::packaged_task<std::any(std::shared_ptr<TLuaArg>)> task([lua, R](std::shared_ptr<TLuaArg> arg) { return Trigger(lua, R, arg); });
|
||||||
std::future<std::any> f1 = task.get_future();
|
std::future<std::any> f1 = task.get_future();
|
||||||
std::thread t(std::move(task), arg);
|
std::thread t(std::move(task), arg);
|
||||||
t.detach();
|
t.detach();
|
||||||
@@ -42,6 +70,35 @@ std::any FutureWait(TLuaFile* lua, const std::string& R, std::shared_ptr<TLuaArg
|
|||||||
return f1.get();
|
return f1.get();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
std::any TriggerLuaEvent(const std::string& Event, bool local, TLuaFile* Caller, std::shared_ptr<TLuaArg> arg, bool Wait) {
|
||||||
|
std::any R;
|
||||||
|
std::string Type;
|
||||||
|
int Ret = 0;
|
||||||
|
for (auto& Script : Engine().LuaFiles()) {
|
||||||
|
if (Script->IsRegistered(Event)) {
|
||||||
|
if (local) {
|
||||||
|
if (Script->GetPluginName() == Caller->GetPluginName()) {
|
||||||
|
R = FutureWait(Script.get(), Script->GetRegistered(Event), arg, Wait);
|
||||||
|
Type = R.type().name();
|
||||||
|
if (Type.find("int") != std::string::npos) {
|
||||||
|
if (std::any_cast<int>(R))
|
||||||
|
Ret++;
|
||||||
|
} else if (Event == "onPlayerAuth")
|
||||||
|
return R;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
R = FutureWait(Script.get(), Script->GetRegistered(Event), arg, Wait);
|
||||||
|
Type = R.type().name();
|
||||||
|
if (Type.find("int") != std::string::npos) {
|
||||||
|
if (std::any_cast<int>(R))
|
||||||
|
Ret++;
|
||||||
|
} else if (Event == "onPlayerAuth")
|
||||||
|
return R;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Ret;
|
||||||
|
}
|
||||||
bool ConsoleCheck(lua_State* L, int r) {
|
bool ConsoleCheck(lua_State* L, int r) {
|
||||||
if (r != LUA_OK) {
|
if (r != LUA_OK) {
|
||||||
std::string msg = lua_tostring(L, -1);
|
std::string msg = lua_tostring(L, -1);
|
||||||
@@ -173,7 +230,21 @@ int lua_Sleep(lua_State* L) {
|
|||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
std::optional<std::weak_ptr<TClient>> GetClient(TServer& Server, int ID) {
|
||||||
|
std::optional<std::weak_ptr<TClient>> MaybeClient { std::nullopt };
|
||||||
|
Server.ForEachClient([&](std::weak_ptr<TClient> CPtr) -> bool {
|
||||||
|
ReadLock Lock(Server.GetClientMutex());
|
||||||
|
if (!CPtr.expired()) {
|
||||||
|
auto C = CPtr.lock();
|
||||||
|
if (C->GetID() == ID) {
|
||||||
|
MaybeClient = CPtr;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
return MaybeClient;
|
||||||
|
}
|
||||||
// CONTINUE
|
// CONTINUE
|
||||||
int lua_isConnected(lua_State* L) {
|
int lua_isConnected(lua_State* L) {
|
||||||
if (lua_isnumber(L, 1)) {
|
if (lua_isnumber(L, 1)) {
|
||||||
@@ -240,6 +311,27 @@ int lua_GetAllPlayers(lua_State* L) {
|
|||||||
return 0;
|
return 0;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
int lua_GetIdentifiers(lua_State* L) {
|
||||||
|
if (lua_isnumber(L, 1)) {
|
||||||
|
auto MaybeClient = GetClient(Engine().Server(), int(lua_tonumber(L, 1)));
|
||||||
|
if (MaybeClient && !MaybeClient.value().expired()) {
|
||||||
|
auto IDs = MaybeClient.value().lock()->GetIdentifiers();
|
||||||
|
if (IDs.empty())
|
||||||
|
return 0;
|
||||||
|
lua_newtable(L);
|
||||||
|
for (const std::string& ID : IDs) {
|
||||||
|
lua_pushstring(L, ID.substr(0, ID.find(':')).c_str());
|
||||||
|
lua_pushstring(L, ID.c_str());
|
||||||
|
lua_settable(L, -3);
|
||||||
|
}
|
||||||
|
} else
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
SendError(Engine(), L, "lua_GetIdentifiers wrong arguments");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
int lua_GetCars(lua_State* L) {
|
int lua_GetCars(lua_State* L) {
|
||||||
if (lua_isnumber(L, 1)) {
|
if (lua_isnumber(L, 1)) {
|
||||||
@@ -498,7 +590,19 @@ int lua_Print(lua_State* L) {
|
|||||||
int lua_TempFix(lua_State* L);
|
int lua_TempFix(lua_State* L);
|
||||||
|
|
||||||
void TLuaFile::Init(const std::string& PluginName, const std::string& FileName, fs::file_time_type LastWrote) {
|
void TLuaFile::Init(const std::string& PluginName, const std::string& FileName, fs::file_time_type LastWrote) {
|
||||||
|
// set global engine for lua_* functions
|
||||||
|
if (!TheEngine) {
|
||||||
|
TheEngine = &mEngine;
|
||||||
|
}
|
||||||
Assert(mLuaState);
|
Assert(mLuaState);
|
||||||
|
if (!PluginName.empty()) {
|
||||||
|
SetPluginName(PluginName);
|
||||||
|
}
|
||||||
|
if (!FileName.empty()) {
|
||||||
|
SetFileName(FileName);
|
||||||
|
}
|
||||||
|
SetLastWrite(LastWrote);
|
||||||
|
Load();
|
||||||
}
|
}
|
||||||
|
|
||||||
TLuaFile::TLuaFile(TLuaEngine& Engine, bool Console)
|
TLuaFile::TLuaFile(TLuaEngine& Engine, bool Console)
|
||||||
@@ -524,7 +628,7 @@ std::string TLuaFile::GetOrigin() {
|
|||||||
return fs::path(GetFileName()).filename().string();
|
return fs::path(GetFileName()).filename().string();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::any CallFunction(TLuaFile* lua, const std::string& FuncName, std::shared_ptr<TLuaArgs> Arg) {
|
std::any CallFunction(TLuaFile* lua, const std::string& FuncName, std::shared_ptr<TLuaArg> Arg) {
|
||||||
RegisterThread(lua->GetFileName());
|
RegisterThread(lua->GetFileName());
|
||||||
lua_State* luaState = lua->GetState();
|
lua_State* luaState = lua->GetState();
|
||||||
lua_getglobal(luaState, FuncName.c_str());
|
lua_getglobal(luaState, FuncName.c_str());
|
||||||
@@ -556,6 +660,32 @@ void TLuaFile::SetFileName(const std::string& Name) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void TLuaFile::Load() {
|
void TLuaFile::Load() {
|
||||||
|
Assert(mLuaState);
|
||||||
|
luaL_openlibs(mLuaState);
|
||||||
|
lua_register(mLuaState, "GetPlayerIdentifiers", lua_GetIdentifiers);
|
||||||
|
lua_register(mLuaState, "TriggerGlobalEvent", lua_TriggerEventG);
|
||||||
|
lua_register(mLuaState, "TriggerLocalEvent", lua_TriggerEventL);
|
||||||
|
lua_register(mLuaState, "TriggerClientEvent", lua_RemoteEvent);
|
||||||
|
lua_register(mLuaState, "GetPlayerCount", lua_GetPlayerCount);
|
||||||
|
lua_register(mLuaState, "isPlayerConnected", lua_isConnected);
|
||||||
|
lua_register(mLuaState, "RegisterEvent", lua_RegisterEvent);
|
||||||
|
lua_register(mLuaState, "GetPlayerName", lua_GetPlayerName);
|
||||||
|
lua_register(mLuaState, "RemoveVehicle", lua_RemoveVehicle);
|
||||||
|
lua_register(mLuaState, "GetPlayerDiscordID", lua_TempFix);
|
||||||
|
lua_register(mLuaState, "CreateThread", lua_CreateThread);
|
||||||
|
lua_register(mLuaState, "GetPlayerVehicles", lua_GetCars);
|
||||||
|
lua_register(mLuaState, "SendChatMessage", lua_sendChat);
|
||||||
|
lua_register(mLuaState, "GetPlayers", lua_GetAllPlayers);
|
||||||
|
lua_register(mLuaState, "GetPlayerGuest", lua_GetGuest);
|
||||||
|
lua_register(mLuaState, "StopThread", lua_StopThread);
|
||||||
|
lua_register(mLuaState, "DropPlayer", lua_dropPlayer);
|
||||||
|
lua_register(mLuaState, "GetPlayerHWID", lua_HWID);
|
||||||
|
lua_register(mLuaState, "exit", lua_ServerExit);
|
||||||
|
lua_register(mLuaState, "Sleep", lua_Sleep);
|
||||||
|
lua_register(mLuaState, "print", lua_Print);
|
||||||
|
lua_register(mLuaState, "Set", lua_Set);
|
||||||
|
if (!mConsole)
|
||||||
|
Reload();
|
||||||
}
|
}
|
||||||
|
|
||||||
void TLuaFile::RegisterEvent(const std::string& Event, const std::string& FunctionName) {
|
void TLuaFile::RegisterEvent(const std::string& Event, const std::string& FunctionName) {
|
||||||
@@ -583,12 +713,6 @@ std::string TLuaFile::GetRegistered(const std::string& Event) const {
|
|||||||
}
|
}
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
bool TLuaFile::operator==(const TLuaFile& Other) const {
|
|
||||||
return mPluginName == Other.mPluginName
|
|
||||||
&& mFileName == Other.mFileName;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string TLuaFile::GetFileName() const {
|
std::string TLuaFile::GetFileName() const {
|
||||||
return mFileName;
|
return mFileName;
|
||||||
}
|
}
|
||||||
@@ -615,6 +739,18 @@ TLuaFile::~TLuaFile() {
|
|||||||
lua_close(mLuaState);
|
lua_close(mLuaState);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SendError(TLuaEngine& Engine, lua_State* L, const std::string& msg) {
|
||||||
|
Assert(L);
|
||||||
|
auto MaybeS = Engine.GetScript(L);
|
||||||
|
std::string a;
|
||||||
|
if (!MaybeS.has_value()) {
|
||||||
|
a = ("_Console");
|
||||||
|
} else {
|
||||||
|
TLuaFile& S = MaybeS.value();
|
||||||
|
a = fs::path(S.GetFileName()).filename().string();
|
||||||
|
}
|
||||||
|
warn(a + (" | Incorrect Call of ") + msg);
|
||||||
|
}
|
||||||
int lua_TempFix(lua_State* L) {
|
int lua_TempFix(lua_State* L) {
|
||||||
if (lua_isnumber(L, 1)) {
|
if (lua_isnumber(L, 1)) {
|
||||||
int ID = int(lua_tonumber(L, 1));
|
int ID = int(lua_tonumber(L, 1));
|
||||||
@@ -633,22 +769,22 @@ int lua_TempFix(lua_State* L) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TLuaFile::PushArgs(const TLuaArgs& args) {
|
void TLuaArg::PushArgs(lua_State* State) {
|
||||||
for (const std::any& arg : args.Args) {
|
for (std::any arg : args) {
|
||||||
if (!arg.has_value())
|
if (!arg.has_value())
|
||||||
return;
|
return;
|
||||||
std::string Type = arg.type().name();
|
std::string Type = arg.type().name();
|
||||||
if (Type.find("bool") != std::string::npos) {
|
if (Type.find("bool") != std::string::npos) {
|
||||||
lua_pushboolean(mLuaState, std::any_cast<bool>(arg));
|
lua_pushboolean(State, std::any_cast<bool>(arg));
|
||||||
}
|
}
|
||||||
if (Type.find("basic_string") != std::string::npos || Type.find("char") != std::string::npos) {
|
if (Type.find("basic_string") != std::string::npos || Type.find("char") != std::string::npos) {
|
||||||
lua_pushstring(mLuaState, std::any_cast<std::string>(arg).c_str());
|
lua_pushstring(State, std::any_cast<std::string>(arg).c_str());
|
||||||
}
|
}
|
||||||
if (Type.find("int") != std::string::npos) {
|
if (Type.find("int") != std::string::npos) {
|
||||||
lua_pushinteger(mLuaState, std::any_cast<int>(arg));
|
lua_pushinteger(State, std::any_cast<int>(arg));
|
||||||
}
|
}
|
||||||
if (Type.find("float") != std::string::npos) {
|
if (Type.find("float") != std::string::npos) {
|
||||||
lua_pushnumber(mLuaState, std::any_cast<float>(arg));
|
lua_pushnumber(State, std::any_cast<float>(arg));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+8
-8
@@ -390,8 +390,8 @@ bool TNetwork::TCPSend(TClient& c, const std::string& Data, bool IsSync) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
Sent += Temp;
|
Sent += Temp;
|
||||||
|
c.UpdatePingTime();
|
||||||
} while (Sent < Size);
|
} while (Sent < Size);
|
||||||
c.UpdatePingTime();
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -757,7 +757,7 @@ void TNetwork::SplitLoad(TClient& c, size_t Sent, size_t Size, bool D, const std
|
|||||||
if (Diff > Split) {
|
if (Diff > Split) {
|
||||||
f.seekg(Sent, std::ios_base::beg);
|
f.seekg(Sent, std::ios_base::beg);
|
||||||
f.read(Data, Split);
|
f.read(Data, Split);
|
||||||
if (!TCPSendRaw(TCPSock, Data, Split)) {
|
if (!TCPSendRaw(c, TCPSock, Data, Split)) {
|
||||||
if (c.GetStatus() > -1)
|
if (c.GetStatus() > -1)
|
||||||
c.SetStatus(-1);
|
c.SetStatus(-1);
|
||||||
break;
|
break;
|
||||||
@@ -766,7 +766,7 @@ void TNetwork::SplitLoad(TClient& c, size_t Sent, size_t Size, bool D, const std
|
|||||||
} else {
|
} else {
|
||||||
f.seekg(Sent, std::ios_base::beg);
|
f.seekg(Sent, std::ios_base::beg);
|
||||||
f.read(Data, Diff);
|
f.read(Data, Diff);
|
||||||
if (!TCPSendRaw(TCPSock, Data, int32_t(Diff))) {
|
if (!TCPSendRaw(c, TCPSock, Data, int32_t(Diff))) {
|
||||||
if (c.GetStatus() > -1)
|
if (c.GetStatus() > -1)
|
||||||
c.SetStatus(-1);
|
c.SetStatus(-1);
|
||||||
break;
|
break;
|
||||||
@@ -778,16 +778,17 @@ void TNetwork::SplitLoad(TClient& c, size_t Sent, size_t Size, bool D, const std
|
|||||||
f.close();
|
f.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool TNetwork::TCPSendRaw(SOCKET C, char* Data, int32_t Size) {
|
bool TNetwork::TCPSendRaw(TClient& C, SOCKET socket, char* Data, int32_t Size) {
|
||||||
intmax_t Sent = 0;
|
intmax_t Sent = 0;
|
||||||
do {
|
do {
|
||||||
intmax_t Temp = send(C, &Data[Sent], int(Size - Sent), 0);
|
intmax_t Temp = send(socket, &Data[Sent], int(Size - Sent), 0);
|
||||||
if (Temp < 1) {
|
if (Temp < 1) {
|
||||||
info("Socket Closed! " + std::to_string(C));
|
info("Socket Closed! " + std::to_string(socket));
|
||||||
CloseSocketProper(C);
|
CloseSocketProper(socket);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
Sent += Temp;
|
Sent += Temp;
|
||||||
|
C.UpdatePingTime();
|
||||||
} while (Sent < Size);
|
} while (Sent < Size);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -854,7 +855,6 @@ bool TNetwork::SyncClient(const std::weak_ptr<TClient>& c) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
res = Respond(*LockedClient, v.Data(), true, true);
|
res = Respond(*LockedClient, v.Data(), true, true);
|
||||||
std::this_thread::sleep_for(std::chrono::seconds(2));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+4
-6
@@ -36,17 +36,15 @@ void TPPSMonitor::operator()() {
|
|||||||
ReadLock Lock(mServer.GetClientMutex());
|
ReadLock Lock(mServer.GetClientMutex());
|
||||||
if (!ClientPtr.expired()) {
|
if (!ClientPtr.expired()) {
|
||||||
c = ClientPtr.lock();
|
c = ClientPtr.lock();
|
||||||
} else return true;
|
} else
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
if (c->GetCarCount() > 0) {
|
if (c->GetCarCount() > 0) {
|
||||||
C++;
|
C++;
|
||||||
V += c->GetCarCount();
|
V += c->GetCarCount();
|
||||||
}
|
}
|
||||||
if (!c->IsSynced() || c->IsSyncing()) {
|
|
||||||
c->UpdatePingTime();
|
|
||||||
}
|
|
||||||
// kick on "no ping"
|
// kick on "no ping"
|
||||||
if (c->SecondsSinceLastPing() > 30 && c->IsSynced() && !c->IsSyncing()) {
|
if (c->SecondsSinceLastPing() > (5 * 60)) {
|
||||||
debug("client " + std::string("(") + std::to_string(c->GetID()) + ")" + c->GetName() + " timing out: " + std::to_string(c->SecondsSinceLastPing()) + ", pps: " + Application::PPS());
|
debug("client " + std::string("(") + std::to_string(c->GetID()) + ")" + c->GetName() + " timing out: " + std::to_string(c->SecondsSinceLastPing()) + ", pps: " + Application::PPS());
|
||||||
TimedOutClients.push_back(c);
|
TimedOutClients.push_back(c);
|
||||||
}
|
}
|
||||||
@@ -54,7 +52,7 @@ void TPPSMonitor::operator()() {
|
|||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
for (auto& ClientToKick : TimedOutClients) {
|
for (auto& ClientToKick : TimedOutClients) {
|
||||||
Network().ClientKick(*ClientToKick, "Timeout (no ping for >30 seconds)");
|
Network().ClientKick(*ClientToKick, "Timeout (no ping for >5 min)");
|
||||||
}
|
}
|
||||||
TimedOutClients.clear();
|
TimedOutClients.clear();
|
||||||
if (C == 0 || mInternalPPS == 0) {
|
if (C == 0 || mInternalPPS == 0) {
|
||||||
|
|||||||
@@ -9,8 +9,6 @@
|
|||||||
#include "TServer.h"
|
#include "TServer.h"
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
TLuaEngine* TheLuaEngine = nullptr;
|
|
||||||
|
|
||||||
#ifdef __unix
|
#ifdef __unix
|
||||||
#include <csignal>
|
#include <csignal>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user