Skip to content

Commit ad415bc

Browse files
committed
[net] Added SetSocketNoDelay() utility function
1 parent 02d64bd commit ad415bc

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
@@ -1069,12 +1069,7 @@ void CConnman::AcceptConnection(const ListenSocket& hListenSocket) {
10691069

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

10791074
if (IsBanned(addr) && !whitelisted)
10801075
{

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)