aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@gmail.com>2026-09-22 17:54:25 +0200
committerFelix Morgner <felix.morgner@gmail.com>2026-09-22 17:54:25 +0200
commit1212a302b500c37420613086a1b85f0659f09d57 (patch)
tree76ef77e7f64c6278f7fd6e6148cc4aae1585eef7
parenta63e7d65293c07e0567cfdafbce693a543bf2a0d (diff)
downloadttwhy-1212a302b500c37420613086a1b85f0659f09d57.tar.xz
ttwhy-1212a302b500c37420613086a1b85f0659f09d57.zip
lib/io: refactor I/O module
-rw-r--r--CMakeLists.txt7
-rw-r--r--ttwhy/io/mod.cppm5
-rw-r--r--ttwhy/io/readers.cppm (renamed from ttwhy/io.cppm)53
-rw-r--r--ttwhy/io/scoped_attributes.cppm (renamed from ttwhy/scoped_attributes.cppm)6
-rw-r--r--ttwhy/io/signal_handling.cppm58
-rw-r--r--ttwhy/lib.cppm5
-rw-r--r--ttwhy/main.cpp4
-rw-r--r--ttwhy/routers/echo.cppm1
8 files changed, 81 insertions, 58 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 3a76144..138a84a 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -63,9 +63,12 @@ target_sources("ttwhy-core" PUBLIC
FILE_SET CXX_MODULES
FILES
- "ttwhy/io.cppm"
"ttwhy/lib.cppm"
- "ttwhy/scoped_attributes.cppm"
+
+ "ttwhy/io/mod.cppm"
+ "ttwhy/io/readers.cppm"
+ "ttwhy/io/scoped_attributes.cppm"
+ "ttwhy/io/signal_handling.cppm"
"ttwhy/routers/mod.cppm"
"ttwhy/routers/concepts.cppm"
diff --git a/ttwhy/io/mod.cppm b/ttwhy/io/mod.cppm
new file mode 100644
index 0000000..8deff38
--- /dev/null
+++ b/ttwhy/io/mod.cppm
@@ -0,0 +1,5 @@
+export module ttwhy.io;
+
+export import :readers;
+export import :scoped_attributes;
+export import :signal_handling;
diff --git a/ttwhy/io.cppm b/ttwhy/io/readers.cppm
index fe973ed..d15cc7a 100644
--- a/ttwhy/io.cppm
+++ b/ttwhy/io/readers.cppm
@@ -3,63 +3,20 @@ module;
#include <asio.hpp>
#include <asio/experimental/awaitable_operators.hpp>
-#include <chrono> // IWYU pragma: keep
-#include <format>
+#include <array>
+#include <chrono>
#include <span>
#include <vector>
-export module ttwhy:io;
+export module ttwhy.io:readers;
+import ttwhy.routers;
import ttwhy.scanners;
namespace ttwhy::io
{
- export template<typename Stream, typename TerminalAttributes>
- auto handle_signals(Stream & stream, TerminalAttributes & attributes) -> asio::awaitable<std::error_code>
- {
- auto executor = co_await asio::this_coro::executor;
- auto signals = asio::signal_set{executor, SIGINT, SIGTERM};
- signals.add(SIGTSTP);
- signals.add(SIGCONT);
-
- while (true)
- {
- auto [error, signal] = co_await signals.async_wait(asio::as_tuple(asio::use_awaitable));
- if (error)
- {
- co_return error;
- }
-
- switch (signal)
- {
- case SIGINT:
- case SIGTERM:
- {
- auto message = std::format("Received signal {}, exiting ...\n", signal);
- co_await asio::async_write(stream, asio::buffer(message), asio::use_awaitable);
- co_return asio::error_code{};
- }
- case SIGTSTP:
- {
- attributes.canonical_mode(true).echo(true);
- ::raise(SIGSTOP);
- break;
- }
- case SIGCONT:
- {
- attributes.canonical_mode(false).echo(false);
- break;
- }
- }
-
- if (!error)
- {
- }
- }
- }
-
- export template<typename InputStream, typename AppRouter>
+ export template<typename InputStream, router AppRouter>
auto read_events(InputStream & stream, AppRouter & router) -> asio::awaitable<void>
{
using namespace asio::experimental::awaitable_operators;
diff --git a/ttwhy/scoped_attributes.cppm b/ttwhy/io/scoped_attributes.cppm
index 080bca7..5e42e39 100644
--- a/ttwhy/scoped_attributes.cppm
+++ b/ttwhy/io/scoped_attributes.cppm
@@ -7,9 +7,9 @@ module;
#include <system_error>
#include <utility>
-export module ttwhy:scoped_attributes;
+export module ttwhy.io:scoped_attributes;
-namespace ttwhy
+namespace ttwhy::io
{
auto static read_attributes(int file_descriptor) -> termios
@@ -132,4 +132,4 @@ namespace ttwhy
termios m_attributes;
};
-} // namespace ttwhy
+} // namespace ttwhy::io
diff --git a/ttwhy/io/signal_handling.cppm b/ttwhy/io/signal_handling.cppm
new file mode 100644
index 0000000..1f3fecc
--- /dev/null
+++ b/ttwhy/io/signal_handling.cppm
@@ -0,0 +1,58 @@
+module;
+
+#include <asio.hpp>
+
+#include <csignal>
+#include <format>
+#include <system_error>
+
+export module ttwhy.io:signal_handling;
+
+namespace ttwhy::io
+{
+
+ export template<typename Stream, typename TerminalAttributes>
+ auto handle_signals(Stream & stream, TerminalAttributes & attributes) -> asio::awaitable<std::error_code>
+ {
+ auto executor = co_await asio::this_coro::executor;
+ auto signals = asio::signal_set{executor, SIGINT, SIGTERM};
+ signals.add(SIGTSTP);
+ signals.add(SIGCONT);
+
+ while (true)
+ {
+ auto [error, signal] = co_await signals.async_wait(asio::as_tuple(asio::use_awaitable));
+ if (error)
+ {
+ co_return error;
+ }
+
+ switch (signal)
+ {
+ case SIGINT:
+ case SIGTERM:
+ {
+ auto message = std::format("Received signal {}, exiting ...\n", signal);
+ co_await asio::async_write(stream, asio::buffer(message), asio::use_awaitable);
+ co_return asio::error_code{};
+ }
+ case SIGTSTP:
+ {
+ attributes.canonical_mode(true).echo(true);
+ ::raise(SIGSTOP);
+ break;
+ }
+ case SIGCONT:
+ {
+ attributes.canonical_mode(false).echo(false);
+ break;
+ }
+ }
+
+ if (!error)
+ {
+ }
+ }
+ }
+
+} // namespace ttwhy::io
diff --git a/ttwhy/lib.cppm b/ttwhy/lib.cppm
index 14e0d5c..474d6b4 100644
--- a/ttwhy/lib.cppm
+++ b/ttwhy/lib.cppm
@@ -1,4 +1,5 @@
export module ttwhy;
-export import :io;
-export import :scoped_attributes;
+export import ttwhy.io;
+export import ttwhy.routers;
+export import ttwhy.scanners;
diff --git a/ttwhy/main.cpp b/ttwhy/main.cpp
index 1cd2ac8..ab635a7 100644
--- a/ttwhy/main.cpp
+++ b/ttwhy/main.cpp
@@ -3,14 +3,14 @@
#include <stdio.h>
-import ttwhy;
+import ttwhy.io;
import ttwhy.routers;
auto app(int in, int out, int error) -> asio::awaitable<void>
{
using namespace asio::experimental::awaitable_operators;
- auto terminal_attributes = ttwhy::scoped_attributes{fileno(stdin)} //
+ auto terminal_attributes = ttwhy::io::scoped_attributes{fileno(stdin)} //
.canonical_mode(false)
.echo(false);
diff --git a/ttwhy/routers/echo.cppm b/ttwhy/routers/echo.cppm
index 6fe60cb..ff309c8 100644
--- a/ttwhy/routers/echo.cppm
+++ b/ttwhy/routers/echo.cppm
@@ -10,7 +10,6 @@ module;
export module ttwhy.routers:echo;
-import ttwhy;
import ttwhy.scanners;
namespace ttwhy::routers