mirror of
https://github.com/zeromq/libzmq.git
synced 2025-03-10 16:06:09 +00:00
Problem: inconsistent error handling and unnecessary code duplication
Solution: make error handling consistent and use retired_fd to remove code duplication
This commit is contained in:
parent
d02ba13576
commit
c432aada84
11
src/ip.cpp
11
src/ip.cpp
@ -76,14 +76,13 @@ zmq::fd_t zmq::open_socket (int domain_, int type_, int protocol_)
|
|||||||
type_ |= SOCK_CLOEXEC;
|
type_ |= SOCK_CLOEXEC;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
fd_t s = socket (domain_, type_, protocol_);
|
const fd_t s = socket (domain_, type_, protocol_);
|
||||||
|
if (s == retired_fd) {
|
||||||
#ifdef ZMQ_HAVE_WINDOWS
|
#ifdef ZMQ_HAVE_WINDOWS
|
||||||
if (s == INVALID_SOCKET)
|
errno = wsa_error_to_errno (WSAGetLastError ());
|
||||||
return INVALID_SOCKET;
|
|
||||||
#else
|
|
||||||
if (s == -1)
|
|
||||||
return -1;
|
|
||||||
#endif
|
#endif
|
||||||
|
return retired_fd;
|
||||||
|
}
|
||||||
|
|
||||||
// If there's no SOCK_CLOEXEC, let's try the second best option. Note that
|
// If there's no SOCK_CLOEXEC, let's try the second best option. Note that
|
||||||
// race condition can cause socket not to be closed (if fork happens
|
// race condition can cause socket not to be closed (if fork happens
|
||||||
|
@ -309,13 +309,8 @@ int zmq::socks_connecter_t::connect_to_proxy ()
|
|||||||
|
|
||||||
// Create the socket.
|
// Create the socket.
|
||||||
s = open_socket (tcp_addr->family (), SOCK_STREAM, IPPROTO_TCP);
|
s = open_socket (tcp_addr->family (), SOCK_STREAM, IPPROTO_TCP);
|
||||||
#ifdef ZMQ_HAVE_WINDOWS
|
if (s == retired_fd)
|
||||||
if (s == INVALID_SOCKET)
|
|
||||||
return -1;
|
return -1;
|
||||||
#else
|
|
||||||
if (s == -1)
|
|
||||||
return -1;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// On some systems, IPv4 mapping in IPv6 sockets is disabled by default.
|
// On some systems, IPv4 mapping in IPv6 sockets is disabled by default.
|
||||||
// Switch it on in such cases.
|
// Switch it on in such cases.
|
||||||
|
@ -279,15 +279,9 @@ int zmq::tcp_connecter_t::open ()
|
|||||||
s = open_socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
s = open_socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef ZMQ_HAVE_WINDOWS
|
if (s == retired_fd) {
|
||||||
if (s == INVALID_SOCKET) {
|
|
||||||
errno = wsa_error_to_errno (WSAGetLastError ());
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
#else
|
|
||||||
if (s == -1)
|
|
||||||
return -1;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// On some systems, IPv4 mapping in IPv6 sockets is disabled by default.
|
// On some systems, IPv4 mapping in IPv6 sockets is disabled by default.
|
||||||
// Switch it on in such cases.
|
// Switch it on in such cases.
|
||||||
|
@ -195,20 +195,15 @@ int zmq::tcp_listener_t::set_address (const char *addr_)
|
|||||||
s = open_socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
s = open_socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef ZMQ_HAVE_WINDOWS
|
if (s == retired_fd) {
|
||||||
if (s == INVALID_SOCKET) {
|
|
||||||
errno = wsa_error_to_errno (WSAGetLastError ());
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
#if !defined _WIN32_WCE && !defined ZMQ_HAVE_WINDOWS_UWP
|
#if defined ZMQ_HAVE_WINDOWS && !defined _WIN32_WCE \
|
||||||
|
&& !defined ZMQ_HAVE_WINDOWS_UWP
|
||||||
// On Windows, preventing sockets to be inherited by child processes.
|
// On Windows, preventing sockets to be inherited by child processes.
|
||||||
BOOL brc = SetHandleInformation ((HANDLE) s, HANDLE_FLAG_INHERIT, 0);
|
BOOL brc = SetHandleInformation ((HANDLE) s, HANDLE_FLAG_INHERIT, 0);
|
||||||
win_assert (brc);
|
win_assert (brc);
|
||||||
#endif
|
#endif
|
||||||
#else
|
|
||||||
if (s == -1)
|
|
||||||
return -1;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// On some systems, IPv4 mapping in IPv6 sockets is disabled by default.
|
// On some systems, IPv4 mapping in IPv6 sockets is disabled by default.
|
||||||
// Switch it on in such cases.
|
// Switch it on in such cases.
|
||||||
@ -306,26 +301,25 @@ zmq::fd_t zmq::tcp_listener_t::accept ()
|
|||||||
::accept (s, reinterpret_cast<struct sockaddr *> (&ss), &ss_len);
|
::accept (s, reinterpret_cast<struct sockaddr *> (&ss), &ss_len);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
if (sock == retired_fd) {
|
||||||
#ifdef ZMQ_HAVE_WINDOWS
|
#ifdef ZMQ_HAVE_WINDOWS
|
||||||
if (sock == INVALID_SOCKET) {
|
|
||||||
const int last_error = WSAGetLastError ();
|
const int last_error = WSAGetLastError ();
|
||||||
wsa_assert (last_error == WSAEWOULDBLOCK || last_error == WSAECONNRESET
|
wsa_assert (last_error == WSAEWOULDBLOCK || last_error == WSAECONNRESET
|
||||||
|| last_error == WSAEMFILE || last_error == WSAENOBUFS);
|
|| last_error == WSAEMFILE || last_error == WSAENOBUFS);
|
||||||
return retired_fd;
|
|
||||||
}
|
|
||||||
#if !defined _WIN32_WCE && !defined ZMQ_HAVE_WINDOWS_UWP
|
|
||||||
// On Windows, preventing sockets to be inherited by child processes.
|
|
||||||
BOOL brc = SetHandleInformation ((HANDLE) sock, HANDLE_FLAG_INHERIT, 0);
|
|
||||||
win_assert (brc);
|
|
||||||
#endif
|
|
||||||
#else
|
#else
|
||||||
if (sock == -1) {
|
|
||||||
errno_assert (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR
|
errno_assert (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR
|
||||||
|| errno == ECONNABORTED || errno == EPROTO
|
|| errno == ECONNABORTED || errno == EPROTO
|
||||||
|| errno == ENOBUFS || errno == ENOMEM || errno == EMFILE
|
|| errno == ENOBUFS || errno == ENOMEM || errno == EMFILE
|
||||||
|| errno == ENFILE);
|
|| errno == ENFILE);
|
||||||
|
#endif
|
||||||
return retired_fd;
|
return retired_fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if defined ZMQ_HAVE_WINDOWS && !defined _WIN32_WCE \
|
||||||
|
&& !defined ZMQ_HAVE_WINDOWS_UWP
|
||||||
|
// On Windows, preventing sockets to be inherited by child processes.
|
||||||
|
BOOL brc = SetHandleInformation ((HANDLE) sock, HANDLE_FLAG_INHERIT, 0);
|
||||||
|
win_assert (brc);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if (!defined ZMQ_HAVE_SOCK_CLOEXEC || !defined HAVE_ACCEPT4) \
|
#if (!defined ZMQ_HAVE_SOCK_CLOEXEC || !defined HAVE_ACCEPT4) \
|
||||||
|
Loading…
x
Reference in New Issue
Block a user