Optimize preprocessor directives on Solaris for evutil_set_tcp_keepalive (#1536)

Changes:
- Optimize preprocessor directives on Solaris for evutil_set_tcp_keepalive
- Reduce the amount of code being compiled and trim trailing whitespace in passing.
- Move comments for Solaris under its macro
- Refactor the control flow
This commit is contained in:
Andy Pan 2024-01-13 20:51:49 +08:00 committed by GitHub
parent 76460fbe66
commit 9d4853b363
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 46 deletions

View File

@ -3063,12 +3063,12 @@ evutil_set_tcp_keepalive(evutil_socket_t fd, int on, int timeout)
*/
#ifndef _WIN32
#ifdef __sun
/* The implementation of TCP keep-alive on Solaris/SmartOS is a bit unusual
* compared to other Unix-like systems.
* Thus, we need to specialize it on Solaris.
*/
#ifdef __sun
/* There are two keep-alive mechanisms on Solaris:
*
* There are two keep-alive mechanisms on Solaris:
* - By default, the first keep-alive probe is sent out after a TCP connection is idle for two hours.
* If the peer does not respond to the probe within eight minutes, the TCP connection is aborted.
* You can alter the interval for sending out the first probe using the socket option TCP_KEEPALIVE_THRESHOLD
@ -3100,15 +3100,15 @@ evutil_set_tcp_keepalive(evutil_socket_t fd, int on, int timeout)
#if defined(TCP_KEEPIDLE) && defined(TCP_KEEPINTVL) && defined(TCP_KEEPCNT)
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &idle, sizeof(idle)))
return -1;
intvl = idle/3;
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &intvl, sizeof(intvl)))
return -1;
cnt = 3;
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &cnt, sizeof(cnt)))
return -1;
return 0;
#endif
#else
/* Fall back to the first implementation of tcp-alive mechanism for older Solaris,
* simulate the tcp-alive mechanism on other platforms via `TCP_KEEPALIVE_THRESHOLD` + `TCP_KEEPALIVE_ABORT_THRESHOLD`.
*/
@ -3124,10 +3124,10 @@ evutil_set_tcp_keepalive(evutil_socket_t fd, int on, int timeout)
int time_to_abort = intvl * cnt;
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPALIVE_ABORT_THRESHOLD, &time_to_abort, sizeof(time_to_abort)))
return -1;
return 0;
#endif
#else /* !__sun */
#ifdef TCP_KEEPIDLE
idle = timeout;
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &idle, sizeof(idle)))
@ -3160,6 +3160,8 @@ evutil_set_tcp_keepalive(evutil_socket_t fd, int on, int timeout)
return -1;
#endif
#endif /* !__sun */
#endif /* !_WIN32 */
return 0;

View File

@ -489,7 +489,7 @@ int evutil_make_tcp_listen_socket_deferred(evutil_socket_t sock);
* TCP_KEEPIDLE, TCP_KEEPINTVL and TCP_KEEPCNT on a socket.
*
* @param sock The socket to be set TCP keep-alive
* @param on nonzero value to enable TCP keep-alive, 0 to disable
* @param on Nonzero value to enable TCP keep-alive, 0 to disable
* @param timeout The timeout in seconds with no activity until
* the first keepalive probe is sent
* @return 0 on success, -1 on failure