From ff92a987ad05e40a1315306b31bbc4a219d2ee1d Mon Sep 17 00:00:00 2001 From: crupest Date: Sun, 6 Jun 2021 21:33:52 +0800 Subject: import(life): ... --- .../computer-network-experiment/CMakeLists.txt | 2 +- works/life/computer-network-experiment/Output.cpp | 18 ++++++ works/life/computer-network-experiment/Output.h | 46 +++++++++++++++ works/life/computer-network-experiment/server.cpp | 65 ++-------------------- 4 files changed, 70 insertions(+), 61 deletions(-) create mode 100644 works/life/computer-network-experiment/Output.cpp create mode 100644 works/life/computer-network-experiment/Output.h (limited to 'works/life/computer-network-experiment') diff --git a/works/life/computer-network-experiment/CMakeLists.txt b/works/life/computer-network-experiment/CMakeLists.txt index 923de1f..ecd8764 100644 --- a/works/life/computer-network-experiment/CMakeLists.txt +++ b/works/life/computer-network-experiment/CMakeLists.txt @@ -9,7 +9,7 @@ set(CMAKE_CXX_STANDARD 17) find_package(fmt CONFIG REQUIRED) find_package(Microsoft.GSL CONFIG REQUIRED) -add_library(base STATIC StringUtil.cpp) +add_library(base STATIC StringUtil.cpp Output.cpp) target_link_libraries(base PUBLIC Microsoft.GSL::GSL fmt::fmt) add_executable(client client.cpp) diff --git a/works/life/computer-network-experiment/Output.cpp b/works/life/computer-network-experiment/Output.cpp new file mode 100644 index 0000000..41e14e6 --- /dev/null +++ b/works/life/computer-network-experiment/Output.cpp @@ -0,0 +1,18 @@ +#include "Output.h" + +folly::MPMCQueue output_queue; + +void OutputThread() { + while (true) { + Output output; + output_queue.blockingRead(output); + switch (output.type) { + case OutputType::Error: + std::wcerr << output.message; + break; + default: + std::wcout << output.message; + break; + } + } +} diff --git a/works/life/computer-network-experiment/Output.h b/works/life/computer-network-experiment/Output.h new file mode 100644 index 0000000..0e53363 --- /dev/null +++ b/works/life/computer-network-experiment/Output.h @@ -0,0 +1,46 @@ +#pragma once +#include "StringUtil.hpp" + +#include + +#include +#include +#include + +enum class OutputType { Normal, Error }; + +struct Output { + Output() = default; + Output(std::wstring message, OutputType type = OutputType::Normal) + : message(std::move(message)), type(type) {} + + CRU_DEFAULT_COPY(Output) + CRU_DEFAULT_MOVE(Output) + ~Output() = default; + + std::wstring message; + OutputType type; +}; + +extern folly::MPMCQueue output_queue; + +inline void SendOutput(Output output) { + output_queue.blockingWrite(std::move(output)); +} + +inline void SendOutput(std::wstring output) { + SendOutput(std::move(output)); +} + +template +void SendOutput(std::wstring_view format, Args &&...args) { + output_queue.blockingWrite(fmt::format(format, std::forward(args)...)); +} + +template +void SendOutput(OutputType type, std::wstring_view format, Args &&...args) { + output_queue.blockingWrite( + Output{fmt::format(format, std::forward(args)...), type}); +} + +void OutputThread(); diff --git a/works/life/computer-network-experiment/server.cpp b/works/life/computer-network-experiment/server.cpp index de5bcd3..4942bb5 100644 --- a/works/life/computer-network-experiment/server.cpp +++ b/works/life/computer-network-experiment/server.cpp @@ -2,6 +2,8 @@ * This is the server program. */ +#include "Output.h" + #include #include #include @@ -9,71 +11,14 @@ #include #include -#include -#include -#include - #include #include -#include "StringUtil.hpp" -#include "fmt/core.h" - #pragma comment(lib, "Ws2_32.lib") const auto bind_address = "127.0.0.1"; // control bind address const u_short port = 1234; // control bind port -enum class OutputType { Normal, Error }; - -struct Output { - Output() = default; - Output(std::wstring message, OutputType type = OutputType::Normal) - : message(std::move(message)), type(type) {} - - CRU_DEFAULT_COPY(Output) - CRU_DEFAULT_MOVE(Output) - ~Output() = default; - - std::wstring message; - OutputType type; -}; - -folly::MPMCQueue output_queue; - -void SendOutput(std::wstring output) { - output_queue.blockingWrite(std::move(output)); -} - -void SendOutput(Output output) { - output_queue.blockingWrite(std::move(output)); -} - -template -void SendOutput(std::wstring_view format, Args &&...args) { - output_queue.blockingWrite(fmt::format(format, std::forward(args)...)); -} - -template -void SendOutput(OutputType type, std::wstring_view format, Args &&...args) { - output_queue.blockingWrite( - {fmt::format(format, std::forward(args)...), type}); -} - -void OutputThread() { - while (true) { - Output output; - output_queue.blockingRead(output); - switch (output.type) { - case OutputType::Error: - std::wcerr << output.message; - break; - default: - std::wcout << output.message; - break; - } - } -} [[noreturn]] void PrintErrorMessageAndExit(std::wstring_view message, @@ -82,15 +27,15 @@ PrintErrorMessageAndExit(std::wstring_view message, SendOutput(L"{}\n", message); if (error_code) { - std::cerr << L"Error code is " << std::hex << *error_code << L'\n'; + SendOutput(OutputType::Error, L"Error code is {}.\n", *error_code); wchar_t buffer[500]; if (!FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ARGUMENT_ARRAY | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr, *error_code, 0, buffer, 500, nullptr)) { - std::wcerr << L"Failed to format error message.\n"; + SendOutput(OutputType::Error, L"Failed to format error message.\n"); } else { - std::wcerr << buffer << L'\n'; + SendOutput(OutputType::Error, L"{}\n", buffer); } } -- cgit v1.2.3