attempt to fix ghost player issue

This commit is contained in:
Lion Kortlepel
2021-02-17 12:51:57 +01:00
committed by Anonymous275
parent 13f8be5d39
commit e04a569e33
8 changed files with 37 additions and 5 deletions

View File

@@ -70,3 +70,12 @@ TServer& TClient::Server() const {
TClient::TClient(TServer& 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();
}

View File

@@ -1,5 +1,6 @@
#include "TPPSMonitor.h"
#include "Client.h"
#include "TTCPServer.h"
TPPSMonitor::TPPSMonitor(TServer& Server)
: mServer(Server) {
@@ -15,6 +16,11 @@ TPPSMonitor::TPPSMonitor(TServer& Server)
Start();
}
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) {
int C = 0, V = 0;
if (mServer.ClientCount() == 0) {
@@ -28,9 +34,16 @@ void TPPSMonitor::operator()() {
C++;
V += c->GetCarCount();
}
// kick on "no ping"
if (c->SecondsSinceLastPing() > 10) {
TimedOutClients.push_back(c);
}
}
return true;
});
for (auto& ClientToKick : TimedOutClients) {
TCPServer().ClientKick(*ClientToKick, "Timeout (no ping for >10 seconds)");
}
if (C == 0 || mInternalPPS == 0) {
Application::SetPPS("-");
} else {

View File

@@ -93,7 +93,8 @@ void TServer::GlobalParser(std::weak_ptr<TClient> Client, std::string Packet, TP
return;
case 'p':
TCPServer.Respond(*LockedClient, ("p"), false);
TCPServer.UpdatePlayers();
TCPServer.UpdatePlayer(*LockedClient);
LockedClient->UpdatePingTime();
return;
case 'O':
if (Packet.length() > 1000) {

View File

@@ -260,6 +260,7 @@ std::string TTCPServer::TCPRcv(TClient& c) {
void TTCPServer::ClientKick(TClient& c, const std::string& R) {
info("Client kicked: " + R);
TCPSend(c, "E" + R);
c.SetStatus(-2);
CloseSocketProper(c.GetTCPSock());
}
@@ -287,8 +288,7 @@ void TTCPServer::TCPClient(std::weak_ptr<TClient> c) {
}
}
void TTCPServer::UpdatePlayers() {
debug("Update Players!");
void TTCPServer::UpdatePlayer(TClient& Client) {
std::string Packet = ("Ss") + std::to_string(mServer.ClientCount()) + "/" + std::to_string(Application::Settings.MaxPlayers) + ":";
mServer.ForEachClient([&](std::weak_ptr<TClient> ClientPtr) -> bool {
if (!ClientPtr.expired()) {
@@ -298,7 +298,7 @@ void TTCPServer::UpdatePlayers() {
return true;
});
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) {

View File

@@ -55,6 +55,7 @@ int main(int argc, char** argv) {
TUDPServer UDPServer(Server, PPSMonitor, TCPServer);
TLuaEngine LuaEngine(Server, TCPServer, UDPServer);
TCPServer.SetUDPServer(UDPServer);
PPSMonitor.SetTCPServer(TCPServer);
Application::Console().InitializeLuaConsole(LuaEngine);
// TODO: replace