diff options
author | crupest <crupest@outlook.com> | 2021-06-08 18:25:36 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-06-08 18:25:36 +0800 |
commit | f7d73f1bf08aaca98e789a6d6c56d672cc0c4c9f (patch) | |
tree | a059a5c93ac38c5826bb59555fa654cebc6ca951 /works/life/computer-network-experiment/server.cpp | |
parent | d9756de78c161c2d33a9d32d2cb1eaa68be1744e (diff) | |
download | crupest-f7d73f1bf08aaca98e789a6d6c56d672cc0c4c9f.tar.gz crupest-f7d73f1bf08aaca98e789a6d6c56d672cc0c4c9f.tar.bz2 crupest-f7d73f1bf08aaca98e789a6d6c56d672cc0c4c9f.zip |
import(life): ...
Diffstat (limited to 'works/life/computer-network-experiment/server.cpp')
-rw-r--r-- | works/life/computer-network-experiment/server.cpp | 34 |
1 files changed, 27 insertions, 7 deletions
diff --git a/works/life/computer-network-experiment/server.cpp b/works/life/computer-network-experiment/server.cpp index 9654687..03d27ad 100644 --- a/works/life/computer-network-experiment/server.cpp +++ b/works/life/computer-network-experiment/server.cpp @@ -36,6 +36,7 @@ void PrintHelp() { struct Connection {
int id;
std::thread thread;
+ std::thread receive_thread;
int socket;
sockaddr_in address;
String address_string;
@@ -65,13 +66,19 @@ void ResponseThreadProc(Connection *connection) { connection->address_string = fmt::format(CRUT("{}:{}"), host, port);
std::string rest;
+ std::string name_data;
+ if (!SafeReadUntil(connection->socket, '\n', name_data, rest)) {
+ SendOutput(OutputType::Error, CRUT("Failed to read name of {}.\n"),
+ connection->address_string);
+ CloseSocket(connection->socket);
+ return;
+ }
- std::string n = SafeReadUntil(connection->socket, '\n', rest);
- connection->name = ConvertCharString(n);
+ connection->name = ConvertCharString(name_data);
SendOutput(OutputColor::Green, CRUT("Connected to {}, whose name is {}.\n"),
connection->address_string, connection->name);
- std::thread revieve_thread(
+ connection->receive_thread = std::thread(
[](Connection *connection) {
std::string rest;
while (true) {
@@ -79,14 +86,22 @@ void ResponseThreadProc(Connection *connection) { break;
}
- std::string s = SafeReadUntil(connection->socket, '\n', rest);
+ std::string data;
+
+ if (!SafeReadUntil(connection->socket, '\n', data, rest)) {
+ SendOutput(OutputType::Error,
+ CRUT("Failed read data from socket of {}({}).\n"),
+ connection->name, connection->address_string);
+ connection->cancellation_source.requestCancellation();
+ return;
+ }
SendOutput(CRUT("{}({}) send a message:\n{}\n"), connection->name,
- connection->address_string, ConvertCharString(s));
+ connection->address_string, ConvertCharString(data));
}
},
connection);
- revieve_thread.detach();
+ connection->receive_thread.detach();
while (true) {
if (connection->cancellation_source.isCancellationRequested()) {
@@ -95,7 +110,12 @@ void ResponseThreadProc(Connection *connection) { std::string s;
if (connection->send_queue.read(s)) {
- SafeSend(connection->socket, s);
+ if (!SafeSend(connection->socket, s)) {
+ SendOutput(OutputType::Error, CRUT("Failed send data to {}({}).\n"),
+ connection->name, connection->address_string);
+ connection->cancellation_source.requestCancellation();
+ break;
+ }
}
}
|