Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions include/sockpp/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,29 @@ class socket
lastErr_ = (ret < 0) ? get_last_error() : 0;
return ret >= 0;
}
/**
* Checks the value and if it is not a valid socket, sets last error
* @tparam T A signed integer type of any size
* @param ret The return value from a library or system call.
* @return Returns the value sent to it, `ret`.
*/
template <typename T>
T check_socket(T ret) const {
lastErr_ = (ret == INVALID_SOCKET) ? get_last_error() : 0;
return ret;
}
/**
* Checks the value and if it is INVALID_SOCKET, sets last error.
* @tparam T A signed integer type of any size
* @param ret The return value from a library or system call.
* @return @em true if the value is a valid socket (not INVALID_SOCKET)
* or @em false is is an error (INVALID_SOCKET)
*/
template <typename T>
bool check_socket_bool(T ret) const{
lastErr_ = (ret == INVALID_SOCKET) ? get_last_error() : 0;
return ret != INVALID_SOCKET;
}

public:
/**
Expand Down
4 changes: 2 additions & 2 deletions src/acceptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ bool acceptor::open(const sock_address& addr, int queSize /*=DFLT_QUE_SIZE*/)
}

socket_t h = stream_socket::create_handle(domain);
if (!check_ret_bool(h))
if (!check_socket_bool(h))
return false;

reset(h);
Expand Down Expand Up @@ -93,7 +93,7 @@ stream_socket acceptor::accept(sock_address* clientAddr /*=nullptr*/)
sockaddr* p = clientAddr ? clientAddr->sockaddr_ptr() : nullptr;
socklen_t len = clientAddr ? clientAddr->size() : 0;

socket_t s = check_ret(::accept(handle(), p, &len));
socket_t s = check_socket(::accept(handle(), p, clientAddr ? &len : nullptr));
return stream_socket(s);
}

Expand Down
2 changes: 1 addition & 1 deletion src/datagram_socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ datagram_socket::datagram_socket(const sock_address& addr)
}

socket_t h = create_handle(domain);
if (!check_ret_bool(h))
if (!check_socket_bool(h))
return;

reset(h);
Expand Down