aboutsummaryrefslogtreecommitdiff
path: root/src/net.h
diff options
context:
space:
mode:
authorJonas Schnelli <dev@jonasschnelli.ch>2019-06-13 10:39:44 +0200
committerJonas Schnelli <dev@jonasschnelli.ch>2019-10-18 08:56:06 +0200
commit6294ecdb8bb4eb7049a18c721ee8cb4a53d80a06 (patch)
treedfe985b6c23e73e00401a6596beedc0938e66781 /src/net.h
parentd5a770b70d55a5874fbd7796a171de22abbbd342 (diff)
Refactor: split network transport deserializing from message container
Diffstat (limited to 'src/net.h')
-rw-r--r--src/net.h43
1 files changed, 38 insertions, 5 deletions
diff --git a/src/net.h b/src/net.h
index 44655abf80..a9ce33957d 100644
--- a/src/net.h
+++ b/src/net.h
@@ -609,8 +609,33 @@ public:
-
+/** Transport protocol agnostic message container.
+ * Ideally it should only contain receive time, payload,
+ * command and size.
+ */
class CNetMessage {
+public:
+ CDataStream m_recv; // received message data
+ int64_t m_time = 0; // time (in microseconds) of message receipt.
+ bool m_valid_netmagic = false;
+ bool m_valid_header = false;
+ bool m_valid_checksum = false;
+ uint32_t m_message_size = 0; // size of the payload
+ 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:
mutable CHash256 hasher;
mutable uint256 data_hash;
@@ -624,14 +649,19 @@ public:
CDataStream vRecv; // received message data
unsigned int nDataPos;
- int64_t nTime; // time (in microseconds) of message receipt.
+ TransportDeserializer(const CMessageHeader::MessageStartChars& pchMessageStartIn, int nTypeIn, int nVersionIn) : hdrbuf(nTypeIn, nVersionIn), hdr(pchMessageStartIn), vRecv(nTypeIn, nVersionIn) {
+ Reset();
+ }
- CNetMessage(const CMessageHeader::MessageStartChars& pchMessageStartIn, int nTypeIn, int nVersionIn) : hdrbuf(nTypeIn, nVersionIn), hdr(pchMessageStartIn), vRecv(nTypeIn, nVersionIn) {
+ void Reset() {
+ vRecv.clear();
+ hdrbuf.clear();
hdrbuf.resize(24);
in_data = false;
nHdrPos = 0;
nDataPos = 0;
- nTime = 0;
+ data_hash.SetNull();
+ hasher.Reset();
}
bool complete() const
@@ -651,14 +681,17 @@ public:
int readHeader(const char *pch, unsigned int nBytes);
int readData(const char *pch, unsigned int nBytes);
-};
+ CNetMessage GetMessage(const CMessageHeader::MessageStartChars& message_start, int64_t time);
+};
/** Information about a peer */
class CNode
{
friend class CConnman;
public:
+ std::unique_ptr<TransportDeserializer> m_deserializer;
+
// socket
std::atomic<ServiceFlags> nServices{NODE_NONE};
SOCKET hSocket GUARDED_BY(cs_hSocket);