Enhance WebSocket server socket options and connection handling
- Add support for setting SO_REUSEADDR in the WebSocket server to allow port reuse, improving server flexibility. - Implement callbacks for socket adoption to ensure SO_REUSEADDR is set when applicable. - Refine server options to streamline connection management and enhance overall performance.
This commit is contained in:
committed by
Torsten (PC)
parent
b3c9c8f37c
commit
3ac9f25284
@@ -6,6 +6,9 @@
|
||||
#include <cstring>
|
||||
#include <future>
|
||||
#include <algorithm>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/tcp.h>
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
@@ -101,11 +104,14 @@ void WebSocketServer::startServer() {
|
||||
info.port = port;
|
||||
info.protocols = protocols;
|
||||
|
||||
// Setze Socket-Optionen-Callback für SO_REUSEADDR
|
||||
// Hinweis: In älteren libwebsockets-Versionen muss SO_REUSEADDR manuell gesetzt werden
|
||||
// Wir versuchen es über einen Callback, falls verfügbar
|
||||
|
||||
// Server-Optionen für mehrere gleichzeitige Verbindungen
|
||||
info.options = LWS_SERVER_OPTION_VALIDATE_UTF8 |
|
||||
LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE |
|
||||
LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME |
|
||||
LWS_SERVER_OPTION_REUSE_ADDR; // Erlaube Port-Wiederverwendung (SO_REUSEADDR)
|
||||
LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME;
|
||||
|
||||
// Erlaube mehrere Verbindungen pro IP
|
||||
info.ka_time = 60;
|
||||
@@ -346,6 +352,18 @@ int WebSocketServer::wsCallback(struct lws *wsi,
|
||||
case LWS_CALLBACK_RAW_CONNECTED:
|
||||
// Raw-Verbindungen behandeln
|
||||
return 0;
|
||||
case LWS_CALLBACK_RAW_ADOPT_FILE:
|
||||
case LWS_CALLBACK_RAW_ADOPT:
|
||||
// Setze SO_REUSEADDR für den Socket (falls noch nicht gesetzt)
|
||||
// Hinweis: Diese Callbacks werden möglicherweise nicht für Listen-Sockets aufgerufen
|
||||
{
|
||||
int fd = lws_get_socket_fd(wsi);
|
||||
if (fd >= 0) {
|
||||
int reuse = 1;
|
||||
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user