Changeset View
Changeset View
Standalone View
Standalone View
src/net.h
Show First 20 Lines • Show All 642 Lines • ▼ Show 20 Lines | struct CNodeStats { | ||||
std::string addrLocal; | std::string addrLocal; | ||||
// Address of this peer | // Address of this peer | ||||
CAddress addr; | CAddress addr; | ||||
// Bind address of our side of the connection | // Bind address of our side of the connection | ||||
CAddress addrBind; | CAddress addrBind; | ||||
uint32_t m_mapped_as; | uint32_t m_mapped_as; | ||||
}; | }; | ||||
/** | |||||
* Transport protocol agnostic message container. | |||||
* Ideally it should only contain receive time, payload, | |||||
* command and size. | |||||
*/ | |||||
class CNetMessage { | class CNetMessage { | ||||
public: | |||||
// received message data | |||||
CDataStream m_recv; | |||||
// time (in microseconds) of message receipt. | |||||
int64_t m_time = 0; | |||||
bool m_valid_netmagic = false; | |||||
bool m_valid_header = false; | |||||
bool m_valid_checksum = false; | |||||
// size of the payload | |||||
uint32_t m_message_size = 0; | |||||
std::string m_command; | |||||
CNetMessage(const CDataStream &recv_in) : m_recv(std::move(recv_in)) {} | |||||
void SetVersion(int nVersionIn) { m_recv.SetVersion(nVersionIn); } | |||||
}; | |||||
/** | |||||
* The TransportDeserializer takes care of holding and deserializing the | |||||
* network receive buffer. It can deserialize the network buffer into a | |||||
* transport protocol agnostic CNetMessage (command & payload) | |||||
*/ | |||||
class TransportDeserializer { | |||||
private: | private: | ||||
mutable CHash256 hasher; | mutable CHash256 hasher; | ||||
mutable uint256 data_hash; | mutable uint256 data_hash; | ||||
public: | public: | ||||
// Parsing header (false) or data (true) | // Parsing header (false) or data (true) | ||||
bool in_data; | bool in_data; | ||||
// Partially received header. | // Partially received header. | ||||
CDataStream hdrbuf; | CDataStream hdrbuf; | ||||
// Complete header. | // Complete header. | ||||
CMessageHeader hdr; | CMessageHeader hdr; | ||||
uint32_t nHdrPos; | uint32_t nHdrPos; | ||||
// Received message data. | // Received message data. | ||||
CDataStream vRecv; | CDataStream vRecv; | ||||
uint32_t nDataPos; | uint32_t nDataPos; | ||||
// Time (in microseconds) of message receipt. | // Time (in microseconds) of message receipt. | ||||
int64_t nTime; | int64_t nTime; | ||||
CNetMessage(const CMessageHeader::MessageMagic &pchMessageStartIn, | TransportDeserializer(const CMessageHeader::MessageMagic &pchMessageStartIn, | ||||
int nTypeIn, int nVersionIn) | int nTypeIn, int nVersionIn) | ||||
: hdrbuf(nTypeIn, nVersionIn), hdr(pchMessageStartIn), | : hdrbuf(nTypeIn, nVersionIn), hdr(pchMessageStartIn), | ||||
vRecv(nTypeIn, nVersionIn) { | vRecv(nTypeIn, nVersionIn) { | ||||
Reset(); | |||||
} | |||||
void Reset() { | |||||
vRecv.clear(); | |||||
hdrbuf.clear(); | |||||
hdrbuf.resize(24); | hdrbuf.resize(24); | ||||
in_data = false; | in_data = false; | ||||
nHdrPos = 0; | nHdrPos = 0; | ||||
nDataPos = 0; | nDataPos = 0; | ||||
nTime = 0; | data_hash.SetNull(); | ||||
hasher.Reset(); | |||||
} | } | ||||
bool complete() const { | bool complete() const { | ||||
if (!in_data) { | if (!in_data) { | ||||
return false; | return false; | ||||
} | } | ||||
return (hdr.nMessageSize == nDataPos); | return (hdr.nMessageSize == nDataPos); | ||||
} | } | ||||
const uint256 &GetMessageHash() const; | const uint256 &GetMessageHash() const; | ||||
void SetVersion(int nVersionIn) { | void SetVersion(int nVersionIn) { | ||||
hdrbuf.SetVersion(nVersionIn); | hdrbuf.SetVersion(nVersionIn); | ||||
vRecv.SetVersion(nVersionIn); | vRecv.SetVersion(nVersionIn); | ||||
} | } | ||||
int readHeader(const Config &config, const char *pch, uint32_t nBytes); | int readHeader(const Config &config, const char *pch, uint32_t nBytes); | ||||
int readData(const char *pch, uint32_t nBytes); | int readData(const char *pch, uint32_t nBytes); | ||||
CNetMessage GetMessage(const Config &config, int64_t time); | |||||
}; | }; | ||||
/** Information about a peer */ | /** Information about a peer */ | ||||
class CNode { | class CNode { | ||||
friend class CConnman; | friend class CConnman; | ||||
public: | public: | ||||
std::unique_ptr<TransportDeserializer> m_deserializer; | |||||
// socket | // socket | ||||
std::atomic<ServiceFlags> nServices{NODE_NONE}; | std::atomic<ServiceFlags> nServices{NODE_NONE}; | ||||
SOCKET hSocket GUARDED_BY(cs_hSocket); | SOCKET hSocket GUARDED_BY(cs_hSocket); | ||||
// Total size of all vSendMsg entries. | // Total size of all vSendMsg entries. | ||||
size_t nSendSize{0}; | size_t nSendSize{0}; | ||||
// Offset inside the first vSendMsg already sent. | // Offset inside the first vSendMsg already sent. | ||||
size_t nSendOffset{0}; | size_t nSendOffset{0}; | ||||
uint64_t nSendBytes GUARDED_BY(cs_vSend){0}; | uint64_t nSendBytes GUARDED_BY(cs_vSend){0}; | ||||
▲ Show 20 Lines • Show All 278 Lines • Show Last 20 Lines |