commit f2a85a75526335d0b3c26a79bd9c50f7f5ef98a6 from: Aleksey Ryndin date: Wed Aug 09 13:29:13 2023 UTC Refectored: error handling commit - 230a46c6267df18bb9b48a66399e9db3b3ece434 commit + f2a85a75526335d0b3c26a79bd9c50f7f5ef98a6 blob - c4d8e6eb13092ac9ee2254406b5ffab30ed5ca1c blob + a1791a32da6f24b2d3a3060d95fb971e744eaecd --- shared/error.h +++ shared/error.h @@ -30,10 +30,13 @@ struct print /** Empty error code printer */ -inline void no_error_code() {} +struct none +{ + void operator() () const {} +}; -/** Error handler: print action, print error, return `false` */ +/** Error handler: print action, print error */ template< typename TPrintAction, typename TPrintError @@ -56,7 +59,13 @@ occurred( TPrintError print_error ) { - occurred([=](){g_log << action;}, print_error); + occurred( + [action] + { + g_log << action; + }, + print_error + ); } blob - ef50e08bc2f5a86b92048b4cb1f110df6803ff2c blob + ff03f00b2d778d4c85fb427b0ce7a9aa43609ff2 --- shared/transport.cc +++ shared/transport.cc @@ -22,7 +22,7 @@ bool init() { if (tls_init() == -1) { - error::occurred("TLS initialization", error::no_error_code); + error::occurred("TLS initialization", error::none{}); return false; } @@ -35,28 +35,34 @@ void create_gemini_server_ctx(not_null cert_ config_t cfg{tls_config_new(), tls_config_free}; if (!cfg) { - error::occurred("Create TLS configuration", error::no_error_code); + error::occurred("Create TLS configuration", error::none{}); return; } if (tls_config_set_protocols(cfg.get(), protocols) == -1) { - error::occurred("Allow TLS 1.2 and TLS 1.3 protocols", error::no_error_code); + error::occurred("Allow TLS 1.2 and TLS 1.3 protocols", error::none{}); return; } if (tls_config_set_cert_file(cfg.get(), cert_file) == -1) { error::occurred( - [=](){error::g_log << "Load certificate file " << cert_file;}, - error::no_error_code + [cert_file] + { + error::g_log << "Load certificate file " << cert_file; + }, + error::none{} ); return; } if (tls_config_set_key_file(cfg.get(), key_file) == -1) { error::occurred( - [=](){error::g_log << "Load key file " << key_file;}, - error::no_error_code + [key_file] + { + error::g_log << "Load key file " << key_file; + }, + error::none{} ); return; } @@ -64,7 +70,7 @@ void create_gemini_server_ctx(not_null cert_ context_t ctx{tls_server(), tls_free}; if (!ctx) { - error::occurred("Create TLS server context", error::no_error_code); + error::occurred("Create TLS server context", error::none{}); return; } @@ -73,7 +79,11 @@ void create_gemini_server_ctx(not_null cert_ auto config_error = tls_config_error(cfg.get()); error::occurred( "Configure TLS context", - [=](){if (config_error) error::g_log << "Error: " << config_error << std::endl;} + [config_error] + { + if (config_error) + error::g_log << "Error: " << config_error << std::endl; + } ); return; } @@ -87,7 +97,7 @@ void accept(struct tls *server_ctx, int client_socket, struct tls *client_ctx = nullptr; if (tls_accept_socket(server_ctx, &client_ctx, client_socket) == -1) { - error::occurred("TLS accept", error::no_error_code); + error::occurred("TLS accept", error::none{}); ret_ctx.reset(); return; } blob - 33d9148d832623c639854216d5b805f0452db1e2 blob + 0a39a90119400afb1373a43976637682d7e806e2 --- vostokd/vostokd.cc +++ vostokd/vostokd.cc @@ -88,8 +88,6 @@ struct command_line_arguments bool server_loop(int server_socket, struct tls *ctx) { - error::g_log << "Enter to server loop..." << std::endl; - for (; ; ) { if (listen(server_socket, 1024) == -1) @@ -104,15 +102,28 @@ bool server_loop(int server_socket, struct tls *ctx) if (!client_socket) { error::occurred("Accept socket", error::print{}); - return false; + continue; } transport::context_t client_ctx{nullptr, tls_free}; transport::accept(ctx, client_socket.get(), client_ctx); if (!client_ctx) - return false; + continue; - error::g_log << inet_ntoa(addr.sin_addr) << " accepted" << std::endl; + try + { + } + catch (const std::system_error &e) + { + error::occurred( + "Create client thread", + [&e] + { + error::g_log << "Error: " << std::dec << e.code() + << ". " << e.what() << std::endl; + } + ); + } } return true; } @@ -144,7 +155,10 @@ bool main(const command_line_arguments &args) if (bind(server_socket.get(), (struct sockaddr *)&sockaddr, sizeof(sockaddr)) == -1) { error::occurred( - [=](){error::g_log << "Bind to " << args.m_addr << ":" << std::dec << args.m_port;}, + [&args] + { + error::g_log << "Bind to " << args.m_addr << ":" << std::dec << args.m_port; + }, error::print{} ); return false;