Füge Unterstützung für Würfelspiele hinzu und verbessere Debugging-Optionen
- Implementiere neue Funktionen in der ChatRoom-Klasse für das Starten, Rollen und Beenden von Würfelspielen. - Füge eine Option zur Aktivierung von Debug-Logging in CMake hinzu, um die Entwicklung zu erleichtern. - Aktualisiere die ChatUser-Klasse, um die Interaktion mit dem Würfelspiel zu ermöglichen. - Verbessere die Socket-Verwaltung im Server, um WebSocket-Verbindungen zu unterstützen und die Handhabung von Anfragen zu optimieren. - Aktualisiere die Konfiguration, um die neue Funktionalität zu unterstützen und die Benutzererfahrung zu verbessern.
This commit is contained in:
@@ -10,7 +10,9 @@
|
||||
#include <json/json.h>
|
||||
#include <iostream>
|
||||
#include <netinet/tcp.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sstream>
|
||||
#include <regex>
|
||||
|
||||
namespace Yc {
|
||||
namespace Lib {
|
||||
@@ -19,18 +21,45 @@ namespace Yc {
|
||||
_config(std::move(config)),
|
||||
_database(std::move(database)),
|
||||
_stop(false) {
|
||||
struct sockaddr_in serverAddr;
|
||||
int opt = true;
|
||||
_socket = socket(AF_INET, SOCK_STREAM, 0);
|
||||
setsockopt(_socket, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt));
|
||||
int flags = 1;
|
||||
setsockopt(_socket, IPPROTO_TCP, TCP_NODELAY, (void *)&flags, sizeof(flags));
|
||||
serverAddr.sin_family = AF_INET;
|
||||
serverAddr.sin_addr.s_addr = INADDR_ANY;
|
||||
serverAddr.sin_port = htons(1235);
|
||||
if (bind(_socket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) < 0) {
|
||||
std::cout << "bind not possible" << std::endl;
|
||||
exit(-1);
|
||||
int port = 1235;
|
||||
try {
|
||||
Json::Value p = _config->value("server", "port");
|
||||
if (p.isInt()) port = p.asInt();
|
||||
} catch (...) {}
|
||||
int opt = 1;
|
||||
// Try IPv6 dual-stack (accepts IPv4 via v4-mapped if v6only=0)
|
||||
_socket = socket(AF_INET6, SOCK_STREAM, 0);
|
||||
if (_socket >= 0) {
|
||||
setsockopt(_socket, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt));
|
||||
int v6only = 0; setsockopt(_socket, IPPROTO_IPV6, IPV6_V6ONLY, &v6only, sizeof(v6only));
|
||||
int flags = 1; setsockopt(_socket, IPPROTO_TCP, TCP_NODELAY, (void *)&flags, sizeof(flags));
|
||||
int ka = 1; setsockopt(_socket, SOL_SOCKET, SO_KEEPALIVE, &ka, sizeof(ka));
|
||||
struct sockaddr_in6 addr6{}; addr6.sin6_family = AF_INET6; addr6.sin6_addr = in6addr_any; addr6.sin6_port = htons(port);
|
||||
if (bind(_socket, (struct sockaddr *)&addr6, sizeof(addr6)) == 0) {
|
||||
std::cout << "[YourChat] Server gestartet. Lausche auf Port " << port << " (IPv6 dual-stack)" << std::endl;
|
||||
} else {
|
||||
// Fallback to IPv4
|
||||
close(_socket);
|
||||
_socket = socket(AF_INET, SOCK_STREAM, 0);
|
||||
setsockopt(_socket, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt));
|
||||
flags = 1; setsockopt(_socket, IPPROTO_TCP, TCP_NODELAY, (void *)&flags, sizeof(flags));
|
||||
ka = 1; setsockopt(_socket, SOL_SOCKET, SO_KEEPALIVE, &ka, sizeof(ka));
|
||||
struct sockaddr_in addr4{}; addr4.sin_family = AF_INET; addr4.sin_addr.s_addr = INADDR_ANY; addr4.sin_port = htons(port);
|
||||
if (bind(_socket, (struct sockaddr *)&addr4, sizeof(addr4)) != 0) {
|
||||
std::cout << "bind not possible" << std::endl; exit(-1);
|
||||
}
|
||||
std::cout << "[YourChat] Server gestartet. Lausche auf Port " << port << " (IPv4)" << std::endl;
|
||||
}
|
||||
} else {
|
||||
// Fallback to IPv4 directly
|
||||
_socket = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (_socket < 0) { std::cout << "socket create failed" << std::endl; exit(-1);}
|
||||
setsockopt(_socket, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt));
|
||||
int flags = 1; setsockopt(_socket, IPPROTO_TCP, TCP_NODELAY, (void *)&flags, sizeof(flags));
|
||||
int ka = 1; setsockopt(_socket, SOL_SOCKET, SO_KEEPALIVE, &ka, sizeof(ka));
|
||||
struct sockaddr_in addr4{}; addr4.sin_family = AF_INET; addr4.sin_addr.s_addr = INADDR_ANY; addr4.sin_port = htons(port);
|
||||
if (bind(_socket, (struct sockaddr *)&addr4, sizeof(addr4)) != 0) { std::cout << "bind not possible" << std::endl; exit(-1);}
|
||||
std::cout << "[YourChat] Server gestartet. Lausche auf Port " << port << " (IPv4)" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,14 +71,58 @@ namespace Yc {
|
||||
timeval origTv;
|
||||
origTv.tv_sec = 5;
|
||||
origTv.tv_usec = 0;
|
||||
int _maxSd = _socket;
|
||||
int _maxSd = _socket;
|
||||
std::set<int> activeSockets;
|
||||
std::mutex socketMutex;
|
||||
|
||||
while (!_stop) {
|
||||
timeval tv(origTv);
|
||||
fd_set fd;
|
||||
FD_ZERO(&fd);
|
||||
FD_SET(_socket, &fd);
|
||||
|
||||
// Alle aktiven Client-Sockets hinzufügen
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(socketMutex);
|
||||
for (int clientSock : activeSockets) {
|
||||
FD_SET(clientSock, &fd);
|
||||
if (clientSock > _maxSd) _maxSd = clientSock;
|
||||
}
|
||||
}
|
||||
|
||||
if (select(_maxSd + 1, &fd, NULL, NULL, &tv) > 0) {
|
||||
std::thread(&Server::handleRequest, this).detach();
|
||||
// Neue Verbindung?
|
||||
if (FD_ISSET(_socket, &fd)) {
|
||||
std::thread(&Server::handleRequest, this).detach();
|
||||
}
|
||||
|
||||
// Client-Socket-Aktivität?
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(socketMutex);
|
||||
auto it = activeSockets.begin();
|
||||
while (it != activeSockets.end()) {
|
||||
if (FD_ISSET(*it, &fd)) {
|
||||
// Socket ist aktiv, aber handleRequest läuft bereits in separatem Thread
|
||||
// Hier könnten wir Heartbeat/Keepalive prüfen
|
||||
++it;
|
||||
} else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Aufgeräumte Sockets entfernen
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(socketMutex);
|
||||
auto it = activeSockets.begin();
|
||||
while (it != activeSockets.end()) {
|
||||
if (activeSockets.count(*it) == 0) {
|
||||
it = activeSockets.erase(it);
|
||||
} else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -73,16 +146,16 @@ namespace Yc {
|
||||
return list;
|
||||
}
|
||||
|
||||
bool Server::roomAllowed(std::string roomName, std::string userName, std::string password){
|
||||
bool Server::roomAllowed(std::string roomName, std::string userName, std::string password){
|
||||
for (auto &room: _rooms) {
|
||||
if (room->name() == roomName && room->accessAllowed(userName, password)) {
|
||||
if (room->name() == roomName && room->accessAllowed(userName, password)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Server::changeRoom(std::shared_ptr<ChatUser> user, std::string newRoom, std::string password) {
|
||||
bool Server::changeRoom(std::shared_ptr<ChatUser> user, std::string newRoom, std::string password) {
|
||||
if (!roomAllowed(newRoom, user->name(), password)) {
|
||||
return false;
|
||||
}
|
||||
@@ -113,56 +186,247 @@ namespace Yc {
|
||||
}
|
||||
|
||||
void Server::createRooms(Json::Value roomList) {
|
||||
// Ignoriere roomList, lade stattdessen aus der Datenbank
|
||||
auto self = shared_from_this();
|
||||
std::string query = R"(
|
||||
SELECT r.id, r.title, r.password_hash, r.room_type_id, r.is_public, r.owner_id, r.min_age, r.max_age, r.created_at, r.updated_at, rt.tr as room_type
|
||||
FROM chat.room r
|
||||
LEFT JOIN chat.room_type rt ON r.room_type_id = rt.id
|
||||
)";
|
||||
auto result = _database->exec(query);
|
||||
for (const auto& row : result) {
|
||||
Json::Value room;
|
||||
room["id"] = row["id"].as<int>();
|
||||
room["name"] = row["title"].c_str();
|
||||
room["password"] = row["password_hash"].is_null() ? "" : row["password_hash"].c_str();
|
||||
room["type"] = row["room_type_id"].is_null() ? 0 : row["room_type_id"].as<int>();
|
||||
room["is_public"] = row["is_public"].as<bool>();
|
||||
room["owner_id"] = row["owner_id"].is_null() ? 0 : row["owner_id"].as<int>();
|
||||
room["min_age"] = row["min_age"].is_null() ? 0 : row["min_age"].as<int>();
|
||||
room["max_age"] = row["max_age"].is_null() ? 0 : row["max_age"].as<int>();
|
||||
room["created_at"] = row["created_at"].c_str();
|
||||
room["updated_at"] = row["updated_at"].c_str();
|
||||
room["room_type"] = row["room_type"].is_null() ? "" : row["room_type"].c_str();
|
||||
// Platzhalter für Felder, die im Konstruktor benötigt werden
|
||||
room["allowed"] = Json::arrayValue; // ggf. später befüllen
|
||||
room["roundlength"] = 60; // Default-Wert
|
||||
auto newRoom = std::make_shared<ChatRoom>(self, room);
|
||||
_rooms.push_back(newRoom);
|
||||
bool created = false;
|
||||
try {
|
||||
std::string query = R"(
|
||||
SELECT r.id, r.title, r.password_hash, r.room_type_id, r.is_public, r.owner_id, r.min_age, r.max_age, r.created_at, r.updated_at, rt.tr as room_type
|
||||
FROM chat.room r
|
||||
LEFT JOIN chat.room_type rt ON r.room_type_id = rt.id
|
||||
)";
|
||||
auto result = _database->exec(query);
|
||||
for (const auto& row : result) {
|
||||
Json::Value room;
|
||||
room["id"] = row["id"].as<int>();
|
||||
room["name"] = row["title"].c_str();
|
||||
room["password"] = row["password_hash"].is_null() ? "" : row["password_hash"].c_str();
|
||||
room["type"] = row["room_type_id"].is_null() ? 0 : row["room_type_id"].as<int>();
|
||||
room["is_public"] = row["is_public"].as<bool>();
|
||||
room["owner_id"] = row["owner_id"].is_null() ? 0 : row["owner_id"].as<int>();
|
||||
room["min_age"] = row["min_age"].is_null() ? 0 : row["min_age"].as<int>();
|
||||
room["max_age"] = row["max_age"].is_null() ? 0 : row["max_age"].as<int>();
|
||||
room["created_at"] = row["created_at"].c_str();
|
||||
room["updated_at"] = row["updated_at"].c_str();
|
||||
room["room_type"] = row["room_type"].is_null() ? "" : row["room_type"].c_str();
|
||||
// Platzhalter für Felder, die im Konstruktor benötigt werden
|
||||
room["allowed"] = Json::arrayValue; // ggf. später befüllen
|
||||
room["roundlength"] = 60; // Default-Wert
|
||||
auto newRoom = std::make_shared<ChatRoom>(self, room);
|
||||
_rooms.push_back(newRoom);
|
||||
created = true;
|
||||
}
|
||||
} catch (...) {
|
||||
// ignore DB errors, fallback below
|
||||
}
|
||||
if (!created) {
|
||||
// fallback to provided JSON room list (if any)
|
||||
if (roomList.isArray() && roomList.size() > 0) {
|
||||
for (const auto& room : roomList) {
|
||||
auto newRoom = std::make_shared<ChatRoom>(self, room);
|
||||
_rooms.push_back(newRoom);
|
||||
created = true;
|
||||
}
|
||||
} else {
|
||||
// final fallback: builtin default room
|
||||
Json::Value room;
|
||||
room["name"] = "Halle";
|
||||
room["password"] = "";
|
||||
room["allowed"] = Json::arrayValue;
|
||||
room["type"] = 0;
|
||||
room["roundlength"] = 0;
|
||||
auto newRoom = std::make_shared<ChatRoom>(self, room);
|
||||
_rooms.push_back(newRoom);
|
||||
created = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Server::handleRequest() {
|
||||
void Server::handleRequest() {
|
||||
struct sockaddr_in sockAddr;
|
||||
socklen_t sockAddrLen = sizeof(sockAddr);
|
||||
int userSock = accept(_socket, (struct sockaddr *)&sockAddr, &sockAddrLen);
|
||||
if (userSock < 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Neuen Socket zur Überwachung hinzufügen
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(socketMutex);
|
||||
activeSockets.insert(userSock);
|
||||
}
|
||||
// Log jede akzeptierte Verbindung
|
||||
char clientIP[INET_ADDRSTRLEN];
|
||||
inet_ntop(AF_INET, &(sockAddr.sin_addr), clientIP, INET_ADDRSTRLEN);
|
||||
std::cout << "[YourChat] Verbindung akzeptiert von " << clientIP << ":" << ntohs(sockAddr.sin_port) << " (fd=" << userSock << ")" << std::endl;
|
||||
int flags = 1;
|
||||
setsockopt(userSock, IPPROTO_TCP, TCP_NODELAY, (void *)&flags, sizeof(flags));
|
||||
int ka2 = 1; setsockopt(userSock, SOL_SOCKET, SO_KEEPALIVE, &ka2, sizeof(ka2));
|
||||
// Begrenze Blockierzeit beim Senden, um langsame Clients nicht alle zu verzögern
|
||||
timeval sendTimeout; sendTimeout.tv_sec = 0; sendTimeout.tv_usec = 500000; // 500ms
|
||||
setsockopt(userSock, SOL_SOCKET, SO_SNDTIMEO, &sendTimeout, sizeof(sendTimeout));
|
||||
std::string msg = readSocket(userSock);
|
||||
#ifdef YC_DEBUG
|
||||
std::cout << "[Debug] Neue Anfrage erhalten: " << msg << std::endl;
|
||||
#endif
|
||||
if (msg == "") {
|
||||
return;
|
||||
}
|
||||
inputSwitcher(userSock, msg);
|
||||
// OPTIONS Request (CORS Preflight)
|
||||
if (msg.rfind("OPTIONS ", 0) == 0) {
|
||||
std::ostringstream resp;
|
||||
resp << "HTTP/1.1 200 OK\r\n"
|
||||
<< "Access-Control-Allow-Origin: *\r\n"
|
||||
<< "Access-Control-Allow-Methods: GET, POST, OPTIONS\r\n"
|
||||
<< "Access-Control-Allow-Headers: Upgrade, Connection, Sec-WebSocket-Key, Sec-WebSocket-Version, Sec-WebSocket-Protocol, Origin\r\n"
|
||||
<< "Access-Control-Allow-Credentials: true\r\n"
|
||||
<< "Access-Control-Max-Age: 86400\r\n"
|
||||
<< "Content-Length: 0\r\n"
|
||||
<< "\r\n";
|
||||
::send(userSock, resp.str().c_str(), resp.str().size(), 0);
|
||||
close(userSock);
|
||||
return;
|
||||
}
|
||||
|
||||
// WebSocket Upgrade?
|
||||
if (msg.rfind("GET ", 0) == 0 && msg.find("Upgrade: websocket") != std::string::npos) {
|
||||
// sehr einfacher Header-Parser
|
||||
std::string key;
|
||||
std::string subprotocol;
|
||||
std::string origin;
|
||||
std::string version;
|
||||
std::string extensions;
|
||||
std::istringstream iss(msg);
|
||||
std::string line;
|
||||
|
||||
#ifdef YC_DEBUG
|
||||
std::cout << "[Debug] === WebSocket Headers ===" << std::endl;
|
||||
#endif
|
||||
|
||||
while (std::getline(iss, line)) {
|
||||
if (!line.empty() && (line.back() == '\r' || line.back() == '\n')) line.pop_back();
|
||||
auto pos = line.find(":");
|
||||
if (pos != std::string::npos) {
|
||||
std::string h = line.substr(0, pos);
|
||||
std::string v = line.substr(pos+1);
|
||||
// trim
|
||||
auto ltrim = [](std::string &s){ s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch){return !std::isspace(ch);}));};
|
||||
auto rtrim = [](std::string &s){ s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch){return !std::isspace(ch);}).base(), s.end());};
|
||||
ltrim(h); rtrim(h); ltrim(v); rtrim(v);
|
||||
|
||||
#ifdef YC_DEBUG
|
||||
std::cout << "[Debug] Header: '" << h << "' = '" << v << "'" << std::endl;
|
||||
#endif
|
||||
|
||||
if (strcasecmp(h.c_str(), "Sec-WebSocket-Key") == 0) {
|
||||
key = v;
|
||||
} else if (strcasecmp(h.c_str(), "Sec-WebSocket-Protocol") == 0) {
|
||||
subprotocol = v;
|
||||
} else if (strcasecmp(h.c_str(), "Origin") == 0) {
|
||||
origin = v;
|
||||
} else if (strcasecmp(h.c_str(), "Sec-WebSocket-Version") == 0) {
|
||||
version = v;
|
||||
} else if (strcasecmp(h.c_str(), "Sec-WebSocket-Extensions") == 0) {
|
||||
extensions = v;
|
||||
}
|
||||
}
|
||||
if (line.empty()) break;
|
||||
}
|
||||
|
||||
#ifdef YC_DEBUG
|
||||
std::cout << "[Debug] === Parsed Values ===" << std::endl;
|
||||
std::cout << "[Debug] Key: " << (key.empty() ? "MISSING" : key) << std::endl;
|
||||
std::cout << "[Debug] Protocol: " << (subprotocol.empty() ? "NONE" : subprotocol) << std::endl;
|
||||
std::cout << "[Debug] Origin: " << (origin.empty() ? "MISSING" : origin) << std::endl;
|
||||
std::cout << "[Debug] Version: " << (version.empty() ? "MISSING" : version) << std::endl;
|
||||
std::cout << "[Debug] Extensions: " << (extensions.empty() ? "NONE" : extensions) << std::endl;
|
||||
std::cout << "[Debug] ======================" << std::endl;
|
||||
#endif
|
||||
if (!key.empty()) {
|
||||
std::string accept = Base::webSocketAcceptKey(key);
|
||||
std::ostringstream resp;
|
||||
resp << "HTTP/1.1 101 Switching Protocols\r\n"
|
||||
<< "Upgrade: websocket\r\n"
|
||||
<< "Connection: Upgrade\r\n"
|
||||
<< "Sec-WebSocket-Accept: " << accept << "\r\n";
|
||||
|
||||
// CORS-Header hinzufügen
|
||||
resp << "Access-Control-Allow-Origin: *\r\n"
|
||||
<< "Access-Control-Allow-Methods: GET, POST, OPTIONS\r\n"
|
||||
<< "Access-Control-Allow-Headers: Upgrade, Connection, Sec-WebSocket-Key, Sec-WebSocket-Version, Sec-WebSocket-Protocol, Origin\r\n"
|
||||
<< "Access-Control-Allow-Credentials: true\r\n";
|
||||
|
||||
// Subprotokoll-Unterstützung
|
||||
if (!subprotocol.empty()) {
|
||||
resp << "Sec-WebSocket-Protocol: " << subprotocol << "\r\n";
|
||||
} else {
|
||||
// Fallback: Standard-Subprotokoll anbieten
|
||||
resp << "Sec-WebSocket-Protocol: chat\r\n";
|
||||
}
|
||||
|
||||
resp << "\r\n";
|
||||
|
||||
#ifdef YC_DEBUG
|
||||
std::cout << "[Debug] === WebSocket Response ===" << std::endl;
|
||||
std::cout << "[Debug] " << resp.str() << std::endl;
|
||||
std::cout << "[Debug] =========================" << std::endl;
|
||||
#endif
|
||||
|
||||
::send(userSock, resp.str().c_str(), resp.str().size(), 0);
|
||||
Base::markWebSocket(userSock);
|
||||
|
||||
// Sofort eine Willkommen-Nachricht senden, um Firefox's Timing-Problem zu lösen
|
||||
// Verwende korrektes WebSocket-Framing
|
||||
std::string welcomeMsg = "{\"type\":\"welcome\",\"message\":\"WebSocket-Verbindung hergestellt\"}";
|
||||
Base::sendWebSocketMessage(userSock, welcomeMsg);
|
||||
|
||||
#ifdef YC_DEBUG
|
||||
std::cout << "[Debug] WS upgrade ok, welcome message sent, waiting for init... fd=" << userSock << std::endl;
|
||||
#endif
|
||||
|
||||
// Jetzt WebSocket Nachrichten lesen und an inputSwitcher weitergeben
|
||||
bool ownedByUser = false;
|
||||
while (true) {
|
||||
std::string wmsg = readSocket(userSock);
|
||||
if (wmsg.empty()) break;
|
||||
ownedByUser = inputSwitcher(userSock, wmsg);
|
||||
if (ownedByUser) break;
|
||||
}
|
||||
if (ownedByUser) {
|
||||
#ifdef YC_DEBUG
|
||||
std::cout << "[Debug] Ownership transferred to ChatUser, stop reading in Server for fd=" << userSock << std::endl;
|
||||
#endif
|
||||
}
|
||||
if (!ownedByUser) {
|
||||
#ifdef YC_DEBUG
|
||||
std::cout << "[Debug] WS closing (no ownership) fd=" << userSock << std::endl;
|
||||
#endif
|
||||
Base::unmarkWebSocket(userSock);
|
||||
close(userSock);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
// Fallback: Plain JSON
|
||||
bool owned = inputSwitcher(userSock, msg);
|
||||
if (!owned) {
|
||||
#ifdef YC_DEBUG
|
||||
std::cout << "[Debug] Plain JSON path without ownership, closing fd=" << userSock << std::endl;
|
||||
#endif
|
||||
close(userSock);
|
||||
}
|
||||
}
|
||||
|
||||
void Server::inputSwitcher(int userSocket, std::string input) {
|
||||
bool Server::inputSwitcher(int userSocket, std::string input) {
|
||||
Json::Value inputTree = getJsonTree(input);
|
||||
#ifdef YC_DEBUG
|
||||
std::cout << "[Debug] inputSwitcher: type=" << inputTree["type"].asString() << std::endl;
|
||||
#endif
|
||||
if (inputTree["type"] == "init") {
|
||||
initUser(userSocket, inputTree);
|
||||
return true; // ChatUser übernimmt nun den Socket
|
||||
} else {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Server::userExists(std::string userName) {
|
||||
@@ -175,7 +439,23 @@ namespace Yc {
|
||||
}
|
||||
|
||||
void Server::initUser(int userSocket, Json::Value data) {
|
||||
if (userExists(data["name"].asString())) {
|
||||
std::string name = data.isMember("name") ? data["name"].asString() : "";
|
||||
std::string room = data.isMember("room") ? data["room"].asString() : "";
|
||||
std::string color = data.isMember("color") ? data["color"].asString() : "#000000";
|
||||
std::string password = data.isMember("password") ? data["password"].asString() : "";
|
||||
#ifdef YC_DEBUG
|
||||
std::cout << "[Debug] initUser: name=" << name << ", room=" << room << ", color=" << color << std::endl;
|
||||
#endif
|
||||
if (name.empty() || room.empty()) {
|
||||
Json::Value errorJson;
|
||||
errorJson["type"] = ChatUser::error;
|
||||
errorJson["message"] = "missing_fields";
|
||||
errorJson["detail"] = "'name' und 'room' müssen gesetzt sein.";
|
||||
send(userSocket, errorJson);
|
||||
close(userSocket);
|
||||
return;
|
||||
}
|
||||
if (userExists(name)) {
|
||||
Json::Value errorJson;
|
||||
errorJson["type"] = ChatUser::error;
|
||||
errorJson["message"] = "loggedin";
|
||||
@@ -183,17 +463,20 @@ namespace Yc {
|
||||
close(userSocket);
|
||||
return;
|
||||
}
|
||||
std::string roomName = data["room"].asString();
|
||||
bool added(false);
|
||||
for (auto &room: _rooms) {
|
||||
if (room->name() == roomName) {
|
||||
if (room->addUser(data["name"].asString(), data["color"].asString(), data["password"].asString(), userSocket)) {
|
||||
for (auto &roomObj: _rooms) {
|
||||
if (roomObj->name() == room) {
|
||||
if (roomObj->addUser(name, color, password, userSocket)) {
|
||||
added = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!added) {
|
||||
Json::Value errorJson;
|
||||
errorJson["type"] = ChatUser::error;
|
||||
errorJson["message"] = "room_not_found_or_join_failed";
|
||||
send(userSocket, errorJson);
|
||||
close(userSocket);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user