Cleanup & optimizations

This commit is contained in:
Anonymous275 2021-02-22 23:01:34 +02:00 committed by Anonymous275
parent 0580ad67fd
commit f52308c439
6 changed files with 34 additions and 26 deletions

View File

@ -8,7 +8,7 @@
class THeartbeatThread : public IThreaded { class THeartbeatThread : public IThreaded {
public: public:
THeartbeatThread(TResourceManager& ResourceManager, TServer& Server); THeartbeatThread(TResourceManager& ResourceManager, TServer& Server);
~THeartbeatThread(); //~THeartbeatThread();
void operator()() override; void operator()() override;
private: private:

View File

@ -8,6 +8,7 @@ public:
[[nodiscard]] uint64_t MaxModSize() const { return mMaxModSize; } [[nodiscard]] uint64_t MaxModSize() const { return mMaxModSize; }
[[nodiscard]] std::string FileList() const { return mFileList; } [[nodiscard]] std::string FileList() const { return mFileList; }
[[nodiscard]] std::string TrimmedList() const { return mTrimmedList; }
[[nodiscard]] std::string FileSizes() const { return mFileSizes; } [[nodiscard]] std::string FileSizes() const { return mFileSizes; }
[[nodiscard]] int ModsLoaded() const { return mModsLoaded; } [[nodiscard]] int ModsLoaded() const { return mModsLoaded; }
@ -15,5 +16,6 @@ private:
uint64_t mMaxModSize = 0; uint64_t mMaxModSize = 0;
std::string mFileSizes; std::string mFileSizes;
std::string mFileList; std::string mFileList;
std::string mTrimmedList;
int mModsLoaded = 0; int mModsLoaded = 0;
}; };

View File

@ -12,7 +12,7 @@ class TResourceManager;
class TTCPServer : public IThreaded { class TTCPServer : public IThreaded {
public: public:
explicit TTCPServer(TServer& Server, TPPSMonitor& PPSMonitor, TResourceManager& ResourceManager); explicit TTCPServer(TServer& Server, TPPSMonitor& PPSMonitor, TResourceManager& ResourceManager);
~TTCPServer(); //~TTCPServer();
void operator()() override; void operator()() override;
@ -27,7 +27,7 @@ public:
TUDPServer& UDPServer() { return mUDPServer->get(); } TUDPServer& UDPServer() { return mUDPServer->get(); }
void SyncClient(std::weak_ptr<TClient> c); void SyncClient(const std::weak_ptr<TClient>& c);
void Identify(SOCKET TCPSock); void Identify(SOCKET TCPSock);
void Authentication(SOCKET TCPSock); void Authentication(SOCKET TCPSock);
bool CheckBytes(TClient& c, int32_t BytesRcv); bool CheckBytes(TClient& c, int32_t BytesRcv);
@ -43,10 +43,10 @@ private:
bool mShutdown { false }; bool mShutdown { false };
void HandleDownload(SOCKET TCPSock); void HandleDownload(SOCKET TCPSock);
void OnConnect(std::weak_ptr<TClient> c); void OnConnect(const std::weak_ptr<TClient>& c);
void TCPClient(std::weak_ptr<TClient> c); void TCPClient(const std::weak_ptr<TClient>& c);
int OpenID(); int OpenID();
void OnDisconnect(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(SOCKET C, char* Data, int32_t Size);

View File

@ -69,7 +69,7 @@ std::string THeartbeatThread::GenerateCall() {
<< "&clientversion=" << Application::ClientVersion() << "&clientversion=" << Application::ClientVersion()
<< "&name=" << Application::Settings.ServerName << "&name=" << Application::Settings.ServerName
<< "&pps=" << Application::PPS() << "&pps=" << Application::PPS()
<< "&modlist=" << mResourceManager.FileList() << "&modlist=" << mResourceManager.TrimmedList()
<< "&modstotalsize=" << mResourceManager.MaxModSize() << "&modstotalsize=" << mResourceManager.MaxModSize()
<< "&modstotal=" << mResourceManager.ModsLoaded() << "&modstotal=" << mResourceManager.ModsLoaded()
<< "&playerslist=" << GetPlayers() << "&playerslist=" << GetPlayers()
@ -91,7 +91,7 @@ THeartbeatThread::THeartbeatThread(TResourceManager& ResourceManager, TServer& S
} }
std::string THeartbeatThread::GetPlayers() { std::string THeartbeatThread::GetPlayers() {
std::string Return; std::string Return;
mServer.ForEachClient([&](std::weak_ptr<TClient> ClientPtr) -> bool { mServer.ForEachClient([&](const std::weak_ptr<TClient>& ClientPtr) -> bool {
if (!ClientPtr.expired()) { if (!ClientPtr.expired()) {
Return += ClientPtr.lock()->GetName() + ";"; Return += ClientPtr.lock()->GetName() + ";";
} }
@ -99,5 +99,5 @@ std::string THeartbeatThread::GetPlayers() {
}); });
return Return; return Return;
} }
THeartbeatThread::~THeartbeatThread() { /*THeartbeatThread::~THeartbeatThread() {
} }*/

View File

@ -10,17 +10,23 @@ TResourceManager::TResourceManager() {
if (!fs::exists(Path)) if (!fs::exists(Path))
fs::create_directories(Path); fs::create_directories(Path);
for (const auto& entry : fs::directory_iterator(Path)) { for (const auto& entry : fs::directory_iterator(Path)) {
auto pos = entry.path().string().find(".zip"); std::string File(entry.path().string());
if (pos != std::string::npos) { if (auto pos = File.find(".zip"); pos != std::string::npos) {
if (entry.path().string().length() - pos == 4) { if (File.length() - pos == 4) {
mFileList += entry.path().string() + ";"; std::replace(File.begin(), File.end(),'\\','/');
mFileSizes += std::to_string(uint64_t(fs::file_size(entry.path()))) + ";"; mFileList += File + ';';
if(auto i = File.find_last_of('/'); i != std::string::npos){
++i;
File = File.substr(i,pos-i);
}
mTrimmedList += File + ';';
mFileSizes += std::to_string(uint64_t(fs::file_size(entry.path()))) + ';';
mMaxModSize += uint64_t(fs::file_size(entry.path())); mMaxModSize += uint64_t(fs::file_size(entry.path()));
mModsLoaded++; mModsLoaded++;
} }
} }
} }
std::replace(mFileList.begin(), mFileList.end(), '\\', '/');
if (mModsLoaded) if (mModsLoaded)
info("Loaded " + std::to_string(mModsLoaded) + " Mods"); info("Loaded " + std::to_string(mModsLoaded) + " Mods");
} }

View File

@ -51,7 +51,7 @@ void TTCPServer::HandleDownload(SOCKET TCPSock) {
return; return;
} }
auto ID = uint8_t(D); auto ID = uint8_t(D);
mServer.ForEachClient([&](std::weak_ptr<TClient> ClientPtr) -> bool { mServer.ForEachClient([&](const std::weak_ptr<TClient>& ClientPtr) -> bool {
if (!ClientPtr.expired()) { if (!ClientPtr.expired()) {
auto c = ClientPtr.lock(); auto c = ClientPtr.lock();
if (c->GetID() == ID) { if (c->GetID() == ID) {
@ -264,7 +264,7 @@ void TTCPServer::ClientKick(TClient& c, const std::string& R) {
CloseSocketProper(c.GetTCPSock()); CloseSocketProper(c.GetTCPSock());
} }
void TTCPServer::TCPClient(std::weak_ptr<TClient> c) { void TTCPServer::TCPClient(const std::weak_ptr<TClient>& c) {
// TODO: the c.expired() might cause issues here, remove if you end up here with your debugger // TODO: the c.expired() might cause issues here, remove if you end up here with your debugger
if (c.expired() || c.lock()->GetTCPSock() == -1) { if (c.expired() || c.lock()->GetTCPSock() == -1) {
mServer.RemoveClient(c); mServer.RemoveClient(c);
@ -290,7 +290,7 @@ void TTCPServer::TCPClient(std::weak_ptr<TClient> c) {
void TTCPServer::UpdatePlayer(TClient& Client) { void TTCPServer::UpdatePlayer(TClient& Client) {
std::string Packet = ("Ss") + std::to_string(mServer.ClientCount()) + "/" + std::to_string(Application::Settings.MaxPlayers) + ":"; std::string Packet = ("Ss") + std::to_string(mServer.ClientCount()) + "/" + std::to_string(Application::Settings.MaxPlayers) + ":";
mServer.ForEachClient([&](std::weak_ptr<TClient> ClientPtr) -> bool { mServer.ForEachClient([&](const std::weak_ptr<TClient>& ClientPtr) -> bool {
if (!ClientPtr.expired()) { if (!ClientPtr.expired()) {
auto c = ClientPtr.lock(); auto c = ClientPtr.lock();
Packet += c->GetName() + ","; Packet += c->GetName() + ",";
@ -301,7 +301,7 @@ void TTCPServer::UpdatePlayer(TClient& Client) {
Respond(Client, Packet, true); Respond(Client, Packet, true);
} }
void TTCPServer::OnDisconnect(std::weak_ptr<TClient> ClientPtr, bool kicked) { void TTCPServer::OnDisconnect(const std::weak_ptr<TClient>& ClientPtr, bool kicked) {
Assert(!ClientPtr.expired()); Assert(!ClientPtr.expired());
auto LockedClientPtr = ClientPtr.lock(); auto LockedClientPtr = ClientPtr.lock();
TClient& c = *LockedClientPtr; TClient& c = *LockedClientPtr;
@ -332,7 +332,7 @@ int TTCPServer::OpenID() {
bool found; bool found;
do { do {
found = true; found = true;
mServer.ForEachClient([&](std::weak_ptr<TClient> ClientPtr) -> bool { mServer.ForEachClient([&](const std::weak_ptr<TClient>& ClientPtr) -> bool {
if (!ClientPtr.expired()) { if (!ClientPtr.expired()) {
auto c = ClientPtr.lock(); auto c = ClientPtr.lock();
if (c->GetID() == ID) { if (c->GetID() == ID) {
@ -346,7 +346,7 @@ int TTCPServer::OpenID() {
return ID; return ID;
} }
void TTCPServer::OnConnect(std::weak_ptr<TClient> c) { void TTCPServer::OnConnect(const std::weak_ptr<TClient>& c) {
Assert(!c.expired()); Assert(!c.expired());
info("Client connected"); info("Client connected");
auto LockedClient = c.lock(); auto LockedClient = c.lock();
@ -522,7 +522,7 @@ void TTCPServer::Respond(TClient& c, const std::string& MSG, bool Rel) {
} }
} }
void TTCPServer::SyncClient(std::weak_ptr<TClient> c) { void TTCPServer::SyncClient(const std::weak_ptr<TClient>& c) {
if (c.expired()) { if (c.expired()) {
return; return;
} }
@ -535,7 +535,7 @@ void TTCPServer::SyncClient(std::weak_ptr<TClient> c) {
UDPServer().SendToAll(LockedClient.get(), ("JWelcome ") + LockedClient->GetName() + "!", false, true); UDPServer().SendToAll(LockedClient.get(), ("JWelcome ") + LockedClient->GetName() + "!", false, true);
TriggerLuaEvent(("onPlayerJoin"), false, nullptr, std::make_unique<TLuaArg>(TLuaArg { { LockedClient->GetID() } }), false); TriggerLuaEvent(("onPlayerJoin"), false, nullptr, std::make_unique<TLuaArg>(TLuaArg { { LockedClient->GetID() } }), false);
bool Return = false; bool Return = false;
mServer.ForEachClient([&](std::weak_ptr<TClient> ClientPtr) -> bool { mServer.ForEachClient([&](const std::weak_ptr<TClient>& ClientPtr) -> bool {
if (!ClientPtr.expired()) { if (!ClientPtr.expired()) {
auto client = ClientPtr.lock(); auto client = ClientPtr.lock();
if (client != LockedClient) { if (client != LockedClient) {
@ -658,5 +658,5 @@ void TTCPServer::operator()() {
CloseSocketProper(client); CloseSocketProper(client);
#endif #endif
} }
TTCPServer::~TTCPServer() { /*TTCPServer::~TTCPServer() {
} }*/