2018-04-22 09:23:08 +00:00
|
|
|
#ifndef STREAM_H_
|
|
|
|
#define STREAM_H_
|
|
|
|
|
2018-04-20 22:03:29 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
class StreamListener
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual size_t onReceive(const uint8_t* p, size_t n) = 0;
|
2018-04-23 23:42:36 +00:00
|
|
|
virtual void onReset() {}
|
2018-04-20 22:03:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class Stream
|
|
|
|
{
|
2018-04-22 09:23:08 +00:00
|
|
|
StreamListener *listener_ = nullptr;
|
2018-04-20 22:03:29 +00:00
|
|
|
std::vector<uint8_t> buffer_;
|
|
|
|
public:
|
|
|
|
Stream();
|
|
|
|
virtual ~Stream();
|
|
|
|
|
|
|
|
void setListener(StreamListener *l) { listener_ = l; }
|
2018-04-23 23:42:36 +00:00
|
|
|
void clearListener(StreamListener *l = nullptr) { if(!l || listener_ == l) listener_ = nullptr; }
|
2018-04-20 22:03:29 +00:00
|
|
|
|
|
|
|
virtual void write(const void* p, size_t n) = 0;
|
2018-04-22 09:23:08 +00:00
|
|
|
virtual void flushWrite() {}
|
2019-01-04 02:35:32 +00:00
|
|
|
size_t read(void *p, size_t n);
|
2018-04-20 22:03:29 +00:00
|
|
|
|
2018-05-08 00:15:05 +00:00
|
|
|
virtual bool readyToWrite() const { return true; }
|
|
|
|
bool readyToRead() const { return !buffer_.empty(); }
|
|
|
|
|
|
|
|
virtual bool allDataArrived() const { return true; }
|
2018-04-20 22:03:29 +00:00
|
|
|
protected:
|
2018-04-22 09:23:08 +00:00
|
|
|
void notifyReceive(const uint8_t* p, size_t n);
|
2018-04-23 23:42:36 +00:00
|
|
|
void notifyReset();
|
|
|
|
};
|
|
|
|
|
2019-01-23 18:41:12 +00:00
|
|
|
class WaitableStream : public Stream
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual void wait() = 0;
|
|
|
|
};
|
|
|
|
|
2018-04-23 23:42:36 +00:00
|
|
|
class StreamWrapper : public Stream, private StreamListener
|
|
|
|
{
|
|
|
|
Stream* underlying_;
|
|
|
|
public:
|
|
|
|
StreamWrapper(Stream* underlying_);
|
|
|
|
virtual ~StreamWrapper();
|
|
|
|
|
|
|
|
StreamWrapper(const StreamWrapper& other) = delete;
|
|
|
|
StreamWrapper& operator=(const StreamWrapper& other) = delete;
|
|
|
|
StreamWrapper(StreamWrapper&& other);
|
|
|
|
StreamWrapper& operator=(StreamWrapper&& other);
|
|
|
|
|
|
|
|
Stream& underlying() const { return *underlying_; }
|
2018-04-20 22:03:29 +00:00
|
|
|
};
|
2018-04-22 09:23:08 +00:00
|
|
|
|
|
|
|
#endif
|