Move GenerateSelectSet logic to private method.
This separates the socket event collection logic from the logic deciding which events we're interested in at all.
This commit is contained in:
parent
1e6afd0dbc
commit
7e403c0ae7
2 changed files with 53 additions and 35 deletions
87
src/net.cpp
87
src/net.cpp
|
@ -1261,28 +1261,10 @@ void CConnman::InactivityCheck(CNode *pnode)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CConnman::SocketHandler()
|
bool CConnman::GenerateSelectSet(std::set<SOCKET> &recv_set, std::set<SOCKET> &send_set, std::set<SOCKET> &error_set)
|
||||||
{
|
{
|
||||||
//
|
|
||||||
// Find which sockets have data to receive
|
|
||||||
//
|
|
||||||
struct timeval timeout;
|
|
||||||
timeout.tv_sec = 0;
|
|
||||||
timeout.tv_usec = SELECT_TIMEOUT_MILLISECONDS * 1000; // frequency to poll pnode->vSend
|
|
||||||
|
|
||||||
fd_set fdsetRecv;
|
|
||||||
fd_set fdsetSend;
|
|
||||||
fd_set fdsetError;
|
|
||||||
FD_ZERO(&fdsetRecv);
|
|
||||||
FD_ZERO(&fdsetSend);
|
|
||||||
FD_ZERO(&fdsetError);
|
|
||||||
SOCKET hSocketMax = 0;
|
|
||||||
bool have_fds = false;
|
|
||||||
|
|
||||||
for (const ListenSocket& hListenSocket : vhListenSocket) {
|
for (const ListenSocket& hListenSocket : vhListenSocket) {
|
||||||
FD_SET(hListenSocket.socket, &fdsetRecv);
|
recv_set.insert(hListenSocket.socket);
|
||||||
hSocketMax = std::max(hSocketMax, hListenSocket.socket);
|
|
||||||
have_fds = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
|
@ -1311,34 +1293,69 @@ void CConnman::SocketHandler()
|
||||||
if (pnode->hSocket == INVALID_SOCKET)
|
if (pnode->hSocket == INVALID_SOCKET)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
FD_SET(pnode->hSocket, &fdsetError);
|
error_set.insert(pnode->hSocket);
|
||||||
hSocketMax = std::max(hSocketMax, pnode->hSocket);
|
|
||||||
have_fds = true;
|
|
||||||
|
|
||||||
if (select_send) {
|
if (select_send) {
|
||||||
FD_SET(pnode->hSocket, &fdsetSend);
|
send_set.insert(pnode->hSocket);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (select_recv) {
|
if (select_recv) {
|
||||||
FD_SET(pnode->hSocket, &fdsetRecv);
|
recv_set.insert(pnode->hSocket);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int nSelect = select(have_fds ? hSocketMax + 1 : 0,
|
return !recv_set.empty() || !send_set.empty() || !error_set.empty();
|
||||||
&fdsetRecv, &fdsetSend, &fdsetError, &timeout);
|
}
|
||||||
|
|
||||||
|
void CConnman::SocketHandler()
|
||||||
|
{
|
||||||
|
std::set<SOCKET> recv_select_set, send_select_set, error_select_set;
|
||||||
|
if (!GenerateSelectSet(recv_select_set, send_select_set, error_select_set)) {
|
||||||
|
interruptNet.sleep_for(std::chrono::milliseconds(SELECT_TIMEOUT_MILLISECONDS));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Find which sockets have data to receive
|
||||||
|
//
|
||||||
|
struct timeval timeout;
|
||||||
|
timeout.tv_sec = 0;
|
||||||
|
timeout.tv_usec = SELECT_TIMEOUT_MILLISECONDS * 1000; // frequency to poll pnode->vSend
|
||||||
|
|
||||||
|
fd_set fdsetRecv;
|
||||||
|
fd_set fdsetSend;
|
||||||
|
fd_set fdsetError;
|
||||||
|
FD_ZERO(&fdsetRecv);
|
||||||
|
FD_ZERO(&fdsetSend);
|
||||||
|
FD_ZERO(&fdsetError);
|
||||||
|
SOCKET hSocketMax = 0;
|
||||||
|
|
||||||
|
for (SOCKET hSocket : recv_select_set) {
|
||||||
|
FD_SET(hSocket, &fdsetRecv);
|
||||||
|
hSocketMax = std::max(hSocketMax, hSocket);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (SOCKET hSocket : send_select_set) {
|
||||||
|
FD_SET(hSocket, &fdsetSend);
|
||||||
|
hSocketMax = std::max(hSocketMax, hSocket);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (SOCKET hSocket : error_select_set) {
|
||||||
|
FD_SET(hSocket, &fdsetError);
|
||||||
|
hSocketMax = std::max(hSocketMax, hSocket);
|
||||||
|
}
|
||||||
|
|
||||||
|
int nSelect = select(hSocketMax + 1, &fdsetRecv, &fdsetSend, &fdsetError, &timeout);
|
||||||
|
|
||||||
if (interruptNet)
|
if (interruptNet)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (nSelect == SOCKET_ERROR)
|
if (nSelect == SOCKET_ERROR)
|
||||||
{
|
{
|
||||||
if (have_fds)
|
int nErr = WSAGetLastError();
|
||||||
{
|
LogPrintf("socket select error %s\n", NetworkErrorString(nErr));
|
||||||
int nErr = WSAGetLastError();
|
for (unsigned int i = 0; i <= hSocketMax; i++)
|
||||||
LogPrintf("socket select error %s\n", NetworkErrorString(nErr));
|
FD_SET(i, &fdsetRecv);
|
||||||
for (unsigned int i = 0; i <= hSocketMax; i++)
|
|
||||||
FD_SET(i, &fdsetRecv);
|
|
||||||
}
|
|
||||||
FD_ZERO(&fdsetSend);
|
FD_ZERO(&fdsetSend);
|
||||||
FD_ZERO(&fdsetError);
|
FD_ZERO(&fdsetError);
|
||||||
if (!interruptNet.sleep_for(std::chrono::milliseconds(SELECT_TIMEOUT_MILLISECONDS)))
|
if (!interruptNet.sleep_for(std::chrono::milliseconds(SELECT_TIMEOUT_MILLISECONDS)))
|
||||||
|
|
|
@ -342,6 +342,7 @@ private:
|
||||||
void DisconnectNodes();
|
void DisconnectNodes();
|
||||||
void NotifyNumConnectionsChanged();
|
void NotifyNumConnectionsChanged();
|
||||||
void InactivityCheck(CNode *pnode);
|
void InactivityCheck(CNode *pnode);
|
||||||
|
bool GenerateSelectSet(std::set<SOCKET> &recv_set, std::set<SOCKET> &send_set, std::set<SOCKET> &error_set);
|
||||||
void SocketHandler();
|
void SocketHandler();
|
||||||
void ThreadSocketHandler();
|
void ThreadSocketHandler();
|
||||||
void ThreadDNSAddressSeed();
|
void ThreadDNSAddressSeed();
|
||||||
|
|
Loading…
Add table
Reference in a new issue