21#ifndef INCLUDE_OLA_IO_DESCRIPTOR_H_
22#define INCLUDE_OLA_IO_DESCRIPTOR_H_
28#include <ola/io/IOQueue.h>
79 GENERIC_DESCRIPTOR = 0,
85struct DescriptorHandle {
92 DescriptorType m_type;
96 uint8_t* m_async_data;
98 uint32_t* m_async_data_size;
103 bool AllocAsyncBuffer();
104 void FreeAsyncBuffer();
106 bool IsValid()
const;
109void* ToHandle(
const DescriptorHandle &handle);
111static DescriptorHandle INVALID_DESCRIPTOR;
112static const uint32_t ASYNC_DATA_BUFFER_SIZE = 1024;
113bool operator!=(
const DescriptorHandle &lhs,
const DescriptorHandle &rhs);
114bool operator==(
const DescriptorHandle &lhs,
const DescriptorHandle &rhs);
115bool operator<(
const DescriptorHandle &lhs,
const DescriptorHandle &rhs);
116std::ostream&
operator<<(std::ostream &stream,
const DescriptorHandle &data);
118typedef int DescriptorHandle;
119static DescriptorHandle INVALID_DESCRIPTOR = -1;
131int ToFD(
const DescriptorHandle& handle);
232 m_on_write = on_write;
260 DescriptorHandle m_handle;
300 virtual ssize_t
Send(
const uint8_t *buffer,
unsigned int size);
323 virtual int Receive(uint8_t *buffer,
325 unsigned int &data_read);
337 virtual bool Close() = 0;
358 m_on_close = on_close;
382 virtual bool IsSocket()
const = 0;
390 OnCloseCallback *m_on_close;
425 bool IsSocket()
const {
return false; }
428 DescriptorHandle m_handle_pair[2];
431 LoopbackDescriptor(
const LoopbackDescriptor &) =
delete;
432 const LoopbackDescriptor &operator=(
const LoopbackDescriptor &) =
delete;
473 bool IsSocket()
const {
return false; }
476 DescriptorHandle m_in_pair[2];
477 DescriptorHandle m_out_pair[2];
478 PipeDescriptor *m_other_end;
480 PipeDescriptor(DescriptorHandle in_pair[2],
481 DescriptorHandle out_pair[2],
482 PipeDescriptor *other_end);
485 PipeDescriptor(
const PipeDescriptor &) =
delete;
486 const PipeDescriptor &operator=(
const PipeDescriptor &) =
delete;
496 m_handle = INVALID_DESCRIPTOR;
528 bool IsSocket()
const {
return true; }
531 DescriptorHandle m_handle;
532 UnixSocket *m_other_end;
533 UnixSocket(
int socket, UnixSocket *other_end);
535 UnixSocket(
const UnixSocket &) =
delete;
536 const UnixSocket &operator=(
const UnixSocket &) =
delete;
560 bool IsSocket()
const {
return false; }
563 DescriptorHandle m_handle;
A 0 argument callback which can be called multiple times.
Definition Callback.h:129
A 0 arg, single use callback that returns void.
Definition Callback.h:157
A 0 argument callback which deletes itself after it's run.
Definition Callback.h:141
A file descriptor that supports both read & write.
Definition Descriptor.h:201
void SetOnData(ola::Callback0< void > *on_read)
Set the callback to be run when data is available for reading.
Definition Descriptor.h:218
void PerformRead()
Called when there is data available on the descriptor.
Definition Descriptor.cpp:212
void SetOnWritable(ola::Callback0< void > *on_write)
Set the callback to be run when the descriptor can be written to.
Definition Descriptor.h:229
void PerformWrite()
Called when the descriptor can be written to.
Definition Descriptor.cpp:221
A BidirectionalFileDescriptor that also generates notifications when closed.
Definition Descriptor.h:283
bool IsClosed() const
Check if the descriptor is closed.
Definition Descriptor.cpp:493
OnCloseCallback * TransferOnClose()
Take ownership of the on_close callback.
Definition Descriptor.h:370
static bool SetNonBlocking(DescriptorHandle fd)
Set a DescriptorHandle to non-blocking mode.
Definition Descriptor.cpp:246
virtual int Receive(uint8_t *buffer, unsigned int size, unsigned int &data_read)
Read data from this descriptor.
Definition Descriptor.cpp:422
bool SetNoSigPipe(DescriptorHandle fd)
Disable SIGPIPE for this descriptor.
Definition Descriptor.cpp:267
void SetOnClose(OnCloseCallback *on_close)
Set the callback to be run when the descriptor is closed.
Definition Descriptor.h:355
virtual ssize_t Send(const uint8_t *buffer, unsigned int size)
Write a buffer to the descriptor.
Definition Descriptor.cpp:318
virtual bool SetReadNonBlocking()
Enable on non-blocking reads..
Definition Descriptor.h:333
int DataRemaining() const
Find out how much data is left to read.
Definition Descriptor.cpp:289
A descriptor which represents a connection to a device.
Definition Descriptor.h:542
DescriptorHandle ReadDescriptor() const
Returns the read descriptor for this socket.
Definition Descriptor.h:551
DescriptorHandle WriteDescriptor() const
Returns the write descriptor for this socket.
Definition Descriptor.h:552
bool Close()
Close this DeviceDescriptor.
Definition Descriptor.cpp:721
DeviceDescriptor(int fd)
Create a new DeviceDescriptor.
Definition Descriptor.cpp:712
A loopback descriptor.
Definition Descriptor.h:399
DescriptorHandle WriteDescriptor() const
Returns the write descriptor for this socket.
Definition Descriptor.h:410
DescriptorHandle ReadDescriptor() const
Returns the read descriptor for this socket.
Definition Descriptor.h:409
bool Close()
Close the loopback descriptor.
Definition Descriptor.cpp:518
bool Init()
Setup this loopback descriptor.
Definition Descriptor.cpp:505
bool CloseClient()
Close the write portion of the loopback descriptor.
Definition Descriptor.cpp:540
A descriptor that uses unix pipes.
Definition Descriptor.h:441
bool Close()
Close this PipeDescriptor.
Definition Descriptor.cpp:600
bool CloseClient()
Close the write portion of this PipeDescriptor.
Definition Descriptor.cpp:622
DescriptorHandle ReadDescriptor() const
Returns the read descriptor for this socket.
Definition Descriptor.h:459
DescriptorHandle WriteDescriptor() const
Returns the write descriptor for this socket.
Definition Descriptor.h:460
bool Init()
Initialize the PipeDescriptor.
Definition Descriptor.cpp:562
PipeDescriptor * OppositeEnd()
Fetch the other end of the PipeDescriptor.
Definition Descriptor.cpp:588
Represents a file descriptor that supports reading data.
Definition Descriptor.h:140
virtual void PerformRead()=0
Called when there is data available on the descriptor.
virtual DescriptorHandle ReadDescriptor() const =0
Returns the read descriptor for this socket.
bool ValidReadDescriptor() const
Check if this file descriptor is valid.
Definition Descriptor.h:154
A unix domain socket pair.
Definition Descriptor.h:492
bool Close()
Close this UnixSocket.
Definition Descriptor.cpp:678
DescriptorHandle WriteDescriptor() const
Returns the write descriptor for this socket.
Definition Descriptor.h:514
UnixSocket * OppositeEnd()
Fetch the other end of the unix socket.
Definition Descriptor.cpp:671
DescriptorHandle ReadDescriptor() const
Returns the read descriptor for this socket.
Definition Descriptor.h:513
bool CloseClient()
Close the write portion of this UnixSocket.
Definition Descriptor.cpp:691
bool Init()
Initialize the UnixSocket.
Definition Descriptor.cpp:649
Allows a FD created by a library to be used with the SelectServer.
Definition Descriptor.h:247
DescriptorHandle ReadDescriptor() const
Returns the read descriptor for this socket.
Definition Descriptor.h:255
DescriptorHandle WriteDescriptor() const
Returns the write descriptor for this socket.
Definition Descriptor.h:256
UnmanagedFileDescriptor(int fd)
Create a new UnmanagedFileDescriptor.
Definition Descriptor.cpp:232
Represents a file descriptor that supports writing data.
Definition Descriptor.h:170
virtual DescriptorHandle WriteDescriptor() const =0
Returns the write descriptor for this socket.
bool ValidWriteDescriptor() const
Check if this file descriptor is valid.
Definition Descriptor.h:184
virtual void PerformWrite()=0
Called when the descriptor can be written to.
int ToFD(const DescriptorHandle &handle)
Definition Descriptor.cpp:125
The namespace containing all OLA symbols.
Definition Credentials.cpp:44
std::ostream & operator<<(std::ostream &out, const DmxBuffer &data)
Stream operator to allow DmxBuffer to be output to stdout.
Definition DmxBuffer.cpp:402
Comparison operator for UnmanagedFileDescriptor.
Definition Descriptor.h:271