mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2025-07-01 23:35:41 +00:00
attempt to fix ghost player issue
This commit is contained in:
parent
13f8be5d39
commit
e04a569e33
@ -1,5 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <chrono>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <unordered_set>
|
#include <unordered_set>
|
||||||
@ -45,6 +46,8 @@ public:
|
|||||||
void SetIsSynced(bool NewIsSynced) { mIsSynced = NewIsSynced; }
|
void SetIsSynced(bool NewIsSynced) { mIsSynced = NewIsSynced; }
|
||||||
void SetIsConnected(bool NewIsConnected) { mIsConnected = NewIsConnected; }
|
void SetIsConnected(bool NewIsConnected) { mIsConnected = NewIsConnected; }
|
||||||
TServer& Server() const;
|
TServer& Server() const;
|
||||||
|
void UpdatePingTime();
|
||||||
|
int SecondsSinceLastPing();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
TServer& mServer;
|
TServer& mServer;
|
||||||
@ -59,4 +62,5 @@ private:
|
|||||||
std::string mDID;
|
std::string mDID;
|
||||||
int mStatus = 0;
|
int mStatus = 0;
|
||||||
int mID = -1;
|
int mID = -1;
|
||||||
|
std::chrono::time_point<std::chrono::high_resolution_clock> mLastPingTime;
|
||||||
};
|
};
|
||||||
|
@ -12,9 +12,13 @@ public:
|
|||||||
void SetInternalPPS(int NewPPS) { mInternalPPS = NewPPS; }
|
void SetInternalPPS(int NewPPS) { mInternalPPS = NewPPS; }
|
||||||
void IncrementInternalPPS() { ++mInternalPPS; }
|
void IncrementInternalPPS() { ++mInternalPPS; }
|
||||||
[[nodiscard]] int InternalPPS() const { return mInternalPPS; }
|
[[nodiscard]] int InternalPPS() const { return mInternalPPS; }
|
||||||
|
void SetTCPServer(TTCPServer& Server) { mTCPServer = std::ref(Server); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
TTCPServer& TCPServer() { return mTCPServer->get(); }
|
||||||
|
|
||||||
TServer& mServer;
|
TServer& mServer;
|
||||||
|
std::optional<std::reference_wrapper<TTCPServer>> mTCPServer { std::nullopt };
|
||||||
bool mShutdown { false };
|
bool mShutdown { false };
|
||||||
int mInternalPPS { 0 };
|
int mInternalPPS { 0 };
|
||||||
};
|
};
|
@ -32,7 +32,7 @@ public:
|
|||||||
bool CheckBytes(TClient& c, int32_t BytesRcv);
|
bool CheckBytes(TClient& c, int32_t BytesRcv);
|
||||||
void SyncResources(TClient& c);
|
void SyncResources(TClient& c);
|
||||||
|
|
||||||
void UpdatePlayers();
|
void UpdatePlayer(TClient& Client);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::optional<std::reference_wrapper<TUDPServer>> mUDPServer { std::nullopt };
|
std::optional<std::reference_wrapper<TUDPServer>> mUDPServer { std::nullopt };
|
||||||
|
@ -70,3 +70,12 @@ TServer& TClient::Server() const {
|
|||||||
TClient::TClient(TServer& Server)
|
TClient::TClient(TServer& Server)
|
||||||
: mServer(Server) {
|
: mServer(Server) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TClient::UpdatePingTime() {
|
||||||
|
mLastPingTime = std::chrono::high_resolution_clock::now();
|
||||||
|
}
|
||||||
|
int TClient::SecondsSinceLastPing() {
|
||||||
|
return std::chrono::duration_cast<std::chrono::seconds>(
|
||||||
|
std::chrono::high_resolution_clock::now() - mLastPingTime)
|
||||||
|
.count();
|
||||||
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
#include "TPPSMonitor.h"
|
#include "TPPSMonitor.h"
|
||||||
#include "Client.h"
|
#include "Client.h"
|
||||||
|
#include "TTCPServer.h"
|
||||||
|
|
||||||
TPPSMonitor::TPPSMonitor(TServer& Server)
|
TPPSMonitor::TPPSMonitor(TServer& Server)
|
||||||
: mServer(Server) {
|
: mServer(Server) {
|
||||||
@ -15,6 +16,11 @@ TPPSMonitor::TPPSMonitor(TServer& Server)
|
|||||||
Start();
|
Start();
|
||||||
}
|
}
|
||||||
void TPPSMonitor::operator()() {
|
void TPPSMonitor::operator()() {
|
||||||
|
while (!mTCPServer) {
|
||||||
|
// hard spi
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||||
|
}
|
||||||
|
std::vector<std::shared_ptr<TClient>> TimedOutClients;
|
||||||
while (!mShutdown) {
|
while (!mShutdown) {
|
||||||
int C = 0, V = 0;
|
int C = 0, V = 0;
|
||||||
if (mServer.ClientCount() == 0) {
|
if (mServer.ClientCount() == 0) {
|
||||||
@ -28,9 +34,16 @@ void TPPSMonitor::operator()() {
|
|||||||
C++;
|
C++;
|
||||||
V += c->GetCarCount();
|
V += c->GetCarCount();
|
||||||
}
|
}
|
||||||
|
// kick on "no ping"
|
||||||
|
if (c->SecondsSinceLastPing() > 10) {
|
||||||
|
TimedOutClients.push_back(c);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
|
for (auto& ClientToKick : TimedOutClients) {
|
||||||
|
TCPServer().ClientKick(*ClientToKick, "Timeout (no ping for >10 seconds)");
|
||||||
|
}
|
||||||
if (C == 0 || mInternalPPS == 0) {
|
if (C == 0 || mInternalPPS == 0) {
|
||||||
Application::SetPPS("-");
|
Application::SetPPS("-");
|
||||||
} else {
|
} else {
|
||||||
|
@ -93,7 +93,8 @@ void TServer::GlobalParser(std::weak_ptr<TClient> Client, std::string Packet, TP
|
|||||||
return;
|
return;
|
||||||
case 'p':
|
case 'p':
|
||||||
TCPServer.Respond(*LockedClient, ("p"), false);
|
TCPServer.Respond(*LockedClient, ("p"), false);
|
||||||
TCPServer.UpdatePlayers();
|
TCPServer.UpdatePlayer(*LockedClient);
|
||||||
|
LockedClient->UpdatePingTime();
|
||||||
return;
|
return;
|
||||||
case 'O':
|
case 'O':
|
||||||
if (Packet.length() > 1000) {
|
if (Packet.length() > 1000) {
|
||||||
|
@ -260,6 +260,7 @@ std::string TTCPServer::TCPRcv(TClient& c) {
|
|||||||
void TTCPServer::ClientKick(TClient& c, const std::string& R) {
|
void TTCPServer::ClientKick(TClient& c, const std::string& R) {
|
||||||
info("Client kicked: " + R);
|
info("Client kicked: " + R);
|
||||||
TCPSend(c, "E" + R);
|
TCPSend(c, "E" + R);
|
||||||
|
c.SetStatus(-2);
|
||||||
CloseSocketProper(c.GetTCPSock());
|
CloseSocketProper(c.GetTCPSock());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -287,8 +288,7 @@ void TTCPServer::TCPClient(std::weak_ptr<TClient> c) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void TTCPServer::UpdatePlayers() {
|
void TTCPServer::UpdatePlayer(TClient& Client) {
|
||||||
debug("Update Players!");
|
|
||||||
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([&](std::weak_ptr<TClient> ClientPtr) -> bool {
|
||||||
if (!ClientPtr.expired()) {
|
if (!ClientPtr.expired()) {
|
||||||
@ -298,7 +298,7 @@ void TTCPServer::UpdatePlayers() {
|
|||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
Packet = Packet.substr(0, Packet.length() - 1);
|
Packet = Packet.substr(0, Packet.length() - 1);
|
||||||
UDPServer().SendToAll(nullptr, Packet, true, true);
|
Respond(Client, Packet, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TTCPServer::OnDisconnect(std::weak_ptr<TClient> ClientPtr, bool kicked) {
|
void TTCPServer::OnDisconnect(std::weak_ptr<TClient> ClientPtr, bool kicked) {
|
||||||
|
@ -55,6 +55,7 @@ int main(int argc, char** argv) {
|
|||||||
TUDPServer UDPServer(Server, PPSMonitor, TCPServer);
|
TUDPServer UDPServer(Server, PPSMonitor, TCPServer);
|
||||||
TLuaEngine LuaEngine(Server, TCPServer, UDPServer);
|
TLuaEngine LuaEngine(Server, TCPServer, UDPServer);
|
||||||
TCPServer.SetUDPServer(UDPServer);
|
TCPServer.SetUDPServer(UDPServer);
|
||||||
|
PPSMonitor.SetTCPServer(TCPServer);
|
||||||
Application::Console().InitializeLuaConsole(LuaEngine);
|
Application::Console().InitializeLuaConsole(LuaEngine);
|
||||||
|
|
||||||
// TODO: replace
|
// TODO: replace
|
||||||
|
Loading…
x
Reference in New Issue
Block a user