#include "Stream.h" #include Stream::Stream() { } Stream::~Stream() { } void Stream::onReceive(const uint8_t* p, size_t n) { if(buffer_.empty()) { size_t consumed = listener_ ? listener_->onReceive(p,n) : 0; if(consumed < n) buffer_.insert(buffer_.end(), p + consumed, p + n); } else { buffer_.insert(buffer_.end(), p, p+n); } while(!buffer_.empty()) { size_t consumed = listener_ ? listener_->onReceive(buffer_.data(), buffer_.size()) : 0; buffer_.erase(buffer_.begin(), buffer_.begin()+consumed); if(!consumed) break; } } long Stream::read(void *p, size_t n) { if(buffer_.size() <= n) { size_t bufsiz = buffer_.size(); memcpy(p, buffer_.data(), buffer_.size()); buffer_.clear(); return bufsiz; } else { memcpy(p, buffer_.data(), n); buffer_.erase(buffer_.begin(), buffer_.begin()+n); return n; } }