From 0775b35ef8e66f20f1d6e25b96e32415d2bbab6a Mon Sep 17 00:00:00 2001 From: Devaev Maxim Date: Mon, 10 Aug 2020 08:12:14 +0300 Subject: [PATCH] option --tcp-nodelay --- src/http/server.c | 11 +++++++++++ src/http/server.h | 1 + src/options.c | 5 +++++ 3 files changed, 17 insertions(+) diff --git a/src/http/server.c b/src/http/server.c index 40fe9d7..a0c3358 100644 --- a/src/http/server.c +++ b/src/http/server.c @@ -31,6 +31,8 @@ #include #include +#include +#include #include #include @@ -539,6 +541,15 @@ static void _http_callback_stream(struct evhttp_request *request, void *v_server client_addr, client_port, client->id, server->run->stream_clients_count); buf_event = evhttp_connection_get_bufferevent(conn); + if (server->tcp_nodelay && !server->run->unix_fd) { + evutil_socket_t fd; + int on = 1; + + assert((fd = bufferevent_getfd(buf_event)) >= 0); + if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (void *)&on, sizeof(on)) != 0) { + LOG_PERROR("HTTP: Can't set TCP_NODELAY to the client socket [%s]:%u", client_addr, client_port); + } + } bufferevent_setcb(buf_event, NULL, NULL, _http_callback_stream_error, (void *)client); bufferevent_enable(buf_event, EV_READ); } else { diff --git a/src/http/server.h b/src/http/server.h index d26ecfc..62027f7 100644 --- a/src/http/server.h +++ b/src/http/server.h @@ -91,6 +91,7 @@ struct http_server_t { char *unix_path; bool unix_rm; mode_t unix_mode; + bool tcp_nodelay; unsigned timeout; char *user; diff --git a/src/options.c b/src/options.c index c444521..11f8141 100644 --- a/src/options.c +++ b/src/options.c @@ -96,6 +96,7 @@ enum _OPT_VALUES { _O_USER, _O_PASSWD, _O_STATIC, + _O_TCP_NODELAY, _O_SERVER_TIMEOUT, #ifdef WITH_GPIO @@ -168,6 +169,7 @@ static const struct option _LONG_OPTS[] = { {"drop-same-frames", required_argument, NULL, _O_DROP_SAME_FRAMES}, {"slowdown", no_argument, NULL, _O_SLOWDOWN}, {"fake-resolution", required_argument, NULL, _O_FAKE_RESOLUTION}, + {"tcp-nodelay", no_argument, NULL, _O_TCP_NODELAY}, {"server-timeout", required_argument, NULL, _O_SERVER_TIMEOUT}, #ifdef WITH_GPIO @@ -382,6 +384,7 @@ int options_parse(struct options_t *options, struct device_t *dev, struct encode case _O_DROP_SAME_FRAMES: OPT_NUMBER("--drop-same-frames", server->drop_same_frames, 0, VIDEO_MAX_FPS, 0); case _O_SLOWDOWN: OPT_SET(server->slowdown, true); case _O_FAKE_RESOLUTION: OPT_RESOLUTION("--fake-resolution", server->fake_width, server->fake_height, false); + case _O_TCP_NODELAY: OPT_SET(server->tcp_nodelay, true); case _O_SERVER_TIMEOUT: OPT_NUMBER("--server-timeout", server->timeout, 1, 60, 0); # ifdef WITH_GPIO @@ -622,6 +625,8 @@ static void _help(struct device_t *dev, struct encoder_t *encoder, struct http_s printf(" -l|--slowdown ────────────── Slowdown capturing to 1 FPS or less when no stream clients are connected.\n"); printf(" Useful to reduce CPU consumption. Default: disabled.\n\n"); printf(" -R|--fake-resolution ─ Override image resolution for the /state. Default: disabled.\n\n"); + printf(" --tcp-nodelay ────────────── Set TCP_NODELAY flag to the client /stream socket. Ignored for --unix.\n"); + printf(" Default: disabled.\n\n"); printf(" --server-timeout ───── Timeout for client connections. Default: %u.\n\n", server->timeout); #ifdef WITH_GPIO printf("GPIO options:\n");