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 */
|
fd_set rdfs; /* Read file descriptor set */
|
||||||
int maxfd; /* Maximum file descriptor used */
|
int maxfd; /* Maximum file descriptor used */
|
||||||
char input_char, output_char;
|
char input_char, output_char;
|
||||||
|
char input_buffer[BUFSIZ];
|
||||||
static char previous_char = 0;
|
static char previous_char = 0;
|
||||||
static bool first = true;
|
static bool first = true;
|
||||||
int status;
|
int status;
|
||||||
|
|
@ -877,10 +878,21 @@ int tty_connect(void)
|
||||||
if (FD_ISSET(fd, &rdfs))
|
if (FD_ISSET(fd, &rdfs))
|
||||||
{
|
{
|
||||||
/* Input from tty device ready */
|
/* 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 */
|
/* 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 */
|
/* Print timestamp on new line if enabled */
|
||||||
if (next_timestamp && input_char != '\n' && input_char != '\r')
|
if (next_timestamp && input_char != '\n' && input_char != '\r')
|
||||||
|
|
@ -903,8 +915,10 @@ int tty_connect(void)
|
||||||
print('\r');
|
print('\r');
|
||||||
print('\n');
|
print('\n');
|
||||||
if (option.timestamp)
|
if (option.timestamp)
|
||||||
|
{
|
||||||
next_timestamp = true;
|
next_timestamp = true;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
/* Print received tty character to stdout */
|
/* Print received tty character to stdout */
|
||||||
|
|
@ -926,27 +940,25 @@ int tty_connect(void)
|
||||||
next_timestamp = true;
|
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))
|
else if (FD_ISSET(STDIN_FILENO, &rdfs))
|
||||||
{
|
{
|
||||||
/* Input from stdin ready */
|
/* Input from stdin ready */
|
||||||
status = read(STDIN_FILENO, &input_char, 1);
|
ssize_t bytes_read = read(STDIN_FILENO, input_buffer, BUFSIZ);
|
||||||
if (status <= 0)
|
if (bytes_read <= 0)
|
||||||
{
|
{
|
||||||
error_printf_silent("Could not read from stdin");
|
error_printf_silent("Could not read from stdin");
|
||||||
goto error_read;
|
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 */
|
/* Forward input to output */
|
||||||
output_char = input_char;
|
output_char = input_char;
|
||||||
forward = true;
|
forward = true;
|
||||||
output_char = input_char;
|
|
||||||
|
|
||||||
if (interactive_mode)
|
if (interactive_mode)
|
||||||
{
|
{
|
||||||
|
|
@ -977,6 +989,7 @@ int tty_connect(void)
|
||||||
forward_to_tty(fd, output_char);
|
forward_to_tty(fd, output_char);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
forward = socket_handle_input(&rdfs, &output_char);
|
forward = socket_handle_input(&rdfs, &output_char);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue