forked from MusicPlayerDaemon/MPD
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
io/UniqueFileDescriptor: use AdoptTag in the constructors that adopt …
…ownership
- Loading branch information
1 parent
765a6a2
commit 35dc1fc
Showing
9 changed files
with
115 additions
and
9 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// SPDX-License-Identifier: BSD-2-Clause | ||
// author: Max Kellermann <[email protected]> | ||
|
||
#pragma once | ||
|
||
#include "SocketDescriptor.hxx" | ||
|
||
#ifndef _WIN32 | ||
#include "io/UniqueFileDescriptor.hxx" | ||
#endif | ||
|
||
#include <utility> | ||
|
||
class StaticSocketAddress; | ||
|
||
/** | ||
* Wrapper for a socket file descriptor. | ||
*/ | ||
class UniqueSocketDescriptor : public SocketDescriptor { | ||
public: | ||
UniqueSocketDescriptor() noexcept | ||
:SocketDescriptor(SocketDescriptor::Undefined()) {} | ||
|
||
explicit UniqueSocketDescriptor(SocketDescriptor _fd) noexcept | ||
:SocketDescriptor(_fd) {} | ||
#ifndef _WIN32 | ||
explicit UniqueSocketDescriptor(FileDescriptor _fd) noexcept | ||
:SocketDescriptor(_fd) {} | ||
|
||
explicit UniqueSocketDescriptor(UniqueFileDescriptor &&_fd) noexcept | ||
:SocketDescriptor(_fd.Release()) {} | ||
#endif // !_WIN32 | ||
|
||
explicit UniqueSocketDescriptor(int _fd) noexcept | ||
:SocketDescriptor(_fd) {} | ||
|
||
#ifdef _WIN32 | ||
UniqueSocketDescriptor(UniqueSocketDescriptor &&other) noexcept | ||
:SocketDescriptor(std::exchange(other.fd, INVALID_SOCKET)) {} | ||
#else // !_WIN32 | ||
UniqueSocketDescriptor(UniqueSocketDescriptor &&other) noexcept | ||
:SocketDescriptor(std::exchange(other.fd, -1)) {} | ||
#endif // !_WIN32 | ||
|
||
~UniqueSocketDescriptor() noexcept { | ||
if (IsDefined()) | ||
Close(); | ||
} | ||
|
||
/** | ||
* Release ownership and return the descriptor as an unmanaged | ||
* #SocketDescriptor instance. | ||
*/ | ||
SocketDescriptor Release() noexcept { | ||
return std::exchange(*(SocketDescriptor *)this, Undefined()); | ||
} | ||
|
||
#ifndef _WIN32 | ||
UniqueFileDescriptor MoveToFileDescriptor() && noexcept { | ||
return UniqueFileDescriptor{Release().ToFileDescriptor()}; | ||
} | ||
#endif | ||
|
||
UniqueSocketDescriptor &operator=(UniqueSocketDescriptor &&src) noexcept { | ||
using std::swap; | ||
swap(fd, src.fd); | ||
return *this; | ||
} | ||
|
||
bool operator==(const UniqueSocketDescriptor &other) const noexcept { | ||
return fd == other.fd; | ||
} | ||
|
||
/** | ||
* @return an "undefined" instance on error | ||
*/ | ||
UniqueSocketDescriptor AcceptNonBlock() const noexcept { | ||
return UniqueSocketDescriptor(SocketDescriptor::AcceptNonBlock()); | ||
} | ||
|
||
/** | ||
* @return an "undefined" instance on error | ||
*/ | ||
UniqueSocketDescriptor AcceptNonBlock(StaticSocketAddress &address) const noexcept { | ||
return UniqueSocketDescriptor(SocketDescriptor::AcceptNonBlock(address)); | ||
} | ||
|
||
#ifndef _WIN32 | ||
static bool CreateSocketPair(int domain, int type, int protocol, | ||
UniqueSocketDescriptor &a, | ||
UniqueSocketDescriptor &b) noexcept { | ||
return SocketDescriptor::CreateSocketPair(domain, type, | ||
protocol, | ||
a, b); | ||
} | ||
|
||
static bool CreateSocketPairNonBlock(int domain, int type, int protocol, | ||
UniqueSocketDescriptor &a, | ||
UniqueSocketDescriptor &b) noexcept { | ||
return SocketDescriptor::CreateSocketPairNonBlock(domain, type, | ||
protocol, | ||
a, b); | ||
} | ||
#endif | ||
}; |
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