mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2025-07-03 00:05:34 +00:00
Add more memory safety, fix print(nil) crash
This commit is contained in:
parent
b9758ddaea
commit
c6fbd3dc49
@ -23,7 +23,7 @@ struct VData{
|
|||||||
|
|
||||||
class Client {
|
class Client {
|
||||||
private:
|
private:
|
||||||
std::set<VData*> VehicleData; //ID and Data;
|
std::set<std::unique_ptr<VData>> VehicleData; //ID and Data;
|
||||||
std::string Name = "Unknown Client";
|
std::string Name = "Unknown Client";
|
||||||
sockaddr_in UDPADDR;
|
sockaddr_in UDPADDR;
|
||||||
std::string Role;
|
std::string Role;
|
||||||
@ -39,7 +39,8 @@ public:
|
|||||||
void SetDID(const std::string& did);
|
void SetDID(const std::string& did);
|
||||||
std::string GetCarData(int ident);
|
std::string GetCarData(int ident);
|
||||||
void SetUDPAddr(sockaddr_in Addr);
|
void SetUDPAddr(sockaddr_in Addr);
|
||||||
std::set<VData*> GetAllCars();
|
std::set<std::unique_ptr<VData>>& GetAllCars();
|
||||||
|
const std::set<std::unique_ptr<VData>>& GetAllCars() const;
|
||||||
void SetTCPSock(SOCKET CSock);
|
void SetTCPSock(SOCKET CSock);
|
||||||
void SetStatus(int status);
|
void SetStatus(int status);
|
||||||
void DeleteCar(int ident);
|
void DeleteCar(int ident);
|
||||||
|
@ -314,7 +314,7 @@ int lua_GetCars(lua_State* L) {
|
|||||||
Client* c = GetClient(ID);
|
Client* c = GetClient(ID);
|
||||||
if (c != nullptr) {
|
if (c != nullptr) {
|
||||||
int i = 1;
|
int i = 1;
|
||||||
for (VData* v : c->GetAllCars()) {
|
for (auto& v : c->GetAllCars()) {
|
||||||
if (v != nullptr) {
|
if (v != nullptr) {
|
||||||
lua_pushinteger(L, v->ID);
|
lua_pushinteger(L, v->ID);
|
||||||
lua_pushstring(L, v->Data.substr(3).c_str());
|
lua_pushstring(L, v->Data.substr(3).c_str());
|
||||||
@ -519,7 +519,12 @@ extern "C" {
|
|||||||
int lua_Print(lua_State* L) {
|
int lua_Print(lua_State* L) {
|
||||||
int Arg = lua_gettop(L);
|
int Arg = lua_gettop(L);
|
||||||
for (int i = 1; i <= Arg; i++) {
|
for (int i = 1; i <= Arg; i++) {
|
||||||
ConsoleOut(lua_tostring(L, i) + std::string("\n"));
|
auto str = lua_tostring(L, i);
|
||||||
|
if (str != nullptr) {
|
||||||
|
ConsoleOut(str + std::string(Sec("\n")));
|
||||||
|
} else {
|
||||||
|
ConsoleOut(Sec("nil\n"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -46,22 +46,14 @@ SOCKET Client::GetTCPSock(){
|
|||||||
return TCPSOCK;
|
return TCPSOCK;
|
||||||
}
|
}
|
||||||
void Client::DeleteCar(int ident){
|
void Client::DeleteCar(int ident){
|
||||||
for(VData* v : VehicleData){
|
for(auto& v : VehicleData){
|
||||||
if(v != nullptr && v->ID == ident){
|
if(v != nullptr && v->ID == ident){
|
||||||
VehicleData.erase(v);
|
VehicleData.erase(v);
|
||||||
delete v;
|
|
||||||
v = nullptr;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void Client::ClearCars(){
|
void Client::ClearCars(){
|
||||||
for(VData* v : VehicleData){
|
|
||||||
if(v != nullptr){
|
|
||||||
delete v;
|
|
||||||
v = nullptr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
VehicleData.clear();
|
VehicleData.clear();
|
||||||
}
|
}
|
||||||
int Client::GetOpenCarID(){
|
int Client::GetOpenCarID(){
|
||||||
@ -69,7 +61,7 @@ int Client::GetOpenCarID(){
|
|||||||
bool found;
|
bool found;
|
||||||
do {
|
do {
|
||||||
found = true;
|
found = true;
|
||||||
for (VData*v : VehicleData) {
|
for (auto& v : VehicleData) {
|
||||||
if (v != nullptr && v->ID == OpenID){
|
if (v != nullptr && v->ID == OpenID){
|
||||||
OpenID++;
|
OpenID++;
|
||||||
found = false;
|
found = false;
|
||||||
@ -79,14 +71,19 @@ int Client::GetOpenCarID(){
|
|||||||
return OpenID;
|
return OpenID;
|
||||||
}
|
}
|
||||||
void Client::AddNewCar(int ident,const std::string& Data){
|
void Client::AddNewCar(int ident,const std::string& Data){
|
||||||
VehicleData.insert(new VData{ident,Data});
|
VehicleData.insert(std::unique_ptr<VData>(new VData{ident,Data}));
|
||||||
}
|
}
|
||||||
|
|
||||||
std::set<VData*> Client::GetAllCars(){
|
std::set<std::unique_ptr<VData>>& Client::GetAllCars(){
|
||||||
return VehicleData;
|
return VehicleData;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const std::set<std::unique_ptr<VData>> &Client::GetAllCars() const {
|
||||||
|
return VehicleData;
|
||||||
|
}
|
||||||
|
|
||||||
std::string Client::GetCarData(int ident){
|
std::string Client::GetCarData(int ident){
|
||||||
for(VData*v : VehicleData){
|
for(auto& v : VehicleData){
|
||||||
if(v != nullptr && v->ID == ident){
|
if(v != nullptr && v->ID == ident){
|
||||||
return v->Data;
|
return v->Data;
|
||||||
}
|
}
|
||||||
@ -95,7 +92,7 @@ std::string Client::GetCarData(int ident){
|
|||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
void Client::SetCarData(int ident,const std::string&Data){
|
void Client::SetCarData(int ident,const std::string&Data){
|
||||||
for(VData*v : VehicleData){
|
for(auto& v : VehicleData){
|
||||||
if(v != nullptr && v->ID == ident){
|
if(v != nullptr && v->ID == ident){
|
||||||
v->Data = Data;
|
v->Data = Data;
|
||||||
return;
|
return;
|
||||||
|
@ -128,7 +128,7 @@ void SyncClient(Client*c){
|
|||||||
for (auto& client : CI->Clients) {
|
for (auto& client : CI->Clients) {
|
||||||
if(client != nullptr){
|
if(client != nullptr){
|
||||||
if (client.get() != c) {
|
if (client.get() != c) {
|
||||||
for (VData *v : client->GetAllCars()) {
|
for (auto& v : client->GetAllCars()) {
|
||||||
if(v != nullptr){
|
if(v != nullptr){
|
||||||
Respond(c, v->Data, true);
|
Respond(c, v->Data, true);
|
||||||
std::this_thread::sleep_for(std::chrono::seconds(2));
|
std::this_thread::sleep_for(std::chrono::seconds(2));
|
||||||
|
@ -63,7 +63,7 @@ void OnDisconnect(Client*c,bool kicked){
|
|||||||
info(c->GetName() + Sec(" Connection Terminated"));
|
info(c->GetName() + Sec(" Connection Terminated"));
|
||||||
if(c == nullptr)return;
|
if(c == nullptr)return;
|
||||||
std::string Packet;
|
std::string Packet;
|
||||||
for(VData*v : c->GetAllCars()){
|
for(auto& v : c->GetAllCars()){
|
||||||
if(v != nullptr) {
|
if(v != nullptr) {
|
||||||
Packet = "Od:" + std::to_string(c->GetID()) + "-" + std::to_string(v->ID);
|
Packet = "Od:" + std::to_string(c->GetID()) + "-" + std::to_string(v->ID);
|
||||||
SendToAll(c, Packet, false, true);
|
SendToAll(c, Packet, false, true);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user