net/SocketDescriptor: add "noexcept"

This commit is contained in:
Max Kellermann 2018-08-21 11:08:34 +02:00
parent 185148f57c
commit e092eadd8d
2 changed files with 79 additions and 73 deletions

View File

@ -66,7 +66,7 @@ SocketDescriptor::IsStream() const noexcept
#ifdef _WIN32 #ifdef _WIN32
void void
SocketDescriptor::Close() SocketDescriptor::Close() noexcept
{ {
if (IsDefined()) if (IsDefined())
::closesocket(Steal()); ::closesocket(Steal());
@ -75,7 +75,7 @@ SocketDescriptor::Close()
#endif #endif
SocketDescriptor SocketDescriptor
SocketDescriptor::Accept() SocketDescriptor::Accept() noexcept
{ {
#ifdef __linux__ #ifdef __linux__
int connection_fd = ::accept4(Get(), nullptr, nullptr, SOCK_CLOEXEC); int connection_fd = ::accept4(Get(), nullptr, nullptr, SOCK_CLOEXEC);
@ -88,7 +88,7 @@ SocketDescriptor::Accept()
} }
SocketDescriptor SocketDescriptor
SocketDescriptor::AcceptNonBlock() const SocketDescriptor::AcceptNonBlock() const noexcept
{ {
#ifdef __linux__ #ifdef __linux__
int connection_fd = ::accept4(Get(), nullptr, nullptr, int connection_fd = ::accept4(Get(), nullptr, nullptr,
@ -102,7 +102,7 @@ SocketDescriptor::AcceptNonBlock() const
} }
SocketDescriptor SocketDescriptor
SocketDescriptor::AcceptNonBlock(StaticSocketAddress &address) const SocketDescriptor::AcceptNonBlock(StaticSocketAddress &address) const noexcept
{ {
address.SetMaxSize(); address.SetMaxSize();
#ifdef __linux__ #ifdef __linux__
@ -117,7 +117,7 @@ SocketDescriptor::AcceptNonBlock(StaticSocketAddress &address) const
} }
bool bool
SocketDescriptor::Connect(SocketAddress address) SocketDescriptor::Connect(SocketAddress address) noexcept
{ {
assert(address.IsDefined()); assert(address.IsDefined());
@ -125,7 +125,7 @@ SocketDescriptor::Connect(SocketAddress address)
} }
bool bool
SocketDescriptor::Create(int domain, int type, int protocol) SocketDescriptor::Create(int domain, int type, int protocol) noexcept
{ {
#ifdef _WIN32 #ifdef _WIN32
static bool initialised = false; static bool initialised = false;
@ -150,7 +150,7 @@ SocketDescriptor::Create(int domain, int type, int protocol)
} }
bool bool
SocketDescriptor::CreateNonBlock(int domain, int type, int protocol) SocketDescriptor::CreateNonBlock(int domain, int type, int protocol) noexcept
{ {
#ifdef SOCK_NONBLOCK #ifdef SOCK_NONBLOCK
type |= SOCK_NONBLOCK; type |= SOCK_NONBLOCK;
@ -170,7 +170,8 @@ SocketDescriptor::CreateNonBlock(int domain, int type, int protocol)
bool bool
SocketDescriptor::CreateSocketPair(int domain, int type, int protocol, SocketDescriptor::CreateSocketPair(int domain, int type, int protocol,
SocketDescriptor &a, SocketDescriptor &b) SocketDescriptor &a,
SocketDescriptor &b) noexcept
{ {
#ifdef SOCK_CLOEXEC #ifdef SOCK_CLOEXEC
/* implemented since Linux 2.6.27 */ /* implemented since Linux 2.6.27 */
@ -188,7 +189,8 @@ SocketDescriptor::CreateSocketPair(int domain, int type, int protocol,
bool bool
SocketDescriptor::CreateSocketPairNonBlock(int domain, int type, int protocol, SocketDescriptor::CreateSocketPairNonBlock(int domain, int type, int protocol,
SocketDescriptor &a, SocketDescriptor &b) SocketDescriptor &a,
SocketDescriptor &b) noexcept
{ {
#ifdef SOCK_CLOEXEC #ifdef SOCK_CLOEXEC
/* implemented since Linux 2.6.27 */ /* implemented since Linux 2.6.27 */
@ -208,7 +210,7 @@ SocketDescriptor::CreateSocketPairNonBlock(int domain, int type, int protocol,
#endif #endif
int int
SocketDescriptor::GetError() SocketDescriptor::GetError() noexcept
{ {
assert(IsDefined()); assert(IsDefined());
@ -222,7 +224,7 @@ SocketDescriptor::GetError()
size_t size_t
SocketDescriptor::GetOption(int level, int name, SocketDescriptor::GetOption(int level, int name,
void *value, size_t size) const void *value, size_t size) const noexcept
{ {
assert(IsDefined()); assert(IsDefined());
@ -259,7 +261,7 @@ SocketDescriptor::SetNonBlocking() noexcept
bool bool
SocketDescriptor::SetOption(int level, int name, SocketDescriptor::SetOption(int level, int name,
const void *value, size_t size) const void *value, size_t size) noexcept
{ {
assert(IsDefined()); assert(IsDefined());
@ -268,13 +270,13 @@ SocketDescriptor::SetOption(int level, int name,
} }
bool bool
SocketDescriptor::SetKeepAlive(bool value) SocketDescriptor::SetKeepAlive(bool value) noexcept
{ {
return SetBoolOption(SOL_SOCKET, SO_KEEPALIVE, value); return SetBoolOption(SOL_SOCKET, SO_KEEPALIVE, value);
} }
bool bool
SocketDescriptor::SetReuseAddress(bool value) SocketDescriptor::SetReuseAddress(bool value) noexcept
{ {
return SetBoolOption(SOL_SOCKET, SO_REUSEADDR, value); return SetBoolOption(SOL_SOCKET, SO_REUSEADDR, value);
} }
@ -284,7 +286,7 @@ SocketDescriptor::SetReuseAddress(bool value)
#ifdef SO_REUSEPORT #ifdef SO_REUSEPORT
bool bool
SocketDescriptor::SetReusePort(bool value) SocketDescriptor::SetReusePort(bool value) noexcept
{ {
return SetBoolOption(SOL_SOCKET, SO_REUSEPORT, value); return SetBoolOption(SOL_SOCKET, SO_REUSEPORT, value);
} }
@ -292,37 +294,37 @@ SocketDescriptor::SetReusePort(bool value)
#endif #endif
bool bool
SocketDescriptor::SetFreeBind(bool value) SocketDescriptor::SetFreeBind(bool value) noexcept
{ {
return SetBoolOption(IPPROTO_IP, IP_FREEBIND, value); return SetBoolOption(IPPROTO_IP, IP_FREEBIND, value);
} }
bool bool
SocketDescriptor::SetNoDelay(bool value) SocketDescriptor::SetNoDelay(bool value) noexcept
{ {
return SetBoolOption(IPPROTO_TCP, TCP_NODELAY, value); return SetBoolOption(IPPROTO_TCP, TCP_NODELAY, value);
} }
bool bool
SocketDescriptor::SetCork(bool value) SocketDescriptor::SetCork(bool value) noexcept
{ {
return SetBoolOption(IPPROTO_TCP, TCP_CORK, value); return SetBoolOption(IPPROTO_TCP, TCP_CORK, value);
} }
bool bool
SocketDescriptor::SetTcpDeferAccept(const int &seconds) SocketDescriptor::SetTcpDeferAccept(const int &seconds) noexcept
{ {
return SetOption(IPPROTO_TCP, TCP_DEFER_ACCEPT, &seconds, sizeof(seconds)); return SetOption(IPPROTO_TCP, TCP_DEFER_ACCEPT, &seconds, sizeof(seconds));
} }
bool bool
SocketDescriptor::SetV6Only(bool value) SocketDescriptor::SetV6Only(bool value) noexcept
{ {
return SetBoolOption(IPPROTO_IPV6, IPV6_V6ONLY, value); return SetBoolOption(IPPROTO_IPV6, IPV6_V6ONLY, value);
} }
bool bool
SocketDescriptor::SetBindToDevice(const char *name) SocketDescriptor::SetBindToDevice(const char *name) noexcept
{ {
return SetOption(SOL_SOCKET, SO_BINDTODEVICE, name, strlen(name)); return SetOption(SOL_SOCKET, SO_BINDTODEVICE, name, strlen(name));
} }
@ -330,7 +332,7 @@ SocketDescriptor::SetBindToDevice(const char *name)
#ifdef TCP_FASTOPEN #ifdef TCP_FASTOPEN
bool bool
SocketDescriptor::SetTcpFastOpen(int qlen) SocketDescriptor::SetTcpFastOpen(int qlen) noexcept
{ {
return SetOption(SOL_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)); return SetOption(SOL_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen));
} }
@ -340,7 +342,7 @@ SocketDescriptor::SetTcpFastOpen(int qlen)
#endif #endif
bool bool
SocketDescriptor::Bind(SocketAddress address) SocketDescriptor::Bind(SocketAddress address) noexcept
{ {
return bind(Get(), address.GetAddress(), address.GetSize()) == 0; return bind(Get(), address.GetAddress(), address.GetSize()) == 0;
} }
@ -348,7 +350,7 @@ SocketDescriptor::Bind(SocketAddress address)
#ifdef __linux__ #ifdef __linux__
bool bool
SocketDescriptor::AutoBind() SocketDescriptor::AutoBind() noexcept
{ {
static constexpr sa_family_t family = AF_LOCAL; static constexpr sa_family_t family = AF_LOCAL;
return Bind(SocketAddress((const struct sockaddr *)&family, return Bind(SocketAddress((const struct sockaddr *)&family,
@ -358,13 +360,13 @@ SocketDescriptor::AutoBind()
#endif #endif
bool bool
SocketDescriptor::Listen(int backlog) SocketDescriptor::Listen(int backlog) noexcept
{ {
return listen(Get(), backlog) == 0; return listen(Get(), backlog) == 0;
} }
StaticSocketAddress StaticSocketAddress
SocketDescriptor::GetLocalAddress() const SocketDescriptor::GetLocalAddress() const noexcept
{ {
assert(IsDefined()); assert(IsDefined());
@ -377,7 +379,7 @@ SocketDescriptor::GetLocalAddress() const
} }
StaticSocketAddress StaticSocketAddress
SocketDescriptor::GetPeerAddress() const SocketDescriptor::GetPeerAddress() const noexcept
{ {
assert(IsDefined()); assert(IsDefined());
@ -390,7 +392,7 @@ SocketDescriptor::GetPeerAddress() const
} }
ssize_t ssize_t
SocketDescriptor::Read(void *buffer, size_t length) SocketDescriptor::Read(void *buffer, size_t length) noexcept
{ {
int flags = 0; int flags = 0;
#ifndef _WIN32 #ifndef _WIN32
@ -401,7 +403,7 @@ SocketDescriptor::Read(void *buffer, size_t length)
} }
ssize_t ssize_t
SocketDescriptor::Write(const void *buffer, size_t length) SocketDescriptor::Write(const void *buffer, size_t length) noexcept
{ {
int flags = 0; int flags = 0;
#ifdef __linux__ #ifdef __linux__
@ -414,7 +416,7 @@ SocketDescriptor::Write(const void *buffer, size_t length)
#ifdef _WIN32 #ifdef _WIN32
int int
SocketDescriptor::WaitReadable(int timeout_ms) const SocketDescriptor::WaitReadable(int timeout_ms) const noexcept
{ {
assert(IsDefined()); assert(IsDefined());
@ -433,7 +435,7 @@ SocketDescriptor::WaitReadable(int timeout_ms) const
} }
int int
SocketDescriptor::WaitWritable(int timeout_ms) const SocketDescriptor::WaitWritable(int timeout_ms) const noexcept
{ {
assert(IsDefined()); assert(IsDefined());
@ -455,7 +457,7 @@ SocketDescriptor::WaitWritable(int timeout_ms) const
ssize_t ssize_t
SocketDescriptor::Read(void *buffer, size_t length, SocketDescriptor::Read(void *buffer, size_t length,
StaticSocketAddress &address) StaticSocketAddress &address) noexcept
{ {
int flags = 0; int flags = 0;
#ifndef _WIN32 #ifndef _WIN32
@ -473,7 +475,7 @@ SocketDescriptor::Read(void *buffer, size_t length,
ssize_t ssize_t
SocketDescriptor::Write(const void *buffer, size_t length, SocketDescriptor::Write(const void *buffer, size_t length,
SocketAddress address) SocketAddress address) noexcept
{ {
int flags = 0; int flags = 0;
#ifndef _WIN32 #ifndef _WIN32

View File

@ -42,16 +42,16 @@ class StaticSocketAddress;
*/ */
class SocketDescriptor : protected FileDescriptor { class SocketDescriptor : protected FileDescriptor {
protected: protected:
explicit constexpr SocketDescriptor(FileDescriptor _fd) explicit constexpr SocketDescriptor(FileDescriptor _fd) noexcept
:FileDescriptor(_fd) {} :FileDescriptor(_fd) {}
public: public:
SocketDescriptor() = default; SocketDescriptor() = default;
explicit constexpr SocketDescriptor(int _fd) explicit constexpr SocketDescriptor(int _fd) noexcept
:FileDescriptor(_fd) {} :FileDescriptor(_fd) {}
constexpr bool operator==(SocketDescriptor other) const { constexpr bool operator==(SocketDescriptor other) const noexcept {
return fd == other.fd; return fd == other.fd;
} }
@ -62,7 +62,7 @@ public:
* same as file descriptors (i.e. not on Windows). Use this only * same as file descriptors (i.e. not on Windows). Use this only
* when you know what you're doing. * when you know what you're doing.
*/ */
static constexpr SocketDescriptor FromFileDescriptor(FileDescriptor fd) { static constexpr SocketDescriptor FromFileDescriptor(FileDescriptor fd) noexcept {
return SocketDescriptor(fd); return SocketDescriptor(fd);
} }
@ -72,7 +72,7 @@ public:
* same as file descriptors (i.e. not on Windows). Use this only * same as file descriptors (i.e. not on Windows). Use this only
* when you know what you're doing. * when you know what you're doing.
*/ */
constexpr const FileDescriptor &ToFileDescriptor() const { constexpr const FileDescriptor &ToFileDescriptor() const noexcept {
return *this; return *this;
} }
#endif #endif
@ -101,7 +101,7 @@ public:
using FileDescriptor::Steal; using FileDescriptor::Steal;
using FileDescriptor::SetUndefined; using FileDescriptor::SetUndefined;
static constexpr SocketDescriptor Undefined() { static constexpr SocketDescriptor Undefined() noexcept {
return SocketDescriptor(FileDescriptor::Undefined()); return SocketDescriptor(FileDescriptor::Undefined());
} }
@ -122,7 +122,7 @@ public:
* careful when dealing with a FileDescriptor reference that is * careful when dealing with a FileDescriptor reference that is
* really a SocketDescriptor. * really a SocketDescriptor.
*/ */
void Close(); void Close() noexcept;
#endif #endif
/** /**
@ -134,26 +134,29 @@ public:
* @return True on success, False on failure * @return True on success, False on failure
* See man 2 socket for detailed information * See man 2 socket for detailed information
*/ */
bool Create(int domain, int type, int protocol); bool Create(int domain, int type, int protocol) noexcept;
/** /**
* Like Create(), but enable non-blocking mode. * Like Create(), but enable non-blocking mode.
*/ */
bool CreateNonBlock(int domain, int type, int protocol); bool CreateNonBlock(int domain, int type, int protocol) noexcept;
#ifndef _WIN32 #ifndef _WIN32
static bool CreateSocketPair(int domain, int type, int protocol, static bool CreateSocketPair(int domain, int type, int protocol,
SocketDescriptor &a, SocketDescriptor &b); SocketDescriptor &a,
SocketDescriptor &b) noexcept;
static bool CreateSocketPairNonBlock(int domain, int type, int protocol, static bool CreateSocketPairNonBlock(int domain, int type, int protocol,
SocketDescriptor &a, SocketDescriptor &b); SocketDescriptor &a,
SocketDescriptor &b) noexcept;
#endif #endif
int GetError(); int GetError() noexcept;
/** /**
* @return the value size or 0 on error * @return the value size or 0 on error
*/ */
size_t GetOption(int level, int name, void *value, size_t size) const; size_t GetOption(int level, int name,
void *value, size_t size) const noexcept;
#ifdef HAVE_STRUCT_UCRED #ifdef HAVE_STRUCT_UCRED
/** /**
@ -164,62 +167,63 @@ public:
struct ucred GetPeerCredentials() const noexcept; struct ucred GetPeerCredentials() const noexcept;
#endif #endif
bool SetOption(int level, int name, const void *value, size_t size); bool SetOption(int level, int name,
const void *value, size_t size) noexcept;
bool SetBoolOption(int level, int name, bool _value) { bool SetBoolOption(int level, int name, bool _value) noexcept {
const int value = _value; const int value = _value;
return SetOption(level, name, &value, sizeof(value)); return SetOption(level, name, &value, sizeof(value));
} }
bool SetKeepAlive(bool value=true); bool SetKeepAlive(bool value=true) noexcept;
bool SetReuseAddress(bool value=true); bool SetReuseAddress(bool value=true) noexcept;
#ifdef __linux__ #ifdef __linux__
bool SetReusePort(bool value=true); bool SetReusePort(bool value=true) noexcept;
bool SetFreeBind(bool value=true); bool SetFreeBind(bool value=true) noexcept;
bool SetNoDelay(bool value=true); bool SetNoDelay(bool value=true) noexcept;
bool SetCork(bool value=true); bool SetCork(bool value=true) noexcept;
bool SetTcpDeferAccept(const int &seconds); bool SetTcpDeferAccept(const int &seconds) noexcept;
bool SetV6Only(bool value); bool SetV6Only(bool value) noexcept;
/** /**
* Setter for SO_BINDTODEVICE. * Setter for SO_BINDTODEVICE.
*/ */
bool SetBindToDevice(const char *name); bool SetBindToDevice(const char *name) noexcept;
bool SetTcpFastOpen(int qlen=16); bool SetTcpFastOpen(int qlen=16) noexcept;
#endif #endif
bool Bind(SocketAddress address); bool Bind(SocketAddress address) noexcept;
#ifdef __linux__ #ifdef __linux__
/** /**
* Binds the socket to a unique abstract address. * Binds the socket to a unique abstract address.
*/ */
bool AutoBind(); bool AutoBind() noexcept;
#endif #endif
bool Listen(int backlog); bool Listen(int backlog) noexcept;
SocketDescriptor Accept(); SocketDescriptor Accept() noexcept;
SocketDescriptor AcceptNonBlock() const; SocketDescriptor AcceptNonBlock() const noexcept;
SocketDescriptor AcceptNonBlock(StaticSocketAddress &address) const; SocketDescriptor AcceptNonBlock(StaticSocketAddress &address) const noexcept;
bool Connect(SocketAddress address); bool Connect(SocketAddress address) noexcept;
gcc_pure gcc_pure
StaticSocketAddress GetLocalAddress() const; StaticSocketAddress GetLocalAddress() const noexcept;
gcc_pure gcc_pure
StaticSocketAddress GetPeerAddress() const; StaticSocketAddress GetPeerAddress() const noexcept;
ssize_t Read(void *buffer, size_t length); ssize_t Read(void *buffer, size_t length) noexcept;
ssize_t Write(const void *buffer, size_t length); ssize_t Write(const void *buffer, size_t length) noexcept;
#ifdef _WIN32 #ifdef _WIN32
int WaitReadable(int timeout_ms) const; int WaitReadable(int timeout_ms) const noexcept;
int WaitWritable(int timeout_ms) const; int WaitWritable(int timeout_ms) const noexcept;
#else #else
using FileDescriptor::WaitReadable; using FileDescriptor::WaitReadable;
using FileDescriptor::WaitWritable; using FileDescriptor::WaitWritable;
@ -230,13 +234,13 @@ public:
* Receive a datagram and return the source address. * Receive a datagram and return the source address.
*/ */
ssize_t Read(void *buffer, size_t length, ssize_t Read(void *buffer, size_t length,
StaticSocketAddress &address); StaticSocketAddress &address) noexcept;
/** /**
* Send a datagram to the specified address. * Send a datagram to the specified address.
*/ */
ssize_t Write(const void *buffer, size_t length, ssize_t Write(const void *buffer, size_t length,
SocketAddress address); SocketAddress address) noexcept;
}; };
static_assert(std::is_trivial<SocketDescriptor>::value, "type is not trivial"); static_assert(std::is_trivial<SocketDescriptor>::value, "type is not trivial");