diff options
author | Martin Sustrik <sustrik@250bpm.com> | 2012-04-13 06:32:24 +0200 |
---|---|---|
committer | Martin Sustrik <sustrik@250bpm.com> | 2012-04-14 04:59:27 +0200 |
commit | f34f71bbd5b9b00b295aa6438dd251845547225c (patch) | |
tree | e5a84ab763991bea17a0aa66ab2c11d12c340f06 /src/tcp_listener.cpp | |
parent | 048f8816f6bea585b092b528b9da648a32a9c94c (diff) |
Set options on new sockets in systematic manner
This patch consolidates the up-to-now scattered code that
sets different options on newly created sockets.
There are open_socket and open_tcp_socket functions that
do the tuning automatically.
In case the socket is not created but got from elsewhere
(such as accept() call) there are tune_socket and tune_tcp_socket
functions that will do the tuning.
Signed-off-by: Martin Sustrik <sustrik@250bpm.com>
Diffstat (limited to 'src/tcp_listener.cpp')
-rw-r--r-- | src/tcp_listener.cpp | 21 |
1 files changed, 4 insertions, 17 deletions
diff --git a/src/tcp_listener.cpp b/src/tcp_listener.cpp index 2fda819..22797e2 100644 --- a/src/tcp_listener.cpp +++ b/src/tcp_listener.cpp @@ -82,12 +82,9 @@ void xs::tcp_listener_t::in_event (fd_t fd_) fd_t fd = accept (); // If connection was reset by the peer in the meantime, just ignore it. - // TODO: Handle specific errors like ENFILE/EMFILE etc. if (fd == retired_fd) return; - tune_tcp_socket (fd, options.keepalive ? true : false); - // Create the engine object for this connection. stream_engine_t *engine = new (std::nothrow) stream_engine_t (fd, options); alloc_assert (engine); @@ -127,11 +124,7 @@ int xs::tcp_listener_t::set_address (const char *addr_) return -1; // Create a listening socket. - s = open_socket (address.family (), SOCK_STREAM, IPPROTO_TCP); -#ifdef XS_HAVE_WINDOWS - if (s == INVALID_SOCKET) - wsa_error_to_errno (); -#endif + s = open_tcp_socket (address.family (), false); // IPv6 address family not supported, try automatic downgrade to IPv4. if (address.family () == AF_INET6 && errno == EAFNOSUPPORT && @@ -139,18 +132,11 @@ int xs::tcp_listener_t::set_address (const char *addr_) rc = address.resolve (addr_, true, true); if (rc != 0) return rc; - s = ::socket (address.family (), SOCK_STREAM, IPPROTO_TCP); + s = open_tcp_socket (address.family (), false); } -#ifdef XS_HAVE_WINDOWS - if (s == INVALID_SOCKET) { - wsa_error_to_errno (); + if (s == retired_fd) return -1; - } -#else - if (s == -1) - return -1; -#endif // On some systems, IPv4 mapping in IPv6 sockets is disabled by default. // Switch it on in such cases. @@ -219,6 +205,7 @@ xs::fd_t xs::tcp_listener_t::accept () return retired_fd; } #endif + tune_tcp_socket (sock, options.keepalive ? true : false); return sock; } |