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

View File

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