From b1a44cfef1f8b04a6303b64ccb6449d2f9a1b68c Mon Sep 17 00:00:00 2001 From: Vadim Sychev Date: Thu, 18 Aug 2022 11:28:55 +0300 Subject: [PATCH] =?UTF-8?q?dev(UML-981):=20=D0=94=D0=BE=D0=B1=D0=B0=D0=B2?= =?UTF-8?q?=D0=B8=D0=BB=20=D0=BE=D0=B1=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=BA?= =?UTF-8?q?=D1=83=20=D0=BD=D0=B5=D0=BF=D1=80=D0=B0=D0=B2=D0=B8=D0=BB=D1=8C?= =?UTF-8?q?=D0=BD=D1=8B=D1=85=20=D0=B7=D0=B0=D0=BF=D1=80=D0=BE=D1=81=D0=BE?= =?UTF-8?q?=D0=B2=20=D0=B8=20=D0=BE=D1=82=D0=BA=D0=BB=D1=8E=D1=87=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BA=D0=BB=D0=B8=D0=B5=D0=BD=D1=82=D0=B0?= =?UTF-8?q?=20=D0=BE=D1=82=20pipe.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/interprocess/pipes/json_rpc_handler.cpp | 16 +++++++++- src/interprocess/pipes/profinet_pipes.cpp | 33 ++++++++++++++++----- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/src/interprocess/pipes/json_rpc_handler.cpp b/src/interprocess/pipes/json_rpc_handler.cpp index 2e5b747..a9cbaea 100644 --- a/src/interprocess/pipes/json_rpc_handler.cpp +++ b/src/interprocess/pipes/json_rpc_handler.cpp @@ -1,6 +1,7 @@ #include "json_rpc_handler.hpp" #include #include +#include using json = nlohmann::json; using string = std::string; @@ -164,7 +165,20 @@ static bool json_readSubmoduleParameter(json& j_in, json& j_out, ProfinetIface * bool json_rpc_handler(std::string& input, std::string& output, ProfinetIface * p_profinet) { - json j_in = json::parse(input); + json j_in; + + // Может прийти неправильная строка + try + { + /// Парсим строку + j_in = json::parse(input); + } + catch(const std::exception& e) + { + std::cerr << e.what() << '\n'; + return false; + } + json j_out; if (j_in.is_null()) diff --git a/src/interprocess/pipes/profinet_pipes.cpp b/src/interprocess/pipes/profinet_pipes.cpp index a231274..0a7cbf6 100644 --- a/src/interprocess/pipes/profinet_pipes.cpp +++ b/src/interprocess/pipes/profinet_pipes.cpp @@ -46,17 +46,36 @@ void ProfinetPipes::listen_pipe(ProfinetIface * p_profinet) std::string answer_str; std::getline( *p_input_stream_.get(), request_str); //std::cout << "Request: " << request_str << std::endl; - json_rpc_handler(request_str, answer_str, p_profinet); - //std::cout << "Answer: " << answer_str << std::endl; - *p_output_stream_ << answer_str << std::endl; - + if (json_rpc_handler(request_str, answer_str, p_profinet)) + { + //std::cout << "Answer: " << answer_str << std::endl; + *p_output_stream_ << answer_str << std::endl; + } + else + { + if (!request_str.empty()) + { + std::cout << "Wrong request: " << request_str << std::endl; + } + else + { + /** + * @brief При отключении стороннего приложения от приемного fifo, в старом линуксе постоянно приходят пустые строки. + * Нужно решить что в этом случае делать. + * В новом линуксе вызывается исключение и прога закрывается. + */ + throw std::runtime_error("listen_pipe: Profinet client was disconnected from profinet server output pipe, application closing..."); + } + + } } } catch ( const std::exception &ex ){ auto answer_str = ex.what(); - std::cout << "Answer: " << answer_str << std::endl; - *p_output_stream_ << answer_str << std::endl; - listen_thread_stop_.store(true, std::memory_order_relaxed); + std::cout << answer_str << std::endl; + //*p_output_stream_ << answer_str << std::endl; + throw std::runtime_error("listen_pipe: Critical Error!"); + //listen_thread_stop_.store(true, std::memory_order_relaxed); } }