mirror of
https://github.com/fpagliughi/sockpp.git
synced 2026-01-12 00:04:45 +08:00
Fixed the streaming create_handle() to return a result.
This commit is contained in:
@@ -76,9 +76,12 @@ protected:
|
||||
* classified (derived from) a streaming socket, since it doesn't
|
||||
* support read and write to the socket.
|
||||
* @param domain The communications domain (address family).
|
||||
* @return An OS handle to a stream socket.
|
||||
* @return An OS handle to a stream socket on success, or an error code
|
||||
* on failure.
|
||||
*/
|
||||
static socket_t create_handle(int domain) { return stream_socket::create_handle(domain); }
|
||||
static result<socket_t> create_handle(int domain) {
|
||||
return stream_socket::create_handle(domain);
|
||||
}
|
||||
|
||||
public:
|
||||
/** The default listener queue size. */
|
||||
@@ -292,9 +295,7 @@ public:
|
||||
* value of zero doesn;t set an option.
|
||||
* @return @em true on success, @em false on error
|
||||
*/
|
||||
result<> open(
|
||||
const addr_t& addr, int queSize = DFLT_QUE_SIZE, int reuse = 0
|
||||
) noexcept {
|
||||
result<> open(const addr_t& addr, int queSize = DFLT_QUE_SIZE, int reuse = 0) noexcept {
|
||||
return base::open(addr, queSize, reuse);
|
||||
}
|
||||
/**
|
||||
@@ -307,9 +308,7 @@ public:
|
||||
* value of zero doesn;t set an option.
|
||||
* @return @em true on success, @em false on error
|
||||
*/
|
||||
result<> open(
|
||||
in_port_t port, int queSize = DFLT_QUE_SIZE, int reuse = 0
|
||||
) noexcept {
|
||||
result<> open(in_port_t port, int queSize = DFLT_QUE_SIZE, int reuse = 0) noexcept {
|
||||
return open(addr_t(port), queSize, reuse);
|
||||
}
|
||||
/**
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
// --------------------------------------------------------------------------
|
||||
// This file is part of the "sockpp" C++ socket library.
|
||||
//
|
||||
// Copyright (c) 2014-2019 Frank Pagliughi
|
||||
// Copyright (c) 2014-2023 Frank Pagliughi
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
@@ -71,7 +71,7 @@ class connector : public stream_socket
|
||||
connector& operator=(const connector&) = delete;
|
||||
|
||||
/** Recreate the socket with a new handle, closing any old one. */
|
||||
result<none> recreate(const sock_address& addr);
|
||||
result<> recreate(const sock_address& addr);
|
||||
|
||||
public:
|
||||
/**
|
||||
@@ -161,34 +161,24 @@ public:
|
||||
base::operator=(std::move(rhs));
|
||||
return *this;
|
||||
}
|
||||
/**
|
||||
* Determines if the socket connected to a remote host.
|
||||
* Note that this is not a reliable determination if the socket is
|
||||
* currently connected, but rather that an initial connection was
|
||||
* established.
|
||||
* @return @em true If the socket connected to a remote host,
|
||||
* @em false if not.
|
||||
*/
|
||||
bool is_connected() const { return is_open(); }
|
||||
/**
|
||||
* Attempts to connect to the specified server.
|
||||
* If the socket is currently connected, this will close the current
|
||||
* connection and open the new one.
|
||||
* @param addr The remote server address.
|
||||
* @return @em true on success, @em false on error
|
||||
* @return The error code on failure.
|
||||
*/
|
||||
result<none> connect(const sock_address& addr);
|
||||
result<> connect(const sock_address& addr);
|
||||
/**
|
||||
* Attempts to connect to the specified server, with a timeout.
|
||||
* If the socket is currently connected, this will close the current
|
||||
* connection and open the new one.
|
||||
* If the operation times out, the @ref last_error will be set to
|
||||
* `timed_out`.
|
||||
* If the operation times out, the @ref error will be `errc::timed_out`.
|
||||
* @param addr The remote server address.
|
||||
* @param timeout The duration after which to give up. Zero means never.
|
||||
* @return @em true on success, @em false on error
|
||||
* @return The error code on failure.
|
||||
*/
|
||||
result<none> connect(const sock_address& addr, microseconds timeout);
|
||||
result<> connect(const sock_address& addr, microseconds timeout);
|
||||
/**
|
||||
* Attempts to connect to the specified server, with a timeout.
|
||||
* If the socket is currently connected, this will close the current
|
||||
@@ -197,10 +187,10 @@ public:
|
||||
* `timed_out`.
|
||||
* @param addr The remote server address.
|
||||
* @param relTime The duration after which to give up. Zero means never.
|
||||
* @return @em true on success, @em false on error
|
||||
* @return The error code on failure
|
||||
*/
|
||||
template <class Rep, class Period>
|
||||
result<none> connect(const sock_address& addr, const duration<Rep, Period>& relTime) {
|
||||
result<> connect(const sock_address& addr, const duration<Rep, Period>& relTime) {
|
||||
return connect(addr, microseconds(relTime));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
// --------------------------------------------------------------------------
|
||||
// This file is part of the "sockpp" C++ socket library.
|
||||
//
|
||||
// Copyright (c) 2014-2019 Frank Pagliughi
|
||||
// Copyright (c) 2014-2023 Frank Pagliughi
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
@@ -244,13 +244,13 @@ public:
|
||||
* destroyed.
|
||||
* @param h An OS socket handle.
|
||||
*/
|
||||
explicit socket(socket_t h) noexcept : handle_(h) {}
|
||||
explicit socket(socket_t h) noexcept : handle_{h} {}
|
||||
/**
|
||||
* Move constructor.
|
||||
* This takes ownership of the underlying handle in sock.
|
||||
* @param sock An rvalue reference to a socket object.
|
||||
*/
|
||||
socket(socket&& sock) noexcept : handle_(sock.handle_) { sock.handle_ = INVALID_SOCKET; }
|
||||
socket(socket&& sock) noexcept : handle_{sock.handle_} { sock.handle_ = INVALID_SOCKET; }
|
||||
/**
|
||||
* Destructor closes the socket.
|
||||
*/
|
||||
@@ -584,16 +584,6 @@ public:
|
||||
result<> send_buffer_size(unsigned int sz) noexcept {
|
||||
return set_option<unsigned int>(SOL_SOCKET, SO_SNDBUF, sz);
|
||||
}
|
||||
/**
|
||||
* Gets a string describing the specified error.
|
||||
* This is typically the returned message from the system strerror().
|
||||
* @param errNum The error number.
|
||||
* @return A string describing the specified error.
|
||||
*/
|
||||
static string error_str(int errNum) {
|
||||
auto ec = error_code{errNum, std::system_category()};
|
||||
return ec.message();
|
||||
}
|
||||
/**
|
||||
* Shuts down all or part of the full-duplex connection.
|
||||
* @param how Which part of the connection should be shut:
|
||||
|
||||
@@ -74,8 +74,8 @@ protected:
|
||||
* Creates a streaming socket.
|
||||
* @return An OS handle to a stream socket.
|
||||
*/
|
||||
static socket_t create_handle(int domain, int protocol = 0) {
|
||||
return (socket_t)::socket(domain, COMM_TYPE, protocol);
|
||||
static result<socket_t> create_handle(int domain, int protocol = 0) {
|
||||
return base::create_handle(domain, COMM_TYPE, protocol);
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
@@ -45,10 +45,10 @@ namespace sockpp {
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
result<acceptor> acceptor::create(int domain) noexcept {
|
||||
acceptor acc(create_handle(domain));
|
||||
if (!acc)
|
||||
return result<acceptor>::from_last_error();
|
||||
return acc;
|
||||
if (auto res = create_handle(domain); !res)
|
||||
return res.error();
|
||||
else
|
||||
return acceptor(res.value());
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
@@ -66,13 +66,11 @@ result<> acceptor::open(
|
||||
if (is_open())
|
||||
return none{};
|
||||
|
||||
sa_family_t domain = addr.family();
|
||||
socket_t h = create_handle(domain);
|
||||
if (auto res = create_handle(addr.family()); !res)
|
||||
return res.error();
|
||||
else
|
||||
reset(res.value());
|
||||
|
||||
if (h == INVALID_SOCKET)
|
||||
return result<>::from_last_error();
|
||||
|
||||
reset(h);
|
||||
if (auto res = bind(addr, reuse); !res) {
|
||||
close();
|
||||
return res;
|
||||
|
||||
@@ -51,15 +51,13 @@ namespace sockpp {
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
result<> connector::recreate(const sock_address& addr) {
|
||||
sa_family_t domain = addr.family();
|
||||
socket_t h = create_handle(domain);
|
||||
|
||||
if (h < 0)
|
||||
return result<>::from_last_error();
|
||||
|
||||
// This will close the old connection, if any.
|
||||
reset(h);
|
||||
return none{};
|
||||
if (auto res = create_handle(addr.family()); !res)
|
||||
return res.error();
|
||||
else {
|
||||
// This will close the old connection, if any.
|
||||
reset(res.value());
|
||||
return none{};
|
||||
}
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@@ -188,7 +188,7 @@ void socket::reset(socket_t h /*=INVALID_SOCKET*/) noexcept {
|
||||
if (h != handle_) {
|
||||
std::swap(h, handle_);
|
||||
if (h != INVALID_SOCKET)
|
||||
::close(h);
|
||||
close(h);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -50,10 +50,10 @@ namespace sockpp {
|
||||
// Creates a stream socket for the given domain/protocol.
|
||||
|
||||
result<stream_socket> stream_socket::create(int domain, int protocol /*=0*/) {
|
||||
stream_socket sock(create_handle(domain, protocol));
|
||||
if (!sock)
|
||||
return result<stream_socket>::from_last_error();
|
||||
return sock;
|
||||
if (auto res = create_handle(domain, protocol); !res)
|
||||
return res.error();
|
||||
else
|
||||
return stream_socket{res.value()};
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user