diff options
author | Jonas Schnelli <dev@jonasschnelli.ch> | 2019-08-07 15:56:24 +0200 |
---|---|---|
committer | Carl Dong <contact@carldong.me> | 2020-01-22 14:55:24 -0500 |
commit | 16d6113f4faa901e248adb693d4768a9e5019a16 (patch) | |
tree | 881ce4f3f41bd30d163915f0c184aa58bf0ec23e /src/net.cpp | |
parent | 04f78b818f02279d32c3ad3a1140e9410bfb26bf (diff) |
Refactor message transport packaging
Diffstat (limited to 'src/net.cpp')
-rw-r--r-- | src/net.cpp | 24 |
1 files changed, 17 insertions, 7 deletions
diff --git a/src/net.cpp b/src/net.cpp index 75b230d0be..f5de582049 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -716,6 +716,19 @@ CNetMessage V1TransportDeserializer::GetMessage(const CMessageHeader::MessageSta return msg; } +void V1TransportSerializer::prepareForTransport(CSerializedNetMsg& msg, std::vector<unsigned char>& header) { + // create dbl-sha256 checksum + uint256 hash = Hash(msg.data.begin(), msg.data.end()); + + // create header + CMessageHeader hdr(Params().MessageStart(), msg.command.c_str(), msg.data.size()); + memcpy(hdr.pchChecksum, hash.begin(), CMessageHeader::CHECKSUM_SIZE); + + // serialize header + header.reserve(CMessageHeader::HEADER_SIZE); + CVectorWriter{SER_NETWORK, INIT_PROTO_VERSION, header, 0, hdr}; +} + size_t CConnman::SocketSendData(CNode *pnode) const EXCLUSIVE_LOCKS_REQUIRED(pnode->cs_vSend) { auto it = pnode->vSendMsg.begin(); @@ -2703,6 +2716,7 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn } m_deserializer = MakeUnique<V1TransportDeserializer>(V1TransportDeserializer(Params().MessageStart(), SER_NETWORK, INIT_PROTO_VERSION)); + m_serializer = MakeUnique<V1TransportSerializer>(V1TransportSerializer()); } CNode::~CNode() @@ -2718,16 +2732,12 @@ bool CConnman::NodeFullyConnected(const CNode* pnode) void CConnman::PushMessage(CNode* pnode, CSerializedNetMsg&& msg) { size_t nMessageSize = msg.data.size(); - size_t nTotalSize = nMessageSize + CMessageHeader::HEADER_SIZE; LogPrint(BCLog::NET, "sending %s (%d bytes) peer=%d\n", SanitizeString(msg.command), nMessageSize, pnode->GetId()); + // make sure we use the appropriate network transport format std::vector<unsigned char> serializedHeader; - serializedHeader.reserve(CMessageHeader::HEADER_SIZE); - uint256 hash = Hash(msg.data.data(), msg.data.data() + nMessageSize); - CMessageHeader hdr(Params().MessageStart(), msg.command.c_str(), nMessageSize); - memcpy(hdr.pchChecksum, hash.begin(), CMessageHeader::CHECKSUM_SIZE); - - CVectorWriter{SER_NETWORK, INIT_PROTO_VERSION, serializedHeader, 0, hdr}; + pnode->m_serializer->prepareForTransport(msg, serializedHeader); + size_t nTotalSize = nMessageSize + serializedHeader.size(); size_t nBytesSent = 0; { |