diff options
Diffstat (limited to 'src/net.h')
-rw-r--r-- | src/net.h | 65 |
1 files changed, 50 insertions, 15 deletions
@@ -1,5 +1,5 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2012 The Bitcoin developers +// Copyright (c) 2009-2013 The Bitcoin developers // Distributed under the MIT/X11 software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_NET_H @@ -8,6 +8,7 @@ #include <deque> #include <boost/array.hpp> #include <boost/foreach.hpp> +#include <boost/signals2/signal.hpp> #include <openssl/rand.h> #ifndef WIN32 @@ -22,9 +23,11 @@ #include "hash.h" #include "bloom.h" +/** The maximum number of entries in an 'inv' protocol message */ +static const unsigned int MAX_INV_SZ = 50000; + class CNode; class CBlockIndex; -extern int nBestHeight; @@ -45,6 +48,17 @@ void StartNode(boost::thread_group& threadGroup); bool StopNode(); void SocketSendData(CNode *pnode); +// Signals for message handling +struct CNodeSignals +{ + boost::signals2::signal<int ()> GetHeight; + boost::signals2::signal<bool (CNode*)> ProcessMessages; + boost::signals2::signal<bool (CNode*, bool)> SendMessages; +}; + +CNodeSignals& GetNodeSignals(); + + enum { LOCAL_NONE, // unknown @@ -105,6 +119,9 @@ public: uint64 nSendBytes; uint64 nRecvBytes; bool fSyncNode; + double dPingTime; + double dPingWait; + std::string addrLocal; }; @@ -220,6 +237,12 @@ public: CCriticalSection cs_inventory; std::multimap<int64, CInv> mapAskFor; + // Ping time measurement + uint64 nPingNonceSent; + int64 nPingUsecStart; + int64 nPingUsecTime; + bool fPingQueued; + CNode(SOCKET hSocketIn, CAddress addrIn, std::string addrNameIn = "", bool fInboundIn=false) : ssSend(SER_NETWORK, MIN_PROTO_VERSION) { nServices = 0; @@ -253,7 +276,11 @@ public: nMisbehavior = 0; fRelayTxes = false; setInventoryKnown.max_size(SendBufferSize() / 1000); - pfilter = NULL; + pfilter = new CBloomFilter(); + nPingNonceSent = 0; + nPingUsecStart = 0; + nPingUsecTime = 0; + fPingQueued = false; // Be shy and don't send version until we hear if (hSocket != INVALID_SOCKET && !fInbound) @@ -272,8 +299,15 @@ public: } private: + // Network usage totals + static CCriticalSection cs_totalBytesRecv; + static CCriticalSection cs_totalBytesSent; + static uint64 nTotalBytesRecv; + static uint64 nTotalBytesSent; + CNode(const CNode&); void operator=(const CNode&); + public: @@ -287,7 +321,7 @@ public: unsigned int GetTotalRecvSize() { unsigned int total = 0; - BOOST_FOREACH(const CNetMessage &msg, vRecvMsg) + BOOST_FOREACH(const CNetMessage &msg, vRecvMsg) total += msg.vRecv.size() + 24; return total; } @@ -358,8 +392,7 @@ public: nRequestTime = it->second; else nRequestTime = 0; - if (fDebugNet) - printf("askfor %s %"PRI64d" (%s)\n", inv.ToString().c_str(), nRequestTime, DateTimeStrFormat("%H:%M:%S", nRequestTime/1000000).c_str()); + LogPrint("net", "askfor %s %"PRI64d" (%s)\n", inv.ToString().c_str(), nRequestTime, DateTimeStrFormat("%H:%M:%S", nRequestTime/1000000).c_str()); // Make sure not to reuse time indexes to keep things in the same order int64 nNow = (GetTime() - 1) * 1000000; @@ -385,8 +418,7 @@ public: ENTER_CRITICAL_SECTION(cs_vSend); assert(ssSend.size() == 0); ssSend << CMessageHeader(pszCommand, 0); - if (fDebug) - printf("sending: %s ", pszCommand); + LogPrint("net", "sending: %s ", pszCommand); } // TODO: Document the precondition of this function. Is cs_vSend locked? @@ -396,8 +428,7 @@ public: LEAVE_CRITICAL_SECTION(cs_vSend); - if (fDebug) - printf("(aborted)\n"); + LogPrint("net", "(aborted)\n"); } // TODO: Document the precondition of this function. Is cs_vSend locked? @@ -405,7 +436,7 @@ public: { if (mapArgs.count("-dropmessagestest") && GetRand(atoi(mapArgs["-dropmessagestest"])) == 0) { - printf("dropmessages DROPPING SEND MESSAGE\n"); + LogPrint("net", "dropmessages DROPPING SEND MESSAGE\n"); AbortMessage(); return; } @@ -424,9 +455,7 @@ public: assert(ssSend.size () >= CMessageHeader::CHECKSUM_OFFSET + sizeof(nChecksum)); memcpy((char*)&ssSend[CMessageHeader::CHECKSUM_OFFSET], &nChecksum, sizeof(nChecksum)); - if (fDebug) { - printf("(%d bytes)\n", nSize); - } + LogPrint("net", "(%d bytes)\n", nSize); std::deque<CSerializeData>::iterator it = vSendMsg.insert(vSendMsg.end(), CSerializeData()); ssSend.GetAndClear(*it); @@ -600,7 +629,6 @@ public: } } - void PushGetBlocks(CBlockIndex* pindexBegin, uint256 hashEnd); bool IsSubscribed(unsigned int nChannel); void Subscribe(unsigned int nChannel, unsigned int nHops=0); void CancelSubscribe(unsigned int nChannel); @@ -626,6 +654,13 @@ public: static bool IsBanned(CNetAddr ip); bool Misbehaving(int howmuch); // 1 == a little, 100 == a lot void copyStats(CNodeStats &stats); + + // Network stats + static void RecordBytesRecv(uint64 bytes); + static void RecordBytesSent(uint64 bytes); + + static uint64 GetTotalBytesRecv(); + static uint64 GetTotalBytesSent(); }; |