io/FileDescriptor, net/SocketDescriptor: make lots of methods "const"

Only the file descriptor value itself is const, but the file itself
may be readable/writable.
This commit is contained in:
Max Kellermann 2022-10-24 15:07:52 +02:00 committed by Max Kellermann
parent 69596106d3
commit bbc82a9892
4 changed files with 89 additions and 86 deletions

View File

@ -185,7 +185,7 @@ FileDescriptor::CreatePipe(FileDescriptor &r, FileDescriptor &w) noexcept
#ifdef _WIN32 #ifdef _WIN32
void void
FileDescriptor::SetBinaryMode() noexcept FileDescriptor::SetBinaryMode() const noexcept
{ {
_setmode(fd, _O_BINARY); _setmode(fd, _O_BINARY);
} }
@ -209,7 +209,7 @@ FileDescriptor::CreatePipeNonBlock(FileDescriptor &r,
} }
void void
FileDescriptor::SetNonBlocking() noexcept FileDescriptor::SetNonBlocking() const noexcept
{ {
assert(IsDefined()); assert(IsDefined());
@ -218,7 +218,7 @@ FileDescriptor::SetNonBlocking() noexcept
} }
void void
FileDescriptor::SetBlocking() noexcept FileDescriptor::SetBlocking() const noexcept
{ {
assert(IsDefined()); assert(IsDefined());
@ -227,7 +227,7 @@ FileDescriptor::SetBlocking() noexcept
} }
void void
FileDescriptor::EnableCloseOnExec() noexcept FileDescriptor::EnableCloseOnExec() const noexcept
{ {
assert(IsDefined()); assert(IsDefined());
@ -236,7 +236,7 @@ FileDescriptor::EnableCloseOnExec() noexcept
} }
void void
FileDescriptor::DisableCloseOnExec() noexcept FileDescriptor::DisableCloseOnExec() const noexcept
{ {
assert(IsDefined()); assert(IsDefined());
@ -251,7 +251,7 @@ FileDescriptor::Duplicate() const noexcept
} }
bool bool
FileDescriptor::CheckDuplicate(FileDescriptor new_fd) noexcept FileDescriptor::CheckDuplicate(FileDescriptor new_fd) const noexcept
{ {
if (*this == new_fd) { if (*this == new_fd) {
DisableCloseOnExec(); DisableCloseOnExec();
@ -285,7 +285,7 @@ FileDescriptor::CreateSignalFD(const sigset_t *mask) noexcept
#endif #endif
bool bool
FileDescriptor::Rewind() noexcept FileDescriptor::Rewind() const noexcept
{ {
assert(IsDefined()); assert(IsDefined());
@ -302,7 +302,7 @@ FileDescriptor::GetSize() const noexcept
} }
void void
FileDescriptor::FullRead(void *_buffer, std::size_t length) FileDescriptor::FullRead(void *_buffer, std::size_t length) const
{ {
auto buffer = (std::byte *)_buffer; auto buffer = (std::byte *)_buffer;
@ -320,7 +320,7 @@ FileDescriptor::FullRead(void *_buffer, std::size_t length)
} }
void void
FileDescriptor::FullWrite(const void *_buffer, std::size_t length) FileDescriptor::FullWrite(const void *_buffer, std::size_t length) const
{ {
auto buffer = (const std::byte *)_buffer; auto buffer = (const std::byte *)_buffer;

View File

@ -151,36 +151,36 @@ public:
static bool CreatePipe(FileDescriptor &r, FileDescriptor &w) noexcept; static bool CreatePipe(FileDescriptor &r, FileDescriptor &w) noexcept;
#ifdef _WIN32 #ifdef _WIN32
void EnableCloseOnExec() noexcept {} void EnableCloseOnExec() const noexcept {}
void DisableCloseOnExec() noexcept {} void DisableCloseOnExec() const noexcept {}
void SetBinaryMode() noexcept; void SetBinaryMode() const noexcept;
#else #else
static bool CreatePipeNonBlock(FileDescriptor &r, static bool CreatePipeNonBlock(FileDescriptor &r,
FileDescriptor &w) noexcept; FileDescriptor &w) noexcept;
void SetBinaryMode() noexcept {} void SetBinaryMode() const noexcept {}
/** /**
* Enable non-blocking mode on this file descriptor. * Enable non-blocking mode on this file descriptor.
*/ */
void SetNonBlocking() noexcept; void SetNonBlocking() const noexcept;
/** /**
* Enable blocking mode on this file descriptor. * Enable blocking mode on this file descriptor.
*/ */
void SetBlocking() noexcept; void SetBlocking() const noexcept;
/** /**
* Auto-close this file descriptor when a new program is * Auto-close this file descriptor when a new program is
* executed. * executed.
*/ */
void EnableCloseOnExec() noexcept; void EnableCloseOnExec() const noexcept;
/** /**
* Do not auto-close this file descriptor when a new program * Do not auto-close this file descriptor when a new program
* is executed. * is executed.
*/ */
void DisableCloseOnExec() noexcept; void DisableCloseOnExec() const noexcept;
/** /**
* Duplicate this file descriptor. * Duplicate this file descriptor.
@ -203,7 +203,7 @@ public:
* this method to inject file descriptors into a new child * this method to inject file descriptors into a new child
* process, to be used by a newly executed program. * process, to be used by a newly executed program.
*/ */
bool CheckDuplicate(FileDescriptor new_fd) noexcept; bool CheckDuplicate(FileDescriptor new_fd) const noexcept;
#endif #endif
#ifdef __linux__ #ifdef __linux__
@ -223,13 +223,13 @@ public:
/** /**
* Rewind the pointer to the beginning of the file. * Rewind the pointer to the beginning of the file.
*/ */
bool Rewind() noexcept; bool Rewind() const noexcept;
off_t Seek(off_t offset) noexcept { off_t Seek(off_t offset) const noexcept {
return lseek(Get(), offset, SEEK_SET); return lseek(Get(), offset, SEEK_SET);
} }
off_t Skip(off_t offset) noexcept { off_t Skip(off_t offset) const noexcept {
return lseek(Get(), offset, SEEK_CUR); return lseek(Get(), offset, SEEK_CUR);
} }
@ -245,12 +245,13 @@ public:
off_t GetSize() const noexcept; off_t GetSize() const noexcept;
#ifndef _WIN32 #ifndef _WIN32
ssize_t ReadAt(off_t offset, void *buffer, std::size_t length) noexcept { ssize_t ReadAt(off_t offset,
void *buffer, std::size_t length) const noexcept {
return ::pread(fd, buffer, length, offset); return ::pread(fd, buffer, length, offset);
} }
#endif #endif
ssize_t Read(void *buffer, std::size_t length) noexcept { ssize_t Read(void *buffer, std::size_t length) const noexcept {
return ::read(fd, buffer, length); return ::read(fd, buffer, length);
} }
@ -258,9 +259,9 @@ public:
* Read until all of the given buffer has been filled. Throws * Read until all of the given buffer has been filled. Throws
* on error. * on error.
*/ */
void FullRead(void *buffer, std::size_t length); void FullRead(void *buffer, std::size_t length) const;
ssize_t Write(const void *buffer, std::size_t length) noexcept { ssize_t Write(const void *buffer, std::size_t length) const noexcept {
return ::write(fd, buffer, length); return ::write(fd, buffer, length);
} }
@ -268,7 +269,7 @@ public:
* Write until all of the given buffer has been written. * Write until all of the given buffer has been written.
* Throws on error. * Throws on error.
*/ */
void FullWrite(const void *buffer, std::size_t length); void FullWrite(const void *buffer, std::size_t length) const;
#ifndef _WIN32 #ifndef _WIN32
int Poll(short events, int timeout) const noexcept; int Poll(short events, int timeout) const noexcept;

View File

@ -78,7 +78,7 @@ SocketDescriptor::Close() noexcept
#endif #endif
SocketDescriptor SocketDescriptor
SocketDescriptor::Accept() noexcept SocketDescriptor::Accept() const noexcept
{ {
#ifdef __linux__ #ifdef __linux__
int connection_fd = ::accept4(Get(), nullptr, nullptr, SOCK_CLOEXEC); int connection_fd = ::accept4(Get(), nullptr, nullptr, SOCK_CLOEXEC);
@ -120,7 +120,7 @@ SocketDescriptor::AcceptNonBlock(StaticSocketAddress &address) const noexcept
} }
bool bool
SocketDescriptor::Connect(SocketAddress address) noexcept SocketDescriptor::Connect(SocketAddress address) const noexcept
{ {
assert(address.IsDefined()); assert(address.IsDefined());
@ -213,7 +213,7 @@ SocketDescriptor::CreateSocketPairNonBlock(int domain, int type, int protocol,
#endif #endif
int int
SocketDescriptor::GetError() noexcept SocketDescriptor::GetError() const noexcept
{ {
assert(IsDefined()); assert(IsDefined());
@ -254,7 +254,7 @@ SocketDescriptor::GetPeerCredentials() const noexcept
#ifdef _WIN32 #ifdef _WIN32
bool bool
SocketDescriptor::SetNonBlocking() noexcept SocketDescriptor::SetNonBlocking() const noexcept
{ {
u_long val = 1; u_long val = 1;
return ioctlsocket(fd, FIONBIO, &val) == 0; return ioctlsocket(fd, FIONBIO, &val) == 0;
@ -264,7 +264,7 @@ SocketDescriptor::SetNonBlocking() noexcept
bool bool
SocketDescriptor::SetOption(int level, int name, SocketDescriptor::SetOption(int level, int name,
const void *value, std::size_t size) noexcept const void *value, std::size_t size) const noexcept
{ {
assert(IsDefined()); assert(IsDefined());
@ -273,13 +273,13 @@ SocketDescriptor::SetOption(int level, int name,
} }
bool bool
SocketDescriptor::SetKeepAlive(bool value) noexcept SocketDescriptor::SetKeepAlive(bool value) const noexcept
{ {
return SetBoolOption(SOL_SOCKET, SO_KEEPALIVE, value); return SetBoolOption(SOL_SOCKET, SO_KEEPALIVE, value);
} }
bool bool
SocketDescriptor::SetReuseAddress(bool value) noexcept SocketDescriptor::SetReuseAddress(bool value) const noexcept
{ {
return SetBoolOption(SOL_SOCKET, SO_REUSEADDR, value); return SetBoolOption(SOL_SOCKET, SO_REUSEADDR, value);
} }
@ -287,50 +287,50 @@ SocketDescriptor::SetReuseAddress(bool value) noexcept
#ifdef __linux__ #ifdef __linux__
bool bool
SocketDescriptor::SetReusePort(bool value) noexcept SocketDescriptor::SetReusePort(bool value) const noexcept
{ {
return SetBoolOption(SOL_SOCKET, SO_REUSEPORT, value); return SetBoolOption(SOL_SOCKET, SO_REUSEPORT, value);
} }
bool bool
SocketDescriptor::SetFreeBind(bool value) noexcept SocketDescriptor::SetFreeBind(bool value) const noexcept
{ {
return SetBoolOption(IPPROTO_IP, IP_FREEBIND, value); return SetBoolOption(IPPROTO_IP, IP_FREEBIND, value);
} }
bool bool
SocketDescriptor::SetNoDelay(bool value) noexcept SocketDescriptor::SetNoDelay(bool value) const noexcept
{ {
return SetBoolOption(IPPROTO_TCP, TCP_NODELAY, value); return SetBoolOption(IPPROTO_TCP, TCP_NODELAY, value);
} }
bool bool
SocketDescriptor::SetCork(bool value) noexcept SocketDescriptor::SetCork(bool value) const noexcept
{ {
return SetBoolOption(IPPROTO_TCP, TCP_CORK, value); return SetBoolOption(IPPROTO_TCP, TCP_CORK, value);
} }
bool bool
SocketDescriptor::SetTcpDeferAccept(const int &seconds) noexcept SocketDescriptor::SetTcpDeferAccept(const int &seconds) const noexcept
{ {
return SetOption(IPPROTO_TCP, TCP_DEFER_ACCEPT, &seconds, sizeof(seconds)); return SetOption(IPPROTO_TCP, TCP_DEFER_ACCEPT, &seconds, sizeof(seconds));
} }
bool bool
SocketDescriptor::SetTcpUserTimeout(const unsigned &milliseconds) noexcept SocketDescriptor::SetTcpUserTimeout(const unsigned &milliseconds) const noexcept
{ {
return SetOption(IPPROTO_TCP, TCP_USER_TIMEOUT, return SetOption(IPPROTO_TCP, TCP_USER_TIMEOUT,
&milliseconds, sizeof(milliseconds)); &milliseconds, sizeof(milliseconds));
} }
bool bool
SocketDescriptor::SetV6Only(bool value) noexcept SocketDescriptor::SetV6Only(bool value) const noexcept
{ {
return SetBoolOption(IPPROTO_IPV6, IPV6_V6ONLY, value); return SetBoolOption(IPPROTO_IPV6, IPV6_V6ONLY, value);
} }
bool bool
SocketDescriptor::SetBindToDevice(const char *name) noexcept SocketDescriptor::SetBindToDevice(const char *name) const noexcept
{ {
return SetOption(SOL_SOCKET, SO_BINDTODEVICE, name, strlen(name)); return SetOption(SOL_SOCKET, SO_BINDTODEVICE, name, strlen(name));
} }
@ -338,7 +338,7 @@ SocketDescriptor::SetBindToDevice(const char *name) noexcept
#ifdef TCP_FASTOPEN #ifdef TCP_FASTOPEN
bool bool
SocketDescriptor::SetTcpFastOpen(int qlen) noexcept SocketDescriptor::SetTcpFastOpen(int qlen) const noexcept
{ {
return SetOption(SOL_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen)); return SetOption(SOL_TCP, TCP_FASTOPEN, &qlen, sizeof(qlen));
} }
@ -346,7 +346,7 @@ SocketDescriptor::SetTcpFastOpen(int qlen) noexcept
#endif #endif
bool bool
SocketDescriptor::AddMembership(const IPv4Address &address) noexcept SocketDescriptor::AddMembership(const IPv4Address &address) const noexcept
{ {
struct ip_mreq r{address.GetAddress(), IPv4Address(0).GetAddress()}; struct ip_mreq r{address.GetAddress(), IPv4Address(0).GetAddress()};
return setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, return setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
@ -354,7 +354,7 @@ SocketDescriptor::AddMembership(const IPv4Address &address) noexcept
} }
bool bool
SocketDescriptor::AddMembership(const IPv6Address &address) noexcept SocketDescriptor::AddMembership(const IPv6Address &address) const noexcept
{ {
struct ipv6_mreq r{address.GetAddress(), 0}; struct ipv6_mreq r{address.GetAddress(), 0};
r.ipv6mr_interface = address.GetScopeId(); r.ipv6mr_interface = address.GetScopeId();
@ -363,7 +363,7 @@ SocketDescriptor::AddMembership(const IPv6Address &address) noexcept
} }
bool bool
SocketDescriptor::AddMembership(SocketAddress address) noexcept SocketDescriptor::AddMembership(SocketAddress address) const noexcept
{ {
switch (address.GetFamily()) { switch (address.GetFamily()) {
case AF_INET: case AF_INET:
@ -381,7 +381,7 @@ SocketDescriptor::AddMembership(SocketAddress address) noexcept
#endif #endif
bool bool
SocketDescriptor::Bind(SocketAddress address) noexcept SocketDescriptor::Bind(SocketAddress address) const noexcept
{ {
return bind(Get(), address.GetAddress(), address.GetSize()) == 0; return bind(Get(), address.GetAddress(), address.GetSize()) == 0;
} }
@ -389,7 +389,7 @@ SocketDescriptor::Bind(SocketAddress address) noexcept
#ifdef __linux__ #ifdef __linux__
bool bool
SocketDescriptor::AutoBind() noexcept SocketDescriptor::AutoBind() const 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,
@ -399,7 +399,7 @@ SocketDescriptor::AutoBind() noexcept
#endif #endif
bool bool
SocketDescriptor::Listen(int backlog) noexcept SocketDescriptor::Listen(int backlog) const noexcept
{ {
return listen(Get(), backlog) == 0; return listen(Get(), backlog) == 0;
} }
@ -431,7 +431,7 @@ SocketDescriptor::GetPeerAddress() const noexcept
} }
ssize_t ssize_t
SocketDescriptor::Read(void *buffer, std::size_t length) noexcept SocketDescriptor::Read(void *buffer, std::size_t length) const noexcept
{ {
int flags = 0; int flags = 0;
#ifndef _WIN32 #ifndef _WIN32
@ -442,7 +442,7 @@ SocketDescriptor::Read(void *buffer, std::size_t length) noexcept
} }
ssize_t ssize_t
SocketDescriptor::Write(const void *buffer, std::size_t length) noexcept SocketDescriptor::Write(const void *buffer, std::size_t length) const noexcept
{ {
int flags = 0; int flags = 0;
#ifdef __linux__ #ifdef __linux__
@ -496,7 +496,7 @@ SocketDescriptor::WaitWritable(int timeout_ms) const noexcept
ssize_t ssize_t
SocketDescriptor::Read(void *buffer, std::size_t length, SocketDescriptor::Read(void *buffer, std::size_t length,
StaticSocketAddress &address) noexcept StaticSocketAddress &address) const noexcept
{ {
int flags = 0; int flags = 0;
#ifndef _WIN32 #ifndef _WIN32
@ -514,7 +514,7 @@ SocketDescriptor::Read(void *buffer, std::size_t length,
ssize_t ssize_t
SocketDescriptor::Write(const void *buffer, std::size_t length, SocketDescriptor::Write(const void *buffer, std::size_t length,
SocketAddress address) noexcept SocketAddress address) const noexcept
{ {
int flags = 0; int flags = 0;
#ifndef _WIN32 #ifndef _WIN32
@ -531,19 +531,19 @@ SocketDescriptor::Write(const void *buffer, std::size_t length,
#ifndef _WIN32 #ifndef _WIN32
void void
SocketDescriptor::Shutdown() noexcept SocketDescriptor::Shutdown() const noexcept
{ {
shutdown(Get(), SHUT_RDWR); shutdown(Get(), SHUT_RDWR);
} }
void void
SocketDescriptor::ShutdownRead() noexcept SocketDescriptor::ShutdownRead() const noexcept
{ {
shutdown(Get(), SHUT_RD); shutdown(Get(), SHUT_RD);
} }
void void
SocketDescriptor::ShutdownWrite() noexcept SocketDescriptor::ShutdownWrite() const noexcept
{ {
shutdown(Get(), SHUT_WR); shutdown(Get(), SHUT_WR);
} }

View File

@ -118,7 +118,7 @@ public:
using FileDescriptor::CheckDuplicate; using FileDescriptor::CheckDuplicate;
using FileDescriptor::Close; using FileDescriptor::Close;
#else #else
bool SetNonBlocking() noexcept; bool SetNonBlocking() const noexcept;
/** /**
* This method replaces FileDescriptor::Close(), using closesocket() * This method replaces FileDescriptor::Close(), using closesocket()
@ -154,7 +154,8 @@ public:
SocketDescriptor &b) noexcept; SocketDescriptor &b) noexcept;
#endif #endif
int GetError() noexcept; [[gnu::pure]]
int GetError() const noexcept;
/** /**
* @return the value size or 0 on error * @return the value size or 0 on error
@ -172,62 +173,63 @@ public:
#endif #endif
bool SetOption(int level, int name, bool SetOption(int level, int name,
const void *value, std::size_t size) noexcept; const void *value, std::size_t size) const noexcept;
bool SetIntOption(int level, int name, const int &value) noexcept { bool SetIntOption(int level, int name,
const int &value) const noexcept {
return SetOption(level, name, &value, sizeof(value)); return SetOption(level, name, &value, sizeof(value));
} }
bool SetBoolOption(int level, int name, bool value) noexcept { bool SetBoolOption(int level, int name, bool value) const noexcept {
return SetIntOption(level, name, value); return SetIntOption(level, name, value);
} }
bool SetKeepAlive(bool value=true) noexcept; bool SetKeepAlive(bool value=true) const noexcept;
bool SetReuseAddress(bool value=true) noexcept; bool SetReuseAddress(bool value=true) const noexcept;
#ifdef __linux__ #ifdef __linux__
bool SetReusePort(bool value=true) noexcept; bool SetReusePort(bool value=true) const noexcept;
bool SetFreeBind(bool value=true) noexcept; bool SetFreeBind(bool value=true) const noexcept;
bool SetNoDelay(bool value=true) noexcept; bool SetNoDelay(bool value=true) const noexcept;
bool SetCork(bool value=true) noexcept; bool SetCork(bool value=true) const noexcept;
bool SetTcpDeferAccept(const int &seconds) noexcept; bool SetTcpDeferAccept(const int &seconds) const noexcept;
/** /**
* Setter for TCP_USER_TIMEOUT. * Setter for TCP_USER_TIMEOUT.
*/ */
bool SetTcpUserTimeout(const unsigned &milliseconds) noexcept; bool SetTcpUserTimeout(const unsigned &milliseconds) const noexcept;
bool SetV6Only(bool value) noexcept; bool SetV6Only(bool value) const noexcept;
/** /**
* Setter for SO_BINDTODEVICE. * Setter for SO_BINDTODEVICE.
*/ */
bool SetBindToDevice(const char *name) noexcept; bool SetBindToDevice(const char *name) const noexcept;
bool SetTcpFastOpen(int qlen=16) noexcept; bool SetTcpFastOpen(int qlen=16) const noexcept;
bool AddMembership(const IPv4Address &address) noexcept; bool AddMembership(const IPv4Address &address) const noexcept;
bool AddMembership(const IPv6Address &address) noexcept; bool AddMembership(const IPv6Address &address) const noexcept;
bool AddMembership(SocketAddress address) noexcept; bool AddMembership(SocketAddress address) const noexcept;
#endif #endif
bool Bind(SocketAddress address) noexcept; bool Bind(SocketAddress address) const noexcept;
#ifdef __linux__ #ifdef __linux__
/** /**
* Binds the socket to a unique abstract address. * Binds the socket to a unique abstract address.
*/ */
bool AutoBind() noexcept; bool AutoBind() const noexcept;
#endif #endif
bool Listen(int backlog) noexcept; bool Listen(int backlog) const noexcept;
SocketDescriptor Accept() noexcept; SocketDescriptor Accept() const noexcept;
SocketDescriptor AcceptNonBlock() const noexcept; SocketDescriptor AcceptNonBlock() const noexcept;
SocketDescriptor AcceptNonBlock(StaticSocketAddress &address) const noexcept; SocketDescriptor AcceptNonBlock(StaticSocketAddress &address) const noexcept;
bool Connect(SocketAddress address) noexcept; bool Connect(SocketAddress address) const noexcept;
[[gnu::pure]] [[gnu::pure]]
StaticSocketAddress GetLocalAddress() const noexcept; StaticSocketAddress GetLocalAddress() const noexcept;
@ -235,8 +237,8 @@ public:
[[gnu::pure]] [[gnu::pure]]
StaticSocketAddress GetPeerAddress() const noexcept; StaticSocketAddress GetPeerAddress() const noexcept;
ssize_t Read(void *buffer, std::size_t length) noexcept; ssize_t Read(void *buffer, std::size_t length) const noexcept;
ssize_t Write(const void *buffer, std::size_t length) noexcept; ssize_t Write(const void *buffer, std::size_t length) const noexcept;
#ifdef _WIN32 #ifdef _WIN32
int WaitReadable(int timeout_ms) const noexcept; int WaitReadable(int timeout_ms) const noexcept;
@ -251,18 +253,18 @@ public:
* Receive a datagram and return the source address. * Receive a datagram and return the source address.
*/ */
ssize_t Read(void *buffer, std::size_t length, ssize_t Read(void *buffer, std::size_t length,
StaticSocketAddress &address) noexcept; StaticSocketAddress &address) const noexcept;
/** /**
* Send a datagram to the specified address. * Send a datagram to the specified address.
*/ */
ssize_t Write(const void *buffer, std::size_t length, ssize_t Write(const void *buffer, std::size_t length,
SocketAddress address) noexcept; SocketAddress address) const noexcept;
#ifndef _WIN32 #ifndef _WIN32
void Shutdown() noexcept; void Shutdown() const noexcept;
void ShutdownRead() noexcept; void ShutdownRead() const noexcept;
void ShutdownWrite() noexcept; void ShutdownWrite() const noexcept;
#endif #endif
}; };