Skip to content

Commit

Permalink
perf: Use flow control
Browse files Browse the repository at this point in the history
For more details please refer to: https://xtermjs.org/docs/guides/flowcontrol/

Signed-off-by: Jianhui Zhao <zhaojh329@gmail.com>
  • Loading branch information
zhaojh329 committed Sep 16, 2021
1 parent 4d6f873 commit 11bdde6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
16 changes: 14 additions & 2 deletions src/rtty.c
Expand Up @@ -106,6 +106,12 @@ static void pty_on_read(struct ev_loop *loop, struct ev_io *w, int revents)
if (detect_file_operation(buf, len, tty->sid, &tty->file))
return;

tty->wait_ack += len;

/* stop until received ack */
if (tty->wait_ack > RTTY_TTY_ACK_BLOCK)
ev_io_stop(loop, w);

buffer_put_u8(wb, MSG_TYPE_TERMDATA);
buffer_put_u16be(wb, 32 + len);
buffer_put_data(wb, tty->sid, 32);
Expand Down Expand Up @@ -319,10 +325,11 @@ static void rtty_register(struct rtty *rtty)

static void parse_tty_msg(struct rtty *rtty, int type, int len)
{
struct buffer *b = &rtty->rb;
struct tty *tty = NULL;
char sid[33] = "";

buffer_pull(&rtty->rb, sid, 32);
buffer_pull(b, sid, 32);
len -= 32;

if (type != MSG_TYPE_LOGIN) {
Expand All @@ -348,7 +355,11 @@ static void parse_tty_msg(struct rtty *rtty, int type, int len)
set_tty_winsize(tty);
break;
case MSG_TYPE_FILE:
parse_file_msg(&tty->file, &rtty->rb, len);
parse_file_msg(&tty->file, b, len);
break;
case MSG_TYPE_ACK:
tty->wait_ack -= buffer_pull_u16be(b);
ev_io_start(rtty->loop, &tty->ior);
break;
default:
/* never to here */
Expand Down Expand Up @@ -395,6 +406,7 @@ static int parse_msg(struct rtty *rtty)
case MSG_TYPE_TERMDATA:
case MSG_TYPE_WINSIZE:
case MSG_TYPE_FILE:
case MSG_TYPE_ACK:
parse_tty_msg(rtty, msgtype, msglen);
break;

Expand Down
5 changes: 4 additions & 1 deletion src/rtty.h
Expand Up @@ -41,6 +41,7 @@
#define RTTY_MAX_TTY 10
#define RTTY_HEARTBEAT_INTEVAL 5.0
#define RTTY_TTY_TIMEOUT 600
#define RTTY_TTY_ACK_BLOCK 4096

enum {
MSG_TYPE_REGISTER,
Expand All @@ -52,7 +53,8 @@ enum {
MSG_TYPE_HEARTBEAT,
MSG_TYPE_FILE,
MSG_TYPE_HTTP,
MSG_TYPE_MAX = MSG_TYPE_HTTP
MSG_TYPE_ACK,
MSG_TYPE_MAX = MSG_TYPE_ACK
};

struct rtty;
Expand All @@ -67,6 +69,7 @@ struct tty {
struct buffer wb;
struct rtty *rtty;
ev_tstamp active;
uint32_t wait_ack;
struct ev_timer tmr;
struct list_head node;
struct file_context file;
Expand Down

0 comments on commit 11bdde6

Please sign in to comment.