mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2026-04-23 16:36:49 +00:00
deprecate ForEachClientWeak in favor of new ForEachClient
This commit is contained in:
6
include/IterationDecision.h
Normal file
6
include/IterationDecision.h
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
enum IterationDecision {
|
||||||
|
Continue,
|
||||||
|
Break,
|
||||||
|
};
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "IThreaded.h"
|
#include "IThreaded.h"
|
||||||
|
#include "IterationDecision.h"
|
||||||
#include "RWMutex.h"
|
#include "RWMutex.h"
|
||||||
#include "TScopedTimer.h"
|
#include "TScopedTimer.h"
|
||||||
#include <functional>
|
#include <functional>
|
||||||
@@ -23,7 +24,9 @@ public:
|
|||||||
void InsertClient(const std::shared_ptr<TClient>& Ptr);
|
void InsertClient(const std::shared_ptr<TClient>& Ptr);
|
||||||
void RemoveClient(const std::weak_ptr<TClient>&);
|
void RemoveClient(const std::weak_ptr<TClient>&);
|
||||||
// in Fn, return true to continue, return false to break
|
// in Fn, return true to continue, return false to break
|
||||||
void ForEachClient(const std::function<bool(std::weak_ptr<TClient>)>& Fn);
|
[[deprecated("use ForEachClient instead")]] void ForEachClientWeak(const std::function<bool(std::weak_ptr<TClient>)>& Fn);
|
||||||
|
// in Fn, return Break or Continue
|
||||||
|
void ForEachClient(const std::function<IterationDecision(const std::shared_ptr<TClient>&)>& Fn);
|
||||||
size_t ClientCount() const;
|
size_t ClientCount() const;
|
||||||
|
|
||||||
static void GlobalParser(const std::weak_ptr<TClient>& Client, std::vector<uint8_t>&& Packet, TNetwork& Network);
|
static void GlobalParser(const std::weak_ptr<TClient>& Client, std::vector<uint8_t>&& Packet, TNetwork& Network);
|
||||||
|
|||||||
@@ -138,7 +138,7 @@ int TClient::SecondsSinceLastPing() {
|
|||||||
|
|
||||||
std::optional<std::weak_ptr<TClient>> GetClient(TServer& Server, int ID) {
|
std::optional<std::weak_ptr<TClient>> GetClient(TServer& Server, int ID) {
|
||||||
std::optional<std::weak_ptr<TClient>> MaybeClient { std::nullopt };
|
std::optional<std::weak_ptr<TClient>> MaybeClient { std::nullopt };
|
||||||
Server.ForEachClient([&](std::weak_ptr<TClient> CPtr) -> bool {
|
Server.ForEachClientWeak([&](std::weak_ptr<TClient> CPtr) -> bool {
|
||||||
ReadLock Lock(Server.GetClientMutex());
|
ReadLock Lock(Server.GetClientMutex());
|
||||||
try {
|
try {
|
||||||
auto C = CPtr.lock();
|
auto C = CPtr.lock();
|
||||||
|
|||||||
@@ -298,7 +298,7 @@ void TConsole::Command_Debug(const std::string&, const std::vector<std::string>&
|
|||||||
connection and do not necessarily reflect the *current* data rate
|
connection and do not necessarily reflect the *current* data rate
|
||||||
of that client.
|
of that client.
|
||||||
)"));
|
)"));
|
||||||
mLuaEngine->Server().ForEachClient([&](std::weak_ptr<TClient> Client) -> bool {
|
mLuaEngine->Server().ForEachClientWeak([&](std::weak_ptr<TClient> Client) -> bool {
|
||||||
if (!Client.expired()) {
|
if (!Client.expired()) {
|
||||||
auto Locked = Client.lock();
|
auto Locked = Client.lock();
|
||||||
std::string State = "";
|
std::string State = "";
|
||||||
@@ -390,7 +390,7 @@ void TConsole::Command_Kick(const std::string&, const std::vector<std::string>&
|
|||||||
beammp_trace("attempt to kick '" + Name + "' for '" + Reason + "'");
|
beammp_trace("attempt to kick '" + Name + "' for '" + Reason + "'");
|
||||||
bool Kicked = false;
|
bool Kicked = false;
|
||||||
|
|
||||||
mLuaEngine->Server().ForEachClient([&](std::weak_ptr<TClient> Client) -> bool {
|
mLuaEngine->Server().ForEachClientWeak([&](std::weak_ptr<TClient> Client) -> bool {
|
||||||
auto Locked = Client.lock();
|
auto Locked = Client.lock();
|
||||||
if (Locked) {
|
if (Locked) {
|
||||||
if (StringStartsWithLower(Locked->GetName(), Name)) {
|
if (StringStartsWithLower(Locked->GetName(), Name)) {
|
||||||
@@ -552,7 +552,7 @@ void TConsole::Command_List(const std::string&, const std::vector<std::string>&
|
|||||||
} else {
|
} else {
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << std::left << std::setw(25) << "Name" << std::setw(6) << "ID" << std::setw(6) << "Cars" << std::endl;
|
ss << std::left << std::setw(25) << "Name" << std::setw(6) << "ID" << std::setw(6) << "Cars" << std::endl;
|
||||||
mLuaEngine->Server().ForEachClient([&](std::weak_ptr<TClient> Client) -> bool {
|
mLuaEngine->Server().ForEachClientWeak([&](std::weak_ptr<TClient> Client) -> bool {
|
||||||
if (!Client.expired()) {
|
if (!Client.expired()) {
|
||||||
auto locked = Client.lock();
|
auto locked = Client.lock();
|
||||||
ss << std::left << std::setw(25) << locked->GetName()
|
ss << std::left << std::setw(25) << locked->GetName()
|
||||||
@@ -579,7 +579,7 @@ void TConsole::Command_Status(const std::string&, const std::vector<std::string>
|
|||||||
size_t SyncingCount = 0;
|
size_t SyncingCount = 0;
|
||||||
size_t MissedPacketQueueSum = 0;
|
size_t MissedPacketQueueSum = 0;
|
||||||
int LargestSecondsSinceLastPing = 0;
|
int LargestSecondsSinceLastPing = 0;
|
||||||
mLuaEngine->Server().ForEachClient([&](std::weak_ptr<TClient> Client) -> bool {
|
mLuaEngine->Server().ForEachClientWeak([&](std::weak_ptr<TClient> Client) -> bool {
|
||||||
if (!Client.expired()) {
|
if (!Client.expired()) {
|
||||||
auto Locked = Client.lock();
|
auto Locked = Client.lock();
|
||||||
CarCount += Locked->GetCarCount();
|
CarCount += Locked->GetCarCount();
|
||||||
@@ -680,7 +680,7 @@ void TConsole::Autocomplete_Lua(const std::string& stub, std::vector<std::string
|
|||||||
void TConsole::Autocomplete_Kick(const std::string& stub, std::vector<std::string>& suggestions) {
|
void TConsole::Autocomplete_Kick(const std::string& stub, std::vector<std::string>& suggestions) {
|
||||||
std::string stub_lower = boost::algorithm::to_lower_copy(stub);
|
std::string stub_lower = boost::algorithm::to_lower_copy(stub);
|
||||||
|
|
||||||
mLuaEngine->Server().ForEachClient([&](std::weak_ptr<TClient> Client) -> bool {
|
mLuaEngine->Server().ForEachClientWeak([&](std::weak_ptr<TClient> Client) -> bool {
|
||||||
auto Locked = Client.lock();
|
auto Locked = Client.lock();
|
||||||
if (Locked) {
|
if (Locked) {
|
||||||
if (StringStartsWithLower(Locked->GetName(), stub_lower)) {
|
if (StringStartsWithLower(Locked->GetName(), stub_lower)) {
|
||||||
|
|||||||
@@ -167,7 +167,7 @@ THeartbeatThread::THeartbeatThread(TResourceManager& ResourceManager, TServer& S
|
|||||||
}
|
}
|
||||||
std::string THeartbeatThread::GetPlayers() {
|
std::string THeartbeatThread::GetPlayers() {
|
||||||
std::string Return;
|
std::string Return;
|
||||||
mServer.ForEachClient([&](const std::weak_ptr<TClient>& ClientPtr) -> bool {
|
mServer.ForEachClientWeak([&](const std::weak_ptr<TClient>& ClientPtr) -> bool {
|
||||||
ReadLock Lock(mServer.GetClientMutex());
|
ReadLock Lock(mServer.GetClientMutex());
|
||||||
if (!ClientPtr.expired()) {
|
if (!ClientPtr.expired()) {
|
||||||
Return += ClientPtr.lock()->GetName() + ";";
|
Return += ClientPtr.lock()->GetName() + ";";
|
||||||
|
|||||||
@@ -546,7 +546,7 @@ sol::table TLuaEngine::StateThreadData::Lua_GetPlayerIdentifiers(int ID) {
|
|||||||
|
|
||||||
sol::table TLuaEngine::StateThreadData::Lua_GetPlayers() {
|
sol::table TLuaEngine::StateThreadData::Lua_GetPlayers() {
|
||||||
sol::table Result = mStateView.create_table();
|
sol::table Result = mStateView.create_table();
|
||||||
mEngine->Server().ForEachClient([&](std::weak_ptr<TClient> Client) -> bool {
|
mEngine->Server().ForEachClientWeak([&](std::weak_ptr<TClient> Client) -> bool {
|
||||||
if (!Client.expired()) {
|
if (!Client.expired()) {
|
||||||
auto locked = Client.lock();
|
auto locked = Client.lock();
|
||||||
Result[locked->GetID()] = locked->GetName();
|
Result[locked->GetID()] = locked->GetName();
|
||||||
@@ -558,7 +558,7 @@ sol::table TLuaEngine::StateThreadData::Lua_GetPlayers() {
|
|||||||
|
|
||||||
int TLuaEngine::StateThreadData::Lua_GetPlayerIDByName(const std::string& Name) {
|
int TLuaEngine::StateThreadData::Lua_GetPlayerIDByName(const std::string& Name) {
|
||||||
int Id = -1;
|
int Id = -1;
|
||||||
mEngine->mServer->ForEachClient([&Id, &Name](std::weak_ptr<TClient> Client) -> bool {
|
mEngine->mServer->ForEachClientWeak([&Id, &Name](std::weak_ptr<TClient> Client) -> bool {
|
||||||
if (!Client.expired()) {
|
if (!Client.expired()) {
|
||||||
auto locked = Client.lock();
|
auto locked = Client.lock();
|
||||||
if (locked->GetName() == Name) {
|
if (locked->GetName() == Name) {
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ TNetwork::TNetwork(TServer& Server, TPPSMonitor& PPSMonitor, TResourceManager& R
|
|||||||
auto Futures = LuaAPI::MP::Engine->TriggerEvent("onShutdown", "");
|
auto Futures = LuaAPI::MP::Engine->TriggerEvent("onShutdown", "");
|
||||||
TLuaEngine::WaitForAll(Futures, std::chrono::seconds(60));
|
TLuaEngine::WaitForAll(Futures, std::chrono::seconds(60));
|
||||||
beammp_debug("Kicking all players due to shutdown");
|
beammp_debug("Kicking all players due to shutdown");
|
||||||
Server.ForEachClient([&](std::weak_ptr<TClient> client) -> bool {
|
Server.ForEachClientWeak([&](std::weak_ptr<TClient> client) -> bool {
|
||||||
if (!client.expired()) {
|
if (!client.expired()) {
|
||||||
ClientKick(*client.lock(), "Server shutdown");
|
ClientKick(*client.lock(), "Server shutdown");
|
||||||
}
|
}
|
||||||
@@ -213,7 +213,7 @@ void TNetwork::HandleDownload(TConnection&& Conn) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto ID = uint8_t(D);
|
auto ID = uint8_t(D);
|
||||||
mServer.ForEachClient([&](const std::weak_ptr<TClient>& ClientPtr) -> bool {
|
mServer.ForEachClientWeak([&](const std::weak_ptr<TClient>& ClientPtr) -> bool {
|
||||||
ReadLock Lock(mServer.GetClientMutex());
|
ReadLock Lock(mServer.GetClientMutex());
|
||||||
if (!ClientPtr.expired()) {
|
if (!ClientPtr.expired()) {
|
||||||
auto c = ClientPtr.lock();
|
auto c = ClientPtr.lock();
|
||||||
@@ -298,7 +298,7 @@ std::shared_ptr<TClient> TNetwork::Authentication(TConnection&& RawConnection) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
beammp_debug("Name -> " + Client->GetName() + ", Guest -> " + std::to_string(Client->IsGuest()) + ", Roles -> " + Client->GetRoles());
|
beammp_debug("Name -> " + Client->GetName() + ", Guest -> " + std::to_string(Client->IsGuest()) + ", Roles -> " + Client->GetRoles());
|
||||||
mServer.ForEachClient([&](const std::weak_ptr<TClient>& ClientPtr) -> bool {
|
mServer.ForEachClientWeak([&](const std::weak_ptr<TClient>& ClientPtr) -> bool {
|
||||||
std::shared_ptr<TClient> Cl;
|
std::shared_ptr<TClient> Cl;
|
||||||
{
|
{
|
||||||
ReadLock Lock(mServer.GetClientMutex());
|
ReadLock Lock(mServer.GetClientMutex());
|
||||||
@@ -534,7 +534,7 @@ void TNetwork::TCPClient(const std::weak_ptr<TClient>& c) {
|
|||||||
|
|
||||||
void TNetwork::UpdatePlayer(TClient& Client) {
|
void TNetwork::UpdatePlayer(TClient& Client) {
|
||||||
std::string Packet = ("Ss") + std::to_string(mServer.ClientCount()) + "/" + std::to_string(Application::GetSettingInt(StrMaxPlayers)) + ":";
|
std::string Packet = ("Ss") + std::to_string(mServer.ClientCount()) + "/" + std::to_string(Application::GetSettingInt(StrMaxPlayers)) + ":";
|
||||||
mServer.ForEachClient([&](const std::weak_ptr<TClient>& ClientPtr) -> bool {
|
mServer.ForEachClientWeak([&](const std::weak_ptr<TClient>& ClientPtr) -> bool {
|
||||||
ReadLock Lock(mServer.GetClientMutex());
|
ReadLock Lock(mServer.GetClientMutex());
|
||||||
if (!ClientPtr.expired()) {
|
if (!ClientPtr.expired()) {
|
||||||
auto c = ClientPtr.lock();
|
auto c = ClientPtr.lock();
|
||||||
@@ -582,7 +582,7 @@ int TNetwork::OpenID() {
|
|||||||
bool found;
|
bool found;
|
||||||
do {
|
do {
|
||||||
found = true;
|
found = true;
|
||||||
mServer.ForEachClient([&](const std::weak_ptr<TClient>& ClientPtr) -> bool {
|
mServer.ForEachClientWeak([&](const std::weak_ptr<TClient>& ClientPtr) -> bool {
|
||||||
ReadLock Lock(mServer.GetClientMutex());
|
ReadLock Lock(mServer.GetClientMutex());
|
||||||
if (!ClientPtr.expired()) {
|
if (!ClientPtr.expired()) {
|
||||||
auto c = ClientPtr.lock();
|
auto c = ClientPtr.lock();
|
||||||
@@ -852,7 +852,7 @@ bool TNetwork::SyncClient(const std::weak_ptr<TClient>& c) {
|
|||||||
LockedClient->SetIsSyncing(true);
|
LockedClient->SetIsSyncing(true);
|
||||||
bool Return = false;
|
bool Return = false;
|
||||||
bool res = true;
|
bool res = true;
|
||||||
mServer.ForEachClient([&](const std::weak_ptr<TClient>& ClientPtr) -> bool {
|
mServer.ForEachClientWeak([&](const std::weak_ptr<TClient>& ClientPtr) -> bool {
|
||||||
std::shared_ptr<TClient> client;
|
std::shared_ptr<TClient> client;
|
||||||
{
|
{
|
||||||
ReadLock Lock(mServer.GetClientMutex());
|
ReadLock Lock(mServer.GetClientMutex());
|
||||||
@@ -893,7 +893,7 @@ void TNetwork::SendToAll(TClient* c, const std::vector<uint8_t>& Data, bool Self
|
|||||||
beammp_assert(c);
|
beammp_assert(c);
|
||||||
char C = Data.at(0);
|
char C = Data.at(0);
|
||||||
bool ret = true;
|
bool ret = true;
|
||||||
mServer.ForEachClient([&](std::weak_ptr<TClient> ClientPtr) -> bool {
|
mServer.ForEachClientWeak([&](std::weak_ptr<TClient> ClientPtr) -> bool {
|
||||||
std::shared_ptr<TClient> Client;
|
std::shared_ptr<TClient> Client;
|
||||||
try {
|
try {
|
||||||
ReadLock Lock(mServer.GetClientMutex());
|
ReadLock Lock(mServer.GetClientMutex());
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ void TPPSMonitor::operator()() {
|
|||||||
Application::SetPPS("-");
|
Application::SetPPS("-");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
mServer.ForEachClient([&](const std::weak_ptr<TClient>& ClientPtr) -> bool {
|
mServer.ForEachClientWeak([&](const std::weak_ptr<TClient>& ClientPtr) -> bool {
|
||||||
std::shared_ptr<TClient> c;
|
std::shared_ptr<TClient> c;
|
||||||
{
|
{
|
||||||
ReadLock Lock(mServer.GetClientMutex());
|
ReadLock Lock(mServer.GetClientMutex());
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
#include "Client.h"
|
#include "Client.h"
|
||||||
#include "Common.h"
|
#include "Common.h"
|
||||||
#include "CustomAssert.h"
|
#include "CustomAssert.h"
|
||||||
|
#include "IterationDecision.h"
|
||||||
#include "TNetwork.h"
|
#include "TNetwork.h"
|
||||||
#include "TPPSMonitor.h"
|
#include "TPPSMonitor.h"
|
||||||
#include <TLuaPlugin.h>
|
#include <TLuaPlugin.h>
|
||||||
@@ -189,7 +190,7 @@ void TServer::RemoveClient(const std::weak_ptr<TClient>& WeakClientPtr) {
|
|||||||
mClients.erase(WeakClientPtr.lock());
|
mClients.erase(WeakClientPtr.lock());
|
||||||
}
|
}
|
||||||
|
|
||||||
void TServer::ForEachClient(const std::function<bool(std::weak_ptr<TClient>)>& Fn) {
|
void TServer::ForEachClientWeak(const std::function<bool(std::weak_ptr<TClient>)>& Fn) {
|
||||||
decltype(mClients) Clients;
|
decltype(mClients) Clients;
|
||||||
{
|
{
|
||||||
ReadLock lock(mClientsMutex);
|
ReadLock lock(mClientsMutex);
|
||||||
@@ -202,6 +203,20 @@ void TServer::ForEachClient(const std::function<bool(std::weak_ptr<TClient>)>& F
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TServer::ForEachClient(const std::function<IterationDecision(const std::shared_ptr<TClient>&)>& Fn) {
|
||||||
|
decltype(mClients) Clients;
|
||||||
|
{
|
||||||
|
ReadLock lock(mClientsMutex);
|
||||||
|
Clients = mClients;
|
||||||
|
}
|
||||||
|
for (auto& Client : Clients) {
|
||||||
|
auto Decision = Fn(Client);
|
||||||
|
if (Decision == IterationDecision::Break) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
size_t TServer::ClientCount() const {
|
size_t TServer::ClientCount() const {
|
||||||
ReadLock Lock(mClientsMutex);
|
ReadLock Lock(mClientsMutex);
|
||||||
return mClients.size();
|
return mClients.size();
|
||||||
|
|||||||
Reference in New Issue
Block a user