Changeset View
Changeset View
Standalone View
Standalone View
src/net.h
Show First 20 Lines • Show All 678 Lines • ▼ Show 20 Lines | |||||
/** | /** | ||||
* Transport protocol agnostic message container. | * Transport protocol agnostic message container. | ||||
* Ideally it should only contain receive time, payload, | * Ideally it should only contain receive time, payload, | ||||
* command and size. | * command and size. | ||||
*/ | */ | ||||
class CNetMessage { | class CNetMessage { | ||||
public: | public: | ||||
// received message data | //! received message data | ||||
CDataStream m_recv; | CDataStream m_recv; | ||||
// time (in microseconds) of message receipt. | //! time of message receipt | ||||
int64_t m_time = 0; | std::chrono::microseconds m_time{0}; | ||||
bool m_valid_netmagic = false; | bool m_valid_netmagic = false; | ||||
bool m_valid_header = false; | bool m_valid_header = false; | ||||
bool m_valid_checksum = false; | bool m_valid_checksum = false; | ||||
// size of the payload | //! size of the payload | ||||
uint32_t m_message_size = 0; | uint32_t m_message_size{0}; | ||||
// used wire size of the message (including header/checksum) | //! used wire size of the message (including header/checksum) | ||||
uint32_t m_raw_message_size = 0; | uint32_t m_raw_message_size{0}; | ||||
std::string m_command; | std::string m_command; | ||||
CNetMessage(CDataStream &&recv_in) : m_recv(std::move(recv_in)) {} | CNetMessage(CDataStream &&recv_in) : m_recv(std::move(recv_in)) {} | ||||
void SetVersion(int nVersionIn) { m_recv.SetVersion(nVersionIn); } | void SetVersion(int nVersionIn) { m_recv.SetVersion(nVersionIn); } | ||||
}; | }; | ||||
/** | /** | ||||
* 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: | ||||
// 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 | ||||
virtual CNetMessage GetMessage(const Config &config, int64_t time) = 0; | virtual CNetMessage GetMessage(const Config &config, | ||||
std::chrono::microseconds time) = 0; | |||||
virtual ~TransportDeserializer() {} | virtual ~TransportDeserializer() {} | ||||
}; | }; | ||||
class V1TransportDeserializer final : public TransportDeserializer { | class V1TransportDeserializer final : public TransportDeserializer { | ||||
private: | private: | ||||
mutable CHash256 hasher; | mutable CHash256 hasher; | ||||
mutable uint256 data_hash; | mutable uint256 data_hash; | ||||
▲ Show 20 Lines • Show All 48 Lines • ▼ Show 20 Lines | int Read(const Config &config, const char *pch, uint32_t nBytes) override { | ||||
int ret = | int ret = | ||||
in_data ? readData(pch, nBytes) : readHeader(config, pch, nBytes); | in_data ? readData(pch, nBytes) : readHeader(config, pch, nBytes); | ||||
if (ret < 0) { | if (ret < 0) { | ||||
Reset(); | Reset(); | ||||
} | } | ||||
return ret; | return ret; | ||||
} | } | ||||
CNetMessage GetMessage(const Config &config, int64_t time) override; | CNetMessage GetMessage(const Config &config, | ||||
std::chrono::microseconds time) override; | |||||
}; | }; | ||||
/** | /** | ||||
* The TransportSerializer prepares messages for the network transport | * The TransportSerializer prepares messages for the network transport | ||||
*/ | */ | ||||
class TransportSerializer { | class TransportSerializer { | ||||
public: | public: | ||||
// prepare message for transport (header construction, error-correction | // prepare message for transport (header construction, error-correction | ||||
▲ Show 20 Lines • Show All 208 Lines • ▼ Show 20 Lines | public: | ||||
// Block and TXN accept times | // Block and TXN accept times | ||||
std::atomic<int64_t> nLastBlockTime{0}; | std::atomic<int64_t> nLastBlockTime{0}; | ||||
std::atomic<int64_t> nLastTXTime{0}; | std::atomic<int64_t> nLastTXTime{0}; | ||||
// Ping time measurement: | // Ping time measurement: | ||||
// The pong reply we're expecting, or 0 if no pong expected. | // The pong reply we're expecting, or 0 if no pong expected. | ||||
std::atomic<uint64_t> nPingNonceSent{0}; | std::atomic<uint64_t> nPingNonceSent{0}; | ||||
// Time (in usec) the last ping was sent, or 0 if no ping was ever sent. | /** When the last ping was sent, or 0 if no ping was ever sent */ | ||||
std::atomic<int64_t> nPingUsecStart{0}; | std::atomic<std::chrono::microseconds> m_ping_start{ | ||||
std::chrono::microseconds{0}}; | |||||
// Last measured round-trip time. | // Last measured round-trip time. | ||||
std::atomic<int64_t> nPingUsecTime{0}; | std::atomic<int64_t> nPingUsecTime{0}; | ||||
// Best measured round-trip time. | // Best measured round-trip time. | ||||
std::atomic<int64_t> nMinPingUsecTime{std::numeric_limits<int64_t>::max()}; | std::atomic<int64_t> nMinPingUsecTime{std::numeric_limits<int64_t>::max()}; | ||||
// Whether a ping is requested. | // Whether a ping is requested. | ||||
std::atomic<bool> fPingQueued{false}; | std::atomic<bool> fPingQueued{false}; | ||||
std::set<TxId> orphan_work_set; | std::set<TxId> orphan_work_set; | ||||
▲ Show 20 Lines • Show All 153 Lines • Show Last 20 Lines |