From d522527c496de36b5cbfd699741874f6104677a5 Mon Sep 17 00:00:00 2001 From: Martin Lund Date: Fri, 24 Jun 2022 01:24:13 +0200 Subject: [PATCH] Enable buffered reading Read block of bytes from input and process byte by byte for output. This will speed things up by reducing I/O overhead. --- src/tty.c | 87 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 50 insertions(+), 37 deletions(-) diff --git a/src/tty.c b/src/tty.c index 04eedee..3e0c35b 100644 --- a/src/tty.c +++ b/src/tty.c @@ -754,6 +754,7 @@ int tty_connect(void) fd_set rdfs; /* Read file descriptor set */ int maxfd; /* Maximum file descriptor used */ char input_char, output_char; + char input_buffer[BUFSIZ]; static char previous_char = 0; static bool first = true; int status; @@ -877,10 +878,21 @@ int tty_connect(void) if (FD_ISSET(fd, &rdfs)) { /* Input from tty device ready */ - if (read(fd, &input_char, 1) > 0) + ssize_t bytes_read = read(fd, input_buffer, BUFSIZ); + if (bytes_read <= 0) { - /* Update receive statistics */ - rx_total++; + /* Error reading - device is likely unplugged */ + error_printf_silent("Could not read from tty device"); + goto error_read; + } + + /* Update receive statistics */ + rx_total += bytes_read; + + /* Process input byte by byte */ + for (int i=0; i