Skip to content

Commit

Permalink
net: rearrange so that socket accesses can be grouped together
Browse files Browse the repository at this point in the history
  • Loading branch information
theuni authored and Fuzzbawls committed Sep 2, 2020
1 parent 6f731dc commit cc8a93c
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1156,9 +1156,6 @@ void CConnman::ThreadSocketHandler()
for (CNode* pnode : vNodes) {
if (pnode->hSocket == INVALID_SOCKET)
continue;
FD_SET(pnode->hSocket, &fdsetError);
hSocketMax = std::max(hSocketMax, pnode->hSocket);
have_fds = true;

// Implement the following logic:
// * If there is data to send, select() for sending data. As this only
Expand All @@ -1170,16 +1167,24 @@ void CConnman::ThreadSocketHandler()
// receiving data.
// * Hand off all complete messages to the processor, to be handled without
// blocking here.

bool select_recv = !pnode->fPauseRecv;
bool select_send;
{
LOCK(pnode->cs_vSend);
if (!pnode->vSendMsg.empty()) {
FD_SET(pnode->hSocket, &fdsetSend);
continue;
}
select_send = !pnode->vSendMsg.empty();
}
{
if (!pnode->fPauseRecv)
FD_SET(pnode->hSocket, &fdsetRecv);

FD_SET(pnode->hSocket, &fdsetError);
hSocketMax = std::max(hSocketMax, pnode->hSocket);
have_fds = true;

if (select_send) {
FD_SET(pnode->hSocket, &fdsetSend);
continue;
}
if (select_recv) {
FD_SET(pnode->hSocket, &fdsetRecv);
}
}
}
Expand Down Expand Up @@ -1228,9 +1233,15 @@ void CConnman::ThreadSocketHandler()
//
// Receive
//
bool recvSet = false;
bool sendSet = false;
bool errorSet = false;
if (pnode->hSocket == INVALID_SOCKET)
continue;
if (FD_ISSET(pnode->hSocket, &fdsetRecv) || FD_ISSET(pnode->hSocket, &fdsetError)) {
recvSet = FD_ISSET(pnode->hSocket, &fdsetRecv);
sendSet = FD_ISSET(pnode->hSocket, &fdsetSend);
errorSet = FD_ISSET(pnode->hSocket, &fdsetError);
if (recvSet || errorSet) {
{
{
// typical socket buffer is 8K-64K
Expand Down Expand Up @@ -1278,9 +1289,7 @@ void CConnman::ThreadSocketHandler()
//
// Send
//
if (pnode->hSocket == INVALID_SOCKET)
continue;
if (FD_ISSET(pnode->hSocket, &fdsetSend)) {
if (sendSet) {
LOCK(pnode->cs_vSend);
size_t nBytes = SocketSendData(pnode);
if (nBytes)
Expand Down

0 comments on commit cc8a93c

Please sign in to comment.