Skip to content

Commit ae78609

Browse files
committed
Merge #10061: [net] Added SetSocketNoDelay() utility function
ad415bc [net] Added SetSocketNoDelay() utility function (Thomas Snider) Tree-SHA512: c19e3c9910b3fc2ef86f2434f3e91d343e9cd9e2116153941de9789e2a6fc0389bffe762d21b55cda4a4b1de993afee0564c6946e65d05cef9e866b58896f9af
2 parents c336525 + ad415bc commit ae78609

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

src/net.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,12 +1071,7 @@ void CConnman::AcceptConnection(const ListenSocket& hListenSocket) {
10711071

10721072
// According to the internet TCP_NODELAY is not carried into accepted sockets
10731073
// on all platforms. Set it again here just to be sure.
1074-
int set = 1;
1075-
#ifdef WIN32
1076-
setsockopt(hSocket, IPPROTO_TCP, TCP_NODELAY, (const char*)&set, sizeof(int));
1077-
#else
1078-
setsockopt(hSocket, IPPROTO_TCP, TCP_NODELAY, (void*)&set, sizeof(int));
1079-
#endif
1074+
SetSocketNoDelay(hSocket);
10801075

10811076
if (IsBanned(addr) && !whitelisted)
10821077
{

src/netbase.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -428,18 +428,14 @@ bool static ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocketRe
428428
if (hSocket == INVALID_SOCKET)
429429
return false;
430430

431-
int set = 1;
432431
#ifdef SO_NOSIGPIPE
432+
int set = 1;
433433
// Different way of disabling SIGPIPE on BSD
434434
setsockopt(hSocket, SOL_SOCKET, SO_NOSIGPIPE, (void*)&set, sizeof(int));
435435
#endif
436436

437437
//Disable Nagle's algorithm
438-
#ifdef WIN32
439-
setsockopt(hSocket, IPPROTO_TCP, TCP_NODELAY, (const char*)&set, sizeof(int));
440-
#else
441-
setsockopt(hSocket, IPPROTO_TCP, TCP_NODELAY, (void*)&set, sizeof(int));
442-
#endif
438+
SetSocketNoDelay(hSocket);
443439

444440
// Set to non-blocking
445441
if (!SetSocketNonBlocking(hSocket, true))
@@ -728,6 +724,13 @@ bool SetSocketNonBlocking(SOCKET& hSocket, bool fNonBlocking)
728724
return true;
729725
}
730726

727+
bool SetSocketNoDelay(SOCKET& hSocket)
728+
{
729+
int set = 1;
730+
int rc = setsockopt(hSocket, IPPROTO_TCP, TCP_NODELAY, (const char*)&set, sizeof(int));
731+
return rc == 0;
732+
}
733+
731734
void InterruptSocks5(bool interrupt)
732735
{
733736
interruptSocks5Recv = interrupt;

src/netbase.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ std::string NetworkErrorString(int err);
5959
bool CloseSocket(SOCKET& hSocket);
6060
/** Disable or enable blocking-mode for a socket */
6161
bool SetSocketNonBlocking(SOCKET& hSocket, bool fNonBlocking);
62+
/** Set the TCP_NODELAY flag on a socket */
63+
bool SetSocketNoDelay(SOCKET& hSocket);
6264
/**
6365
* Convert milliseconds to a struct timeval for e.g. select.
6466
*/

0 commit comments

Comments
 (0)