Retro68/LaunchAPPL/Common/Stream.cc

51 lines
1013 B
C++
Raw Normal View History

2018-04-20 22:03:29 +00:00
#include "Stream.h"
#include <string.h>
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;
}
}