Refactor message sending in WebSocket server to utilize sendMessageToConnection
- Replace manual message queuing and error handling with the sendMessageToConnection method, which consolidates necessary checks and improves code clarity. - Remove redundant null checks and logging related to message queue access, streamlining the callback logic. - Enhance overall stability by leveraging existing functionality for message delivery during WebSocket events.
This commit is contained in:
committed by
Torsten (PC)
parent
7f65f5e40e
commit
45d549aa4e
@@ -366,89 +366,8 @@ int WebSocketServer::wsCallback(struct lws *wsi,
|
||||
}
|
||||
|
||||
// Lege Nachricht in die Queue, ohne sofort lws_callback_on_writable aufzurufen
|
||||
try {
|
||||
std::cout << "Versuche Mutex zu locken..." << std::endl;
|
||||
|
||||
// Prüfe ob ud noch gültig ist, bevor wir den Mutex locken
|
||||
if (!ud) {
|
||||
std::cerr << "[RECEIVE] ud ist nullptr vor Mutex-Lock" << std::endl;
|
||||
break;
|
||||
}
|
||||
|
||||
// Kopiere die Nachricht in eine lokale Variable, bevor wir den Mutex locken
|
||||
std::string messageToQueue = errorResponse.dump();
|
||||
std::cout << "Nachricht kopiert: " << messageToQueue.length() << " Bytes" << std::endl;
|
||||
|
||||
// Prüfe ob ud noch gültig ist, bevor wir den Mutex locken
|
||||
if (!ud) {
|
||||
std::cerr << "[RECEIVE] ud ist nullptr nach Kopieren der Nachricht" << std::endl;
|
||||
break;
|
||||
}
|
||||
|
||||
// Speichere ud-Pointer vor dem Locken, um sicherzustellen, dass er gültig bleibt
|
||||
WebSocketUserData* udCopy = ud;
|
||||
if (!udCopy) {
|
||||
std::cerr << "[RECEIVE] udCopy ist nullptr" << std::endl;
|
||||
break;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock(udCopy->messageQueueMutex);
|
||||
std::cout << "Mutex gelockt, füge Nachricht zur Queue hinzu..." << std::endl;
|
||||
|
||||
// Prüfe ob udCopy noch gültig ist, nachdem wir den Mutex gelockt haben
|
||||
if (!udCopy) {
|
||||
std::cerr << "[RECEIVE] udCopy ist nullptr nach Mutex-Lock" << std::endl;
|
||||
break;
|
||||
}
|
||||
|
||||
// Test-Zugriff auf messageQueue, um zu prüfen ob sie gültig ist
|
||||
try {
|
||||
volatile size_t testSize = udCopy->messageQueue.size();
|
||||
(void)testSize;
|
||||
std::cout << "messageQueue ist gültig, aktuelle Größe: " << udCopy->messageQueue.size() << std::endl;
|
||||
} catch (...) {
|
||||
std::cerr << "[RECEIVE] messageQueue ist ungültig (Exception beim Zugriff)" << std::endl;
|
||||
break;
|
||||
}
|
||||
|
||||
// Versuche, die Nachricht zur Queue hinzuzufügen
|
||||
try {
|
||||
std::cout << "Versuche push() aufzurufen mit Nachricht: " << messageToQueue.length() << " Bytes" << std::endl;
|
||||
// Erstelle eine Kopie der Nachricht, um sicherzustellen, dass sie gültig ist
|
||||
std::string msgCopy = messageToQueue;
|
||||
std::cout << "Nachricht kopiert für push: " << msgCopy.length() << " Bytes" << std::endl;
|
||||
|
||||
// Prüfe ob udCopy noch gültig ist
|
||||
if (!udCopy) {
|
||||
std::cerr << "[RECEIVE] udCopy ist nullptr vor push()" << std::endl;
|
||||
break;
|
||||
}
|
||||
|
||||
// Prüfe ob messageQueue noch gültig ist
|
||||
try {
|
||||
volatile size_t testSize2 = udCopy->messageQueue.size();
|
||||
(void)testSize2;
|
||||
} catch (...) {
|
||||
std::cerr << "[RECEIVE] messageQueue ist ungültig vor push()" << std::endl;
|
||||
break;
|
||||
}
|
||||
|
||||
udCopy->messageQueue.push(msgCopy);
|
||||
std::cout << "push() erfolgreich, neue Größe: " << udCopy->messageQueue.size() << std::endl;
|
||||
} catch (const std::exception &e) {
|
||||
std::cerr << "[RECEIVE] Exception beim push(): " << e.what() << std::endl;
|
||||
break;
|
||||
} catch (...) {
|
||||
std::cerr << "[RECEIVE] Unbekannte Exception beim push()" << std::endl;
|
||||
break;
|
||||
}
|
||||
} catch (const std::exception &e) {
|
||||
std::cerr << "[RECEIVE] Fehler beim Zugriff auf messageQueue: " << e.what() << std::endl;
|
||||
break;
|
||||
} catch (...) {
|
||||
std::cerr << "[RECEIVE] Unbekannter Fehler beim Zugriff auf messageQueue" << std::endl;
|
||||
break;
|
||||
}
|
||||
// Verwende sendMessageToConnection, das bereits alle notwendigen Prüfungen hat
|
||||
instance->sendMessageToConnection(wsi, ud, errorResponse.dump());
|
||||
|
||||
// Verwende lws_cancel_service, um den Service zu benachrichtigen
|
||||
if (instance->context) {
|
||||
@@ -502,16 +421,8 @@ int WebSocketServer::wsCallback(struct lws *wsi,
|
||||
{"data", connections}
|
||||
};
|
||||
if (instance && wsi && ud) {
|
||||
// Sende die Nachricht, aber nicht während des Callbacks
|
||||
// Die Nachricht wird in die Queue gelegt und beim nächsten WRITEABLE gesendet
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(ud->messageQueueMutex);
|
||||
ud->messageQueue.push(response.dump());
|
||||
}
|
||||
// Verwende lws_cancel_service, um den Service zu benachrichtigen
|
||||
if (instance->context) {
|
||||
lws_cancel_service(instance->context);
|
||||
}
|
||||
// Verwende sendMessageToConnection, das bereits alle notwendigen Prüfungen hat
|
||||
instance->sendMessageToConnection(wsi, ud, response.dump());
|
||||
std::cout << "[RECEIVE] getConnections: Verbindungen an Mainadmin gesendet (" << response.dump().length() << " Bytes)" << std::endl;
|
||||
}
|
||||
} catch (const std::exception &e) {
|
||||
|
||||
Reference in New Issue
Block a user