From: Shuanglei Tao Date: Sat, 13 Jan 2018 03:28:52 +0000 (+0800) Subject: protocol: avoid extra copy of pty data X-Git-Url: http://git.prime8.dev/?a=commitdiff_plain;h=9dd8a97e6b022983c965c191f7822d98d8615255;p=ttyd.git protocol: avoid extra copy of pty data --- diff --git a/src/protocol.c b/src/protocol.c index 65764ab..d4db6ae 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -172,7 +172,7 @@ thread_run_command(void *args) { continue; } memset(client->pty_buffer, 0, sizeof(client->pty_buffer)); - client->pty_len = read(pty, client->pty_buffer, sizeof(client->pty_buffer)); + client->pty_len = read(pty, client->pty_buffer + LWS_PRE + 1, BUF_SIZE); client->state = STATE_READY; pthread_mutex_unlock(&client->mutex); break; @@ -190,6 +190,7 @@ callback_tty(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len) { struct tty_client *client = (struct tty_client *) user; char buf[256]; + size_t n = 0; switch (reason) { case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION: @@ -257,18 +258,12 @@ callback_tty(struct lws *wsi, enum lws_callback_reasons reason, return -1; } - { - size_t n = (size_t) client->pty_len + 1; - unsigned char message[LWS_PRE + n]; - unsigned char *p = &message[LWS_PRE]; - *p = OUTPUT; - memcpy(p + 1, client->pty_buffer, client->pty_len); - client->state = STATE_DONE; - - if (lws_write(wsi, p, n, LWS_WRITE_BINARY) < n) { - lwsl_err("write data to WS\n"); - } + client->pty_buffer[LWS_PRE] = OUTPUT; + n = (size_t) (client->pty_len + 1); + if (lws_write(wsi, (unsigned char *) client->pty_buffer + LWS_PRE, n, LWS_WRITE_BINARY) < n) { + lwsl_err("write data to WS\n"); } + client->state = STATE_DONE; break; case LWS_CALLBACK_RECEIVE: diff --git a/src/server.h b/src/server.h index 881b4e8..e1c6469 100644 --- a/src/server.h +++ b/src/server.h @@ -89,7 +89,7 @@ struct tty_client { int pid; int pty; enum pty_state state; - char pty_buffer[BUF_SIZE]; + char pty_buffer[LWS_PRE + 1 + BUF_SIZE]; ssize_t pty_len; pthread_t thread; pthread_mutex_t mutex;