Lock when accessing vehicles

This commit is contained in:
Anonymous-275 2021-03-31 12:42:29 +03:00
parent 3c68dfaeaf
commit 7231c69e10
2 changed files with 10 additions and 1 deletions

View File

@ -6,6 +6,7 @@
// FIXME: add debug prints
void TClient::DeleteCar(int Ident) {
std::unique_lock lock(mVehicleDataMutex);
for (auto& v : mVehicleData) {
if (v.ID() == Ident) {
mVehicleData.erase(v);
@ -15,6 +16,7 @@ void TClient::DeleteCar(int Ident) {
}
void TClient::ClearCars() {
std::unique_lock lock(mVehicleDataMutex);
mVehicleData.clear();
}
@ -34,6 +36,7 @@ int TClient::GetOpenCarID() const {
}
void TClient::AddNewCar(int Ident, const std::string& Data) {
std::unique_lock lock(mVehicleDataMutex);
mVehicleData.emplace(Ident, Data);
}
@ -42,6 +45,7 @@ TClient::TVehicleDataLockPair TClient::GetAllCars() {
}
std::string TClient::GetCarData(int Ident) {
std::unique_lock lock(mVehicleDataMutex);
for (auto& v : mVehicleData) {
if (v.ID() == Ident) {
return v.Data();
@ -52,6 +56,7 @@ std::string TClient::GetCarData(int Ident) {
}
void TClient::SetCarData(int Ident, const std::string& Data) {
std::unique_lock lock(mVehicleDataMutex);
for (auto& v : mVehicleData) {
if (v.ID() == Ident) {
v.SetData(Data);

View File

@ -280,7 +280,11 @@ void TServer::ParseVehicle(TClient& c, const std::string& Pckt, TNetwork& Networ
void TServer::Apply(TClient& c, int VID, const std::string& pckt) {
std::string Packet = pckt.substr(pckt.find('{')), VD = c.GetCarData(VID);
std::string Header = VD.substr(0, VD.find('{'));
VD = VD.substr(VD.find('{'));
if(VD.empty()){
error("Applying config for a non existing car?");
abort();
}
VD = VD.substr(VD.find('{')); //TODO somehow this got hit while VD is empty
rapidjson::Document Veh, Pack;
Veh.Parse(VD.c_str());
if (Veh.HasParseError()) {