aboutsummaryrefslogtreecommitdiff
path: root/src/net.h
diff options
context:
space:
mode:
authorJonas Schnelli <dev@jonasschnelli.ch>2019-06-13 11:25:54 +0200
committerJonas Schnelli <dev@jonasschnelli.ch>2019-10-18 08:56:08 +0200
commitefecb74677222f6c70adf7f860c315f430d39ec4 (patch)
tree68233f63743319b0785bc189cc6d466bb53c5269 /src/net.h
parent1a5c656c3169ba525f84145d19ce8c64f2cf1efb (diff)
downloadbitcoin-efecb74677222f6c70adf7f860c315f430d39ec4.tar.xz
Use adapter pattern for the network deserializer
Diffstat (limited to 'src/net.h')
-rw-r--r--src/net.h46
1 files changed, 32 insertions, 14 deletions
diff --git a/src/net.h b/src/net.h
index 15996438c9..16435a3aa1 100644
--- a/src/net.h
+++ b/src/net.h
@@ -637,20 +637,40 @@ public:
* transport protocol agnostic CNetMessage (command & payload)
*/
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 = 0;
+ // set the serialization context version
+ virtual void SetVersion(int version) = 0;
+ // read and deserialize data
+ virtual int Read(const char *data, unsigned int bytes) = 0;
+ // decomposes a message from the context
+ virtual CNetMessage GetMessage(const CMessageHeader::MessageStartChars& message_start, int64_t time) = 0;
+ virtual ~TransportDeserializer() {}
+};
+
+class V1TransportDeserializer : public TransportDeserializer
+{
private:
mutable CHash256 hasher;
mutable uint256 data_hash;
-public:
bool in_data; // parsing header (false) or data (true)
-
CDataStream hdrbuf; // partially received header
CMessageHeader hdr; // complete header
- unsigned int nHdrPos;
-
CDataStream vRecv; // received message data
+ unsigned int nHdrPos;
unsigned int nDataPos;
- TransportDeserializer(const CMessageHeader::MessageStartChars& pchMessageStartIn, int nTypeIn, int nVersionIn) : hdrbuf(nTypeIn, nVersionIn), hdr(pchMessageStartIn), vRecv(nTypeIn, nVersionIn) {
+ const uint256& GetMessageHash() const;
+ int readHeader(const char *pch, unsigned int nBytes);
+ int readData(const char *pch, unsigned int nBytes);
+public:
+
+ V1TransportDeserializer(const CMessageHeader::MessageStartChars& pchMessageStartIn, int nTypeIn, int nVersionIn) : hdrbuf(nTypeIn, nVersionIn), hdr(pchMessageStartIn), vRecv(nTypeIn, nVersionIn) {
Reset();
}
@@ -664,25 +684,23 @@ public:
data_hash.SetNull();
hasher.Reset();
}
-
- bool complete() const
+ bool Complete() const
{
if (!in_data)
return false;
return (hdr.nMessageSize == nDataPos);
}
-
- const uint256& GetMessageHash() const;
-
void SetVersion(int nVersionIn)
{
hdrbuf.SetVersion(nVersionIn);
vRecv.SetVersion(nVersionIn);
}
-
- int readHeader(const char *pch, unsigned int nBytes);
- int readData(const char *pch, unsigned int nBytes);
-
+ bool OversizedMessageDetected() const {
+ return (in_data && hdr.nMessageSize > MAX_PROTOCOL_MESSAGE_LENGTH);
+ }
+ int Read(const char *pch, unsigned int nBytes) {
+ return in_data ? readData(pch, nBytes) : readHeader(pch, nBytes);
+ }
CNetMessage GetMessage(const CMessageHeader::MessageStartChars& message_start, int64_t time);
};