protocol: avoid extra copy of pty data

This commit is contained in:
Shuanglei Tao
2018-01-13 11:28:52 +08:00
parent 26f8b17891
commit 9dd8a97e6b
2 changed files with 8 additions and 13 deletions

View 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:

View 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;