-
-
Notifications
You must be signed in to change notification settings - Fork 515
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core: Correctly close sockets (#2357)
* core: Correctly close sockets * Update socket_holder.cpp - Fix newline style * Update socket_holder.cpp * Update src/mavsdk/core/tcp_client_connection.cpp Co-authored-by: Jonas Vautherin <[email protected]> * Update socket_holder.cpp - fix code style * Update tcp_client_connection.cpp - fix code style * Update tcp_server_connection.cpp - fix code style * Update socket_holder.h - fix code style * Update src/mavsdk/core/socket_holder.h Co-authored-by: Julian Oes <[email protected]> * Update socket_holder.h - use 64 bit descriptor type on Win64 * Update socket_holder.cpp - use 64 bit descriptor type on Win64 * Update socket_holder.cpp - minor improving of if logic * Remove default move constructor It is currently not in use, and the default implementation is not suitable because it does not change _fd to INVALID for the object being copied from --------- Co-authored-by: Jonas Vautherin <[email protected]> Co-authored-by: Julian Oes <[email protected]>
- Loading branch information
1 parent
93b91d6
commit 73b42d3
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