Skip to content

Commit 0c70e84

Browse files
committed
Merge #10865: Move CloseSocket out of SetSocketNonBlocking and pass socket as const reference
05e023f Move CloseSocket out of SetSocketNonBlocking and pass SOCKET by const reference in SetSocket* functions (Dag Robole) Pull request description: Rationale: Readability, SetSocketNonBlocking does what it says on the tin. Consistency, More consistent with the rest of the API in this unit. Reusability, SetSocketNonBlocking can also be used by clients that may not want to close the socket on failure. This also moves the responsibility of closing the socket back to the caller that opened it, which in general should know better how and when to close it. Tree-SHA512: 85027137f1b626e2b636549ee38cc757a587adcf464c84be6e65ca16e3b75d7ed1a1b21dd70dbe34c7c5d599af39e53b89932dfe3c74f91a22341ff3af5ea80a
2 parents 6ef3c7e + 05e023f commit 0c70e84

File tree

4 files changed

+10
-9
lines changed

4 files changed

+10
-9
lines changed

src/compat.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ typedef unsigned int SOCKET;
7676
size_t strnlen( const char *start, size_t max_len);
7777
#endif // HAVE_DECL_STRNLEN
7878

79-
bool static inline IsSelectableSocket(SOCKET s) {
79+
bool static inline IsSelectableSocket(const SOCKET& s) {
8080
#ifdef WIN32
8181
return true;
8282
#else

src/net.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2076,6 +2076,7 @@ bool CConnman::BindListenPort(const CService &addrBind, std::string& strError, b
20762076

20772077
// Set to non-blocking, incoming connections will also inherit this
20782078
if (!SetSocketNonBlocking(hListenSocket, true)) {
2079+
CloseSocket(hListenSocket);
20792080
strError = strprintf("BindListenPort: Setting listening socket to non-blocking failed, error %s\n", NetworkErrorString(WSAGetLastError()));
20802081
LogPrintf("%s\n", strError);
20812082
return false;

src/netbase.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ enum class IntrRecvError {
203203
*
204204
* @note This function requires that hSocket is in non-blocking mode.
205205
*/
206-
static IntrRecvError InterruptibleRecv(char* data, size_t len, int timeout, SOCKET& hSocket)
206+
static IntrRecvError InterruptibleRecv(char* data, size_t len, int timeout, const SOCKET& hSocket)
207207
{
208208
int64_t curTime = GetTimeMillis();
209209
int64_t endTime = curTime + timeout;
@@ -424,8 +424,10 @@ bool static ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocketRe
424424
SetSocketNoDelay(hSocket);
425425

426426
// Set to non-blocking
427-
if (!SetSocketNonBlocking(hSocket, true))
427+
if (!SetSocketNonBlocking(hSocket, true)) {
428+
CloseSocket(hSocket);
428429
return error("ConnectSocketDirectly: Setting socket to non-blocking failed, error %s\n", NetworkErrorString(WSAGetLastError()));
430+
}
429431

430432
if (connect(hSocket, (struct sockaddr*)&sockaddr, len) == SOCKET_ERROR)
431433
{
@@ -682,7 +684,7 @@ bool CloseSocket(SOCKET& hSocket)
682684
return ret != SOCKET_ERROR;
683685
}
684686

685-
bool SetSocketNonBlocking(SOCKET& hSocket, bool fNonBlocking)
687+
bool SetSocketNonBlocking(const SOCKET& hSocket, bool fNonBlocking)
686688
{
687689
if (fNonBlocking) {
688690
#ifdef WIN32
@@ -692,7 +694,6 @@ bool SetSocketNonBlocking(SOCKET& hSocket, bool fNonBlocking)
692694
int fFlags = fcntl(hSocket, F_GETFL, 0);
693695
if (fcntl(hSocket, F_SETFL, fFlags | O_NONBLOCK) == SOCKET_ERROR) {
694696
#endif
695-
CloseSocket(hSocket);
696697
return false;
697698
}
698699
} else {
@@ -703,15 +704,14 @@ bool SetSocketNonBlocking(SOCKET& hSocket, bool fNonBlocking)
703704
int fFlags = fcntl(hSocket, F_GETFL, 0);
704705
if (fcntl(hSocket, F_SETFL, fFlags & ~O_NONBLOCK) == SOCKET_ERROR) {
705706
#endif
706-
CloseSocket(hSocket);
707707
return false;
708708
}
709709
}
710710

711711
return true;
712712
}
713713

714-
bool SetSocketNoDelay(SOCKET& hSocket)
714+
bool SetSocketNoDelay(const SOCKET& hSocket)
715715
{
716716
int set = 1;
717717
int rc = setsockopt(hSocket, IPPROTO_TCP, TCP_NODELAY, (const char*)&set, sizeof(int));

src/netbase.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ std::string NetworkErrorString(int err);
5757
/** Close socket and set hSocket to INVALID_SOCKET */
5858
bool CloseSocket(SOCKET& hSocket);
5959
/** Disable or enable blocking-mode for a socket */
60-
bool SetSocketNonBlocking(SOCKET& hSocket, bool fNonBlocking);
60+
bool SetSocketNonBlocking(const SOCKET& hSocket, bool fNonBlocking);
6161
/** Set the TCP_NODELAY flag on a socket */
62-
bool SetSocketNoDelay(SOCKET& hSocket);
62+
bool SetSocketNoDelay(const SOCKET& hSocket);
6363
/**
6464
* Convert milliseconds to a struct timeval for e.g. select.
6565
*/

0 commit comments

Comments
 (0)