Skip to content

Commit

Permalink
Merge pull request #2 from jfmartinez/feat/configurable-idle-timeout
Browse files Browse the repository at this point in the history
feat(idleTimeout): Timeout option configurable
  • Loading branch information
jfmartinez authored Apr 17, 2020
2 parents d6567c6 + 1226d05 commit 0b55469
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
@@ -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')
Expand Down
25 changes: 16 additions & 9 deletions src/G729CodecService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ struct PerSocketData {
std::string * sessionId;
};

struct WebSocketServerBehaviorOptions {
int idleTimeout;
};

std::string capabilities;


Expand Down Expand Up @@ -90,12 +94,12 @@ std::string loadDefaultCapabilities(bool annexb) {
}

template <typename App>
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) {
Expand Down Expand Up @@ -230,19 +234,19 @@ void addWebSocketBehavior(App *server, std::string pattern) {
template <typename AppType> class G729CodecService {
private:
AppType *wsApp;
void init() { addWebSocketBehavior<AppType>(wsApp, "/*"); }
void init(WebSocketServerBehaviorOptions behaviorOptions) { addWebSocketBehavior<AppType>(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) {
Expand All @@ -268,11 +272,14 @@ int main() {

NetworkOptions networkOptions = generateNetworkOptions();
TLSOptions * tlsOptions = getTLSOptions();
WebSocketServerBehaviorOptions behaviorOptions = {
networkOptions.idleTimeout,
};
if (tlsOptions->enabled) {
auto * service = new G729CodecService<uWS::SSLApp>(tlsOptions);
auto * service = new G729CodecService<uWS::SSLApp>(behaviorOptions, tlsOptions);
service->listen(networkOptions.addr, networkOptions.port).run();
} else {
auto * service = new G729CodecService<uWS::App>();
auto * service = new G729CodecService<uWS::App>(behaviorOptions);
service->listen(networkOptions.addr, networkOptions.port).run();
}
}
8 changes: 7 additions & 1 deletion src/NetworkOptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

0 comments on commit 0b55469

Please sign in to comment.