-
-
Notifications
You must be signed in to change notification settings - Fork 510
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2360 from mavlink/pr-backport-socket
[BACKPORT v2.12] core: Correctly close sockets
- Loading branch information
Showing
7 changed files
with
114 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#include "socket_holder.h" | ||
|
||
#ifndef WINDOWS | ||
#include <sys/socket.h> | ||
#include <unistd.h> | ||
#endif | ||
|
||
namespace mavsdk { | ||
|
||
SocketHolder::SocketHolder(DescriptorType fd) noexcept : _fd{fd} {} | ||
|
||
SocketHolder::~SocketHolder() noexcept | ||
{ | ||
close(); | ||
} | ||
|
||
void SocketHolder::reset(DescriptorType fd) noexcept | ||
{ | ||
if (_fd != fd) { | ||
close(); | ||
_fd = fd; | ||
} | ||
} | ||
|
||
void SocketHolder::close() noexcept | ||
{ | ||
if (!empty()) { | ||
#if defined(WINDOWS) | ||
shutdown(_fd, SD_BOTH); | ||
closesocket(_fd); | ||
WSACleanup(); | ||
#else | ||
// This should interrupt a recv/recvfrom call. | ||
shutdown(_fd, SHUT_RDWR); | ||
|
||
// But on Mac, closing is also needed to stop blocking recv/recvfrom. | ||
::close(_fd); | ||
#endif | ||
_fd = invalid_socket_fd; | ||
} | ||
} | ||
|
||
bool SocketHolder::empty() const noexcept | ||
{ | ||
return _fd == invalid_socket_fd; | ||
} | ||
|
||
SocketHolder::DescriptorType SocketHolder::get() const noexcept | ||
{ | ||
return _fd; | ||
} | ||
|
||
} // namespace mavsdk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#pragma once | ||
|
||
#if defined(WINDOWS) | ||
#include <winsock2.h> | ||
#endif | ||
|
||
namespace mavsdk { | ||
|
||
class SocketHolder { | ||
public: | ||
#if defined(WINDOWS) | ||
using DescriptorType = SOCKET; | ||
static constexpr DescriptorType invalid_socket_fd = INVALID_SOCKET; | ||
#else | ||
using DescriptorType = int; | ||
static constexpr DescriptorType invalid_socket_fd = -1; | ||
#endif | ||
|
||
SocketHolder() noexcept = default; | ||
explicit SocketHolder(DescriptorType socket_fd) noexcept; | ||
|
||
~SocketHolder() noexcept; | ||
|
||
void reset(DescriptorType fd) noexcept; | ||
void close() noexcept; | ||
|
||
bool empty() const noexcept; | ||
DescriptorType get() const noexcept; | ||
|
||
private: | ||
SocketHolder(const SocketHolder&) = delete; | ||
SocketHolder& operator=(const SocketHolder&) = delete; | ||
|
||
DescriptorType _fd = invalid_socket_fd; | ||
}; | ||
|
||
} // namespace mavsdk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters