Skip to content

Commit

Permalink
Getters for socket buffer sizes
Browse files Browse the repository at this point in the history
Add get_so_rcv_buf and get_so_snd_buf functions for reading socket
buffer sizes.

Closes reactivemarkets#35
  • Loading branch information
markaylett committed Aug 29, 2019
1 parent efe3fcd commit 2b61a57
Showing 1 changed file with 51 additions and 7 deletions.
58 changes: 51 additions & 7 deletions toolbox/net/Socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -647,16 +647,36 @@ inline std::error_code get_so_error(int sockfd)
return make_sys_error(optval);
}

inline void set_so_reuse_addr(int sockfd, bool enabled, std::error_code& ec) noexcept
inline int get_so_rcv_buf(int sockfd, std::error_code& ec) noexcept
{
int optval{enabled ? 1 : 0};
os::setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval), ec);
int optval{};
socklen_t optlen{sizeof(optval)};
os::getsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &optval, optlen, ec);
return optval;
}

inline void set_so_reuse_addr(int sockfd, bool enabled)
inline int get_so_rcv_buf(int sockfd)
{
int optval{enabled ? 1 : 0};
os::setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
int optval{};
socklen_t optlen{sizeof(optval)};
os::getsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &optval, optlen);
return optval;
}

inline int get_so_snd_buf(int sockfd, std::error_code& ec) noexcept
{
int optval{};
socklen_t optlen{sizeof(optval)};
os::getsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &optval, optlen, ec);
return optval;
}

inline int get_so_snd_buf(int sockfd)
{
int optval{};
socklen_t optlen{sizeof(optval)};
os::getsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &optval, optlen);
return optval;
}

inline void set_so_rcv_buf(int sockfd, int size, std::error_code& ec) noexcept
Expand All @@ -669,6 +689,18 @@ inline void set_so_rcv_buf(int sockfd, int size)
os::setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
}

inline void set_so_reuse_addr(int sockfd, bool enabled, std::error_code& ec) noexcept
{
int optval{enabled ? 1 : 0};
os::setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval), ec);
}

inline void set_so_reuse_addr(int sockfd, bool enabled)
{
int optval{enabled ? 1 : 0};
os::setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
}

inline void set_so_snd_buf(int sockfd, int size, std::error_code& ec) noexcept
{
os::setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size), ec);
Expand Down Expand Up @@ -706,12 +738,24 @@ struct Sock {
bool is_ip_family() const noexcept { return family_ == AF_INET || family_ == AF_INET6; }

// Logically const.
std::error_code get_so_error(std::error_code& ec) const
std::error_code get_so_error(std::error_code& ec) const noexcept
{
return toolbox::get_so_error(*sock_, ec);
}
std::error_code get_so_error() const { return toolbox::get_so_error(*sock_); }

int get_so_rcv_buf(std::error_code& ec) const noexcept
{
return toolbox::get_so_rcv_buf(*sock_, ec);
}
int get_so_rcv_buf() const { return toolbox::get_so_rcv_buf(*sock_); }

int get_so_snd_buf(std::error_code& ec) const noexcept
{
return toolbox::get_so_snd_buf(*sock_, ec);
}
int get_so_snd_buf() const { return toolbox::get_so_snd_buf(*sock_); }

void close() { sock_.reset(); }

void set_non_block(std::error_code& ec) noexcept { toolbox::set_non_block(*sock_, ec); }
Expand Down

0 comments on commit 2b61a57

Please sign in to comment.