mirror of
https://github.com/fpagliughi/sockpp.git
synced 2026-01-12 00:04:45 +08:00
connector bool functions now return result<none>
This commit is contained in:
@@ -70,7 +70,7 @@ class connector : public stream_socket
|
||||
connector& operator=(const connector&) = delete;
|
||||
|
||||
/** Recreate the socket with a new handle, closing any old one. */
|
||||
bool recreate(const sock_address& addr);
|
||||
result<none> recreate(const sock_address& addr);
|
||||
|
||||
public:
|
||||
/**
|
||||
@@ -133,7 +133,7 @@ public:
|
||||
* @param addr The remote server address.
|
||||
* @return @em true on success, @em false on error
|
||||
*/
|
||||
bool connect(const sock_address& addr);
|
||||
result<none> 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
|
||||
@@ -144,7 +144,7 @@ public:
|
||||
* @param timeout The duration after which to give up. Zero means never.
|
||||
* @return @em true on success, @em false on error
|
||||
*/
|
||||
bool connect(const sock_address& addr, std::chrono::microseconds timeout);
|
||||
result<none> connect(const sock_address& addr, std::chrono::microseconds timeout);
|
||||
/**
|
||||
* Attempts to connect to the specified server, with a timeout.
|
||||
* If the socket is currently connected, this will close the current
|
||||
@@ -156,7 +156,7 @@ public:
|
||||
* @return @em true on success, @em false on error
|
||||
*/
|
||||
template <class Rep, class Period>
|
||||
bool connect(
|
||||
result<none> connect(
|
||||
const sock_address& addr, const std::chrono::duration<Rep, Period>& relTime
|
||||
) {
|
||||
return connect(addr, std::chrono::microseconds(relTime));
|
||||
|
||||
@@ -122,9 +122,14 @@ public:
|
||||
result() = default;
|
||||
/**
|
||||
* Construct a "success" result with the specified value.
|
||||
* @param val The success return value
|
||||
* @param val The success value
|
||||
*/
|
||||
result(const T& val) : val_{val} {}
|
||||
/**
|
||||
* Construct a "success" result with the specified value.
|
||||
* @param val The success value
|
||||
*/
|
||||
result(T&& val) : val_{std::move(val)} {}
|
||||
/**
|
||||
* Creates an unsuccesful result from a portable error condition.
|
||||
* @param err The error
|
||||
@@ -227,6 +232,17 @@ result<T> success(const T& val) {
|
||||
return result<T>(val);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a successful result with the specified value.
|
||||
*
|
||||
* @param val The succesful return value from the operation.
|
||||
* @return A success result.
|
||||
*/
|
||||
template <typename T>
|
||||
result<T> success(T&& val) {
|
||||
return result<T>(std::move(val));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a failed result with the specified error code.
|
||||
*
|
||||
|
||||
@@ -50,38 +50,40 @@ namespace sockpp {
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
bool connector::recreate(const sock_address& addr) {
|
||||
result<none> connector::recreate(const sock_address& addr) {
|
||||
sa_family_t domain = addr.family();
|
||||
socket_t h = create_handle(domain);
|
||||
|
||||
if (!check_socket_bool(h))
|
||||
return false;
|
||||
return last_error();
|
||||
|
||||
// This will close the old connection, if any.
|
||||
reset(h);
|
||||
return true;
|
||||
return none{};
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
bool connector::connect(const sock_address& addr) {
|
||||
if (!recreate(addr))
|
||||
return false;
|
||||
result<none> connector::connect(const sock_address& addr) {
|
||||
auto res = recreate(addr);
|
||||
if (!res)
|
||||
return res;
|
||||
|
||||
if (!check_ret_bool(::connect(handle(), addr.sockaddr_ptr(), addr.size())))
|
||||
return close_on_err();
|
||||
return last_error();
|
||||
|
||||
return true;
|
||||
return none{};
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
bool connector::connect(const sock_address& addr, microseconds timeout) {
|
||||
result<none> connector::connect(const sock_address& addr, microseconds timeout) {
|
||||
if (timeout.count() <= 0)
|
||||
return connect(addr);
|
||||
|
||||
if (!recreate(addr))
|
||||
return false;
|
||||
auto res = recreate(addr);
|
||||
if (!res)
|
||||
return res;
|
||||
|
||||
bool non_blocking =
|
||||
#if defined(_WIN32)
|
||||
@@ -126,9 +128,10 @@ bool connector::connect(const sock_address& addr, microseconds timeout) {
|
||||
}
|
||||
}
|
||||
|
||||
if (last_error()) {
|
||||
auto last_err = last_error();
|
||||
if (last_err) {
|
||||
close();
|
||||
return false;
|
||||
return last_err;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -136,7 +139,7 @@ bool connector::connect(const sock_address& addr, microseconds timeout) {
|
||||
if (!non_blocking)
|
||||
set_non_blocking(false);
|
||||
|
||||
return true;
|
||||
return none{};
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
Reference in New Issue
Block a user