Verbessere Socket-Validierung und Fehlerbehandlung in ChatUser und Base
- Füge Überprüfungen hinzu, um sicherzustellen, dass der Socket gültig ist, bevor Nachrichten gesendet werden. - Implementiere detaillierte Fehlerprotokollierung für verschiedene Socket-Fehler, um die Diagnose zu erleichtern. - Ergänze eine kurze Verzögerung im ChatRoom, um den Abschluss des WebSocket-Handshakes zu gewährleisten.
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
#include <unistd.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <fcntl.h>
|
||||
#include "chat_room.h"
|
||||
#include <iostream>
|
||||
#include <netinet/in.h>
|
||||
@@ -336,6 +337,8 @@ namespace Yc
|
||||
void ChatUser::stop()
|
||||
{
|
||||
_stop = true;
|
||||
// Socket als ungültig markieren
|
||||
_socket = -1;
|
||||
}
|
||||
|
||||
std::string ChatUser::color() const
|
||||
@@ -349,12 +352,38 @@ namespace Yc
|
||||
}
|
||||
|
||||
void ChatUser::send(std::string out) {
|
||||
// Prüfe ob Socket noch gültig ist
|
||||
if (_socket < 0 || _stop) {
|
||||
#ifdef YC_DEBUG
|
||||
std::cout << "[Debug] Skipping send - socket invalid or user stopped" << std::endl;
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
// Zusätzliche Socket-Validierung mit fcntl
|
||||
int flags = fcntl(_socket, F_GETFL);
|
||||
if (flags == -1) {
|
||||
#ifdef YC_DEBUG
|
||||
std::cout << "[Debug] Socket " << _socket << " is invalid (fcntl failed), skipping send" << std::endl;
|
||||
#endif
|
||||
_socket = -1; // Markiere als ungültig
|
||||
return;
|
||||
}
|
||||
|
||||
// Entferne ggf. Token-Felder aus JSON-Strings und sende über Socket/WebSocket
|
||||
Base::sanitizeTokensInString(out);
|
||||
Base::send(_socket, out);
|
||||
}
|
||||
|
||||
void ChatUser::send(Json::Value out) {
|
||||
// Prüfe ob Socket noch gültig ist
|
||||
if (_socket < 0 || _stop) {
|
||||
#ifdef YC_DEBUG
|
||||
std::cout << "[Debug] Skipping send - socket invalid or user stopped" << std::endl;
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
// Entferne rekursiv alle Token-Felder und sende über Socket/WebSocket
|
||||
Base::sanitizeTokens(out);
|
||||
Base::send(_socket, out);
|
||||
|
||||
Reference in New Issue
Block a user