9LinkStream::LinkStream(Channel& channel, uint32_t my_stream_id)
10 : link_(channel, my_stream_id) {}
12LinkStream::LinkStream(Link socket) : link_(std::move(socket)) {}
14int LinkStream::available() {
return in().available(); }
16int LinkStream::read() {
17 if (available() == 0) {
24int LinkStream::peek() {
return in().peek(); }
26size_t LinkStream::readBytes(
char* buffer,
size_t length) {
27 return timedRead((roo::byte*)buffer, length, roo_time::Millis(getTimeout()));
30size_t LinkStream::readBytes(uint8_t* buffer,
size_t length) {
31 return timedRead((roo::byte*)buffer, length, roo_time::Millis(getTimeout()));
34size_t LinkStream::write(uint8_t val) {
35 out().write((
const roo::byte*)&val, 1);
39size_t LinkStream::write(
const uint8_t* buffer,
size_t size) {
40 return out().writeFully((
const roo::byte*)buffer, size);
43int LinkStream::availableForWrite() {
return out().availableForWrite(); }
45void LinkStream::flush() { out().flush(); }
47LinkStatus LinkStream::status()
const {
return link_.status(); }
49void LinkStream::awaitConnected() { link_.awaitConnected(); }
51bool LinkStream::awaitConnected(roo_time::Duration timeout) {
52 return link_.awaitConnected(timeout);
55size_t LinkStream::timedRead(roo::byte* buf,
size_t count,
56 roo_time::Duration timeout) {
57 roo_time::Uptime start = roo_time::Uptime::Now();
59 if (in().status() != roo_io::kOk)
return -1;
61 for (
int i = 0; i < 100; ++i) {
62 size_t result = in().tryRead(buf, count);
64 if (in().status() != roo_io::kOk)
return -1;
70 if (count == 0)
return total;
73 if (roo_time::Uptime::Now() - start > timeout)
break;