Changeset View
Changeset View
Standalone View
Standalone View
src/net.h
Show First 20 Lines • Show All 674 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: | public: | ||||
// prepare for next message | |||||
virtual void Reset() = 0; | |||||
// returns true if the current deserialization is complete | // returns true if the current deserialization is complete | ||||
virtual bool Complete() const = 0; | virtual bool Complete() const = 0; | ||||
// set the serialization context version | // set the serialization context version | ||||
virtual void SetVersion(int version) = 0; | virtual void SetVersion(int version) = 0; | ||||
// read and deserialize data | // read and deserialize data | ||||
virtual int Read(const Config &config, const char *data, | virtual int Read(const Config &config, const char *data, | ||||
uint32_t bytes) = 0; | uint32_t bytes) = 0; | ||||
// decomposes a message from the context | // decomposes a message from the context | ||||
Show All 16 Lines | private: | ||||
CDataStream vRecv; | CDataStream vRecv; | ||||
uint32_t nHdrPos; | uint32_t nHdrPos; | ||||
uint32_t nDataPos; | uint32_t nDataPos; | ||||
const uint256 &GetMessageHash() const; | const uint256 &GetMessageHash() const; | ||||
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); | ||||
public: | |||||
V1TransportDeserializer( | |||||
const CMessageHeader::MessageMagic &pchMessageStartIn, int nTypeIn, | |||||
int nVersionIn) | |||||
: hdrbuf(nTypeIn, nVersionIn), hdr(pchMessageStartIn), | |||||
vRecv(nTypeIn, nVersionIn) { | |||||
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(); | ||||
} | } | ||||
public: | |||||
V1TransportDeserializer( | |||||
const CMessageHeader::MessageMagic &pchMessageStartIn, int nTypeIn, | |||||
int nVersionIn) | |||||
: hdrbuf(nTypeIn, nVersionIn), hdr(pchMessageStartIn), | |||||
vRecv(nTypeIn, nVersionIn) { | |||||
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); | ||||
} | } | ||||
void SetVersion(int nVersionIn) { | void SetVersion(int nVersionIn) { | ||||
hdrbuf.SetVersion(nVersionIn); | hdrbuf.SetVersion(nVersionIn); | ||||
vRecv.SetVersion(nVersionIn); | vRecv.SetVersion(nVersionIn); | ||||
} | } | ||||
int Read(const Config &config, const char *pch, uint32_t nBytes) { | int Read(const Config &config, const char *pch, uint32_t nBytes) { | ||||
return in_data ? readData(pch, nBytes) | int ret = | ||||
: readHeader(config, pch, nBytes); | in_data ? readData(pch, nBytes) : readHeader(config, pch, nBytes); | ||||
if (ret < 0) { | |||||
Reset(); | |||||
} | |||||
return ret; | |||||
} | } | ||||
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 290 Lines • Show Last 20 Lines |