diff options
Diffstat (limited to 'store/works/life/computer-network-experiment/IO.cpp')
-rw-r--r-- | store/works/life/computer-network-experiment/IO.cpp | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/store/works/life/computer-network-experiment/IO.cpp b/store/works/life/computer-network-experiment/IO.cpp new file mode 100644 index 0000000..5d3fe12 --- /dev/null +++ b/store/works/life/computer-network-experiment/IO.cpp @@ -0,0 +1,81 @@ +#include "IO.h"
+
+#include <folly/CancellationToken.h>
+
+#include <mutex>
+#include <ostream>
+#include <thread>
+#include <type_traits>
+
+folly::MPMCQueue<Output> output_queue(100);
+
+namespace {
+folly::CancellationSource cancellation_source;
+std::thread io_thread;
+}
+
+void PrintOutput(const Output &output) {
+ std::basic_ostream<Char> *stream;
+
+ switch (output.type) {
+ case OutputType::Error:
+ stream = &error_stream;
+ break;
+ default:
+ stream = &output_stream;
+ break;
+ }
+
+ switch (output.color) {
+ case OutputColor::Normal:
+ (*stream) << output.message;
+ break;
+ case OutputColor::Green:
+ (*stream) << CRUT("\x1b[32m") << output.message << CRUT("\x1b[39m")
+ << std::flush;
+ break;
+ case OutputColor::Red:
+ (*stream) << CRUT("\x1b[31m") << output.message << CRUT("\x1b[39m")
+ << std::flush;
+ break;
+ case OutputColor::Yellow:
+ (*stream) << CRUT("\x1b[33m") << output.message << CRUT("\x1b[39m")
+ << std::flush;
+ break;
+ }
+}
+
+String ReadInputLine() {
+ String line;
+ std::getline(input_stream, line);
+ return line;
+}
+
+void IOThread() {
+ while (true) {
+ if (cancellation_source.isCancellationRequested()) {
+ while (true) {
+ Output output;
+ if (output_queue.readIfNotEmpty(output)) {
+ PrintOutput(output);
+ } else {
+ return;
+ }
+ }
+ }
+
+ Output output;
+ while (output_queue.readIfNotEmpty(output))
+ PrintOutput(output);
+
+ PrintOutput({CRUT("> ")});
+ OnInputLine(ReadInputLine());
+ }
+}
+
+void SignalAndWaitForOutputThreadStop() {
+ cancellation_source.requestCancellation();
+ io_thread.join();
+}
+
+void StartIOThread() { io_thread = std::thread(IOThread); }
|