From 5d2424c8f2e24a1dd6bf8683461b46e25687716c Mon Sep 17 00:00:00 2001 From: Jonas Vautherin Date: Wed, 16 Dec 2020 14:22:16 +0100 Subject: [PATCH 1/4] Do not throw SIGPIPE when TCP connection is closed --- src/core/tcp_connection.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/core/tcp_connection.cpp b/src/core/tcp_connection.cpp index 11043f0d6d..ff52e7548a 100644 --- a/src/core/tcp_connection.cpp +++ b/src/core/tcp_connection.cpp @@ -153,11 +153,17 @@ bool TcpConnection::send_message(const mavlink_message_t& message) // TODO: remove this assert again assert(buffer_len <= MAVLINK_MAX_PACKET_LEN); +#if defined(WINDOWS) + auto flags = 0; +#else + auto flags = MSG_NOSIGNAL; +#endif + const auto send_len = sendto( _socket_fd, reinterpret_cast(buffer), buffer_len, - 0, + flags, reinterpret_cast(&dest_addr), sizeof(dest_addr)); From 1127e96f9eb442badd4a3d3031822271d6e55f98 Mon Sep 17 00:00:00 2001 From: Jonas Vautherin Date: Wed, 16 Dec 2020 17:20:30 +0100 Subject: [PATCH 2/4] Ignore send_message when TCP connection is down --- src/core/tcp_connection.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/core/tcp_connection.cpp b/src/core/tcp_connection.cpp index ff52e7548a..a892e81ed5 100644 --- a/src/core/tcp_connection.cpp +++ b/src/core/tcp_connection.cpp @@ -130,6 +130,10 @@ ConnectionResult TcpConnection::stop() bool TcpConnection::send_message(const mavlink_message_t& message) { + if (!_is_ok) { + return false; + } + if (_remote_ip.empty()) { LogErr() << "Remote IP unknown"; return false; From faecdf81c2ff3a8c11c350d10be7d1f957ae45af Mon Sep 17 00:00:00 2001 From: Jonas Vautherin Date: Wed, 16 Dec 2020 17:21:00 +0100 Subject: [PATCH 3/4] Call _new_system_callback in notify_on_timeout, too --- src/core/mavsdk_impl.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/core/mavsdk_impl.cpp b/src/core/mavsdk_impl.cpp index 9291de0606..986dee3db9 100644 --- a/src/core/mavsdk_impl.cpp +++ b/src/core/mavsdk_impl.cpp @@ -452,6 +452,12 @@ void MavsdkImpl::notify_on_timeout(const uint64_t uuid) if (_on_timeout_callback) { _on_timeout_callback(uuid); } + + std::lock_guard lock(_new_system_callback_mutex); + if (_new_system_callback) { + auto temp_callback = _new_system_callback; + call_user_callback([temp_callback]() { temp_callback(); }); + } } void MavsdkImpl::subscribe_on_new_system(Mavsdk::NewSystemCallback callback) From fbc54ea51940224112f6d60b6043c5c8f12b2195 Mon Sep 17 00:00:00 2001 From: Jonas Vautherin Date: Wed, 16 Dec 2020 17:39:52 +0100 Subject: [PATCH 4/4] Call _new_system_callback on subscribe --- .../src/core/core_service_impl.h | 30 ++++++++++++------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/src/mavsdk_server/src/core/core_service_impl.h b/src/mavsdk_server/src/core/core_service_impl.h index 4cfd0949af..18b868e708 100644 --- a/src/mavsdk_server/src/core/core_service_impl.h +++ b/src/mavsdk_server/src/core/core_service_impl.h @@ -23,18 +23,13 @@ class CoreServiceImpl final : public mavsdk::rpc::core::CoreService::Service { { std::mutex connection_state_mutex{}; - _mavsdk.subscribe_on_new_system([this, &writer, &connection_state_mutex]() { - auto systems = _mavsdk.systems(); - - for (auto system : systems) { - const auto rpc_connection_state_response = - createRpcConnectionStateResponse(system->is_connected()); - - std::lock_guard lock(connection_state_mutex); - writer->Write(rpc_connection_state_response); - } + _mavsdk.subscribe_on_new_system([this, writer, &connection_state_mutex]() { + publish_system_state(writer, connection_state_mutex); }); + // Publish the current state on subscribe + publish_system_state(writer, connection_state_mutex); + _stop_future.wait(); return grpc::Status::OK; } @@ -88,6 +83,21 @@ class CoreServiceImpl final : public mavsdk::rpc::core::CoreService::Service { return rpc_connection_state_response; } + + void publish_system_state( + grpc::ServerWriter* writer, + std::mutex& connection_state_mutex) + { + auto systems = _mavsdk.systems(); + + for (auto system : systems) { + const auto rpc_connection_state_response = + createRpcConnectionStateResponse(system->is_connected()); + + std::lock_guard lock(connection_state_mutex); + writer->Write(rpc_connection_state_response); + } + } }; } // namespace mavsdk_server