Skip to content

Commit

Permalink
Merge pull request #1288 from mavlink/fix-tcp-sigpipe
Browse files Browse the repository at this point in the history
Do not throw SIGPIPE when TCP connection is closed
  • Loading branch information
JonasVautherin authored Dec 16, 2020
2 parents b930a9b + fbc54ea commit 8c4f4dc
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
6 changes: 6 additions & 0 deletions src/core/mavsdk_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,12 @@ void MavsdkImpl::notify_on_timeout(const uint64_t uuid)
if (_on_timeout_callback) {
_on_timeout_callback(uuid);
}

std::lock_guard<std::mutex> 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)
Expand Down
12 changes: 11 additions & 1 deletion src/core/tcp_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -153,11 +157,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<char*>(buffer),
buffer_len,
0,
flags,
reinterpret_cast<const sockaddr*>(&dest_addr),
sizeof(dest_addr));

Expand Down
30 changes: 20 additions & 10 deletions src/mavsdk_server/src/core/core_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::mutex> 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;
}
Expand Down Expand Up @@ -88,6 +83,21 @@ class CoreServiceImpl final : public mavsdk::rpc::core::CoreService::Service {

return rpc_connection_state_response;
}

void publish_system_state(
grpc::ServerWriter<rpc::core::ConnectionStateResponse>* 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<std::mutex> lock(connection_state_mutex);
writer->Write(rpc_connection_state_response);
}
}
};

} // namespace mavsdk_server
Expand Down

0 comments on commit 8c4f4dc

Please sign in to comment.