@@ -629,34 +629,21 @@ void CNode::copyStats(CNodeStats &stats, const std::vector<bool> &m_asmap)
629629}
630630#undef X
631631
632- /* *
633- * Receive bytes from the buffer and deserialize them into messages.
634- *
635- * @param[in] pch A pointer to the raw data
636- * @param[in] nBytes Size of the data
637- * @param[out] complete Set True if at least one message has been
638- * deserialized and is ready to be processed
639- * @return True if the peer should stay connected,
640- * False if the peer should be disconnected from.
641- */
642- bool CNode::ReceiveMsgBytes (const char *pch, unsigned int nBytes, bool & complete)
632+ bool CNode::ReceiveMsgBytes (Span<const char > msg_bytes, bool & complete)
643633{
644634 complete = false ;
645635 const auto time = GetTime<std::chrono::microseconds>();
646636 LOCK (cs_vRecv);
647637 nLastRecv = std::chrono::duration_cast<std::chrono::seconds>(time).count ();
648- nRecvBytes += nBytes ;
649- while (nBytes > 0 ) {
638+ nRecvBytes += msg_bytes. size () ;
639+ while (msg_bytes. size () > 0 ) {
650640 // absorb network data
651- int handled = m_deserializer->Read (pch, nBytes );
641+ int handled = m_deserializer->Read (msg_bytes );
652642 if (handled < 0 ) {
653643 // Serious header problem, disconnect from the peer.
654644 return false ;
655645 }
656646
657- pch += handled;
658- nBytes -= handled;
659-
660647 if (m_deserializer->Complete ()) {
661648 // decompose a transport agnostic CNetMessage from the deserializer
662649 uint32_t out_err_raw_size{0 };
@@ -686,13 +673,13 @@ bool CNode::ReceiveMsgBytes(const char *pch, unsigned int nBytes, bool& complete
686673 return true ;
687674}
688675
689- int V1TransportDeserializer::readHeader (const char *pch, unsigned int nBytes )
676+ int V1TransportDeserializer::readHeader (Span< const char > msg_bytes )
690677{
691678 // copy data to temporary parsing buffer
692679 unsigned int nRemaining = CMessageHeader::HEADER_SIZE - nHdrPos;
693- unsigned int nCopy = std::min (nRemaining, nBytes );
680+ unsigned int nCopy = std::min< unsigned int > (nRemaining, msg_bytes. size () );
694681
695- memcpy (&hdrbuf[nHdrPos], pch , nCopy);
682+ memcpy (&hdrbuf[nHdrPos], msg_bytes. data () , nCopy);
696683 nHdrPos += nCopy;
697684
698685 // if header incomplete, exit
@@ -726,18 +713,18 @@ int V1TransportDeserializer::readHeader(const char *pch, unsigned int nBytes)
726713 return nCopy;
727714}
728715
729- int V1TransportDeserializer::readData (const char *pch, unsigned int nBytes )
716+ int V1TransportDeserializer::readData (Span< const char > msg_bytes )
730717{
731718 unsigned int nRemaining = hdr.nMessageSize - nDataPos;
732- unsigned int nCopy = std::min (nRemaining, nBytes );
719+ unsigned int nCopy = std::min< unsigned int > (nRemaining, msg_bytes. size () );
733720
734721 if (vRecv.size () < nDataPos + nCopy) {
735722 // Allocate up to 256 KiB ahead, but never more than the total message size.
736723 vRecv.resize (std::min (hdr.nMessageSize , nDataPos + nCopy + 256 * 1024 ));
737724 }
738725
739- hasher.Write ({( const unsigned char *)pch, nCopy} );
740- memcpy (&vRecv[nDataPos], pch , nCopy);
726+ hasher.Write (MakeUCharSpan (msg_bytes. first ( nCopy)) );
727+ memcpy (&vRecv[nDataPos], msg_bytes. data () , nCopy);
741728 nDataPos += nCopy;
742729
743730 return nCopy;
@@ -1487,7 +1474,7 @@ void CConnman::SocketHandler()
14871474 if (nBytes > 0 )
14881475 {
14891476 bool notify = false ;
1490- if (!pnode->ReceiveMsgBytes (pchBuf, nBytes, notify))
1477+ if (!pnode->ReceiveMsgBytes (Span< const char >( pchBuf, nBytes) , notify))
14911478 pnode->CloseSocketDisconnect ();
14921479 RecordBytesRecv (nBytes);
14931480 if (notify) {
0 commit comments