#include <Pt/System/IODevice.h>
Endpoint for I/O operations.
More...
Inherits Selectable.
Inherited by WebSocket, TcpSocket, UdpSocket, FileDevice, and SerialDevice.
|
|
virtual | ~IODevice () |
| | Destructor.
|
|
void | close () |
| | Closes the device.
|
|
void | setTimeout (std::size_t timeout) |
| | Sets the timeout for blocking I/O in milliseconds.
|
|
void | beginRead (char *buffer, std::size_t n) |
| | Begins to read data.
|
|
std::size_t | endRead () |
| | Ends reading data.
|
| std::size_t | read (char *buffer, std::size_t n) |
| | Read data from I/O device.
|
|
void | beginWrite (const char *buffer, std::size_t n) |
| | Begins to write data.
|
|
std::size_t | endWrite () |
| | Ends writing data.
|
| std::size_t | write (const char *buffer, std::size_t n) |
| | Write data to I/O device.
|
|
bool | seekable () const |
| | Returns true if device is seekable.
|
| pos_type | seek (off_type offset, seekdir sd) |
| | Moves the read position to the given offset.
|
| std::size_t | peek (char *buffer, std::size_t n) |
| | Peek data from I/O device without consuming them.
|
| void | sync () |
| | Synchronize device.
|
| pos_type | position () |
| | Returns the current I/O position.
|
|
bool | isEof () const |
| | Returns if the device has reached EOF.
|
| Signal< IODevice & > & | inputReady () |
| | Notifies about available data.
|
| Signal< IODevice & > & | outputReady () |
| | Notifies when data can be written.
|
|
bool | isReading () const |
| | Returns true if the device is reading.
|
|
bool | isWriting () const |
| | Returns true if the device is writing.
|
|
EventLoop * | loop () const |
| | Returns the used event loop.
|
|
void | setActive (EventLoop &parent) |
| | Sets the parent loop, so that operations can be run.
|
|
void | detach () |
| | Remove from event loop and cancels outstanding operations.
|
|
void | cancel () |
| | Cancels all operations.
|
|
bool | run () |
| | Run operation if it is ready.
|
|
EventLoop * | parent () const |
| | Returns the used event loop.
|
|
|
| IODevice () |
| | Default Constructor.
|
|
virtual void | onCancel () |
| | Blocks until operation has cancelled.
|
|
void | post () |
| | Posts this selectable to its event loop from any thread.
|
|
virtual void | onAttach (EventLoop &) |
| | Attached to loop.
|
|
virtual void | onDetach (EventLoop &) |
| | Detached from loop.
|
|
virtual bool | onRun ()=0 |
| | Check if ready and run.
|
IODevice is the endpoint for I/O. It is a Selectable. A file, a pipe end, and a serial port are IODevice types. I/O buffers and I/O streams use this endpoint, so a standard C++ stream can be built at runtime.
read() and write() transfer bytes and may return fewer than requested. They throw IOError on failure. When the device reaches the end of the stream, isEof() is true. setTimeout() limits how long a blocking transfer waits. close() releases the endpoint.
beginRead() and beginWrite() start an asynchronous transfer. The device must be attached to an EventLoop with setActive(). When the transfer finishes, inputReady or outputReady is emitted. endRead() and endWrite() complete the operation and return the number of bytes. Only one read and one write may run at a time.
Some devices can seek. seekable() reports that. seek() and position() throw IOError when the device cannot seek. peek() copies bytes without consuming them. sync() commits written data to the device.
class Reader
{
public:
: _device(&device)
{
_device->inputReady() +=
Pt::slot(*
this, &Reader::onInput);
}
void begin()
{
_device->beginRead(_buffer, sizeof(_buffer));
}
{
}
private:
char _buffer[256];
};
ConstMethodSlot< R, ClassT, As... > slot(ClassT &object, R(BaseT::*method)(As...) const)
Returns a slot object for the given object/member pair.
Definition ConstMethod.h:172
Endpoint for I/O operations.
Definition IODevice.h:96
std::size_t endRead()
Ends reading data.
void beginRead(char *buffer, std::size_t n)
Begins to read data.
◆ read()
| std::size_t read |
( |
char * | buffer, |
|
|
std::size_t | n ) |
Reads up to n bytes and stores them in buffer. Returns the number of bytes read, which may be less than requested and even 0 if the device operates in asynchronous (non-blocking) mode. In case of EOF the IODevice is set to eof.
- Parameters
-
| buffer | buffer where to place the data to be read. |
| n | number of bytes to read |
- Returns
- number of bytes read, which may be less than requested.
- Exceptions
-
◆ write()
| std::size_t write |
( |
const char * | buffer, |
|
|
std::size_t | n ) |
Writes n bytes from buffer to this I/O device. Returns the number of bytes written, which may be less than requested. In case of EOF the IODevice is set to eof.
- Exceptions
-
◆ seek()
| pos_type seek |
( |
off_type | offset, |
|
|
seekdir | sd ) |
◆ peek()
| std::size_t peek |
( |
char * | buffer, |
|
|
std::size_t | n ) |
◆ sync()
Commits written data to physical device.
- Exceptions
-
◆ position()
The current I/O position is returned or an IOError is thrown if the device is not seekable. Seekability can be tested with seekable().
- Exceptions
-
◆ inputReady()
This signal is send when the IODevice is monitored in an EventLoop and data becomes available.
◆ outputReady()
This signal is send when the IODevice is monitored in an EventLoop and the device is ready to write data.