Skip to content

Commit b2b173a

Browse files
committed
Merge pull request #6867
a4e28b3 Set TCP_NODELAY on P2P sockets. (Gregory Maxwell)
2 parents f2c869a + a4e28b3 commit b2b173a

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

src/compat.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include <sys/types.h>
3939
#include <net/if.h>
4040
#include <netinet/in.h>
41+
#include <netinet/tcp.h>
4142
#include <arpa/inet.h>
4243
#include <ifaddrs.h>
4344
#include <limits.h>

src/net.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,15 @@ static void AcceptConnection(const ListenSocket& hListenSocket) {
963963
return;
964964
}
965965

966+
// According to the internet TCP_NODELAY is not carried into accepted sockets
967+
// on all platforms. Set it again here just to be sure.
968+
int set = 1;
969+
#ifdef WIN32
970+
setsockopt(hSocket, IPPROTO_TCP, TCP_NODELAY, (const char*)&set, sizeof(int));
971+
#else
972+
setsockopt(hSocket, IPPROTO_TCP, TCP_NODELAY, (void*)&set, sizeof(int));
973+
#endif
974+
966975
if (CNode::IsBanned(addr) && !whitelisted)
967976
{
968977
LogPrintf("connection from %s dropped (banned)\n", addr.ToString());
@@ -1790,8 +1799,11 @@ bool BindListenPort(const CService &addrBind, string& strError, bool fWhiteliste
17901799
// Allow binding if the port is still in TIME_WAIT state after
17911800
// the program was closed and restarted.
17921801
setsockopt(hListenSocket, SOL_SOCKET, SO_REUSEADDR, (void*)&nOne, sizeof(int));
1802+
// Disable Nagle's algorithm
1803+
setsockopt(hListenSocket, IPPROTO_TCP, TCP_NODELAY, (void*)&nOne, sizeof(int));
17931804
#else
17941805
setsockopt(hListenSocket, SOL_SOCKET, SO_REUSEADDR, (const char*)&nOne, sizeof(int));
1806+
setsockopt(hListenSocket, IPPROTO_TCP, TCP_NODELAY, (const char*)&nOne, sizeof(int));
17951807
#endif
17961808

17971809
// Set to non-blocking, incoming connections will also inherit this

src/netbase.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,12 +444,19 @@ bool static ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocketRe
444444
if (hSocket == INVALID_SOCKET)
445445
return false;
446446

447-
#ifdef SO_NOSIGPIPE
448447
int set = 1;
448+
#ifdef SO_NOSIGPIPE
449449
// Different way of disabling SIGPIPE on BSD
450450
setsockopt(hSocket, SOL_SOCKET, SO_NOSIGPIPE, (void*)&set, sizeof(int));
451451
#endif
452452

453+
//Disable Nagle's algorithm
454+
#ifdef WIN32
455+
setsockopt(hSocket, IPPROTO_TCP, TCP_NODELAY, (const char*)&set, sizeof(int));
456+
#else
457+
setsockopt(hSocket, IPPROTO_TCP, TCP_NODELAY, (void*)&set, sizeof(int));
458+
#endif
459+
453460
// Set to non-blocking
454461
if (!SetSocketNonBlocking(hSocket, true))
455462
return error("ConnectSocketDirectly: Setting socket to non-blocking failed, error %s\n", NetworkErrorString(WSAGetLastError()));

0 commit comments

Comments
 (0)