net: Use Span in ReceiveMsgBytes

This commit is contained in:
MarcoFalke
2020-09-30 17:08:26 +02:00
parent 69a73803df
commit fa5ed3b4ca
6 changed files with 43 additions and 45 deletions

View File

@@ -745,8 +745,8 @@ public:
virtual bool Complete() 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;
/** read and deserialize data, advances msg_bytes data pointer */
virtual int Read(Span<const char>& msg_bytes) = 0;
// decomposes a message from the context
virtual Optional<CNetMessage> GetMessage(std::chrono::microseconds time, uint32_t& out_err) = 0;
virtual ~TransportDeserializer() {}
@@ -767,8 +767,8 @@ private:
unsigned int nDataPos;
const uint256& GetMessageHash() const;
int readHeader(const char *pch, unsigned int nBytes);
int readData(const char *pch, unsigned int nBytes);
int readHeader(Span<const char> msg_bytes);
int readData(Span<const char> msg_bytes);
void Reset() {
vRecv.clear();
@@ -802,9 +802,14 @@ public:
hdrbuf.SetVersion(nVersionIn);
vRecv.SetVersion(nVersionIn);
}
int Read(const char *pch, unsigned int nBytes) override {
int ret = in_data ? readData(pch, nBytes) : readHeader(pch, nBytes);
if (ret < 0) Reset();
int Read(Span<const char>& msg_bytes) override
{
int ret = in_data ? readData(msg_bytes) : readHeader(msg_bytes);
if (ret < 0) {
Reset();
} else {
msg_bytes = msg_bytes.subspan(ret);
}
return ret;
}
Optional<CNetMessage> GetMessage(std::chrono::microseconds time, uint32_t& out_err_raw_size) override;
@@ -1088,7 +1093,16 @@ public:
return nRefCount;
}
bool ReceiveMsgBytes(const char *pch, unsigned int nBytes, bool& complete);
/**
* Receive bytes from the buffer and deserialize them into messages.
*
* @param[in] msg_bytes The raw data
* @param[out] complete Set True if at least one message has been
* deserialized and is ready to be processed
* @return True if the peer should stay connected,
* False if the peer should be disconnected from.
*/
bool ReceiveMsgBytes(Span<const char> msg_bytes, bool& complete);
void SetCommonVersion(int greatest_common_version)
{