Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not throw SIGPIPE when TCP connection is closed #1288

Merged
merged 4 commits into from
Dec 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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