From 7b19d03cf35ae4cb0c7d9a5b9c768cafe06876dc Mon Sep 17 00:00:00 2001 From: Paul Hariel Date: Thu, 28 Sep 2023 12:23:34 +0200 Subject: [PATCH] Use error_code instead of exception and don't display error message on disconnection --- shared/src/Connection.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/shared/src/Connection.cpp b/shared/src/Connection.cpp index 7f80b5d..4378a66 100644 --- a/shared/src/Connection.cpp +++ b/shared/src/Connection.cpp @@ -32,17 +32,18 @@ namespace pine if (!bufferSize) co_return buffer; - try - { - size_t n = socket.receive(asio::buffer(buffer)); - buffer.resize(n); - } - catch (std::exception& e) + asio::error_code ec; + auto flags = asio::socket_base::message_peek; + size_t n = socket.receive(asio::buffer(buffer), flags, ec); + + if (ec && ec != asio::error::connection_reset) { - std::cout << "Failed to receive message: " << e.what() << std::endl; + std::cout << "Failed to receive message: " << std::dec << ec.value() << " -> " << ec.message() << std::endl; co_return buffer; } + buffer.resize(n); + co_return buffer; }