diff options
Diffstat (limited to 'works/life')
| -rw-r--r-- | works/life/computer-network-experiment/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | works/life/computer-network-experiment/Output.cpp | 18 | ||||
| -rw-r--r-- | works/life/computer-network-experiment/Output.h | 46 | ||||
| -rw-r--r-- | works/life/computer-network-experiment/server.cpp | 65 | 
4 files changed, 70 insertions, 61 deletions
| 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> 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 <iostream>
 +
 +#include <fmt/format.h>
 +#include <folly/MPMCPipeline.h>
 +#include <folly/MPMCQueue.h>
 +
 +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> output_queue;
 +
 +inline void SendOutput(Output output) {
 +  output_queue.blockingWrite(std::move(output));
 +}
 +
 +inline void SendOutput(std::wstring output) {
 +  SendOutput(std::move(output));
 +}
 +
 +template <typename... Args>
 +void SendOutput(std::wstring_view format, Args &&...args) {
 +  output_queue.blockingWrite(fmt::format(format, std::forward<Args>(args)...));
 +}
 +
 +template <typename... Args>
 +void SendOutput(OutputType type, std::wstring_view format, Args &&...args) {
 +  output_queue.blockingWrite(
 +      Output{fmt::format(format, std::forward<Args>(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 <cstdlib>
  #include <iostream>
  #include <optional>
 @@ -9,71 +11,14 @@  #include <string_view>
  #include <thread>
 -#include <fmt/format.h>
 -#include <folly/MPMCPipeline.h>
 -#include <folly/MPMCQueue.h>
 -
  #include <Windows.h>
  #include <winsock.h>
 -#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> output_queue;
 -
 -void SendOutput(std::wstring output) {
 -  output_queue.blockingWrite(std::move(output));
 -}
 -
 -void SendOutput(Output output) {
 -  output_queue.blockingWrite(std::move(output));
 -}
 -
 -template <typename... Args>
 -void SendOutput(std::wstring_view format, Args &&...args) {
 -  output_queue.blockingWrite(fmt::format(format, std::forward<Args>(args)...));
 -}
 -
 -template <typename... Args>
 -void SendOutput(OutputType type, std::wstring_view format, Args &&...args) {
 -  output_queue.blockingWrite(
 -      {fmt::format(format, std::forward<Args>(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);
      }
    }
 | 
