Using boost::signals2 to message main from net.cpp.

This commit is contained in:
Eric Lombrozo 2013-06-05 20:21:41 -07:00
parent aabdf9e899
commit 501da2503a
5 changed files with 37 additions and 37 deletions

View File

@ -571,9 +571,7 @@ bool AppInit2(boost::thread_group& threadGroup)
// ********************************************************* Step 6: network initialization // ********************************************************* Step 6: network initialization
SetProcessMessagesHandler(ProcessMessages); RegisterNodeSignals(GetNodeSignals());
SetSendMessagesHandler(SendMessages);
SetStartShutdownHandler(StartShutdown);
int nSocksVersion = GetArg("-socks", 5); int nSocksVersion = GetArg("-socks", 5);
if (nSocksVersion != 4 && nSocksVersion != 5) if (nSocksVersion != 4 && nSocksVersion != 5)

View File

@ -160,7 +160,22 @@ void static ResendWalletTransactions()
//////////////////////////////////////////////////////////////////////////////
//
// Registration of network node signals.
//
void RegisterNodeSignals(CNodeSignals& nodeSignals)
{
nodeSignals.ProcessMessages.connect(&ProcessMessages);
nodeSignals.SendMessages.connect(&SendMessages);
}
void UnregisterNodeSignals(CNodeSignals& nodeSignals)
{
nodeSignals.ProcessMessages.disconnect(&ProcessMessages);
nodeSignals.SendMessages.disconnect(&SendMessages);
}

View File

@ -124,6 +124,11 @@ void UnregisterWallet(CWallet* pwalletIn);
/** Push an updated transaction to all registered wallets */ /** Push an updated transaction to all registered wallets */
void SyncWithWallets(const uint256 &hash, const CTransaction& tx, const CBlock* pblock = NULL, bool fUpdate = false); void SyncWithWallets(const uint256 &hash, const CTransaction& tx, const CBlock* pblock = NULL, bool fUpdate = false);
/** Register with a network node to receive its signals */
void RegisterNodeSignals(CNodeSignals& nodeSignals);
/** Unregister a network node */
void UnregisterNodeSignals(CNodeSignals& nodeSignals);
void PushGetBlocks(CNode* pnode, CBlockIndex* pindexBegin, uint256 hashEnd); void PushGetBlocks(CNode* pnode, CBlockIndex* pindexBegin, uint256 hashEnd);
/** Process an incoming block */ /** Process an incoming block */

View File

@ -68,27 +68,9 @@ CCriticalSection cs_vAddedNodes;
static CSemaphore *semOutbound = NULL; static CSemaphore *semOutbound = NULL;
// // Signals for message handling
// Handlers that need to be registered static CNodeSignals g_signals;
// CNodeSignals& GetNodeSignals() { return g_signals; }
static ProcessMessagesHandler fnProcessMessages = NULL;
static SendMessagesHandler fnSendMessages = NULL;
static StartShutdownHandler fnStartShutdown = NULL;
void SetProcessMessagesHandler(ProcessMessagesHandler handler)
{
fnProcessMessages = handler;
}
void SetSendMessagesHandler(SendMessagesHandler handler)
{
fnSendMessages = handler;
}
void SetStartShutdownHandler(StartShutdownHandler handler)
{
fnStartShutdown = handler;
}
void AddOneShot(string strDest) void AddOneShot(string strDest)
{ {
@ -1646,7 +1628,7 @@ void ThreadMessageHandler()
{ {
TRY_LOCK(pnode->cs_vRecvMsg, lockRecv); TRY_LOCK(pnode->cs_vRecvMsg, lockRecv);
if (lockRecv) if (lockRecv)
if (!ProcessMessages(pnode)) if (!g_signals.ProcessMessages(pnode))
pnode->CloseSocketDisconnect(); pnode->CloseSocketDisconnect();
} }
boost::this_thread::interruption_point(); boost::this_thread::interruption_point();
@ -1654,8 +1636,8 @@ void ThreadMessageHandler()
// Send messages // Send messages
{ {
TRY_LOCK(pnode->cs_vSend, lockSend); TRY_LOCK(pnode->cs_vSend, lockSend);
if (lockSend && fnSendMessages) if (lockSend)
fnSendMessages(pnode, pnode == pnodeTrickle); g_signals.SendMessages(pnode, pnode == pnodeTrickle);
} }
boost::this_thread::interruption_point(); boost::this_thread::interruption_point();
} }

View File

@ -8,6 +8,7 @@
#include <deque> #include <deque>
#include <boost/array.hpp> #include <boost/array.hpp>
#include <boost/foreach.hpp> #include <boost/foreach.hpp>
#include <boost/signals2/signal.hpp>
#include <openssl/rand.h> #include <openssl/rand.h>
#ifndef WIN32 #ifndef WIN32
@ -45,16 +46,15 @@ void StartNode(boost::thread_group& threadGroup);
bool StopNode(); bool StopNode();
void SocketSendData(CNode *pnode); void SocketSendData(CNode *pnode);
// // Signals for message handling
// Handlers that require registration struct CNodeSignals
// {
typedef bool (*ProcessMessagesHandler)(CNode* pfrom); boost::signals2::signal<bool (CNode*)> ProcessMessages;
typedef bool (*SendMessagesHandler)(CNode* pto, bool fSendTrickle); boost::signals2::signal<bool (CNode*, bool)> SendMessages;
typedef void (*StartShutdownHandler)(); };
CNodeSignals& GetNodeSignals();
void SetProcessMessagesHandler(ProcessMessagesHandler handler);
void SetSendMessagesHandler(SendMessagesHandler handler);
void SetStartShutdownHandler(StartShutdownHandler handler);
enum enum
{ {