Skip to content

Commit 897e342

Browse files
committed
[net] Encapsulate CNode message polling
1 parent cc5cdf8 commit 897e342

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

src/net.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2812,6 +2812,20 @@ void CNode::MarkReceivedMsgsForProcessing(unsigned int recv_flood_size)
28122812
fPauseRecv = nProcessQueueSize > recv_flood_size;
28132813
}
28142814

2815+
std::optional<std::pair<CNetMessage, bool>> CNode::PollMessage(size_t recv_flood_size)
2816+
{
2817+
LOCK(cs_vProcessMsg);
2818+
if (vProcessMsg.empty()) return std::nullopt;
2819+
2820+
std::list<CNetMessage> msgs;
2821+
// Just take one message
2822+
msgs.splice(msgs.begin(), vProcessMsg, vProcessMsg.begin());
2823+
nProcessQueueSize -= msgs.front().m_raw_message_size;
2824+
fPauseRecv = nProcessQueueSize > recv_flood_size;
2825+
2826+
return std::make_pair(std::move(msgs.front()), !vProcessMsg.empty());
2827+
}
2828+
28152829
bool CConnman::NodeFullyConnected(const CNode* pnode)
28162830
{
28172831
return pnode && pnode->fSuccessfullyConnected && !pnode->fDisconnect;

src/net.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,14 @@ class CNode
426426
void MarkReceivedMsgsForProcessing(unsigned int recv_flood_size)
427427
EXCLUSIVE_LOCKS_REQUIRED(!cs_vProcessMsg);
428428

429+
/** Poll the next message from the processing queue of this connection.
430+
*
431+
* Returns std::nullopt if the processing queue is empty, or a pair
432+
* consisting of the message and a bool that indicates if the processing
433+
* queue has more entries. */
434+
std::optional<std::pair<CNetMessage, bool>> PollMessage(size_t recv_flood_size)
435+
EXCLUSIVE_LOCKS_REQUIRED(!cs_vProcessMsg);
436+
429437
bool IsOutboundOrBlockRelayConn() const {
430438
switch (m_conn_type) {
431439
case ConnectionType::OUTBOUND_FULL_RELAY:

src/net_processing.cpp

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4854,8 +4854,6 @@ bool PeerManagerImpl::ProcessMessages(CNode* pfrom, std::atomic<bool>& interrupt
48544854
{
48554855
AssertLockHeld(g_msgproc_mutex);
48564856

4857-
bool fMoreWork = false;
4858-
48594857
PeerRef peer = GetPeerRef(pfrom->GetId());
48604858
if (peer == nullptr) return false;
48614859

@@ -4883,17 +4881,14 @@ bool PeerManagerImpl::ProcessMessages(CNode* pfrom, std::atomic<bool>& interrupt
48834881
// Don't bother if send buffer is too full to respond anyway
48844882
if (pfrom->fPauseSend) return false;
48854883

4886-
std::list<CNetMessage> msgs;
4887-
{
4888-
LOCK(pfrom->cs_vProcessMsg);
4889-
if (pfrom->vProcessMsg.empty()) return false;
4890-
// Just take one message
4891-
msgs.splice(msgs.begin(), pfrom->vProcessMsg, pfrom->vProcessMsg.begin());
4892-
pfrom->nProcessQueueSize -= msgs.front().m_raw_message_size;
4893-
pfrom->fPauseRecv = pfrom->nProcessQueueSize > m_connman.GetReceiveFloodSize();
4894-
fMoreWork = !pfrom->vProcessMsg.empty();
4895-
}
4896-
CNetMessage& msg(msgs.front());
4884+
auto poll_result{pfrom->PollMessage(m_connman.GetReceiveFloodSize())};
4885+
if (!poll_result) {
4886+
// No message to process
4887+
return false;
4888+
}
4889+
4890+
CNetMessage& msg{poll_result->first};
4891+
bool fMoreWork = poll_result->second;
48974892

48984893
TRACE6(net, inbound_message,
48994894
pfrom->GetId(),

0 commit comments

Comments
 (0)