From 32774617cdf61f94e1073c47e590f8b125a3b4be Mon Sep 17 00:00:00 2001 From: "Torsten Schulz (local)" Date: Fri, 5 Sep 2025 13:10:30 +0200 Subject: [PATCH] =?UTF-8?q?F=C3=BCge=20Debug-Ausgaben=20in=20ChatUser,=20S?= =?UTF-8?q?erver,=20SSLServer=20und=20Base=20hinzu?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Ergänze Debug-Logs in den Konstruktoren und wichtigen Methoden, um den Ablauf und die Konfiguration während der Server- und Benutzerinitialisierung zu protokollieren. - Verbessere die Nachverfolgbarkeit von WebSocket-Verbindungen und Nachrichtenübertragungen durch zusätzliche Ausgaben in den entsprechenden Methoden. - Diese Änderungen unterstützen die Fehlersuche und verbessern die Transparenz des Systemverhaltens während der Laufzeit. --- src/core/chat_user.cpp | 3 +++ src/core/server.cpp | 27 +++++++++++++++++++++++++++ src/core/ssl_server.cpp | 36 ++++++++++++++++++++++++++++++++++++ src/lib/base.cpp | 15 +++++++++++++++ 4 files changed, 81 insertions(+) diff --git a/src/core/chat_user.cpp b/src/core/chat_user.cpp index 5999f6c..ed3b83e 100644 --- a/src/core/chat_user.cpp +++ b/src/core/chat_user.cpp @@ -122,6 +122,9 @@ namespace Yc _socket(socket), _stop(false) { + #ifdef YC_DEBUG + std::cout << "[Debug] ChatUser constructor: name=" << _name << ", socket=" << _socket << std::endl; + #endif // Verwende die direkt übergebene Datenbank if (!database) { // Fallback wenn keine Datenbank verfügbar diff --git a/src/core/server.cpp b/src/core/server.cpp index 3e4a18d..1062a81 100755 --- a/src/core/server.cpp +++ b/src/core/server.cpp @@ -24,11 +24,19 @@ namespace Yc { _database(std::move(database)), _stop(false), _socket(-1) { + #ifdef YC_DEBUG + std::cout << "[Debug] Server constructor called" << std::endl; + #endif + int port = 1235; try { Json::Value p = _config->value("server", "port"); if (p.isInt()) port = p.asInt(); } catch (...) {} + + #ifdef YC_DEBUG + std::cout << "[Debug] Server port: " << port << std::endl; + #endif int opt = 1; // Try IPv6 dual-stack (accepts IPv4 via v4-mapped if v6only=0) _socket = socket(AF_INET6, SOCK_STREAM, 0); @@ -111,6 +119,10 @@ namespace Yc { } void Server::run() { + #ifdef YC_DEBUG + std::cout << "[Debug] Server::run() called" << std::endl; + #endif + if (_socket < 0) { std::cout << "Invalid socket, cannot start server" << std::endl; return; @@ -120,6 +132,10 @@ namespace Yc { std::cout << "listen not possible: " << strerror(errno) << std::endl; return; } + + #ifdef YC_DEBUG + std::cout << "[Debug] Server listening on socket: " << _socket << std::endl; + #endif timeval origTv; origTv.tv_sec = 1; // Shorter timeout for more responsive shutdown origTv.tv_usec = 0; @@ -421,13 +437,24 @@ namespace Yc { } void Server::handleRequest() { + #ifdef YC_DEBUG + std::cout << "[Debug] handleRequest called" << std::endl; + #endif + struct sockaddr_in sockAddr; socklen_t sockAddrLen = sizeof(sockAddr); int userSock = accept(_socket, (struct sockaddr *)&sockAddr, &sockAddrLen); if (userSock < 0) { + #ifdef YC_DEBUG + std::cout << "[Debug] accept failed: " << strerror(errno) << std::endl; + #endif return; } + #ifdef YC_DEBUG + std::cout << "[Debug] New connection accepted, socket: " << userSock << std::endl; + #endif + // Neuen Socket zur Überwachung hinzufügen { std::lock_guard lock(socketMutex); diff --git a/src/core/ssl_server.cpp b/src/core/ssl_server.cpp index 31971a8..ff0c31b 100644 --- a/src/core/ssl_server.cpp +++ b/src/core/ssl_server.cpp @@ -29,12 +29,22 @@ SSLServer::SSLServer(std::shared_ptr config, std::shared_ptr d : _config(std::move(config)), _database(std::move(database)) { _instance = this; + #ifdef YC_DEBUG + std::cout << "[Debug] SSLServer constructor called" << std::endl; + #endif + // Load SSL settings from config _useSSL = _config->value("server", "ssl_enabled").asBool(); _certPath = _config->value("server", "ssl_cert_path").asString(); _keyPath = _config->value("server", "ssl_key_path").asString(); _port = _config->value("server", "port").asInt(); + #ifdef YC_DEBUG + std::cout << "[Debug] SSLServer config loaded - SSL: " << _useSSL << ", Port: " << _port << std::endl; + std::cout << "[Debug] SSLServer cert path: " << _certPath << std::endl; + std::cout << "[Debug] SSLServer key path: " << _keyPath << std::endl; + #endif + if (_useSSL && (_certPath.empty() || _keyPath.empty())) { throw std::runtime_error("SSL enabled but certificate or key path not provided"); } @@ -162,6 +172,9 @@ int SSLServer::wsCallback(struct lws *wsi, enum lws_callback_reasons reason, voi switch (reason) { case LWS_CALLBACK_ESTABLISHED: std::cout << "[YourChat] WebSocket-Verbindung hergestellt" << std::endl; + #ifdef YC_DEBUG + std::cout << "[Debug] WebSocket connection established, requesting writable callback" << std::endl; + #endif // Request callback when writable to send initial message lws_callback_on_writable(wsi); break; @@ -174,6 +187,10 @@ int SSLServer::wsCallback(struct lws *wsi, enum lws_callback_reasons reason, voi std::string msg(reinterpret_cast(in), len); std::cout << "[YourChat] WebSocket-Nachricht empfangen: " << msg << std::endl; + #ifdef YC_DEBUG + std::cout << "[Debug] WebSocket message received, length: " << len << std::endl; + #endif + _instance->handleWebSocketMessage(wsi, msg); break; } @@ -273,9 +290,17 @@ void SSLServer::handleWebSocketMessage(struct lws *wsi, const std::string& messa ud->currentRoom = room; ud->authenticated = true; + #ifdef YC_DEBUG + std::cout << "[Debug] WebSocket user data stored - token: " << token << ", name: " << name << ", room: " << room << std::endl; + #endif + // Add to connections addConnection(token, wsi); + #ifdef YC_DEBUG + std::cout << "[Debug] WebSocket connection added to connections map" << std::endl; + #endif + // Try to add user to room bool added = false; @@ -291,12 +316,23 @@ void SSLServer::handleWebSocketMessage(struct lws *wsi, const std::string& messa if (roomObj->name() == room) { std::cout << "[YourChat] Found room '" << room << "', attempting to add user..." << std::endl; // Add user to room (ChatUser will be created by addUser) + #ifdef YC_DEBUG + std::cout << "[Debug] Attempting to add user '" << name << "' to room '" << room << "' with socket: " << lws_get_socket_fd(wsi) << std::endl; + #endif + if (roomObj->addUser(name, color, password, lws_get_socket_fd(wsi))) { std::cout << "[YourChat] Successfully added user '" << name << "' to room '" << room << "'" << std::endl; // Find the created ChatUser auto chatUser = roomObj->findUserByName(name); if (chatUser) { _users[token] = chatUser; + #ifdef YC_DEBUG + std::cout << "[Debug] ChatUser found and stored in _users map for token: " << token << std::endl; + #endif + } else { + #ifdef YC_DEBUG + std::cout << "[Debug] WARNING: ChatUser not found after successful addUser!" << std::endl; + #endif } added = true; break; diff --git a/src/lib/base.cpp b/src/lib/base.cpp index 74c9eb0..063316b 100755 --- a/src/lib/base.cpp +++ b/src/lib/base.cpp @@ -58,9 +58,16 @@ namespace Yc { } void Base::send(int socket, std::string out) { + #ifdef YC_DEBUG + std::cout << "[Debug] Base::send called with socket: " << socket << ", length: " << out.length() << std::endl; + #endif + // Token-Felder aus String-Payloads entfernen (falls JSON) sanitizeTokensInString(out); if (isWebSocket(socket)) { + #ifdef YC_DEBUG + std::cout << "[Debug] Socket " << socket << " is WebSocket, using sendWebSocketMessage" << std::endl; + #endif sendWebSocketMessage(socket, out); return; } @@ -97,6 +104,10 @@ namespace Yc { } void Base::send(int socket, Json::Value out) { + #ifdef YC_DEBUG + std::cout << "[Debug] Base::send(Json::Value) called with socket: " << socket << std::endl; + #endif + // Entferne alle Token-Felder rekursiv aus Antworten sanitizeTokens(out); std::string outString = getJsonString(out); @@ -235,6 +246,10 @@ namespace Yc { } void Base::sendWebSocketMessage(int socket, const std::string& out) { + #ifdef YC_DEBUG + std::cout << "[Debug] Base::sendWebSocketMessage called with socket: " << socket << ", length: " << out.length() << std::endl; + #endif + // Socket-Validierung vor dem Senden if (socket < 0) { #ifdef YC_DEBUG