Update CMake configuration and refactor code to use smart pointers for memory management

This commit is contained in:
Torsten Schulz
2025-08-11 11:15:54 +02:00
parent ba6b788075
commit f44d780537
10 changed files with 110 additions and 77 deletions

View File

@@ -15,9 +15,9 @@
namespace Yc {
namespace Lib {
Server::Server(Yc::Lib::Config *config) :
_config(config),
_stop(false) {
Server::Server(std::shared_ptr<Yc::Lib::Config> config) :
_config(std::move(config)),
_stop(false) {
struct sockaddr_in serverAddr;
int opt = true;
_socket = socket(AF_INET, SOCK_STREAM, 0);
@@ -31,7 +31,7 @@ namespace Yc {
std::cout << "bind not possible" << std::endl;
exit(-1);
}
createRooms(config->group("rooms"));
createRooms(_config->group("rooms"));
}
void Server::run() {
@@ -49,14 +49,14 @@ namespace Yc {
FD_ZERO(&fd);
FD_SET(_socket, &fd);
if (select(_maxSd + 1, &fd, NULL, NULL, &tv) > 0) {
std::async(std::launch::async, &Server::handleRequest, this);
std::thread(&Server::handleRequest, this).detach();
}
}
}
std::vector<std::string> Server::roomList() {
std::vector<std::string> list;
for (auto &room: _rooms) {
for (const auto &room: _rooms) {
list.push_back(room->name());
}
return list;
@@ -82,7 +82,7 @@ namespace Yc {
return false;
}
bool Server::changeRoom(User *user, std::string newRoom, std::string password) {
bool Server::changeRoom(std::shared_ptr<User> user, std::string newRoom, std::string password) {
if (!roomAllowed(newRoom, user->name(), password)) {
return false;
}
@@ -113,8 +113,9 @@ namespace Yc {
}
void Server::createRooms(Json::Value roomList) {
auto self = shared_from_this();
for (auto &room: roomList) {
Room *newRoom = new Room(this, room);
auto newRoom = std::make_shared<Room>(self, room);
_rooms.push_back(newRoom);
}
}
@@ -143,7 +144,7 @@ namespace Yc {
}
bool Server::userExists(std::string userName) {
for (auto &room: _rooms) {
for (const auto &room: _rooms) {
if (room->userNameExists(userName)) {
return true;
}
@@ -160,10 +161,10 @@ namespace Yc {
close(userSocket);
return;
}
std::string room = data["room"].asString();
std::string roomName = data["room"].asString();
bool added(false);
for (auto &room: _rooms) {
if (room->name() == data["room"].asString()) {
if (room->name() == roomName) {
if (room->addUser(data["name"].asString(), data["color"].asString(), data["password"].asString(), userSocket)) {
added = true;
break;