Skip to content

Commit f52b6b2

Browse files
committed
net: split CConnman::SocketHandler()
`CConnman::SocketHandler()` does 3 things: 1. Check sockets for readiness 2. Process ready listening sockets 3. Process ready connected sockets Split the processing (2. and 3.) into separate methods to make the code easier to grasp. Also, move the processing of listening sockets after the processing of connected sockets to make it obvious that there is no dependency and also explicitly release the snapshot before dealing with listening sockets - it is only necessary for the connected sockets part.
1 parent c7eb19e commit f52b6b2

File tree

2 files changed

+57
-17
lines changed

2 files changed

+57
-17
lines changed

src/net.cpp

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,28 +1502,33 @@ void CConnman::SocketEvents(const std::vector<CNode*>& nodes,
15021502

15031503
void CConnman::SocketHandler()
15041504
{
1505-
const NodesSnapshot snap{*this, /*shuffle=*/false};
1505+
std::set<SOCKET> recv_set;
1506+
std::set<SOCKET> send_set;
1507+
std::set<SOCKET> error_set;
15061508

1507-
std::set<SOCKET> recv_set, send_set, error_set;
1508-
SocketEvents(snap.Nodes(), recv_set, send_set, error_set);
1509+
{
1510+
const NodesSnapshot snap{*this, /*shuffle=*/false};
15091511

1510-
if (interruptNet) return;
1512+
// Check for the readiness of the already connected sockets and the
1513+
// listening sockets in one call ("readiness" as in poll(2) or
1514+
// select(2)). If none are ready, wait for a short while and return
1515+
// empty sets.
1516+
SocketEvents(snap.Nodes(), recv_set, send_set, error_set);
15111517

1512-
//
1513-
// Accept new connections
1514-
//
1515-
for (const ListenSocket& hListenSocket : vhListenSocket)
1516-
{
1517-
if (hListenSocket.socket != INVALID_SOCKET && recv_set.count(hListenSocket.socket) > 0)
1518-
{
1519-
AcceptConnection(hListenSocket);
1520-
}
1518+
// Service (send/receive) each of the already connected nodes.
1519+
SocketHandlerConnected(snap.Nodes(), recv_set, send_set, error_set);
15211520
}
15221521

1523-
//
1524-
// Service each socket
1525-
//
1526-
for (CNode* pnode : snap.Nodes()) {
1522+
// Accept new connections from listening sockets.
1523+
SocketHandlerListening(recv_set);
1524+
}
1525+
1526+
void CConnman::SocketHandlerConnected(const std::vector<CNode*>& nodes,
1527+
const std::set<SOCKET>& recv_set,
1528+
const std::set<SOCKET>& send_set,
1529+
const std::set<SOCKET>& error_set)
1530+
{
1531+
for (CNode* pnode : nodes) {
15271532
if (interruptNet)
15281533
return;
15291534

@@ -1607,6 +1612,18 @@ void CConnman::SocketHandler()
16071612
}
16081613
}
16091614

1615+
void CConnman::SocketHandlerListening(const std::set<SOCKET>& recv_set)
1616+
{
1617+
for (const ListenSocket& listen_socket : vhListenSocket) {
1618+
if (interruptNet) {
1619+
return;
1620+
}
1621+
if (listen_socket.socket != INVALID_SOCKET && recv_set.count(listen_socket.socket) > 0) {
1622+
AcceptConnection(listen_socket);
1623+
}
1624+
}
1625+
}
1626+
16101627
void CConnman::ThreadSocketHandler()
16111628
{
16121629
SetSyscallSandboxPolicy(SyscallSandboxPolicy::NET);

src/net.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,7 +1010,30 @@ class CConnman
10101010
std::set<SOCKET>& send_set,
10111011
std::set<SOCKET>& error_set);
10121012

1013+
/**
1014+
* Check connected and listening sockets for IO readiness and process them accordingly.
1015+
*/
10131016
void SocketHandler();
1017+
1018+
/**
1019+
* Do the read/write for connected sockets that are ready for IO.
1020+
* @param[in] nodes Nodes to process. The socket of each node is checked against
1021+
* `recv_set`, `send_set` and `error_set`.
1022+
* @param[in] recv_set Sockets that are ready for read.
1023+
* @param[in] send_set Sockets that are ready for send.
1024+
* @param[in] error_set Sockets that have an exceptional condition (error).
1025+
*/
1026+
void SocketHandlerConnected(const std::vector<CNode*>& nodes,
1027+
const std::set<SOCKET>& recv_set,
1028+
const std::set<SOCKET>& send_set,
1029+
const std::set<SOCKET>& error_set);
1030+
1031+
/**
1032+
* Accept incoming connections, one from each read-ready listening socket.
1033+
* @param[in] recv_set Sockets that are ready for read.
1034+
*/
1035+
void SocketHandlerListening(const std::set<SOCKET>& recv_set);
1036+
10141037
void ThreadSocketHandler();
10151038
void ThreadDNSAddressSeed();
10161039

0 commit comments

Comments
 (0)