Füge Debug-Ausgaben in ChatUser, Server, SSLServer und Base hinzu

- 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.
This commit is contained in:
Torsten Schulz (local)
2025-09-05 13:10:30 +02:00
parent 8e6640a937
commit 32774617cd
4 changed files with 81 additions and 0 deletions

View File

@@ -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

View File

@@ -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<std::mutex> lock(socketMutex);

View File

@@ -29,12 +29,22 @@ SSLServer::SSLServer(std::shared_ptr<Config> config, std::shared_ptr<Database> 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<char*>(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;

View File

@@ -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