fix client insert/create issue

This commit is contained in:
Lion Kortlepel 2021-02-17 10:09:50 +01:00 committed by Anonymous275
parent b3256062f7
commit bca4b3f140
4 changed files with 33 additions and 27 deletions

View File

@ -18,6 +18,7 @@ public:
TServer(int argc, char** argv); TServer(int argc, char** argv);
void InsertClient(std::shared_ptr<TClient> Ptr);
std::weak_ptr<TClient> InsertNewClient(); std::weak_ptr<TClient> InsertNewClient();
void RemoveClient(std::weak_ptr<TClient>); void RemoveClient(std::weak_ptr<TClient>);
// in Fn, return true to continue, return false to break // in Fn, return true to continue, return false to break

View File

@ -17,7 +17,7 @@ public:
bool TCPSend(TClient& c, const std::string& Data); bool TCPSend(TClient& c, const std::string& Data);
void SendLarge(TClient& c, std::string Data); void SendLarge(TClient& c, std::string Data);
void Respond(TClient& c, const std::string& MSG, bool Rel); void Respond(TClient& c, const std::string& MSG, bool Rel);
std::weak_ptr<TClient> CreateClient(SOCKET TCPSock); std::shared_ptr<TClient> CreateClient(SOCKET TCPSock);
std::string TCPRcv(TClient& c); std::string TCPRcv(TClient& c);
void ClientKick(TClient& c, const std::string& R); void ClientKick(TClient& c, const std::string& R);
@ -32,6 +32,7 @@ public:
void SyncResources(TClient& c); void SyncResources(TClient& c);
void UpdatePlayers(); void UpdatePlayers();
private: private:
std::optional<std::reference_wrapper<TUDPServer>> mUDPServer { std::nullopt }; std::optional<std::reference_wrapper<TUDPServer>> mUDPServer { std::nullopt };
TServer& mServer; TServer& mServer;

View File

@ -269,3 +269,9 @@ void TServer::Apply(TClient& c, int VID, const std::string& pckt) {
Veh.Accept(writer); Veh.Accept(writer);
c.SetCarData(VID, Header + Buffer.GetString()); c.SetCarData(VID, Header + Buffer.GetString());
} }
void TServer::InsertClient(std::shared_ptr<TClient> NewClient) {
debug("inserting client (" + std::to_string(ClientCount()) + ")");
WriteLock Lock(mClientsMutex);
(void)mClients.insert(NewClient);
}

View File

@ -57,32 +57,29 @@ void TTCPServer::HandleDownload(SOCKET TCPSock) {
} }
void TTCPServer::Authentication(SOCKET TCPSock) { void TTCPServer::Authentication(SOCKET TCPSock) {
auto c = CreateClient(TCPSock); auto Client = CreateClient(TCPSock);
std::string Rc; std::string Rc;
info("Identifying new client..."); info("Identifying new client...");
Assert(!c.expired()); Rc = TCPRcv(*Client);
auto LockedClient = c.lock();
Rc = TCPRcv(*LockedClient);
if (Rc.size() > 3 && Rc.substr(0, 2) == "VC") { if (Rc.size() > 3 && Rc.substr(0, 2) == "VC") {
Rc = Rc.substr(2); Rc = Rc.substr(2);
if (Rc.length() > 4 || Rc != Application::ClientVersion()) { if (Rc.length() > 4 || Rc != Application::ClientVersion()) {
ClientKick(*LockedClient, "Outdated Version!"); ClientKick(*Client, "Outdated Version!");
return; return;
} }
} else { } else {
ClientKick(*LockedClient, "Invalid version header!"); ClientKick(*Client, "Invalid version header!");
return; return;
} }
TCPSend(*LockedClient, "S"); TCPSend(*Client, "S");
Rc = TCPRcv(*LockedClient); Rc = TCPRcv(*Client);
if (Rc.size() > 50) { if (Rc.size() > 50) {
ClientKick(*LockedClient, "Invalid Key!"); ClientKick(*Client, "Invalid Key!");
return; return;
} }
@ -95,26 +92,26 @@ void TTCPServer::Authentication(SOCKET TCPSock) {
json::Document AuthResponse; json::Document AuthResponse;
AuthResponse.Parse(Rc.c_str()); AuthResponse.Parse(Rc.c_str());
if (Rc == "-1" || AuthResponse.HasParseError()) { if (Rc == "-1" || AuthResponse.HasParseError()) {
ClientKick(*LockedClient, "Invalid key! Please restart your game."); ClientKick(*Client, "Invalid key! Please restart your game.");
return; return;
} }
if (AuthResponse["username"].IsString() && AuthResponse["roles"].IsString() && AuthResponse["guest"].IsBool()) { if (AuthResponse["username"].IsString() && AuthResponse["roles"].IsString() && AuthResponse["guest"].IsBool()) {
LockedClient->SetName(AuthResponse["username"].GetString()); Client->SetName(AuthResponse["username"].GetString());
LockedClient->SetRoles(AuthResponse["roles"].GetString()); Client->SetRoles(AuthResponse["roles"].GetString());
LockedClient->SetIsGuest(AuthResponse["guest"].GetBool()); Client->SetIsGuest(AuthResponse["guest"].GetBool());
} else { } else {
ClientKick(*LockedClient, "Invalid authentication data!"); ClientKick(*Client, "Invalid authentication data!");
return; return;
} }
debug("Name -> " + LockedClient->GetName() + ", Guest -> " + std::to_string(LockedClient->IsGuest()) + ", Roles -> " + LockedClient->GetRoles()); debug("Name -> " + Client->GetName() + ", Guest -> " + std::to_string(Client->IsGuest()) + ", Roles -> " + Client->GetRoles());
debug("There are " + std::to_string(mServer.ClientCount()) + " known clients"); debug("There are " + std::to_string(mServer.ClientCount()) + " known clients");
mServer.ForEachClient([&](std::weak_ptr<TClient> ClientPtr) -> bool { mServer.ForEachClient([&](std::weak_ptr<TClient> ClientPtr) -> bool {
if (!ClientPtr.expired()) { if (!ClientPtr.expired()) {
auto Cl = ClientPtr.lock(); auto Cl = ClientPtr.lock();
info("Client Iteration: Name -> " + LockedClient->GetName() + ", Guest -> " + std::to_string(LockedClient->IsGuest()) + ", Roles -> " + LockedClient->GetRoles()); info("Client Iteration: Name -> " + Client->GetName() + ", Guest -> " + std::to_string(Client->IsGuest()) + ", Roles -> " + Client->GetRoles());
if (Cl->GetName() == LockedClient->GetName() && Cl->IsGuest() == LockedClient->IsGuest()) { if (Cl->GetName() == Client->GetName() && Cl->IsGuest() == Client->IsGuest()) {
info("New client matched with current iteration"); info("New client matched with current iteration");
info("Old client (" + Cl->GetName() + ") kicked: Reconnecting"); info("Old client (" + Cl->GetName() + ") kicked: Reconnecting");
CloseSocketProper(Cl->GetTCPSock()); CloseSocketProper(Cl->GetTCPSock());
@ -125,27 +122,28 @@ void TTCPServer::Authentication(SOCKET TCPSock) {
return true; return true;
}); });
auto arg = std::make_unique<TLuaArg>(TLuaArg { { LockedClient->GetName(), LockedClient->GetRoles(), LockedClient->IsGuest() } }); auto arg = std::make_unique<TLuaArg>(TLuaArg { { Client->GetName(), Client->GetRoles(), Client->IsGuest() } });
std::any Res = TriggerLuaEvent("onPlayerAuth", false, nullptr, std::move(arg), true); std::any Res = TriggerLuaEvent("onPlayerAuth", false, nullptr, std::move(arg), true);
std::string Type = Res.type().name(); std::string Type = Res.type().name();
if (Type.find("int") != std::string::npos && std::any_cast<int>(Res)) { if (Type.find("int") != std::string::npos && std::any_cast<int>(Res)) {
ClientKick(*LockedClient, "you are not allowed on the server!"); ClientKick(*Client, "you are not allowed on the server!");
return; return;
} else if (Type.find("string") != std::string::npos) { } else if (Type.find("string") != std::string::npos) {
ClientKick(*LockedClient, std::any_cast<std::string>(Res)); ClientKick(*Client, std::any_cast<std::string>(Res));
return; return;
} }
if (mServer.ClientCount() < size_t(Application::Settings.MaxPlayers)) { if (mServer.ClientCount() < size_t(Application::Settings.MaxPlayers)) {
info("Identification success"); info("Identification success");
TCPClient(c); mServer.InsertClient(Client);
TCPClient(Client);
} else } else
ClientKick(*LockedClient, "Server full!"); ClientKick(*Client, "Server full!");
} }
std::weak_ptr<TClient> TTCPServer::CreateClient(SOCKET TCPSock) { std::shared_ptr<TClient> TTCPServer::CreateClient(SOCKET TCPSock) {
auto c = mServer.InsertNewClient(); auto c = std::make_shared<TClient>(mServer);
c.lock()->SetTCPSock(TCPSock); c->SetTCPSock(TCPSock);
return c; return c;
} }