Improve error handling of UDP connections

This commit is contained in:
Carles Fernandez
2024-05-06 18:58:09 +02:00
parent fbc216c1a3
commit 079ef0f07d
10 changed files with 138 additions and 84 deletions
+25 -11
View File
@@ -18,9 +18,15 @@
#include "nav_message_udp_sink.h"
#include <iostream>
#include <sstream>
#if USE_GLOG_AND_GFLAGS
#include <glog/logging.h>
#else
#include <absl/log/log.h>
#endif
Nav_Message_Udp_Sink::Nav_Message_Udp_Sink(const std::vector<std::string>& addresses, const uint16_t& port) : socket{io_context}
Nav_Message_Udp_Sink::Nav_Message_Udp_Sink(const std::vector<std::string>& addresses, const uint16_t& port)
: socket{io_context}
{
for (const auto& address : addresses)
{
@@ -35,22 +41,30 @@ bool Nav_Message_Udp_Sink::write_nav_message(const std::shared_ptr<Nav_Message_P
{
std::string outbound_data = serdes_nav.createProtobuffer(nav_meg_packet);
for (const auto& endpoint : endpoints)
try
{
socket.open(endpoint.protocol(), error);
socket.connect(endpoint, error);
try
for (const auto& endpoint : endpoints)
{
if (socket.send(boost::asio::buffer(outbound_data)) == 0)
socket.open(endpoint.protocol(), error); // NOLINT(bugprone-unused-return-value)
socket.connect(endpoint, error); // NOLINT(bugprone-unused-return-value)
if (error)
{
LOG(WARNING) << "Error connecting to IP address " << endpoint.address()
<< ", port " << static_cast<int>(endpoint.port()) << ": " << error.message();
return false;
}
if (socket.send(boost::asio::buffer(outbound_data)) == 0) // this can throw
{
return false;
}
}
catch (boost::system::system_error const& e)
{
return false;
}
}
catch (const boost::system::system_error& e)
{
std::cerr << "Error sending navigation data: " << e.what() << '\n';
return false;
}
return true;
}
+13 -11
View File
@@ -53,22 +53,24 @@ bool Gnss_Synchro_Udp_Sink::write_gnss_synchro(const std::vector<Gnss_Synchro>&
{
outbound_data = serdes.createProtobuffer(stocks);
}
for (const auto& endpoint : endpoints)
{
socket.open(endpoint.protocol(), error);
try
try
{
for (const auto& endpoint : endpoints)
{
if (socket.send_to(boost::asio::buffer(outbound_data), endpoint) == 0)
socket.open(endpoint.protocol(), error); // NOLINT(bugprone-unused-return-value)
if (socket.send_to(boost::asio::buffer(outbound_data), endpoint) == 0) // this can throw
{
std::cerr << "Gnss_Synchro_Udp_Sink sent 0 bytes\n";
return false;
}
}
catch (boost::system::system_error const& e)
{
std::cerr << e.what() << '\n';
return false;
}
}
catch (const boost::system::system_error& e)
{
std::cerr << "Error sending data: " << e.what() << '\n';
return false;
}
return true;
}
+2 -2
View File
@@ -323,10 +323,10 @@ void TcpCmdInterface::run_cmd_server(int tcp_port)
std::cout << "TcpCmdInterface: Telecommand TCP interface listening on port " << tcp_port << '\n';
boost::asio::ip::tcp::socket socket(context);
acceptor.accept(socket, not_throw);
acceptor.accept(socket, not_throw); // NOLINT(bugprone-unused-return-value)
if (not_throw)
{
std::cerr << "TcpCmdInterface: Error when binding the port in the socket\n";
std::cerr << "TcpCmdInterface: Error when binding the port to the socket: " << not_throw.message() << '\n';
continue;
}