Browse Source

Move CloseSocket out of SetSocketNonBlocking and pass SOCKET by const reference in SetSocket* functions

0.15
Dag Robole 7 years ago
parent
commit
05e023f2ec
  1. 2
      src/compat.h
  2. 1
      src/net.cpp
  3. 12
      src/netbase.cpp
  4. 4
      src/netbase.h

2
src/compat.h

@ -76,7 +76,7 @@ typedef unsigned int SOCKET;
size_t strnlen( const char *start, size_t max_len); size_t strnlen( const char *start, size_t max_len);
#endif // HAVE_DECL_STRNLEN #endif // HAVE_DECL_STRNLEN
bool static inline IsSelectableSocket(SOCKET s) { bool static inline IsSelectableSocket(const SOCKET& s) {
#ifdef WIN32 #ifdef WIN32
return true; return true;
#else #else

1
src/net.cpp

@ -2076,6 +2076,7 @@ bool CConnman::BindListenPort(const CService &addrBind, std::string& strError, b
// Set to non-blocking, incoming connections will also inherit this // Set to non-blocking, incoming connections will also inherit this
if (!SetSocketNonBlocking(hListenSocket, true)) { if (!SetSocketNonBlocking(hListenSocket, true)) {
CloseSocket(hListenSocket);
strError = strprintf("BindListenPort: Setting listening socket to non-blocking failed, error %s\n", NetworkErrorString(WSAGetLastError())); strError = strprintf("BindListenPort: Setting listening socket to non-blocking failed, error %s\n", NetworkErrorString(WSAGetLastError()));
LogPrintf("%s\n", strError); LogPrintf("%s\n", strError);
return false; return false;

12
src/netbase.cpp

@ -203,7 +203,7 @@ enum class IntrRecvError {
* *
* @note This function requires that hSocket is in non-blocking mode. * @note This function requires that hSocket is in non-blocking mode.
*/ */
static IntrRecvError InterruptibleRecv(char* data, size_t len, int timeout, SOCKET& hSocket) static IntrRecvError InterruptibleRecv(char* data, size_t len, int timeout, const SOCKET& hSocket)
{ {
int64_t curTime = GetTimeMillis(); int64_t curTime = GetTimeMillis();
int64_t endTime = curTime + timeout; int64_t endTime = curTime + timeout;
@ -424,8 +424,10 @@ bool static ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocketRe
SetSocketNoDelay(hSocket); SetSocketNoDelay(hSocket);
// Set to non-blocking // Set to non-blocking
if (!SetSocketNonBlocking(hSocket, true)) if (!SetSocketNonBlocking(hSocket, true)) {
CloseSocket(hSocket);
return error("ConnectSocketDirectly: Setting socket to non-blocking failed, error %s\n", NetworkErrorString(WSAGetLastError())); return error("ConnectSocketDirectly: Setting socket to non-blocking failed, error %s\n", NetworkErrorString(WSAGetLastError()));
}
if (connect(hSocket, (struct sockaddr*)&sockaddr, len) == SOCKET_ERROR) if (connect(hSocket, (struct sockaddr*)&sockaddr, len) == SOCKET_ERROR)
{ {
@ -682,7 +684,7 @@ bool CloseSocket(SOCKET& hSocket)
return ret != SOCKET_ERROR; return ret != SOCKET_ERROR;
} }
bool SetSocketNonBlocking(SOCKET& hSocket, bool fNonBlocking) bool SetSocketNonBlocking(const SOCKET& hSocket, bool fNonBlocking)
{ {
if (fNonBlocking) { if (fNonBlocking) {
#ifdef WIN32 #ifdef WIN32
@ -692,7 +694,6 @@ bool SetSocketNonBlocking(SOCKET& hSocket, bool fNonBlocking)
int fFlags = fcntl(hSocket, F_GETFL, 0); int fFlags = fcntl(hSocket, F_GETFL, 0);
if (fcntl(hSocket, F_SETFL, fFlags | O_NONBLOCK) == SOCKET_ERROR) { if (fcntl(hSocket, F_SETFL, fFlags | O_NONBLOCK) == SOCKET_ERROR) {
#endif #endif
CloseSocket(hSocket);
return false; return false;
} }
} else { } else {
@ -703,7 +704,6 @@ bool SetSocketNonBlocking(SOCKET& hSocket, bool fNonBlocking)
int fFlags = fcntl(hSocket, F_GETFL, 0); int fFlags = fcntl(hSocket, F_GETFL, 0);
if (fcntl(hSocket, F_SETFL, fFlags & ~O_NONBLOCK) == SOCKET_ERROR) { if (fcntl(hSocket, F_SETFL, fFlags & ~O_NONBLOCK) == SOCKET_ERROR) {
#endif #endif
CloseSocket(hSocket);
return false; return false;
} }
} }
@ -711,7 +711,7 @@ bool SetSocketNonBlocking(SOCKET& hSocket, bool fNonBlocking)
return true; return true;
} }
bool SetSocketNoDelay(SOCKET& hSocket) bool SetSocketNoDelay(const SOCKET& hSocket)
{ {
int set = 1; int set = 1;
int rc = setsockopt(hSocket, IPPROTO_TCP, TCP_NODELAY, (const char*)&set, sizeof(int)); int rc = setsockopt(hSocket, IPPROTO_TCP, TCP_NODELAY, (const char*)&set, sizeof(int));

4
src/netbase.h

@ -57,9 +57,9 @@ std::string NetworkErrorString(int err);
/** Close socket and set hSocket to INVALID_SOCKET */ /** Close socket and set hSocket to INVALID_SOCKET */
bool CloseSocket(SOCKET& hSocket); bool CloseSocket(SOCKET& hSocket);
/** Disable or enable blocking-mode for a socket */ /** Disable or enable blocking-mode for a socket */
bool SetSocketNonBlocking(SOCKET& hSocket, bool fNonBlocking); bool SetSocketNonBlocking(const SOCKET& hSocket, bool fNonBlocking);
/** Set the TCP_NODELAY flag on a socket */ /** Set the TCP_NODELAY flag on a socket */
bool SetSocketNoDelay(SOCKET& hSocket); bool SetSocketNoDelay(const SOCKET& hSocket);
/** /**
* Convert milliseconds to a struct timeval for e.g. select. * Convert milliseconds to a struct timeval for e.g. select.
*/ */

Loading…
Cancel
Save