mirror of
https://github.com/tsl0922/ttyd.git
synced 2026-02-11 12:34:34 +01:00
protocol: avoid extra copy of pty data
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user