From 2de4663d385ab54c5d8b8adc68611ad67636f56f Mon Sep 17 00:00:00 2001 From: crupest Date: Sun, 6 Jun 2021 23:51:32 +0800 Subject: import(life): ... --- works/life/computer-network-experiment/Output.cpp | 2 +- works/life/computer-network-experiment/Output.h | 5 ++--- works/life/computer-network-experiment/server.cpp | 7 ++++++- 3 files changed, 9 insertions(+), 5 deletions(-) (limited to 'works/life/computer-network-experiment') diff --git a/works/life/computer-network-experiment/Output.cpp b/works/life/computer-network-experiment/Output.cpp index ac4a47b..2968c19 100644 --- a/works/life/computer-network-experiment/Output.cpp +++ b/works/life/computer-network-experiment/Output.cpp @@ -1,6 +1,6 @@ #include "Output.h" -folly::MPMCQueue output_queue; +folly::MPMCQueue output_queue(100); void OutputThread() { while (true) { diff --git a/works/life/computer-network-experiment/Output.h b/works/life/computer-network-experiment/Output.h index 157e5c8..b81dbfd 100644 --- a/works/life/computer-network-experiment/Output.h +++ b/works/life/computer-network-experiment/Output.h @@ -29,10 +29,9 @@ inline void SendOutput(Output output) { output_queue.blockingWrite(std::move(output)); } -inline void SendOutput(String output) { SendOutput(std::move(output)); } +inline void SendOutput(String output) { SendOutput(Output{std::move(output)}); } -template -void SendOutput(StringView format, Args &&...args) { +template void SendOutput(StringView format, Args &&...args) { output_queue.blockingWrite(fmt::format(format, std::forward(args)...)); } diff --git a/works/life/computer-network-experiment/server.cpp b/works/life/computer-network-experiment/server.cpp index ff03783..a5a7a9b 100644 --- a/works/life/computer-network-experiment/server.cpp +++ b/works/life/computer-network-experiment/server.cpp @@ -56,6 +56,8 @@ void ResponseThreadProc(int socket, sockaddr_in address) { } int Main() { + std::thread output_thread(OutputThread); + int server_socket; if ((server_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1) { @@ -84,7 +86,10 @@ int Main() { unsigned sin_size = sizeof(sockaddr_in); client_socket = accept(server_socket, reinterpret_cast(&client_address), - &sin_size); +#ifdef WIN32 + reinterpret_cast +#endif + (&sin_size)); if (client_socket == -1) { PrintErrorMessageAndExit(CRUT("Failed to accecpt")); -- cgit v1.2.3