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

View File

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

View File

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

View File

@ -118,7 +118,7 @@ public:
using FileDescriptor::CheckDuplicate;
using FileDescriptor::Close;
#else
bool SetNonBlocking() noexcept;
bool SetNonBlocking() const noexcept;
/**
* This method replaces FileDescriptor::Close(), using closesocket()
@ -154,7 +154,8 @@ public:
SocketDescriptor &b) noexcept;
#endif
int GetError() noexcept;
[[gnu::pure]]
int GetError() const noexcept;
/**
* @return the value size or 0 on error
@ -172,62 +173,63 @@ public:
#endif
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));
}
bool SetBoolOption(int level, int name, bool value) noexcept {
bool SetBoolOption(int level, int name, bool value) const noexcept {
return SetIntOption(level, name, value);
}
bool SetKeepAlive(bool value=true) noexcept;
bool SetReuseAddress(bool value=true) noexcept;
bool SetKeepAlive(bool value=true) const noexcept;
bool SetReuseAddress(bool value=true) const noexcept;
#ifdef __linux__
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 SetReusePort(bool value=true) const noexcept;
bool SetFreeBind(bool value=true) const noexcept;
bool SetNoDelay(bool value=true) const 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.
*/
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.
*/
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 IPv6Address &address) noexcept;
bool AddMembership(SocketAddress address) noexcept;
bool AddMembership(const IPv4Address &address) const noexcept;
bool AddMembership(const IPv6Address &address) const noexcept;
bool AddMembership(SocketAddress address) const noexcept;
#endif
bool Bind(SocketAddress address) noexcept;
bool Bind(SocketAddress address) const noexcept;
#ifdef __linux__
/**
* Binds the socket to a unique abstract address.
*/
bool AutoBind() noexcept;
bool AutoBind() const noexcept;
#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(StaticSocketAddress &address) const noexcept;
bool Connect(SocketAddress address) noexcept;
bool Connect(SocketAddress address) const noexcept;
[[gnu::pure]]
StaticSocketAddress GetLocalAddress() const noexcept;
@ -235,8 +237,8 @@ public:
[[gnu::pure]]
StaticSocketAddress GetPeerAddress() const noexcept;
ssize_t Read(void *buffer, std::size_t length) noexcept;
ssize_t Write(const 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) const noexcept;
#ifdef _WIN32
int WaitReadable(int timeout_ms) const noexcept;
@ -251,18 +253,18 @@ public:
* Receive a datagram and return the source address.
*/
ssize_t Read(void *buffer, std::size_t length,
StaticSocketAddress &address) noexcept;
StaticSocketAddress &address) const noexcept;
/**
* Send a datagram to the specified address.
*/
ssize_t Write(const void *buffer, std::size_t length,
SocketAddress address) noexcept;
SocketAddress address) const noexcept;
#ifndef _WIN32
void Shutdown() noexcept;
void ShutdownRead() noexcept;
void ShutdownWrite() noexcept;
void Shutdown() const noexcept;
void ShutdownRead() const noexcept;
void ShutdownWrite() const noexcept;
#endif
};