]> prime8.dev >> repos - ttyd.git/commitdiff
protocol: avoid extra copy of pty data
authorShuanglei Tao <tsl0922@gmail.com>
Sat, 13 Jan 2018 03:28:52 +0000 (11:28 +0800)
committerShuanglei Tao <tsl0922@gmail.com>
Sat, 13 Jan 2018 03:30:37 +0000 (11:30 +0800)
src/protocol.c
src/server.h

index 65764ab21ea0fbc247850f019bedbc6be4d8477f..d4db6ae433207937745d1c811e315d255a65a627 100644 (file)
@@ -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:
index 881b4e8e27b40c992bd15f5187e522ecdacbbbbe..e1c64696bcd92bd82b30b059571a0c50c8b28cd1 100644 (file)
@@ -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;