nidas v1.2.3
Public Member Functions | Protected Attributes | Private Attributes | List of all members
nidas::core::UnixIODevice Class Reference

A basic Unix I/O device, such as a named pipe, or a watched file. More...

#include <UnixIODevice.h>

Inheritance diagram for nidas::core::UnixIODevice:
Inheritance graph
[legend]

Public Member Functions

 UnixIODevice ()
 Constructor.
 
 UnixIODevice (const std::string &name)
 Constructor, passing the name of the device.
 
 ~UnixIODevice ()
 Destructor.
 
int getReadFd () const
 The file descriptor used when reading from this device.
 
int getWriteFd () const
 The file descriptor used when writing to this device.
 
void open (int flags)
 open the device.
 
size_t read (void *buf, size_t len)
 Read from the device.
 
size_t read (void *buf, size_t len, int msecTimeout)
 Read from the device with a timeout in milliseconds.
 
size_t write (const void *buf, size_t len)
 Write to the device.
 
void ioctl (int request, void *buf, size_t)
 
void close ()
 close the device
 
virtual void setName (const std::string &val)
 Set the device name to be opened for this sensor.
 
virtual const std::string & getName () const
 
virtual size_t getBytesAvailable () const
 Return how many bytes are available to read on this IODevice.
 

Protected Attributes

int _fd
 

Private Attributes

std::string _devname
 Whether to reopen this sensor on an IOException.
 

Detailed Description

A basic Unix I/O device, such as a named pipe, or a watched file.

Constructor & Destructor Documentation

◆ UnixIODevice() [1/2]

nidas::core::UnixIODevice::UnixIODevice ( )
inline

Constructor.

Does not open any actual device.

◆ UnixIODevice() [2/2]

nidas::core::UnixIODevice::UnixIODevice ( const std::string & name)
inline

Constructor, passing the name of the device.

Does not open the device.

References nidas::core::IODevice::setName().

◆ ~UnixIODevice()

nidas::core::UnixIODevice::~UnixIODevice ( )
inline

Destructor.

Does not close the device.

Member Function Documentation

◆ close()

void nidas::core::UnixIODevice::close ( )
inlinevirtual

close the device

Exceptions
nidas::util::IOException

Implements nidas::core::IODevice.

References _fd, close(), fd, and nidas::core::IODevice::getName().

Referenced by close().

◆ getBytesAvailable()

virtual size_t nidas::core::IODevice::getBytesAvailable ( ) const
inlinevirtualinherited

Return how many bytes are available to read on this IODevice.

This method is only useful when ioctl FIONREAD is supported on this this IODevice, as for example with a UDP socket. It is not available, and not necessary, on most other devices, like serial ports, TCP sockets, or devices with nidas driver module support, in which case it will return an nidas::util::IOException. It is an optimization for use with UDP sockets, where after select determines that data is available on the socket file descriptor, a read will only read one datagram, even if there are more than one packet available, which would not be optimal if a sensor generated many small packets. Rather than returning back to select, we check if there are more datagrams to read. This is not necessary on other types of IODevices, where it is just a matter of using a big enough buffer to get all (or most) available data after a select.

According to man udp(7), FIONREAD will return 0 if the size of the next packet is 0. So, if select/poll indicate there is data available, one read at least should be done, just in case a sensor sends a datagram of length 0, otherwise it will never be consumed.

Exceptions
nidas::util::IOException

References err, nidas::core::IODevice::getName(), nidas::core::IODevice::getReadFd(), and nidas::core::IODevice::ioctl().

Referenced by nidas::core::DSMSensor::getBytesAvailable().

◆ getName()

virtual const std::string & nidas::core::IODevice::getName ( ) const
inlinevirtualinherited

◆ getReadFd()

int nidas::core::UnixIODevice::getReadFd ( ) const
inlinevirtual

The file descriptor used when reading from this device.

Implements nidas::core::IODevice.

References _fd.

◆ getWriteFd()

int nidas::core::UnixIODevice::getWriteFd ( ) const
inlinevirtual

The file descriptor used when writing to this device.

Implements nidas::core::IODevice.

References _fd.

◆ ioctl()

void nidas::core::UnixIODevice::ioctl ( int request,
void * buf,
size_t  )
inlinevirtual

◆ open()

void nidas::core::UnixIODevice::open ( int flags)
inlinevirtual

◆ read() [1/2]

size_t nidas::core::UnixIODevice::read ( void * buf,
size_t len )
inlinevirtual

Read from the device.

Exceptions
nidas::util::IOException

Implements nidas::core::IODevice.

References _fd, nidas::core::IODevice::getName(), nidas::core::getSample(), len, and read().

Referenced by read(), and read().

◆ read() [2/2]

size_t UnixIODevice::read ( void * buf,
size_t len,
int msecTimeout )
virtual

Read from the device with a timeout in milliseconds.

Exceptions
nidas::util::IOException

Implements nidas::core::IODevice.

References _fd, nidas::core::IODevice::getName(), nidas::core::getSample(), len, MSECS_PER_SEC, NSECS_PER_MSEC, and read().

◆ setName()

virtual void nidas::core::IODevice::setName ( const std::string & val)
inlinevirtualinherited

Set the device name to be opened for this sensor.

References nidas::core::IODevice::_devname, and nidas::core::getSample().

Referenced by nidas::core::DSMSensor::open(), and UnixIODevice().

◆ write()

size_t nidas::core::UnixIODevice::write ( const void * buf,
size_t len )
inlinevirtual

Write to the device.

Exceptions
nidas::util::IOException

Implements nidas::core::IODevice.

References _fd, nidas::core::IODevice::getName(), nidas::core::getSample(), len, and write().

Referenced by write().

Member Data Documentation

◆ _devname

std::string nidas::core::IODevice::_devname
privateinherited

Whether to reopen this sensor on an IOException.

Referenced by nidas::core::IODevice::getName(), and nidas::core::IODevice::setName().

◆ _fd

int nidas::core::UnixIODevice::_fd
protected

The documentation for this class was generated from the following files: