mirror of
https://github.com/autc04/Retro68.git
synced 2024-11-19 03:05:15 +00:00
51 lines
1013 B
C++
51 lines
1013 B
C++
|
#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;
|
||
|
}
|
||
|
}
|