Skip to content

Commit a2c4a7a

Browse files
committed
net: use Sock::SetSockOpt() instead of standalone SetSocketNoDelay()
Since the former is mockable, this makes it easier to test higher level code that sets the TCP_NODELAY flag.
1 parent d65b6c3 commit a2c4a7a

File tree

3 files changed

+9
-11
lines changed

3 files changed

+9
-11
lines changed

src/net.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1190,7 +1190,11 @@ void CConnman::CreateNodeFromAcceptedSocket(std::unique_ptr<Sock>&& sock,
11901190

11911191
// According to the internet TCP_NODELAY is not carried into accepted sockets
11921192
// on all platforms. Set it again here just to be sure.
1193-
SetSocketNoDelay(sock->Get());
1193+
const int on{1};
1194+
if (sock->SetSockOpt(IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) == SOCKET_ERROR) {
1195+
LogPrint(BCLog::NET, "connection from %s: unable to set TCP_NODELAY, continuing anyway\n",
1196+
addr.ToString());
1197+
}
11941198

11951199
// Don't accept connections from banned peers.
11961200
bool banned = m_banman && m_banman->IsBanned(addr);

src/netbase.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,10 @@ std::unique_ptr<Sock> CreateSockTCP(const CService& address_family)
519519
#endif
520520

521521
// Set the no-delay option (disable Nagle's algorithm) on the TCP socket.
522-
SetSocketNoDelay(sock->Get());
522+
const int on{1};
523+
if (sock->SetSockOpt(IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) == SOCKET_ERROR) {
524+
LogPrint(BCLog::NET, "Unable to set TCP_NODELAY on a newly created socket, continuing anyway\n");
525+
}
523526

524527
// Set the non-blocking option on the socket.
525528
if (!SetSocketNonBlocking(sock->Get())) {
@@ -729,13 +732,6 @@ bool SetSocketNonBlocking(const SOCKET& hSocket)
729732
return true;
730733
}
731734

732-
bool SetSocketNoDelay(const SOCKET& hSocket)
733-
{
734-
int set = 1;
735-
int rc = setsockopt(hSocket, IPPROTO_TCP, TCP_NODELAY, (const char*)&set, sizeof(int));
736-
return rc == 0;
737-
}
738-
739735
void InterruptSocks5(bool interrupt)
740736
{
741737
interruptSocks5Recv = interrupt;

src/netbase.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,6 @@ bool ConnectThroughProxy(const Proxy& proxy, const std::string& strDest, uint16_
223223

224224
/** Enable non-blocking mode for a socket */
225225
bool SetSocketNonBlocking(const SOCKET& hSocket);
226-
/** Set the TCP_NODELAY flag on a socket */
227-
bool SetSocketNoDelay(const SOCKET& hSocket);
228226
void InterruptSocks5(bool interrupt);
229227

230228
/**

0 commit comments

Comments
 (0)