mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-01-20 15:19:07 +01:00
net: push only raw data into CConnman
This fixes one of the last major layer violations in the networking stack. The network side is no longer in charge of message serialization, so it is now decoupled from Bitcoin structures. Only the header is serialized and attached to the payload.
This commit is contained in:
46
src/net.cpp
46
src/net.cpp
@@ -768,13 +768,13 @@ const uint256& CNetMessage::GetMessageHash() const
|
||||
// requires LOCK(cs_vSend)
|
||||
size_t SocketSendData(CNode *pnode)
|
||||
{
|
||||
std::deque<CSerializeData>::iterator it = pnode->vSendMsg.begin();
|
||||
auto it = pnode->vSendMsg.begin();
|
||||
size_t nSentSize = 0;
|
||||
|
||||
while (it != pnode->vSendMsg.end()) {
|
||||
const CSerializeData &data = *it;
|
||||
const auto &data = *it;
|
||||
assert(data.size() > pnode->nSendOffset);
|
||||
int nBytes = send(pnode->hSocket, &data[pnode->nSendOffset], data.size() - pnode->nSendOffset, MSG_NOSIGNAL | MSG_DONTWAIT);
|
||||
int nBytes = send(pnode->hSocket, reinterpret_cast<const char*>(data.data()) + pnode->nSendOffset, data.size() - pnode->nSendOffset, MSG_NOSIGNAL | MSG_DONTWAIT);
|
||||
if (nBytes > 0) {
|
||||
pnode->nLastSend = GetTime();
|
||||
pnode->nSendBytes += nBytes;
|
||||
@@ -2612,30 +2612,19 @@ void CNode::AskFor(const CInv& inv)
|
||||
mapAskFor.insert(std::make_pair(nRequestTime, inv));
|
||||
}
|
||||
|
||||
CDataStream CConnman::BeginMessage(CNode* pnode, int nVersion, int flags, const std::string& sCommand)
|
||||
void CConnman::PushMessage(CNode* pnode, CSerializedNetMsg&& msg)
|
||||
{
|
||||
return {SER_NETWORK, (nVersion ? nVersion : pnode->GetSendVersion()) | flags, CMessageHeader(Params().MessageStart(), sCommand.c_str(), 0) };
|
||||
}
|
||||
size_t nMessageSize = msg.data.size();
|
||||
size_t nTotalSize = nMessageSize + CMessageHeader::HEADER_SIZE;
|
||||
LogPrint("net", "sending %s (%d bytes) peer=%d\n", SanitizeString(msg.command.c_str()), nMessageSize, pnode->id);
|
||||
|
||||
void CConnman::EndMessage(CDataStream& strm)
|
||||
{
|
||||
// Set the size
|
||||
assert(strm.size () >= CMessageHeader::HEADER_SIZE);
|
||||
unsigned int nSize = strm.size() - CMessageHeader::HEADER_SIZE;
|
||||
WriteLE32((uint8_t*)&strm[CMessageHeader::MESSAGE_SIZE_OFFSET], nSize);
|
||||
// Set the checksum
|
||||
uint256 hash = Hash(strm.begin() + CMessageHeader::HEADER_SIZE, strm.end());
|
||||
memcpy((char*)&strm[CMessageHeader::CHECKSUM_OFFSET], hash.begin(), CMessageHeader::CHECKSUM_SIZE);
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
void CConnman::PushMessage(CNode* pnode, CDataStream& strm, const std::string& sCommand)
|
||||
{
|
||||
if(strm.empty())
|
||||
return;
|
||||
|
||||
unsigned int nSize = strm.size() - CMessageHeader::HEADER_SIZE;
|
||||
LogPrint("net", "sending %s (%d bytes) peer=%d\n", SanitizeString(sCommand.c_str()), nSize, pnode->id);
|
||||
CVectorWriter{SER_NETWORK, INIT_PROTO_VERSION, serializedHeader, 0, hdr};
|
||||
|
||||
size_t nBytesSent = 0;
|
||||
{
|
||||
@@ -2644,11 +2633,14 @@ void CConnman::PushMessage(CNode* pnode, CDataStream& strm, const std::string& s
|
||||
return;
|
||||
}
|
||||
bool optimisticSend(pnode->vSendMsg.empty());
|
||||
pnode->vSendMsg.emplace_back(strm.begin(), strm.end());
|
||||
|
||||
//log total amount of bytes per command
|
||||
pnode->mapSendBytesPerMsgCmd[sCommand] += strm.size();
|
||||
pnode->nSendSize += strm.size();
|
||||
pnode->mapSendBytesPerMsgCmd[msg.command] += nTotalSize;
|
||||
pnode->nSendSize += nTotalSize;
|
||||
|
||||
pnode->vSendMsg.push_back(std::move(serializedHeader));
|
||||
if (nMessageSize)
|
||||
pnode->vSendMsg.push_back(std::move(msg.data));
|
||||
|
||||
// If write queue empty, attempt "optimistic write"
|
||||
if (optimisticSend == true)
|
||||
|
||||
Reference in New Issue
Block a user