From bd6d1656cfcca1f0f351defcd3dcfa617812a909 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Mon, 7 Jun 2021 07:49:55 +0200 Subject: Handle invalid_argument in EDI input --- src/input/Edi.cpp | 79 +++++++++++++++++++++++++++---------------------------- 1 file changed, 39 insertions(+), 40 deletions(-) (limited to 'src') diff --git a/src/input/Edi.cpp b/src/input/Edi.cpp index 338a8e6..be1b4a3 100644 --- a/src/input/Edi.cpp +++ b/src/input/Edi.cpp @@ -339,55 +339,54 @@ size_t Edi::readFrame(uint8_t *buffer, size_t size, std::time_t seconds, int utc void Edi::m_run() { while (m_running) { - switch (m_input_used) { - case InputUsed::UDP: - { - constexpr size_t packsize = 2048; - auto packet = m_udp_sock.receive(packsize); - if (packet.buffer.size() == packsize) { - fprintf(stderr, "Warning, possible UDP truncation\n"); - } - if (not packet.buffer.empty()) { - try { + try { + switch (m_input_used) { + case InputUsed::UDP: + { + constexpr size_t packsize = 2048; + auto packet = m_udp_sock.receive(packsize); + if (packet.buffer.size() == packsize) { + fprintf(stderr, "Warning, possible UDP truncation\n"); + } + + if (not packet.buffer.empty()) { EdiDecoder::Packet p(move(packet.buffer)); m_sti_decoder.push_packet(p); } - catch (const runtime_error& e) { - etiLog.level(warn) << "EDI input " << m_name << " exception: " << e.what(); - this_thread::sleep_for(chrono::milliseconds(24)); + else { + this_thread::sleep_for(chrono::milliseconds(12)); } } - else { - this_thread::sleep_for(chrono::milliseconds(12)); - } - } - break; - case InputUsed::TCP: - { - auto message = m_tcp_receive_server.receive(); - if (auto data = dynamic_pointer_cast(message)) { - try { + break; + case InputUsed::TCP: + { + auto message = m_tcp_receive_server.receive(); + if (auto data = dynamic_pointer_cast(message)) { m_sti_decoder.push_bytes(data->data); } - catch (const runtime_error& e) { - etiLog.level(warn) << "EDI input " << m_name << " exception: " << e.what(); - this_thread::sleep_for(chrono::milliseconds(24)); + else if (dynamic_pointer_cast(message)) { + etiLog.level(info) << "EDI input " << m_name << " disconnected"; + m_sti_decoder.push_bytes({}); // Push an empty frame to clear the internal state + } + else if (dynamic_pointer_cast(message)) { + this_thread::sleep_for(chrono::milliseconds(12)); + } + else { + throw logic_error("unimplemented TCPReceiveMessage type"); } } - else if (dynamic_pointer_cast(message)) { - etiLog.level(info) << "EDI input " << m_name << " disconnected"; - m_sti_decoder.push_bytes({}); // Push an empty frame to clear the internal state - } - else if (dynamic_pointer_cast(message)) { - this_thread::sleep_for(chrono::milliseconds(12)); - } - else { - throw logic_error("unimplemented TCPReceiveMessage type"); - } - } - break; - default: - throw logic_error("unimplemented input"); + break; + default: + throw logic_error("unimplemented input"); + } + } + catch (const invalid_argument& e) { + etiLog.level(warn) << "EDI input " << m_name << " exception: " << e.what(); + this_thread::sleep_for(chrono::milliseconds(24)); + } + catch (const runtime_error& e) { + etiLog.level(warn) << "EDI input " << m_name << " exception: " << e.what(); + this_thread::sleep_for(chrono::milliseconds(24)); } } } -- cgit v1.2.3