Changeset View
Changeset View
Standalone View
Standalone View
src/net.h
Show First 20 Lines • Show All 673 Lines • ▼ Show 20 Lines | |||||
}; | }; | ||||
/** | /** | ||||
* The TransportDeserializer takes care of holding and deserializing the | * The TransportDeserializer takes care of holding and deserializing the | ||||
* network receive buffer. It can deserialize the network buffer into a | * network receive buffer. It can deserialize the network buffer into a | ||||
* transport protocol agnostic CNetMessage (command & payload) | * transport protocol agnostic CNetMessage (command & payload) | ||||
*/ | */ | ||||
class TransportDeserializer { | class TransportDeserializer { | ||||
public: | |||||
// prepare for next message | |||||
virtual void Reset() = 0; | |||||
// returns true if the current deserialization is complete | |||||
virtual bool Complete() const = 0; | |||||
// checks if the potential message in deserialization is oversized | |||||
virtual bool OversizedMessageDetected(const Config &config) const = 0; | |||||
// set the serialization context version | |||||
virtual void SetVersion(int version) = 0; | |||||
// read and deserialize data | |||||
virtual int Read(const Config &config, const char *data, | |||||
uint32_t bytes) = 0; | |||||
// decomposes a message from the context | |||||
virtual CNetMessage GetMessage(const Config &config, int64_t time) = 0; | |||||
virtual ~TransportDeserializer() {} | |||||
}; | |||||
class V1TransportDeserializer : public TransportDeserializer { | |||||
private: | private: | ||||
mutable CHash256 hasher; | mutable CHash256 hasher; | ||||
mutable uint256 data_hash; | mutable uint256 data_hash; | ||||
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; | |||||
// Received message data. | // Received message data. | ||||
CDataStream vRecv; | CDataStream vRecv; | ||||
uint32_t nHdrPos; | |||||
uint32_t nDataPos; | uint32_t nDataPos; | ||||
// Time (in microseconds) of message receipt. | const uint256 &GetMessageHash() const; | ||||
int64_t nTime; | int readHeader(const Config &config, const char *pch, uint32_t nBytes); | ||||
int readData(const char *pch, uint32_t nBytes); | |||||
TransportDeserializer(const CMessageHeader::MessageMagic &pchMessageStartIn, | public: | ||||
int nTypeIn, int nVersionIn) | V1TransportDeserializer( | ||||
const CMessageHeader::MessageMagic &pchMessageStartIn, int nTypeIn, | |||||
int nVersionIn) | |||||
: hdrbuf(nTypeIn, nVersionIn), hdr(pchMessageStartIn), | : hdrbuf(nTypeIn, nVersionIn), hdr(pchMessageStartIn), | ||||
vRecv(nTypeIn, nVersionIn) { | vRecv(nTypeIn, nVersionIn) { | ||||
Reset(); | Reset(); | ||||
} | } | ||||
void Reset() { | void Reset() { | ||||
vRecv.clear(); | vRecv.clear(); | ||||
hdrbuf.clear(); | hdrbuf.clear(); | ||||
hdrbuf.resize(24); | hdrbuf.resize(24); | ||||
in_data = false; | in_data = false; | ||||
nHdrPos = 0; | nHdrPos = 0; | ||||
nDataPos = 0; | nDataPos = 0; | ||||
data_hash.SetNull(); | data_hash.SetNull(); | ||||
hasher.Reset(); | 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; | |||||
void SetVersion(int nVersionIn) { | void SetVersion(int nVersionIn) { | ||||
hdrbuf.SetVersion(nVersionIn); | hdrbuf.SetVersion(nVersionIn); | ||||
vRecv.SetVersion(nVersionIn); | vRecv.SetVersion(nVersionIn); | ||||
} | } | ||||
bool OversizedMessageDetected(const Config &config) const { | |||||
int readHeader(const Config &config, const char *pch, uint32_t nBytes); | return (in_data && hdr.IsOversized(config)); | ||||
int readData(const char *pch, uint32_t nBytes); | } | ||||
int Read(const Config &config, const char *pch, uint32_t nBytes) { | |||||
return in_data ? readData(pch, nBytes) | |||||
: readHeader(config, pch, nBytes); | |||||
} | |||||
CNetMessage GetMessage(const Config &config, int64_t time); | 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; | ||||
▲ Show 20 Lines • Show All 289 Lines • Show Last 20 Lines |