@@ -1261,28 +1261,10 @@ void CConnman::InactivityCheck(CNode *pnode)
12611261 }
12621262}
12631263
1264- void CConnman::SocketHandler ( )
1264+ bool CConnman::GenerateSelectSet (std::set<SOCKET> &recv_set, std::set<SOCKET> &send_set, std::set<SOCKET> &error_set )
12651265{
1266- //
1267- // Find which sockets have data to receive
1268- //
1269- struct timeval timeout;
1270- timeout.tv_sec = 0 ;
1271- timeout.tv_usec = SELECT_TIMEOUT_MILLISECONDS * 1000 ; // frequency to poll pnode->vSend
1272-
1273- fd_set fdsetRecv;
1274- fd_set fdsetSend;
1275- fd_set fdsetError;
1276- FD_ZERO (&fdsetRecv);
1277- FD_ZERO (&fdsetSend);
1278- FD_ZERO (&fdsetError);
1279- SOCKET hSocketMax = 0 ;
1280- bool have_fds = false ;
1281-
12821266 for (const ListenSocket& hListenSocket : vhListenSocket) {
1283- FD_SET (hListenSocket.socket , &fdsetRecv);
1284- hSocketMax = std::max (hSocketMax, hListenSocket.socket );
1285- have_fds = true ;
1267+ recv_set.insert (hListenSocket.socket );
12861268 }
12871269
12881270 {
@@ -1311,34 +1293,69 @@ void CConnman::SocketHandler()
13111293 if (pnode->hSocket == INVALID_SOCKET)
13121294 continue ;
13131295
1314- FD_SET (pnode->hSocket , &fdsetError);
1315- hSocketMax = std::max (hSocketMax, pnode->hSocket );
1316- have_fds = true ;
1317-
1296+ error_set.insert (pnode->hSocket );
13181297 if (select_send) {
1319- FD_SET (pnode->hSocket , &fdsetSend );
1298+ send_set. insert (pnode->hSocket );
13201299 continue ;
13211300 }
13221301 if (select_recv) {
1323- FD_SET (pnode->hSocket , &fdsetRecv );
1302+ recv_set. insert (pnode->hSocket );
13241303 }
13251304 }
13261305 }
13271306
1328- int nSelect = select (have_fds ? hSocketMax + 1 : 0 ,
1329- &fdsetRecv, &fdsetSend, &fdsetError, &timeout);
1307+ return !recv_set.empty () || !send_set.empty () || !error_set.empty ();
1308+ }
1309+
1310+ void CConnman::SocketHandler ()
1311+ {
1312+ std::set<SOCKET> recv_select_set, send_select_set, error_select_set;
1313+ if (!GenerateSelectSet (recv_select_set, send_select_set, error_select_set)) {
1314+ interruptNet.sleep_for (std::chrono::milliseconds (SELECT_TIMEOUT_MILLISECONDS));
1315+ return ;
1316+ }
1317+
1318+ //
1319+ // Find which sockets have data to receive
1320+ //
1321+ struct timeval timeout;
1322+ timeout.tv_sec = 0 ;
1323+ timeout.tv_usec = SELECT_TIMEOUT_MILLISECONDS * 1000 ; // frequency to poll pnode->vSend
1324+
1325+ fd_set fdsetRecv;
1326+ fd_set fdsetSend;
1327+ fd_set fdsetError;
1328+ FD_ZERO (&fdsetRecv);
1329+ FD_ZERO (&fdsetSend);
1330+ FD_ZERO (&fdsetError);
1331+ SOCKET hSocketMax = 0 ;
1332+
1333+ for (SOCKET hSocket : recv_select_set) {
1334+ FD_SET (hSocket, &fdsetRecv);
1335+ hSocketMax = std::max (hSocketMax, hSocket);
1336+ }
1337+
1338+ for (SOCKET hSocket : send_select_set) {
1339+ FD_SET (hSocket, &fdsetSend);
1340+ hSocketMax = std::max (hSocketMax, hSocket);
1341+ }
1342+
1343+ for (SOCKET hSocket : error_select_set) {
1344+ FD_SET (hSocket, &fdsetError);
1345+ hSocketMax = std::max (hSocketMax, hSocket);
1346+ }
1347+
1348+ int nSelect = select (hSocketMax + 1 , &fdsetRecv, &fdsetSend, &fdsetError, &timeout);
1349+
13301350 if (interruptNet)
13311351 return ;
13321352
13331353 if (nSelect == SOCKET_ERROR)
13341354 {
1335- if (have_fds)
1336- {
1337- int nErr = WSAGetLastError ();
1338- LogPrintf (" socket select error %s\n " , NetworkErrorString (nErr));
1339- for (unsigned int i = 0 ; i <= hSocketMax; i++)
1340- FD_SET (i, &fdsetRecv);
1341- }
1355+ int nErr = WSAGetLastError ();
1356+ LogPrintf (" socket select error %s\n " , NetworkErrorString (nErr));
1357+ for (unsigned int i = 0 ; i <= hSocketMax; i++)
1358+ FD_SET (i, &fdsetRecv);
13421359 FD_ZERO (&fdsetSend);
13431360 FD_ZERO (&fdsetError);
13441361 if (!interruptNet.sleep_for (std::chrono::milliseconds (SELECT_TIMEOUT_MILLISECONDS)))
0 commit comments