mirror of
https://github.com/tio/tio.git
synced 2026-05-01 23:07:58 +02:00
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.
This commit is contained in:
parent
4e08c68533
commit
d522527c49
1 changed files with 50 additions and 37 deletions
35
src/tty.c
35
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)
|
||||
{
|
||||
/* Error reading - device is likely unplugged */
|
||||
error_printf_silent("Could not read from tty device");
|
||||
goto error_read;
|
||||
}
|
||||
|
||||
/* Update receive statistics */
|
||||
rx_total++;
|
||||
rx_total += bytes_read;
|
||||
|
||||
/* Process input byte by byte */
|
||||
for (int i=0; i<bytes_read; i++)
|
||||
{
|
||||
input_char = input_buffer[i];
|
||||
|
||||
/* Print timestamp on new line if enabled */
|
||||
if (next_timestamp && input_char != '\n' && input_char != '\r')
|
||||
|
|
@ -903,8 +915,10 @@ int tty_connect(void)
|
|||
print('\r');
|
||||
print('\n');
|
||||
if (option.timestamp)
|
||||
{
|
||||
next_timestamp = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Print received tty character to stdout */
|
||||
|
|
@ -926,27 +940,25 @@ int tty_connect(void)
|
|||
next_timestamp = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Error reading - device is likely unplugged */
|
||||
error_printf_silent("Could not read from tty device");
|
||||
goto error_read;
|
||||
}
|
||||
}
|
||||
else if (FD_ISSET(STDIN_FILENO, &rdfs))
|
||||
{
|
||||
/* Input from stdin ready */
|
||||
status = read(STDIN_FILENO, &input_char, 1);
|
||||
if (status <= 0)
|
||||
ssize_t bytes_read = read(STDIN_FILENO, input_buffer, BUFSIZ);
|
||||
if (bytes_read <= 0)
|
||||
{
|
||||
error_printf_silent("Could not read from stdin");
|
||||
goto error_read;
|
||||
}
|
||||
|
||||
/* Process input byte by byte */
|
||||
for (int i=0; i<bytes_read; i++)
|
||||
{
|
||||
input_char = input_buffer[i];
|
||||
|
||||
/* Forward input to output */
|
||||
output_char = input_char;
|
||||
forward = true;
|
||||
output_char = input_char;
|
||||
|
||||
if (interactive_mode)
|
||||
{
|
||||
|
|
@ -977,6 +989,7 @@ int tty_connect(void)
|
|||
forward_to_tty(fd, output_char);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
forward = socket_handle_input(&rdfs, &output_char);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue