diff options
author | Christian Grothoff <christian@grothoff.org> | 2019-08-01 20:44:41 +0200 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2019-08-01 20:44:41 +0200 |
commit | c5081fb276b52fc9d48b9ba441686727eafaaad8 (patch) | |
tree | d8741e84111ed226c2bc2426e18d4e90bbd85184 /src/microhttpd/mhd_sockets.c | |
parent | da7cb682a24915a4bc37faf2c76583996fe26aea (diff) | |
download | libmicrohttpd-c5081fb276b52fc9d48b9ba441686727eafaaad8.tar.gz libmicrohttpd-c5081fb276b52fc9d48b9ba441686727eafaaad8.zip |
always set nodelay, except if we cannot cork
Diffstat (limited to 'src/microhttpd/mhd_sockets.c')
-rw-r--r-- | src/microhttpd/mhd_sockets.c | 92 |
1 files changed, 68 insertions, 24 deletions
diff --git a/src/microhttpd/mhd_sockets.c b/src/microhttpd/mhd_sockets.c index 0bfe2517..b4e73480 100644 --- a/src/microhttpd/mhd_sockets.c +++ b/src/microhttpd/mhd_sockets.c | |||
@@ -462,6 +462,64 @@ MHD_socket_noninheritable_ (MHD_socket sock) | |||
462 | 462 | ||
463 | 463 | ||
464 | /** | 464 | /** |
465 | * Disable Nagle's algorithm on @a sock. This is what we do by default for | ||
466 | * all TCP sockets in MHD, unless the platform does not support the MSG_MORE | ||
467 | * or MSG_CORK or MSG_NOPUSH options. | ||
468 | * | ||
469 | * @param sock socket to manipulate | ||
470 | * @param on value to use | ||
471 | * @return 0 on success | ||
472 | */ | ||
473 | int | ||
474 | MHD_socket_set_nodelay_ (MHD_socket sock, | ||
475 | bool on) | ||
476 | { | ||
477 | #ifdef TCP_NODELAY | ||
478 | { | ||
479 | const MHD_SCKT_OPT_BOOL_ off_val = 0; | ||
480 | const MHD_SCKT_OPT_BOOL_ on_val = 1; | ||
481 | /* Disable Nagle's algorithm for normal buffering */ | ||
482 | return setsockopt (sock, | ||
483 | IPPROTO_TCP, | ||
484 | TCP_NODELAY, | ||
485 | (const void *) (on) ? &on_val : &off_val, | ||
486 | sizeof (on_val)); | ||
487 | } | ||
488 | #else | ||
489 | (void) sock; | ||
490 | return 0; | ||
491 | #endif /* TCP_NODELAY */ | ||
492 | } | ||
493 | |||
494 | |||
495 | /** | ||
496 | * Enable/disable the cork option. | ||
497 | * | ||
498 | * @param sock socket to manipulate | ||
499 | * @param on set to true to enable CORK, false to disable | ||
500 | * @return non-zero if succeeded, zero otherwise | ||
501 | */ | ||
502 | int | ||
503 | MHD_socket_cork_ (MHD_socket sock, | ||
504 | bool on) | ||
505 | { | ||
506 | #if defined(MHD_TCP_CORK_NOPUSH) | ||
507 | const MHD_SCKT_OPT_BOOL_ off_val = 0; | ||
508 | const MHD_SCKT_OPT_BOOL_ on_val = 1; | ||
509 | |||
510 | /* Disable extra buffering */ | ||
511 | return (0 == setsockopt (sock, | ||
512 | IPPROTO_TCP, | ||
513 | MHD_TCP_CORK_NOPUSH, | ||
514 | (const void *) (on ? &on_val : &off_val), | ||
515 | sizeof (off_val))); | ||
516 | #else | ||
517 | return 0; | ||
518 | #endif /* MHD_TCP_CORK_NOPUSH */ | ||
519 | } | ||
520 | |||
521 | |||
522 | /** | ||
465 | * Change socket buffering mode to default. | 523 | * Change socket buffering mode to default. |
466 | * | 524 | * |
467 | * @param sock socket to manipulate | 525 | * @param sock socket to manipulate |
@@ -470,33 +528,19 @@ MHD_socket_noninheritable_ (MHD_socket sock) | |||
470 | int | 528 | int |
471 | MHD_socket_buffering_reset_ (MHD_socket sock) | 529 | MHD_socket_buffering_reset_ (MHD_socket sock) |
472 | { | 530 | { |
473 | int res = !0; | ||
474 | #if defined(TCP_NODELAY) || defined(MHD_TCP_CORK_NOPUSH) | ||
475 | const MHD_SCKT_OPT_BOOL_ off_val = 0; | ||
476 | #if defined(MHD_TCP_CORK_NOPUSH) | 531 | #if defined(MHD_TCP_CORK_NOPUSH) |
477 | //#if OLD_SOCKOPT | 532 | int res = ! MHD_socket_set_nodelay_ (sock, |
533 | true); | ||
478 | /* Disable extra buffering */ | 534 | /* Disable extra buffering */ |
479 | res = (0 == setsockopt (sock, | 535 | return MHD_socket_cork_ (sock, |
480 | IPPROTO_TCP, | 536 | false) && res; |
481 | MHD_TCP_CORK_NOPUSH, | 537 | #elif defined(HAVE_MSG_MORE) |
482 | (const void *) &off_val, | 538 | return ! MHD_socket_set_nodelay_ (sock, |
483 | sizeof (off_val))) && res; | 539 | true); |
484 | //#endif | 540 | #else |
541 | return ! MHD_socket_set_nodelay_ (sock, | ||
542 | false); | ||
485 | #endif /* MHD_TCP_CORK_NOPUSH */ | 543 | #endif /* MHD_TCP_CORK_NOPUSH */ |
486 | #if defined(TCP_NODELAY) | ||
487 | //#if OLD_SOCKOPT | ||
488 | /* Enable Nagle's algorithm for normal buffering */ | ||
489 | res = (0 == setsockopt (sock, | ||
490 | IPPROTO_TCP, | ||
491 | TCP_NODELAY, | ||
492 | (const void *) &off_val, | ||
493 | sizeof (off_val))) && res; | ||
494 | //#endif | ||
495 | #endif /* TCP_NODELAY */ | ||
496 | #else /* !TCP_NODELAY && !MHD_TCP_CORK_NOPUSH */ | ||
497 | (void) sock; | ||
498 | #endif /* !TCP_NODELAY && !MHD_TCP_CORK_NOPUSH */ | ||
499 | return res; | ||
500 | } | 544 | } |
501 | 545 | ||
502 | 546 | ||