47 #ifndef __sockpp_socket_h 48 #define __sockpp_socket_h 59 #if !defined(SOCKPP_SOCKET_T_DEFINED) 62 #define SOCKPP_SOCKET_T_DEFINED 66 timeval to_timeval(
const std::chrono::microseconds& dur);
68 template<
class Rep,
class Period>
69 timeval to_timeval(
const std::chrono::duration<Rep,Period>& dur) {
70 return to_timeval(std::chrono::duration_cast<std::chrono::microseconds>(dur));
95 bool close(socket_t h);
136 template <
typename T>
148 template <
typename T>
176 return ret != INVALID_SOCKET;
183 socket() : handle_(INVALID_SOCKET), lastErr_(0) {}
190 explicit socket(socket_t h) : handle_(h), lastErr_(0) {}
197 : handle_(sock.handle_), lastErr_(sock.lastErr_) {
198 sock.handle_ = INVALID_SOCKET;
233 static socket create(
int domain,
int type,
int protocol=0);
238 bool is_open()
const {
return handle_ != INVALID_SOCKET; }
245 return handle_ == INVALID_SOCKET || lastErr_ != 0;
252 explicit operator bool()
const {
253 return handle_ != INVALID_SOCKET && lastErr_ == 0;
259 socket_t
handle()
const {
return handle_; }
301 static std::tuple<socket, socket>
pair(
int domain,
int type,
int protocol=0);
306 void clear(
int val=0) { lastErr_ = val; }
312 socket_t h = handle_;
313 handle_ = INVALID_SOCKET;
320 void reset(socket_t h=INVALID_SOCKET);
329 std::swap(handle_, sock.handle_);
330 lastErr_ = sock.lastErr_;
364 bool get_option(
int level,
int optname,
void* optval, socklen_t* optlen)
const;
375 template <
typename T>
377 socklen_t len =
sizeof(T);
378 return get_option(level, optname, (
void*) val, &len);
394 bool set_option(
int level,
int optname,
const void* optval, socklen_t optlen);
406 template <
typename T>
408 return set_option(level, optname, (
void*) &val,
sizeof(T));
425 static std::string
error_str(
int errNum);
481 #endif // __sockpp_socket_h Generic socket address.
Definition: sock_address.h:64
static std::tuple< socket, socket > pair(int domain, int type, int protocol=0)
Creates a pair of connected sockets.
sock_address_any address() const
Gets the local address to which the socket is bound.
T check_ret(T ret) const
Checks the value and if less than zero, sets last error.
Definition: socket.h:137
bool get_option(int level, int optname, T *val) const
Gets the value of a socket option.
Definition: socket.h:376
void set_last_error()
Cache the last system error code into this object.
Definition: socket.h:127
Generic socket address.
Definition: sock_address.h:108
static void destroy()
Shuts down the socket library.
socket(socket_t h)
Creates a socket from an existing OS socket handle.
Definition: socket.h:190
bool close_on_err()
Closes the socket without checking for errors or updating the last error.
Definition: socket.h:111
virtual sa_family_t family() const
Gets the network family of the address to which the socket is bound.
Definition: socket.h:266
void clear(int val=0)
Clears the error flag for the object.
Definition: socket.h:306
bool set_option(int level, int optname, const T &val)
Sets the value of a socket option.
Definition: socket.h:407
bool operator!() const
Determines if the socket is closed or in an error state.
Definition: socket.h:244
socket(socket &&sock) noexcept
Move constructor.
Definition: socket.h:196
bool set_option(int level, int optname, const void *optval, socklen_t optlen)
Sets the value of a socket option.
virtual ~socket()
Destructor closes the socket.
Definition: socket.h:203
bool bind(const sock_address &addr)
Binds the socket to the specified address.
bool check_ret_bool(T ret) const
Checks the value and if less than zero, sets last error.
Definition: socket.h:149
socket & operator=(socket &&sock) noexcept
Move assignment.
Definition: socket.h:327
bool set_non_blocking(bool on=true)
Places the socket into or out of non-blocking mode.
bool is_open() const
Determines if the socket is open (valid).
Definition: socket.h:238
socket()
Creates an unconnected (invalid) socket.
Definition: socket.h:183
bool close()
Closes the socket.
Definition: acceptor.h:51
socket clone() const
Creates a new socket that refers to this one.
static socket create(int domain, int type, int protocol=0)
Creates a socket with the specified communications characterics.
int last_error() const
Gets the code for the last errror.
Definition: socket.h:431
socket_t check_socket(socket_t ret) const
Checks the value and if it is not a valid socket, sets last error.
Definition: socket.h:161
static std::string error_str(int errNum)
Gets a string describing the specified error.
Base class for socket objects.
Definition: socket.h:84
bool shutdown(int how=SHUT_RDWR)
Shuts down all or part of the full-duplex connection.
static int get_last_error()
OS-specific means to retrieve the last error from an operation.
std::string last_error_str() const
Gets a string describing the last errror.
Definition: socket.h:437
int socket_t
The OS socket handle.
Definition: socket.h:60
void reset(socket_t h=INVALID_SOCKET)
Replaces the underlying managed socket object.
sock_address_any peer_address() const
Gets the address of the remote peer, if this socket is connected.
socket_t release()
Releases ownership of the underlying socket object.
Definition: socket.h:311
RAII class to initialize and then shut down the library.
Definition: socket.h:470
virtual sa_family_t family() const
Gets the network family of the address.
Definition: sock_address.h:93
socket_t handle() const
Get the underlying OS socket handle.
Definition: socket.h:259
bool get_option(int level, int optname, void *optval, socklen_t *optlen) const
Gets the value of a socket option.
static void initialize()
Initializes the socket (sockpp) library.
Generic address class for sockpp.
const socket_t INVALID_SOCKET
Invalid socket descriptor.
Definition: socket.h:61
bool check_socket_bool(socket_t ret) const
Checks the value and if it is INVALID_SOCKET, sets last error.
Definition: socket.h:174