diff options
author | crupest <crupest@outlook.com> | 2021-06-07 23:24:43 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-06-07 23:24:43 +0800 |
commit | 9c42586bc406015be0145576fd6cb3586686b4ca (patch) | |
tree | 92d0a84756dcc587f7e514ab98c5584e81852cc1 /works/life | |
parent | 08325faff52d0704c6f17d065ca8d72ea07ca6a0 (diff) | |
download | crupest-9c42586bc406015be0145576fd6cb3586686b4ca.tar.gz crupest-9c42586bc406015be0145576fd6cb3586686b4ca.tar.bz2 crupest-9c42586bc406015be0145576fd6cb3586686b4ca.zip |
import(life): ...
Diffstat (limited to 'works/life')
-rw-r--r-- | works/life/computer-network-experiment/CMakeLists.txt | 2 | ||||
-rw-r--r-- | works/life/computer-network-experiment/Common.cpp | 8 | ||||
-rw-r--r-- | works/life/computer-network-experiment/Common.h | 5 | ||||
-rw-r--r-- | works/life/computer-network-experiment/IO.cpp (renamed from works/life/computer-network-experiment/Output.cpp) | 30 | ||||
-rw-r--r-- | works/life/computer-network-experiment/IO.h (renamed from works/life/computer-network-experiment/Output.h) | 7 | ||||
-rw-r--r-- | works/life/computer-network-experiment/client.cpp | 2 | ||||
-rw-r--r-- | works/life/computer-network-experiment/server.cpp | 24 |
7 files changed, 46 insertions, 32 deletions
diff --git a/works/life/computer-network-experiment/CMakeLists.txt b/works/life/computer-network-experiment/CMakeLists.txt index 4d4277c..09c1ea4 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 Common.cpp StringUtil.cpp Output.cpp)
+add_library(base STATIC Common.cpp StringUtil.cpp IO.cpp)
target_link_libraries(base PUBLIC Microsoft.GSL::GSL fmt::fmt Folly::folly)
if(WIN32)
target_link_libraries(base PUBLIC Ws2_32)
diff --git a/works/life/computer-network-experiment/Common.cpp b/works/life/computer-network-experiment/Common.cpp index e4615fc..ec6fd1a 100644 --- a/works/life/computer-network-experiment/Common.cpp +++ b/works/life/computer-network-experiment/Common.cpp @@ -1,6 +1,6 @@ #include "Common.h"
-#include "Output.h"
+#include "IO.h"
#ifdef WIN32
#include <Windows.h>
@@ -70,12 +70,6 @@ void BeforeExit() { SignalAndWaitForOutputThreadStop();
}
-String ReadInputLine() {
- String line;
- std::getline(input_stream, line);
- return line;
-}
-
void SafeSend(int socket, std::string_view buffer) {
const int total_byte_count = buffer.size();
int byte_count_sent = 0;
diff --git a/works/life/computer-network-experiment/Common.h b/works/life/computer-network-experiment/Common.h index 6886e38..d2f2e52 100644 --- a/works/life/computer-network-experiment/Common.h +++ b/works/life/computer-network-experiment/Common.h @@ -2,6 +2,7 @@ #include "StringUtil.hpp"
#include <iostream>
+#include <sstream>
#include <string>
#include <string_view>
@@ -9,6 +10,7 @@ using Char = wchar_t;
using String = std::wstring;
using StringView = std::wstring_view;
+using StringStream = std::wstringstream;
inline auto &input_stream = std::wcin;
inline auto &output_stream = std::wcout;
inline auto &error_stream = std::wcerr;
@@ -25,6 +27,7 @@ inline std::string ConvertCharStringBack(StringView s) { using Char = char;
using String = std::string;
using StringView = std::string_view;
+using StringStream = std::stringstream;
inline auto &input_stream = std::cin;
inline auto &output_stream = std::cout;
inline auto &error_stream = std::cerr;
@@ -43,7 +46,5 @@ int CloseSocket(int socket); void BeforeExit();
-String ReadInputLine();
-
void SafeSend(int socket, std::string_view buffer);
std::string SafeReadUntil(int socket, char c, std::string &rest);
diff --git a/works/life/computer-network-experiment/Output.cpp b/works/life/computer-network-experiment/IO.cpp index fbbd6ba..0c20537 100644 --- a/works/life/computer-network-experiment/Output.cpp +++ b/works/life/computer-network-experiment/IO.cpp @@ -1,18 +1,17 @@ -#include "Output.h"
+#include "IO.h"
#include <folly/CancellationToken.h>
#include <mutex>
#include <ostream>
+#include <thread>
#include <type_traits>
-std::mutex m;
-
folly::MPMCQueue<Output> output_queue(100);
folly::CancellationSource cancellation_source;
-std::thread output_thread(OutputThread);
+std::thread io_thread;
void PrintOutput(const Output &output) {
std::basic_ostream<Char> *stream;
@@ -45,11 +44,15 @@ void PrintOutput(const Output &output) { }
}
-void OutputThread() {
- while (true) {
- std::lock_guard<std::mutex> guard(m);
+String ReadInputLine() {
+ String line;
+ std::getline(input_stream, line);
+ return line;
+}
- if (cancellation_source.getToken().isCancellationRequested()) {
+void IOThread() {
+ while (true) {
+ if (cancellation_source.isCancellationRequested()) {
while (true) {
Output output;
if (output_queue.readIfNotEmpty(output)) {
@@ -61,16 +64,17 @@ void OutputThread() { }
Output output;
- if (output_queue.readIfNotEmpty(output))
+ while (output_queue.readIfNotEmpty(output))
PrintOutput(output);
+
+ PrintOutput({CRUT("> ")});
+ OnInputLine(ReadInputLine());
}
}
void SignalAndWaitForOutputThreadStop() {
cancellation_source.requestCancellation();
- output_thread.join();
+ io_thread.join();
}
-std::lock_guard<std::mutex> BlockOutputThread() {
- return std::lock_guard<std::mutex>(m);
-}
+void StartIOThread() { io_thread = std::thread(IOThread); }
diff --git a/works/life/computer-network-experiment/Output.h b/works/life/computer-network-experiment/IO.h index 689c3d3..b0cf489 100644 --- a/works/life/computer-network-experiment/Output.h +++ b/works/life/computer-network-experiment/IO.h @@ -8,7 +8,6 @@ #include <folly/MPMCQueue.h>
#include <iostream>
-#include <mutex>
#include <thread>
enum class OutputType { Normal, Error };
@@ -60,8 +59,8 @@ void SendOutput(OutputColor color, StringView format, Args &&...args) { Output{fmt::format(format, std::forward<Args>(args)...), color});
}
-void OutputThread();
-
void SignalAndWaitForOutputThreadStop();
-std::lock_guard<std::mutex> BlockOutputThread();
+void OnInputLine(StringView line);
+
+void StartIOThread();
diff --git a/works/life/computer-network-experiment/client.cpp b/works/life/computer-network-experiment/client.cpp index c25a26b..a8ce8cf 100644 --- a/works/life/computer-network-experiment/client.cpp +++ b/works/life/computer-network-experiment/client.cpp @@ -3,7 +3,7 @@ */
#include "Common.h"
-#include "Output.h"
+#include "IO.h"
#ifdef WIN32
#include <Windows.h>
diff --git a/works/life/computer-network-experiment/server.cpp b/works/life/computer-network-experiment/server.cpp index 3c87ea0..7008c7b 100644 --- a/works/life/computer-network-experiment/server.cpp +++ b/works/life/computer-network-experiment/server.cpp @@ -3,8 +3,7 @@ */
#include "Common.h"
-#include "Output.h"
-#include "fmt/core.h"
+#include "IO.h"
#include <folly/CancellationToken.h>
#include <folly/ProducerConsumerQueue.h>
@@ -25,7 +24,15 @@ const auto bind_address = "127.0.0.1"; // control bind address
const u_short port = 1234; // control bind port
+void PrintHelp() {
+ SendOutput(CRUT(
+ "Input and run one of following command:\n\t> NOTHING -> Continue and "
+ "print new messages.\n\t> list -> List all connected client.\n\t> send "
+ "[i] [message] -> Send messages to client with number i.\n"));
+}
+
struct Connection {
+ int id;
std::thread thread;
int socket;
sockaddr_in address;
@@ -35,6 +42,8 @@ struct Connection { folly::CancellationSource cancellation_source;
};
+std::vector<Connection> connections;
+
void ResponseThreadProc(Connection *connection) {
auto host = ConvertCharString(inet_ntoa(connection->address.sin_addr));
auto port = htons(connection->address.sin_port);
@@ -77,9 +86,11 @@ void ResponseThreadProc(Connection *connection) { CloseSocket(connection->socket);
}
-int Main() {
- std::vector<Connection> connections;
+void OnInputLine(StringView line) { StringStream ss{String(line)};
+ ss.
+ }
+int Main() {
int server_socket;
if ((server_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
@@ -105,6 +116,10 @@ int Main() { SendOutput(OutputColor::Green,
CRUT("Now start to accept incoming connection.\n"));
+ StartIOThread();
+
+ int current_id = 1;
+
while (true) {
sockaddr_in client_address;
int client_socket;
@@ -121,6 +136,7 @@ int Main() { }
Connection connection;
+ connection.id = current_id++;
connection.socket = client_socket;
connection.address = client_address;
connections.push_back(std::move(connection));
|