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:
@@ -122,6 +122,9 @@ namespace Yc
|
|||||||
_socket(socket),
|
_socket(socket),
|
||||||
_stop(false)
|
_stop(false)
|
||||||
{
|
{
|
||||||
|
#ifdef YC_DEBUG
|
||||||
|
std::cout << "[Debug] ChatUser constructor: name=" << _name << ", socket=" << _socket << std::endl;
|
||||||
|
#endif
|
||||||
// Verwende die direkt übergebene Datenbank
|
// Verwende die direkt übergebene Datenbank
|
||||||
if (!database) {
|
if (!database) {
|
||||||
// Fallback wenn keine Datenbank verfügbar
|
// Fallback wenn keine Datenbank verfügbar
|
||||||
|
|||||||
@@ -24,11 +24,19 @@ namespace Yc {
|
|||||||
_database(std::move(database)),
|
_database(std::move(database)),
|
||||||
_stop(false),
|
_stop(false),
|
||||||
_socket(-1) {
|
_socket(-1) {
|
||||||
|
#ifdef YC_DEBUG
|
||||||
|
std::cout << "[Debug] Server constructor called" << std::endl;
|
||||||
|
#endif
|
||||||
|
|
||||||
int port = 1235;
|
int port = 1235;
|
||||||
try {
|
try {
|
||||||
Json::Value p = _config->value("server", "port");
|
Json::Value p = _config->value("server", "port");
|
||||||
if (p.isInt()) port = p.asInt();
|
if (p.isInt()) port = p.asInt();
|
||||||
} catch (...) {}
|
} catch (...) {}
|
||||||
|
|
||||||
|
#ifdef YC_DEBUG
|
||||||
|
std::cout << "[Debug] Server port: " << port << std::endl;
|
||||||
|
#endif
|
||||||
int opt = 1;
|
int opt = 1;
|
||||||
// Try IPv6 dual-stack (accepts IPv4 via v4-mapped if v6only=0)
|
// Try IPv6 dual-stack (accepts IPv4 via v4-mapped if v6only=0)
|
||||||
_socket = socket(AF_INET6, SOCK_STREAM, 0);
|
_socket = socket(AF_INET6, SOCK_STREAM, 0);
|
||||||
@@ -111,6 +119,10 @@ namespace Yc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Server::run() {
|
void Server::run() {
|
||||||
|
#ifdef YC_DEBUG
|
||||||
|
std::cout << "[Debug] Server::run() called" << std::endl;
|
||||||
|
#endif
|
||||||
|
|
||||||
if (_socket < 0) {
|
if (_socket < 0) {
|
||||||
std::cout << "Invalid socket, cannot start server" << std::endl;
|
std::cout << "Invalid socket, cannot start server" << std::endl;
|
||||||
return;
|
return;
|
||||||
@@ -120,6 +132,10 @@ namespace Yc {
|
|||||||
std::cout << "listen not possible: " << strerror(errno) << std::endl;
|
std::cout << "listen not possible: " << strerror(errno) << std::endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef YC_DEBUG
|
||||||
|
std::cout << "[Debug] Server listening on socket: " << _socket << std::endl;
|
||||||
|
#endif
|
||||||
timeval origTv;
|
timeval origTv;
|
||||||
origTv.tv_sec = 1; // Shorter timeout for more responsive shutdown
|
origTv.tv_sec = 1; // Shorter timeout for more responsive shutdown
|
||||||
origTv.tv_usec = 0;
|
origTv.tv_usec = 0;
|
||||||
@@ -421,13 +437,24 @@ namespace Yc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Server::handleRequest() {
|
void Server::handleRequest() {
|
||||||
|
#ifdef YC_DEBUG
|
||||||
|
std::cout << "[Debug] handleRequest called" << std::endl;
|
||||||
|
#endif
|
||||||
|
|
||||||
struct sockaddr_in sockAddr;
|
struct sockaddr_in sockAddr;
|
||||||
socklen_t sockAddrLen = sizeof(sockAddr);
|
socklen_t sockAddrLen = sizeof(sockAddr);
|
||||||
int userSock = accept(_socket, (struct sockaddr *)&sockAddr, &sockAddrLen);
|
int userSock = accept(_socket, (struct sockaddr *)&sockAddr, &sockAddrLen);
|
||||||
if (userSock < 0) {
|
if (userSock < 0) {
|
||||||
|
#ifdef YC_DEBUG
|
||||||
|
std::cout << "[Debug] accept failed: " << strerror(errno) << std::endl;
|
||||||
|
#endif
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef YC_DEBUG
|
||||||
|
std::cout << "[Debug] New connection accepted, socket: " << userSock << std::endl;
|
||||||
|
#endif
|
||||||
|
|
||||||
// Neuen Socket zur Überwachung hinzufügen
|
// Neuen Socket zur Überwachung hinzufügen
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(socketMutex);
|
std::lock_guard<std::mutex> lock(socketMutex);
|
||||||
|
|||||||
@@ -29,12 +29,22 @@ SSLServer::SSLServer(std::shared_ptr<Config> config, std::shared_ptr<Database> d
|
|||||||
: _config(std::move(config)), _database(std::move(database)) {
|
: _config(std::move(config)), _database(std::move(database)) {
|
||||||
_instance = this;
|
_instance = this;
|
||||||
|
|
||||||
|
#ifdef YC_DEBUG
|
||||||
|
std::cout << "[Debug] SSLServer constructor called" << std::endl;
|
||||||
|
#endif
|
||||||
|
|
||||||
// Load SSL settings from config
|
// Load SSL settings from config
|
||||||
_useSSL = _config->value("server", "ssl_enabled").asBool();
|
_useSSL = _config->value("server", "ssl_enabled").asBool();
|
||||||
_certPath = _config->value("server", "ssl_cert_path").asString();
|
_certPath = _config->value("server", "ssl_cert_path").asString();
|
||||||
_keyPath = _config->value("server", "ssl_key_path").asString();
|
_keyPath = _config->value("server", "ssl_key_path").asString();
|
||||||
_port = _config->value("server", "port").asInt();
|
_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())) {
|
if (_useSSL && (_certPath.empty() || _keyPath.empty())) {
|
||||||
throw std::runtime_error("SSL enabled but certificate or key path not provided");
|
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) {
|
switch (reason) {
|
||||||
case LWS_CALLBACK_ESTABLISHED:
|
case LWS_CALLBACK_ESTABLISHED:
|
||||||
std::cout << "[YourChat] WebSocket-Verbindung hergestellt" << std::endl;
|
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
|
// Request callback when writable to send initial message
|
||||||
lws_callback_on_writable(wsi);
|
lws_callback_on_writable(wsi);
|
||||||
break;
|
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::string msg(reinterpret_cast<char*>(in), len);
|
||||||
std::cout << "[YourChat] WebSocket-Nachricht empfangen: " << msg << std::endl;
|
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);
|
_instance->handleWebSocketMessage(wsi, msg);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -273,9 +290,17 @@ void SSLServer::handleWebSocketMessage(struct lws *wsi, const std::string& messa
|
|||||||
ud->currentRoom = room;
|
ud->currentRoom = room;
|
||||||
ud->authenticated = true;
|
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
|
// Add to connections
|
||||||
addConnection(token, wsi);
|
addConnection(token, wsi);
|
||||||
|
|
||||||
|
#ifdef YC_DEBUG
|
||||||
|
std::cout << "[Debug] WebSocket connection added to connections map" << std::endl;
|
||||||
|
#endif
|
||||||
|
|
||||||
// Try to add user to room
|
// Try to add user to room
|
||||||
bool added = false;
|
bool added = false;
|
||||||
|
|
||||||
@@ -291,12 +316,23 @@ void SSLServer::handleWebSocketMessage(struct lws *wsi, const std::string& messa
|
|||||||
if (roomObj->name() == room) {
|
if (roomObj->name() == room) {
|
||||||
std::cout << "[YourChat] Found room '" << room << "', attempting to add user..." << std::endl;
|
std::cout << "[YourChat] Found room '" << room << "', attempting to add user..." << std::endl;
|
||||||
// Add user to room (ChatUser will be created by addUser)
|
// 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))) {
|
if (roomObj->addUser(name, color, password, lws_get_socket_fd(wsi))) {
|
||||||
std::cout << "[YourChat] Successfully added user '" << name << "' to room '" << room << "'" << std::endl;
|
std::cout << "[YourChat] Successfully added user '" << name << "' to room '" << room << "'" << std::endl;
|
||||||
// Find the created ChatUser
|
// Find the created ChatUser
|
||||||
auto chatUser = roomObj->findUserByName(name);
|
auto chatUser = roomObj->findUserByName(name);
|
||||||
if (chatUser) {
|
if (chatUser) {
|
||||||
_users[token] = 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;
|
added = true;
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -58,9 +58,16 @@ namespace Yc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Base::send(int socket, std::string out) {
|
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)
|
// Token-Felder aus String-Payloads entfernen (falls JSON)
|
||||||
sanitizeTokensInString(out);
|
sanitizeTokensInString(out);
|
||||||
if (isWebSocket(socket)) {
|
if (isWebSocket(socket)) {
|
||||||
|
#ifdef YC_DEBUG
|
||||||
|
std::cout << "[Debug] Socket " << socket << " is WebSocket, using sendWebSocketMessage" << std::endl;
|
||||||
|
#endif
|
||||||
sendWebSocketMessage(socket, out);
|
sendWebSocketMessage(socket, out);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -97,6 +104,10 @@ namespace Yc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Base::send(int socket, Json::Value out) {
|
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
|
// Entferne alle Token-Felder rekursiv aus Antworten
|
||||||
sanitizeTokens(out);
|
sanitizeTokens(out);
|
||||||
std::string outString = getJsonString(out);
|
std::string outString = getJsonString(out);
|
||||||
@@ -235,6 +246,10 @@ namespace Yc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Base::sendWebSocketMessage(int socket, const std::string& out) {
|
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
|
// Socket-Validierung vor dem Senden
|
||||||
if (socket < 0) {
|
if (socket < 0) {
|
||||||
#ifdef YC_DEBUG
|
#ifdef YC_DEBUG
|
||||||
|
|||||||
Reference in New Issue
Block a user