|
SatCat5
|
Abstract API for reading byte-streams and packets.
The abstract Readable interface is for reading data from a device or buffer. Anything that provides a byte-stream, with or without packets, should usually implement this interface to allow flexible reconnection with other SatCat5 tools.
Note: If frame boundaries are supported, read_* methods MUST NOT read past the boundary until read_finalize() is called.
Definition at line 68 of file io_readable.h.
#include <io_readable.h>
Public Member Functions | |
| virtual void | set_callback (satcat5::io::EventListener *callback) |
| Update registered callback for data_rcvd() events. More... | |
| virtual unsigned | get_read_ready () const =0 |
| How many bytes can be read without blocking? More... | |
| u8 | read_u8 () |
| One of many functions for reading integer/floating point values, see details. More... | |
| virtual bool | read_bytes (unsigned nbytes, void *dst) |
| Read 0 or more bytes into a buffer. More... | |
| virtual bool | read_consume (unsigned nbytes) |
| Read and discard 0 or more bytes. More... | |
| unsigned | read_str (unsigned dst_size, char *dst) |
| Safely read a null-terminated input string. More... | |
| virtual void | read_finalize () |
| Consume any remaining bytes in this frame, if applicable. More... | |
| template<class T > | |
| bool | read_obj (T &t) |
Templated wrapper for any object with the following method: bool read_from(satcat5::io::Readable* rd); | |
| unsigned | copy_to (satcat5::io::Writeable *dst) |
| Copy data to a Writeable object, without finalizing. More... | |
| bool | copy_and_finalize (satcat5::io::Writeable *dst, satcat5::io::CopyMode mode=CopyMode::PACKET) |
| Copy data to a Writeable object, then finalize. More... | |
| void | request_poll () |
| Call this method to request polling at a later time. More... | |
| void | request_cancel () |
| Call this method to cancel a previous request_poll(). | |
Static Public Member Functions | |
| static unsigned | count_ondemand () |
| Count queued objects of this type (i.e., non-idle). | |
Protected Member Functions | |
| constexpr | Readable (satcat5::io::EventListener *callback=0) |
| Only children should create or destroy the base class. | |
| virtual u8 | read_next ()=0 |
| Read the next byte from the underlying buffer or device. More... | |
| void | read_notify () |
Attempt notification by calling m_callback->data_rcvd(). More... | |
| virtual void | read_underflow () |
| Optional error handling for read underflow. More... | |
Private Member Functions | |
| void | poll_demand () |
| Event handler for on-demand polling. | |
Private Attributes | |
| satcat5::io::EventListener * | m_callback |
| Pointer to the callback object, or NULL. More... | |
| satcat5::poll::OnDemand * | m_next |
| bool | m_idle |
| bool Readable::copy_and_finalize | ( | satcat5::io::Writeable * | dst, |
| satcat5::io::CopyMode | mode = CopyMode::PACKET |
||
| ) |
Copy data to a Writeable object, then finalize.
As copy_to(), but also calls read_finalize() and/or write_finalize() depending on the input/output mode.
In packet mode (the default), call both read_finalize() and write_finalize() if the operation copies all available data. Use this mode whenever the input is packetized.
In byte-stream mode, call write_finalize() if the operation copies any data, but never call read_finalize(). Use this mode for inputs that do not delimit packet boundaries.
| dst | The destination object. |
| mode | Select packet mode or stream mode. |
Definition at line 234 of file io_readable.cc.
| unsigned Readable::copy_to | ( | satcat5::io::Writeable * | dst | ) |
Copy data to a Writeable object, without finalizing.
Copy the contents of this Readable to a Writeable object, stopping at end-of-input, end-of-frame, or the capacity of the destination buffer (whichever comes first). This method does not call read_finalize or write_finalize(). To automatically make finalize calls,
Definition at line 214 of file io_readable.cc.
|
pure virtual |
How many bytes can be read without blocking?
Child objects of io::Readable MUST override this method.
Implemented in satcat5::port::Mailmap, satcat5::io::PacketBuffer, satcat5::io::MultiPacket::Reader, satcat5::io::NullRead, satcat5::io::ReadableRedirect, satcat5::io::LimitedRead, satcat5::io::ArrayRead, and satcat5::io::FileReader.
|
virtual |
Read 0 or more bytes into a buffer.
Child objects of io::Readable MAY override this method for improved performance.
Reimplemented in satcat5::port::MailmapAligned, satcat5::port::Mailmap, satcat5::io::PacketBuffer, satcat5::io::MultiPacket::Reader, satcat5::io::ReadableRedirect, satcat5::io::LimitedRead, and satcat5::io::FileReader.
Definition at line 190 of file io_readable.cc.
|
virtual |
Read and discard 0 or more bytes.
Child objects of io::Readable MAY override this method for improved performance.
Reimplemented in satcat5::io::PacketBuffer, satcat5::io::MultiPacket::Reader, satcat5::io::ReadableRedirect, satcat5::io::LimitedRead, and satcat5::io::FileReader.
Definition at line 204 of file io_readable.cc.
|
virtual |
Consume any remaining bytes in this frame, if applicable.
Child objects of io::Readable SHOULD override this method if they support framing.
Reimplemented in satcat5::port::Mailmap, satcat5::io::PacketBuffer, satcat5::io::MultiReader, satcat5::io::MultiPacket::Reader, satcat5::io::ReadableRedirect, satcat5::io::LimitedRead, satcat5::io::ArrayRead, satcat5::test::EthernetInterface, satcat5::io::ReadPcap, and satcat5::io::FileReader.
Definition at line 270 of file io_readable.cc.
|
protectedpure virtual |
Read the next byte from the underlying buffer or device.
Child objects of io::Readable MUST override this method.
Implemented in satcat5::port::MailmapAligned, satcat5::port::Mailmap, satcat5::io::PacketBuffer, satcat5::io::MultiPacket::Reader, satcat5::io::NullRead, satcat5::io::ReadableRedirect, satcat5::io::LimitedRead, satcat5::io::ArrayRead, and satcat5::io::FileReader.
|
protected |
Attempt notification by calling m_callback->data_rcvd().
Child objects of io::Readable MAY call this to override default notifications.
Definition at line 254 of file io_readable.cc.
| unsigned Readable::read_str | ( | unsigned | dst_size, |
| char * | dst | ||
| ) |
Safely read a null-terminated input string.
The input is always consumed up to the end-of-input or the first zero byte, whichever comes first.
Definition at line 179 of file io_readable.cc.
| u8 Readable::read_u8 | ( | ) |
One of many functions for reading integer/floating point values, see details.
Several functions are provided for reading and writing scalar types to/from io::Readable and io::Writeable instances. Since there are many of these that are frequently inherited, they are hidden from documentation. These functions follow a shared template:
read_ or write_ prefix.u for unsigned, s for signed, f for floating-point.l suffix if little-endian, no suffix if big-endian.Example: write_s48l is "Write 48 bits (6 bytes) to this
io::Writeable as a signed value in little-endian order".
Definition at line 44 of file io_readable.cc.
|
protectedvirtual |
Optional error handling for read underflow.
Child objects of io::Readable MAY override this method.
Reimplemented in satcat5::port::Mailmap, and satcat5::io::ReadableRedirect.
Definition at line 271 of file io_readable.cc.
|
inherited |
Call this method to request polling at a later time.
Safe to stack requests, but only one call to poll().
Definition at line 208 of file polling.cc.
|
virtual |
Update registered callback for data_rcvd() events.
Child objects of io::Readable SHOULD usually leave this method as-is.
Reimplemented in satcat5::io::ReadableRedirect, satcat5::io::Override, satcat5::io::MuxPort, and satcat5::test::EthernetInterface.
Definition at line 39 of file io_readable.cc.
|
private |
Pointer to the callback object, or NULL.
This can only be modified through the set_callback(...) method.
Definition at line 199 of file io_readable.h.