Skip to content

Commit 6532464

Browse files
committed
Add set_socket_opt function and corresponding test for TCP_NODELAY option (Resolve #2411)
1 parent 6fd97ae commit 6532464

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

httplib.h

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,6 +1480,8 @@ using SocketOptions = std::function<void(socket_t sock)>;
14801480

14811481
void default_socket_options(socket_t sock);
14821482

1483+
bool set_socket_opt(socket_t sock, int level, int optname, int optval);
1484+
14831485
const char *status_message(int status);
14841486

14851487
std::string to_string(Error error);
@@ -4338,10 +4340,6 @@ inline bool set_socket_opt_impl(socket_t sock, int level, int optname,
43384340
optlen) == 0;
43394341
}
43404342

4341-
inline bool set_socket_opt(socket_t sock, int level, int optname, int optval) {
4342-
return set_socket_opt_impl(sock, level, optname, &optval, sizeof(optval));
4343-
}
4344-
43454343
inline bool set_socket_opt_time(socket_t sock, int level, int optname,
43464344
time_t sec, time_t usec) {
43474345
#ifdef _WIN32
@@ -6089,7 +6087,7 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port,
60896087
#ifdef _WIN32
60906088
// Setting SO_REUSEADDR seems not to work well with AF_UNIX on windows, so
60916089
// remove the option.
6092-
detail::set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 0);
6090+
set_socket_opt(sock, SOL_SOCKET, SO_REUSEADDR, 0);
60936091
#endif
60946092

60956093
bool dummy;
@@ -9135,13 +9133,18 @@ inline bool setup_client_tls_session(const std::string &host, tls::ctx_t &ctx,
91359133
*/
91369134

91379135
inline void default_socket_options(socket_t sock) {
9138-
detail::set_socket_opt(sock, SOL_SOCKET,
9136+
set_socket_opt(sock, SOL_SOCKET,
91399137
#ifdef SO_REUSEPORT
9140-
SO_REUSEPORT,
9138+
SO_REUSEPORT,
91419139
#else
9142-
SO_REUSEADDR,
9140+
SO_REUSEADDR,
91439141
#endif
9144-
1);
9142+
1);
9143+
}
9144+
9145+
inline bool set_socket_opt(socket_t sock, int level, int optname, int optval) {
9146+
return detail::set_socket_opt_impl(sock, level, optname, &optval,
9147+
sizeof(optval));
91459148
}
91469149

91479150
inline std::string get_bearer_token_auth(const Request &req) {
@@ -11561,9 +11564,7 @@ inline bool Server::listen_internal() {
1156111564
detail::set_socket_opt_time(sock, SOL_SOCKET, SO_SNDTIMEO,
1156211565
write_timeout_sec_, write_timeout_usec_);
1156311566

11564-
if (tcp_nodelay_) {
11565-
detail::set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1);
11566-
}
11567+
if (tcp_nodelay_) { set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1); }
1156711568

1156811569
if (!task_queue->enqueue(
1156911570
[this, sock]() { process_and_close_socket(sock); })) {

test/test.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,19 @@ TEST(SocketStream, wait_writable_INET) {
316316
}
317317
#endif // #ifndef _WIN32
318318

319+
TEST(SetSocketOptTest, TcpNoDelay) {
320+
auto sock = ::socket(AF_INET, SOCK_STREAM, 0);
321+
ASSERT_NE(sock, INVALID_SOCKET);
322+
EXPECT_TRUE(set_socket_opt(sock, IPPROTO_TCP, TCP_NODELAY, 1));
323+
324+
int val = 0;
325+
socklen_t len = sizeof(val);
326+
ASSERT_EQ(0, ::getsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
327+
reinterpret_cast<char *>(&val), &len));
328+
EXPECT_NE(val, 0);
329+
detail::close_socket(sock);
330+
}
331+
319332
TEST(ClientTest, MoveConstructible) {
320333
EXPECT_FALSE(std::is_copy_constructible<Client>::value);
321334
EXPECT_TRUE(std::is_nothrow_move_constructible<Client>::value);

0 commit comments

Comments
 (0)