Skip to content

Commit

Permalink
server: add --exit-no-conn option (#1029)
Browse files Browse the repository at this point in the history
Co-authored-by: Shuanglei Tao <tsl0922@gmail.com>
  • Loading branch information
baitian752 and tsl0922 authored Mar 5, 2024
1 parent c5f742f commit 116ce8f
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ OPTIONS:
-O, --check-origin Do not allow websocket connection from different origin
-m, --max-clients Maximum clients to support (default: 0, no limit)
-o, --once Accept only one client and exit on disconnection
-q, --exit-no-conn Exit on all clients disconnection
-B, --browser Open terminal with the default system browser
-I, --index Custom index.html path
-b, --base-path Expected base path for requests coming from a reverse proxy (eg: /mounted/here, max length: 128)
Expand Down
4 changes: 4 additions & 0 deletions man/ttyd.1
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ Cross platform: macOS, Linux, FreeBSD/OpenBSD, OpenWrt/LEDE, Windows
-o, --once
Accept only one client and exit on disconnection

.PP
-q, --exit-no-conn
Exit on all clients disconnection

.PP
-B, --browser
Open terminal with the default system browser
Expand Down
3 changes: 3 additions & 0 deletions man/ttyd.man.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ ttyd 1 "September 2016" ttyd "User Manual"
-o, --once
Accept only one client and exit on disconnection

-q, --exit-no-conn
Exit on all clients disconnection

-B, --browser
Open terminal with the default system browser

Expand Down
4 changes: 2 additions & 2 deletions src/protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,8 @@ int callback_tty(struct lws *wsi, enum lws_callback_reasons reason, void *user,
}
}

if (server->once && server->client_count == 0) {
lwsl_notice("exiting due to the --once option.\n");
if ((server->once || server->exit_no_conn) && server->client_count == 0) {
lwsl_notice("exiting due to the --once/--exit-no-conn option.\n");
force_exit = true;
lws_cancel_service(context);
exit(0);
Expand Down
8 changes: 7 additions & 1 deletion src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,13 @@ static const struct option options[] = {{"port", required_argument, NULL, 'p'},
{"check-origin", no_argument, NULL, 'O'},
{"max-clients", required_argument, NULL, 'm'},
{"once", no_argument, NULL, 'o'},
{"exit-no-conn", no_argument, NULL, 'q'},
{"browser", no_argument, NULL, 'B'},
{"debug", required_argument, NULL, 'd'},
{"version", no_argument, NULL, 'v'},
{"help", no_argument, NULL, 'h'},
{NULL, 0, 0, 0}};
static const char *opt_string = "p:i:U:c:H:u:g:s:w:I:b:P:6aSC:K:A:Wt:T:Om:oBd:vh";
static const char *opt_string = "p:i:U:c:H:u:g:s:w:I:b:P:6aSC:K:A:Wt:T:Om:oqBd:vh";

static void print_help() {
// clang-format off
Expand All @@ -108,6 +109,7 @@ static void print_help() {
" -O, --check-origin Do not allow websocket connection from different origin\n"
" -m, --max-clients Maximum clients to support (default: 0, no limit)\n"
" -o, --once Accept only one client and exit on disconnection\n"
" -q, --exit-no-conn Exit on all clients disconnection\n"
" -B, --browser Open terminal with the default system browser\n"
" -I, --index Custom index.html path\n"
" -b, --base-path Expected base path for requests coming from a reverse proxy (eg: /mounted/here, max length: 128)\n"
Expand Down Expand Up @@ -150,6 +152,7 @@ static void print_config() {
if (server->url_arg) lwsl_notice(" allow url arg: true\n");
if (server->max_clients > 0) lwsl_notice(" max clients: %d\n", server->max_clients);
if (server->once) lwsl_notice(" once: true\n");
if (server->exit_no_conn) lwsl_notice(" exit_no_conn: true\n");
if (server->index != NULL) lwsl_notice(" custom index.html: %s\n", server->index);
if (server->cwd != NULL) lwsl_notice(" working directory: %s\n", server->cwd);
if (!server->writable) lwsl_notice("The --writable option is not set, will start in readonly mode");
Expand Down Expand Up @@ -367,6 +370,9 @@ int main(int argc, char **argv) {
case 'o':
server->once = true;
break;
case 'q':
server->exit_no_conn = true;
break;
case 'B':
browser = true;
break;
Expand Down
3 changes: 2 additions & 1 deletion src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ struct pss_tty {
typedef struct {
struct pss_tty *pss;
bool ws_closed;
} pty_ctx_t ;
} pty_ctx_t;

struct server {
int client_count; // client count
Expand All @@ -78,6 +78,7 @@ struct server {
bool check_origin; // whether allow websocket connection from different origin
int max_clients; // maximum clients to support
bool once; // whether accept only one client and exit on disconnection
bool exit_no_conn; // whether exit on all clients disconnection
char socket_path[255]; // UNIX domain socket path
char terminal_type[30]; // terminal type to report

Expand Down

0 comments on commit 116ce8f

Please sign in to comment.