diff options
| author | _ov4 <ov4@ov4.org> | 2026-10-18 09:42:45 +0000 |
|---|---|---|
| committer | _ov4 <ov4@ov4.org> | 2026-10-18 09:42:45 +0000 |
| commit | c8479ceff6aea37def538c902324c68930947f21 (patch) | |
| tree | 815cbb6043da08495c51e5ad30c7238c6f5573be | |
| parent | 53784946192225525a269423a3bdf65d1e9ea326 (diff) | |
add self pipe, and non-block-input
| -rw-r--r-- | include/error.h | 3 | ||||
| -rw-r--r-- | include/pipe.h | 9 | ||||
| -rw-r--r-- | include/util/non_block_input.h | 8 | ||||
| -rw-r--r-- | src/self_pipe.cc | 21 | ||||
| -rw-r--r-- | src/util/non_block_io.cc | 49 |
5 files changed, 90 insertions, 0 deletions
diff --git a/include/error.h b/include/error.h index 50ca23e..ec0bffe 100644 --- a/include/error.h +++ b/include/error.h @@ -26,6 +26,9 @@ enum class error_code FILE_OFLAG_ERROR, FILE_POINTER_FAIL_TO_OPEN, + // pipe + FAIL_TO_OPEN_PIPE, + }; class T_error : public std::runtime_error { diff --git a/include/pipe.h b/include/pipe.h new file mode 100644 index 0000000..e7d7b67 --- /dev/null +++ b/include/pipe.h @@ -0,0 +1,9 @@ +#pragma once + + +namespace ov4 +{ + +inline int self_pipe_fd[2] = {-1, -1}; + +}
\ No newline at end of file diff --git a/include/util/non_block_input.h b/include/util/non_block_input.h new file mode 100644 index 0000000..f5937a7 --- /dev/null +++ b/include/util/non_block_input.h @@ -0,0 +1,8 @@ +#pragma once + +namespace pv4 +{ + +int non_block_read_char(char *c, int *src = nullptr); + +}
\ No newline at end of file diff --git a/src/self_pipe.cc b/src/self_pipe.cc new file mode 100644 index 0000000..42e61d8 --- /dev/null +++ b/src/self_pipe.cc @@ -0,0 +1,21 @@ +#include "pipe.h" + +#include <unistd.h> +#include <cstdlib> +#include <fcntl.h> + +#include "error.h" + +namespace ov4 +{ + +void self_pipe_init() +{ + int ret = pipe2(self_pipe_fd, O_NONBLOCK); + if (ret != 0) + { + throw T_error(error_code::FAIL_TO_OPEN_PIPE, "Fail to open self pipe"); + } +} + +}
\ No newline at end of file diff --git a/src/util/non_block_io.cc b/src/util/non_block_io.cc new file mode 100644 index 0000000..31edbc9 --- /dev/null +++ b/src/util/non_block_io.cc @@ -0,0 +1,49 @@ +#include "util/non_block_input.h" + +#include <unistd.h> +#include <sys/select.h> + +#include "pipe.h" + +using namespace std; + +namespace ov4 +{ + + +/* + * read from both stdin, and self pipe + * returns immediately as soon as stdin or pipe is "readable" + * returns read()'s ret value + * and set src to the the read source (stdin or pipe) + */ +int non_block_read_char(char *c, int *src) +{ + // get fd check range + int max_fd; + if (STDIN_FILENO > self_pipe_fd[0]) max_fd = STDIN_FILENO; + else max_fd = self_pipe_fd[0]; + + // monitor STDIN, and self pipe + fd_set rset; + FD_ZERO(&rset); + FD_SET(STDIN_FILENO, &rset); + FD_SET(self_pipe_fd[0], &rset); + + select(max_fd+1, &rset, nullptr, nullptr, nullptr); + + int ret = -1; + if (FD_ISSET(self_pipe_fd[0], &rset)) + { + ret = read(self_pipe_fd[0], c, 1); + if (src != nullptr) *src = self_pipe_fd[0]; + } + else if (FD_ISSET(STDIN_FILENO, &rset)) + { + ret = read(STDIN_FILENO, c, 1); + if (src != nullptr) *src = self_pipe_fd[0]; + } + return ret; +} + +}
\ No newline at end of file |
