diff --git a/README.md b/README.md index 2311e7b..1d51d69 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,7 @@ Configuration is done through specification of environment variables. | `WEBSOCKET_SERVER_HOST` | `0.0.0.0` | Specifies the host interface to listen on for WebSocket requests. | | `WEBSOCKET_SERVER_CERT_FILE_NAME` | `N/A` | Path to certificate file that will be used for secure websocket requests. | | `WEBSOCKET_SERVER_KEY_FILE_NAME` | `N/A` | Path to key that will be used for secure websoket server requests. | +| `WEBSOCKET_SERVER_IDLE_TIMEOUT` | `600` | Specifies the amount of time in seconds to wait between WebSocket connection messages before closing down the connection.| | `LOG_LEVEL` | `INFO` | Specifies the log level. Valid values are `TRACE`, `DEBUG`, `INFO`, `WARN` and `OFF`. | | `ANNEXB_ENABLED` | `YES` | Specifies if annexb should be used for the codec. | diff --git a/meson.build b/meson.build index 7a4c85e..c3b160c 100644 --- a/meson.build +++ b/meson.build @@ -1,4 +1,4 @@ -project('g729-codec-service','c', 'cpp', version: '0.0.1', default_options : ['c_std=c17', 'cpp_std=c++17']) +project('g729-codec-service','c', 'cpp', version: '0.0.2', default_options : ['c_std=c17', 'cpp_std=c++17']) usockets = subproject('usockets') rapidjson = subproject('rapidjson') uwebsockets = subproject('uwebsockets') diff --git a/src/G729CodecService.cpp b/src/G729CodecService.cpp index f37272b..3f2466c 100644 --- a/src/G729CodecService.cpp +++ b/src/G729CodecService.cpp @@ -59,6 +59,10 @@ struct PerSocketData { std::string * sessionId; }; +struct WebSocketServerBehaviorOptions { + int idleTimeout; +}; + std::string capabilities; @@ -90,12 +94,12 @@ std::string loadDefaultCapabilities(bool annexb) { } template -void addWebSocketBehavior(App *server, std::string pattern) { +void addWebSocketBehavior(App *server, std::string pattern, WebSocketServerBehaviorOptions behaviorOptions) { typename App::WebSocketBehavior behavior = {}; bool annexbEnabled = isAnnexBEnabled(); capabilities = loadDefaultCapabilities(annexbEnabled); - behavior.idleTimeout = 30; + behavior.idleTimeout = behaviorOptions.idleTimeout; behavior.compression = uWS::SHARED_COMPRESSOR; behavior.maxPayloadLength = 16 * 1024; behavior.open = [](auto *ws, auto *req) { @@ -230,19 +234,19 @@ void addWebSocketBehavior(App *server, std::string pattern) { template class G729CodecService { private: AppType *wsApp; - void init() { addWebSocketBehavior(wsApp, "/*"); } + void init(WebSocketServerBehaviorOptions behaviorOptions) { addWebSocketBehavior(wsApp, "/*", behaviorOptions); } public: - G729CodecService() { + G729CodecService(WebSocketServerBehaviorOptions behaviorOptions) { wsApp = new AppType(); - init(); + init(behaviorOptions); } - G729CodecService(TLSOptions *options) { + G729CodecService(WebSocketServerBehaviorOptions behaviorOptions, TLSOptions *options) { wsApp = new AppType({ .key_file_name = options->keyFileName.c_str(), .cert_file_name = options->certFileName.c_str() }); - init(); + init(behaviorOptions); }; G729CodecService &&listen(std::string host, int port) { @@ -268,11 +272,14 @@ int main() { NetworkOptions networkOptions = generateNetworkOptions(); TLSOptions * tlsOptions = getTLSOptions(); + WebSocketServerBehaviorOptions behaviorOptions = { + networkOptions.idleTimeout, + }; if (tlsOptions->enabled) { - auto * service = new G729CodecService(tlsOptions); + auto * service = new G729CodecService(behaviorOptions, tlsOptions); service->listen(networkOptions.addr, networkOptions.port).run(); } else { - auto * service = new G729CodecService(); + auto * service = new G729CodecService(behaviorOptions); service->listen(networkOptions.addr, networkOptions.port).run(); } } diff --git a/src/NetworkOptions.hpp b/src/NetworkOptions.hpp index 126d354..6bd9a4b 100644 --- a/src/NetworkOptions.hpp +++ b/src/NetworkOptions.hpp @@ -22,14 +22,16 @@ const std::string defaultAddress = "0.0.0.0"; const int defaultPort = 9001; +const int defaultIdleTimeout = 600; // Seconds struct NetworkOptions { std::string addr; int port; + int idleTimeout; }; NetworkOptions generateNetworkOptions() { - NetworkOptions networkOptions = {defaultAddress, defaultPort}; + NetworkOptions networkOptions = {defaultAddress, defaultPort, defaultIdleTimeout}; if (const char *websocketServerHost = std::getenv("WEBSOCKET_SERVER_HOST")) { networkOptions.addr = websocketServerHost; @@ -43,5 +45,9 @@ NetworkOptions generateNetworkOptions() { networkOptions.port = (int)std::strtol(port, NULL, 10); } + if (const char *idleTimeout = std::getenv("WEBSOCKET_SERVER_IDLE_TIMEOUT")) { + networkOptions.idleTimeout = (int) std::strtol(idleTimeout, NULL, 10); + } + return networkOptions; }