From 2dd10c96456043da0e3a56f7d709f5ecf3ff7c8c Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sun, 12 Jun 2016 09:50:51 +0900 Subject: [PATCH] Revert "Remove dead code" This reverts commit fd024e677307798ae27b61f14cc89a5b7cf9182d. --- src/PeerConnection.cc | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/PeerConnection.cc b/src/PeerConnection.cc index 88948239..56da81a2 100644 --- a/src/PeerConnection.cc +++ b/src/PeerConnection.cc @@ -150,13 +150,22 @@ bool PeerConnection::receiveMessage(unsigned char* data, size_t& dataLength) else { assert(resbufOffset_ == resbufLength_); if (resbufLength_ != 0) { - // Shift buffer so that resbuf_[msgOffset_] moves to - // rebuf_[0]. - memmove(resbuf_.get(), resbuf_.get() + msgOffset_, - resbufLength_ - msgOffset_); - resbufLength_ -= msgOffset_; - resbufOffset_ = resbufLength_; - msgOffset_ = 0; + if (msgOffset_ == 0 && resbufLength_ == currentPayloadLength_ + 4) { + // All bytes in buffer have been processed, so clear it + // away. + resbufLength_ = 0; + resbufOffset_ = 0; + msgOffset_ = 0; + } + else { + // Shift buffer so that resbuf_[msgOffset_] moves to + // rebuf_[0]. + memmove(resbuf_.get(), resbuf_.get() + msgOffset_, + resbufLength_ - msgOffset_); + resbufLength_ -= msgOffset_; + resbufOffset_ = resbufLength_; + msgOffset_ = 0; + } } size_t nread; // To reduce the amount of copy involved in buffer shift, large