@@ -1261,28 +1261,10 @@ void CConnman::InactivityCheck(CNode *pnode)
1261
1261
}
1262
1262
}
1263
1263
1264
- void CConnman::SocketHandler ( )
1264
+ bool CConnman::GenerateSelectSet (std::set<SOCKET> &recv_set, std::set<SOCKET> &send_set, std::set<SOCKET> &error_set )
1265
1265
{
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
-
1282
1266
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 );
1286
1268
}
1287
1269
1288
1270
{
@@ -1311,34 +1293,69 @@ void CConnman::SocketHandler()
1311
1293
if (pnode->hSocket == INVALID_SOCKET)
1312
1294
continue ;
1313
1295
1314
- FD_SET (pnode->hSocket , &fdsetError);
1315
- hSocketMax = std::max (hSocketMax, pnode->hSocket );
1316
- have_fds = true ;
1317
-
1296
+ error_set.insert (pnode->hSocket );
1318
1297
if (select_send) {
1319
- FD_SET (pnode->hSocket , &fdsetSend );
1298
+ send_set. insert (pnode->hSocket );
1320
1299
continue ;
1321
1300
}
1322
1301
if (select_recv) {
1323
- FD_SET (pnode->hSocket , &fdsetRecv );
1302
+ recv_set. insert (pnode->hSocket );
1324
1303
}
1325
1304
}
1326
1305
}
1327
1306
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
+
1330
1350
if (interruptNet)
1331
1351
return ;
1332
1352
1333
1353
if (nSelect == SOCKET_ERROR)
1334
1354
{
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);
1342
1359
FD_ZERO (&fdsetSend);
1343
1360
FD_ZERO (&fdsetError);
1344
1361
if (!interruptNet.sleep_for (std::chrono::milliseconds (SELECT_TIMEOUT_MILLISECONDS)))
0 commit comments