Skip to content

Commit 723e580

Browse files
committed
Merge #10756: net processing: swap out signals for an interface class
2525b97 net: stop both net/net_processing before destroying them (Cory Fields) 80e2e9d net: drop unused connman param (Cory Fields) 8ad663c net: use an interface class rather than signals for message processing (Cory Fields) 28f11e9 net: pass CConnman via pointer rather than reference (Cory Fields) Pull request description: See individual commits. Benefits: - Allows us to begin moving stuff out of CNode and into CNodeState (after #10652 and follow-ups) - Drops boost dependency and overhead - Drops global signal registration - Friendlier backtraces Tree-SHA512: af2038c959dbec25f0c90c74c88dc6a630e6b9e984adf52aceadd6954aa463b6aadfccf979c2459a9f3354326b5077ee02048128eda2a649236fadb595b66ee3
2 parents 638e6c5 + 2525b97 commit 723e580

File tree

8 files changed

+186
-206
lines changed

8 files changed

+186
-206
lines changed

src/init.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,15 @@ void Shutdown()
194194
}
195195
#endif
196196
MapPort(false);
197+
198+
// Because these depend on each-other, we make sure that neither can be
199+
// using the other before destroying them.
197200
UnregisterValidationInterface(peerLogic.get());
201+
g_connman->Stop();
198202
peerLogic.reset();
199203
g_connman.reset();
200204

201205
StopTorControl();
202-
UnregisterNodeSignals(GetNodeSignals());
203206
if (fDumpMempoolLater && gArgs.GetArg("-persistmempool", DEFAULT_PERSIST_MEMPOOL)) {
204207
DumpMempool();
205208
}
@@ -1268,7 +1271,6 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
12681271

12691272
peerLogic.reset(new PeerLogicValidation(&connman));
12701273
RegisterValidationInterface(peerLogic.get());
1271-
RegisterNodeSignals(GetNodeSignals());
12721274

12731275
// sanitize comments per BIP-0014, format user agent and check total size
12741276
std::vector<std::string> uacomments;
@@ -1659,6 +1661,7 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
16591661
connOptions.nMaxFeeler = 1;
16601662
connOptions.nBestHeight = chainActive.Height();
16611663
connOptions.uiInterface = &uiInterface;
1664+
connOptions.m_msgproc = peerLogic.get();
16621665
connOptions.nSendBufferMaxSize = 1000*gArgs.GetArg("-maxsendbuffer", DEFAULT_MAXSENDBUFFER);
16631666
connOptions.nReceiveFloodSize = 1000*gArgs.GetArg("-maxreceivebuffer", DEFAULT_MAXRECEIVEBUFFER);
16641667

src/net.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,6 @@ std::string strSubVersion;
8989

9090
limitedmap<uint256, int64_t> mapAlreadyAskedFor(MAX_INV_SZ);
9191

92-
// Signals for message handling
93-
static CNodeSignals g_signals;
94-
CNodeSignals& GetNodeSignals() { return g_signals; }
95-
9692
void CConnman::AddOneShot(const std::string& strDest)
9793
{
9894
LOCK(cs_vOneShots);
@@ -1114,7 +1110,7 @@ void CConnman::AcceptConnection(const ListenSocket& hListenSocket) {
11141110
CNode* pnode = new CNode(id, nLocalServices, GetBestHeight(), hSocket, addr, CalculateKeyedNetGroup(addr), nonce, addr_bind, "", true);
11151111
pnode->AddRef();
11161112
pnode->fWhitelisted = whitelisted;
1117-
GetNodeSignals().InitializeNode(pnode, *this);
1113+
m_msgproc->InitializeNode(pnode);
11181114

11191115
LogPrint(BCLog::NET, "connection from %s accepted\n", addr.ToString());
11201116

@@ -1966,7 +1962,7 @@ bool CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai
19661962
if (fAddnode)
19671963
pnode->fAddnode = true;
19681964

1969-
GetNodeSignals().InitializeNode(pnode, *this);
1965+
m_msgproc->InitializeNode(pnode);
19701966
{
19711967
LOCK(cs_vNodes);
19721968
vNodes.push_back(pnode);
@@ -1996,16 +1992,16 @@ void CConnman::ThreadMessageHandler()
19961992
continue;
19971993

19981994
// Receive messages
1999-
bool fMoreNodeWork = GetNodeSignals().ProcessMessages(pnode, *this, flagInterruptMsgProc);
1995+
bool fMoreNodeWork = m_msgproc->ProcessMessages(pnode, flagInterruptMsgProc);
20001996
fMoreWork |= (fMoreNodeWork && !pnode->fPauseSend);
20011997
if (flagInterruptMsgProc)
20021998
return;
2003-
20041999
// Send messages
20052000
{
20062001
LOCK(pnode->cs_sendProcessing);
2007-
GetNodeSignals().SendMessages(pnode, *this, flagInterruptMsgProc);
2002+
m_msgproc->SendMessages(pnode, flagInterruptMsgProc);
20082003
}
2004+
20092005
if (flagInterruptMsgProc)
20102006
return;
20112007
}
@@ -2324,6 +2320,7 @@ bool CConnman::Start(CScheduler& scheduler, const Options& connOptions)
23242320
//
23252321
// Start threads
23262322
//
2323+
assert(m_msgproc);
23272324
InterruptSocks5(false);
23282325
interruptNet.reset();
23292326
flagInterruptMsgProc = false;
@@ -2450,9 +2447,10 @@ void CConnman::DeleteNode(CNode* pnode)
24502447
{
24512448
assert(pnode);
24522449
bool fUpdateConnectionTime = false;
2453-
GetNodeSignals().FinalizeNode(pnode->GetId(), fUpdateConnectionTime);
2454-
if(fUpdateConnectionTime)
2450+
m_msgproc->FinalizeNode(pnode->GetId(), fUpdateConnectionTime);
2451+
if(fUpdateConnectionTime) {
24552452
addrman.Connected(pnode->addr);
2453+
}
24562454
delete pnode;
24572455
}
24582456

src/net.h

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
#include <arpa/inet.h>
3434
#endif
3535

36-
#include <boost/signals2/signal.hpp>
3736

3837
class CScheduler;
3938
class CNode;
@@ -116,7 +115,7 @@ struct CSerializedNetMsg
116115
std::string command;
117116
};
118117

119-
118+
class NetEventsInterface;
120119
class CConnman
121120
{
122121
public:
@@ -138,6 +137,7 @@ class CConnman
138137
int nMaxFeeler = 0;
139138
int nBestHeight = 0;
140139
CClientUIInterface* uiInterface = nullptr;
140+
NetEventsInterface* m_msgproc = nullptr;
141141
unsigned int nSendBufferMaxSize = 0;
142142
unsigned int nReceiveFloodSize = 0;
143143
uint64_t nMaxOutboundTimeframe = 0;
@@ -158,6 +158,7 @@ class CConnman
158158
nMaxFeeler = connOptions.nMaxFeeler;
159159
nBestHeight = connOptions.nBestHeight;
160160
clientInterface = connOptions.uiInterface;
161+
m_msgproc = connOptions.m_msgproc;
161162
nSendBufferMaxSize = connOptions.nSendBufferMaxSize;
162163
nReceiveFloodSize = connOptions.nReceiveFloodSize;
163164
nMaxOutboundTimeframe = connOptions.nMaxOutboundTimeframe;
@@ -398,6 +399,7 @@ class CConnman
398399
int nMaxFeeler;
399400
std::atomic<int> nBestHeight;
400401
CClientUIInterface* clientInterface;
402+
NetEventsInterface* m_msgproc;
401403

402404
/** SipHasher seeds for deterministic randomness */
403405
const uint64_t nSeed0, nSeed1;
@@ -438,19 +440,18 @@ struct CombinerAll
438440
}
439441
};
440442

441-
// Signals for message handling
442-
struct CNodeSignals
443+
/**
444+
* Interface for message handling
445+
*/
446+
class NetEventsInterface
443447
{
444-
boost::signals2::signal<bool (CNode*, CConnman&, std::atomic<bool>&), CombinerAll> ProcessMessages;
445-
boost::signals2::signal<bool (CNode*, CConnman&, std::atomic<bool>&), CombinerAll> SendMessages;
446-
boost::signals2::signal<void (CNode*, CConnman&)> InitializeNode;
447-
boost::signals2::signal<void (NodeId, bool&)> FinalizeNode;
448+
public:
449+
virtual bool ProcessMessages(CNode* pnode, std::atomic<bool>& interrupt) = 0;
450+
virtual bool SendMessages(CNode* pnode, std::atomic<bool>& interrupt) = 0;
451+
virtual void InitializeNode(CNode* pnode) = 0;
452+
virtual void FinalizeNode(NodeId id, bool& update_connection_time) = 0;
448453
};
449454

450-
451-
CNodeSignals& GetNodeSignals();
452-
453-
454455
enum
455456
{
456457
LOCAL_NONE, // unknown

0 commit comments

Comments
 (0)