Skip to content

Commit

Permalink
net: Add a CMessageProcessorInterface and use it instead of boost sig…
Browse files Browse the repository at this point in the history
…nals
  • Loading branch information
theuni committed Dec 3, 2016
1 parent 2efcfa5 commit 3f598db
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 129 deletions.
7 changes: 4 additions & 3 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ static const bool DEFAULT_REST_ENABLE = false;
static const bool DEFAULT_DISABLE_SAFEMODE = false;
static const bool DEFAULT_STOPAFTERBLOCKIMPORT = false;

std::unique_ptr<CMessageProcessorInterface> g_msgProc;
std::unique_ptr<CConnman> g_connman;
std::unique_ptr<PeerLogicValidation> peerLogic;

Expand Down Expand Up @@ -207,7 +208,6 @@ void Shutdown()
g_connman.reset();

StopTorControl();
UnregisterNodeSignals(GetNodeSignals());
DumpMempool();

if (fFeeEstimatesInitialized)
Expand Down Expand Up @@ -1134,13 +1134,14 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
// is not yet setup and may end up being set up twice if we
// need to reindex later.

assert(!g_msgProc);
g_msgProc = std::unique_ptr<CMessageProcessorInterface>(new CMessageProcessor);
assert(!g_connman);
g_connman = std::unique_ptr<CConnman>(new CConnman(GetRand(std::numeric_limits<uint64_t>::max()), GetRand(std::numeric_limits<uint64_t>::max())));
g_connman = std::unique_ptr<CConnman>(new CConnman(GetRand(std::numeric_limits<uint64_t>::max()), GetRand(std::numeric_limits<uint64_t>::max()), *g_msgProc));
CConnman& connman = *g_connman;

peerLogic.reset(new PeerLogicValidation(&connman));
RegisterValidationInterface(peerLogic.get());
RegisterNodeSignals(GetNodeSignals());

// sanitize comments per BIP-0014, format user agent and check total size
std::vector<string> uacomments;
Expand Down
25 changes: 25 additions & 0 deletions src/messageinterface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2015 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef BITCOIN_MESSAGE_INTERFACE_H
#define BITCOIN_MESSAGE_INTERFACE_H

typedef int NodeId;

class CConnman;
class CNode;
class CMessageProcessorInterface
{
public:
virtual ~CMessageProcessorInterface() = default;
virtual void OnStartup(CConnman* connman) = 0;
virtual void OnShutdown(CConnman* connman) = 0;
virtual bool ProcessMessages(CNode* pfrom) = 0;
virtual bool SendMessages(CNode* pto) = 0;
virtual void InitializeNode(CNode *pnode) = 0;
virtual void FinalizeNode(NodeId nodeid, bool& fUpdateConnectionTime) = 0;
};

#endif // BITCOIN_MESSAGE_INTERFACE_H
18 changes: 8 additions & 10 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,6 @@ std::string strSubVersion;

limitedmap<uint256, int64_t> mapAlreadyAskedFor(MAX_INV_SZ);

// Signals for message handling
static CNodeSignals g_signals;
CNodeSignals& GetNodeSignals() { return g_signals; }

void CConnman::AddOneShot(const std::string& strDest)
{
LOCK(cs_vOneShots);
Expand Down Expand Up @@ -395,7 +391,7 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
pnode->nServicesExpected = ServiceFlags(addrConnect.nServices & nRelevantServices);
pnode->nTimeConnected = GetTime();
pnode->AddRef();
GetNodeSignals().InitializeNode(pnode, *this);
msgProc.InitializeNode(pnode);
{
LOCK(cs_vNodes);
vNodes.push_back(pnode);
Expand Down Expand Up @@ -1029,7 +1025,7 @@ void CConnman::AcceptConnection(const ListenSocket& hListenSocket) {
CNode* pnode = new CNode(id, nLocalServices, GetBestHeight(), hSocket, addr, CalculateKeyedNetGroup(addr), nonce, "", true);
pnode->AddRef();
pnode->fWhitelisted = whitelisted;
GetNodeSignals().InitializeNode(pnode, *this);
msgProc.InitializeNode(pnode);

LogPrint("net", "connection from %s accepted\n", addr.ToString());

Expand Down Expand Up @@ -1848,7 +1844,7 @@ void CConnman::ThreadMessageHandler()
TRY_LOCK(pnode->cs_vRecvMsg, lockRecv);
if (lockRecv)
{
if (!GetNodeSignals().ProcessMessages(pnode, *this))
if (!msgProc.ProcessMessages(pnode))
pnode->CloseSocketDisconnect();

if (pnode->nSendSize < GetSendBufferSize())
Expand All @@ -1866,7 +1862,7 @@ void CConnman::ThreadMessageHandler()
{
TRY_LOCK(pnode->cs_vSend, lockSend);
if (lockSend)
GetNodeSignals().SendMessages(pnode, *this);
msgProc.SendMessages(pnode);
}
boost::this_thread::interruption_point();
}
Expand Down Expand Up @@ -2057,7 +2053,7 @@ void CConnman::SetNetworkActive(bool active)
uiInterface.NotifyNetworkActiveChanged(fNetworkActive);
}

CConnman::CConnman(uint64_t nSeed0In, uint64_t nSeed1In) : nSeed0(nSeed0In), nSeed1(nSeed1In)
CConnman::CConnman(uint64_t nSeed0In, uint64_t nSeed1In, CMessageProcessorInterface& msgProcIn) : nSeed0(nSeed0In), nSeed1(nSeed1In), msgProc(msgProcIn)
{
fNetworkActive = true;
setBannedIsDirty = false;
Expand All @@ -2079,6 +2075,7 @@ NodeId CConnman::GetNewNodeId()

bool CConnman::Start(boost::thread_group& threadGroup, CScheduler& scheduler, std::string& strNodeError, Options connOptions)
{
msgProc.OnStartup(this);
nTotalBytesRecv = 0;
nTotalBytesSent = 0;
nMaxOutboundTotalBytesSentInCycle = 0;
Expand Down Expand Up @@ -2186,6 +2183,7 @@ instance_of_cnetcleanup;

void CConnman::Stop()
{
msgProc.OnShutdown(this);
LogPrintf("%s\n",__func__);
if (semOutbound)
for (int i=0; i<(nMaxOutbound + nMaxFeeler); i++)
Expand Down Expand Up @@ -2224,7 +2222,7 @@ void CConnman::DeleteNode(CNode* pnode)
{
assert(pnode);
bool fUpdateConnectionTime = false;
GetNodeSignals().FinalizeNode(pnode->GetId(), fUpdateConnectionTime);
msgProc.FinalizeNode(pnode->GetId(), fUpdateConnectionTime);
if(fUpdateConnectionTime)
addrman.Connected(pnode->addr);
delete pnode;
Expand Down
7 changes: 4 additions & 3 deletions src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include <arpa/inet.h>
#endif

#include "messageinterface.h"

#include <boost/filesystem/path.hpp>
#include <boost/foreach.hpp>
#include <boost/signals2/signal.hpp>
Expand Down Expand Up @@ -87,8 +89,6 @@ static const ServiceFlags REQUIRED_SERVICES = NODE_NETWORK;
// NOTE: When adjusting this, update rpcnet:setban's help ("24h")
static const unsigned int DEFAULT_MISBEHAVING_BANTIME = 60 * 60 * 24; // Default 24-hour ban

typedef int NodeId;

struct AddedNodeInfo
{
std::string strAddedNode;
Expand Down Expand Up @@ -140,7 +140,7 @@ class CConnman
uint64_t nMaxOutboundTimeframe = 0;
uint64_t nMaxOutboundLimit = 0;
};
CConnman(uint64_t seed0, uint64_t seed1);
CConnman(uint64_t seed0, uint64_t seed1, CMessageProcessorInterface& msgProcIn);
~CConnman();
bool Start(boost::thread_group& threadGroup, CScheduler& scheduler, std::string& strNodeError, Options options);
void Stop();
Expand Down Expand Up @@ -419,6 +419,7 @@ class CConnman

/** SipHasher seeds for deterministic randomness */
const uint64_t nSeed0, nSeed1;
CMessageProcessorInterface& msgProc;
};
extern std::unique_ptr<CConnman> g_connman;
void Discover(boost::thread_group& threadGroup);
Expand Down
Loading

0 comments on commit 3f598db

Please sign in to comment.