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

An IODevice supporting a TCP or UNIX server socket. More...

#include <ServerSocketIODevice.h>

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

Public Member Functions

 ServerSocketIODevice ()
 Create a SocketIODevice.
 
virtual ~ServerSocketIODevice ()
 
int getReadFd () const
 The file descriptor used when reading from this SocketIODevice.
 
int getWriteFd () const
 The file descriptor used when writing to this sensor.
 
void open (int flags)
 Open the socket.
 
size_t read (void *buf, size_t len)
 Read from the sensor.
 
size_t read (void *buf, size_t len, int msecTimeout)
 Read from the sensor with a timeout in milliseconds.
 
size_t write (const void *buf, size_t len)
 Write to the sensor.
 
void ioctl (int, void *, size_t)
 Perform an ioctl on the device.
 
void close ()
 close the sensor (and any associated FIFOs).
 
void setTcpNoDelay (bool val)
 
bool getTcpNoDelay ()
 
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 Member Functions

void closeServerSocket ()
 

Private Member Functions

 ServerSocketIODevice (const ServerSocketIODevice &)
 No copying.
 
ServerSocketIODeviceoperator= (const ServerSocketIODevice &)
 No assignment.
 

Private Attributes

int _addrtype
 The type of the destination address, AF_INET or AF_UNIX.
 
std::string _unixPath
 Path name of AF_UNIX socket.
 
int _sockPort
 Port number that is parsed from sensor name.
 
nidas::util::auto_ptr< nidas::util::SocketAddress_sockAddr
 The destination socket address.
 
nidas::util::ServerSocket_serverSocket
 The listen socket.
 
nidas::util::Socket_socket
 
bool _tcpNoDelay
 
std::string _devname
 Whether to reopen this sensor on an IOException.
 

Detailed Description

An IODevice supporting a TCP or UNIX server socket.

This class has a critical limitation and isn't currently used anywhere in NIDAS. The IODevice::open() method should not block, and this class violates that because open() does a nidas::util::ServerSocket::accept() which can block forever. To really support this class we need to spawn a ServerSocket listening thread.

Constructor & Destructor Documentation

◆ ServerSocketIODevice() [1/2]

ServerSocketIODevice::ServerSocketIODevice ( )

Create a SocketIODevice.

No IO operations to the sensor are performed in the constructor (hence no IOExceptions).

◆ ~ServerSocketIODevice()

ServerSocketIODevice::~ServerSocketIODevice ( )
virtual

References close(), and closeServerSocket().

◆ ServerSocketIODevice() [2/2]

nidas::core::ServerSocketIODevice::ServerSocketIODevice ( const ServerSocketIODevice & )
private

No copying.

Member Function Documentation

◆ close()

void ServerSocketIODevice::close ( )
virtual

◆ closeServerSocket()

void ServerSocketIODevice::closeServerSocket ( )
protected

◆ 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::ServerSocketIODevice::getReadFd ( ) const
inlinevirtual

The file descriptor used when reading from this SocketIODevice.

Implements nidas::core::IODevice.

References _socket, and nidas::util::Socket::getFd().

◆ getTcpNoDelay()

bool nidas::core::ServerSocketIODevice::getTcpNoDelay ( )
inline
Exceptions
nidas::util::IOException

References _tcpNoDelay.

Referenced by open().

◆ getWriteFd()

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

The file descriptor used when writing to this sensor.

Implements nidas::core::IODevice.

References _socket, and nidas::util::Socket::getFd().

◆ ioctl()

void nidas::core::ServerSocketIODevice::ioctl ( int ,
void * ,
size_t  )
inlinevirtual

Perform an ioctl on the device.

Not necessary for a socket, and will throw an IOException.

Exceptions
nidas::util::IOException

Implements nidas::core::IODevice.

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

◆ open()

void ServerSocketIODevice::open ( int flags)
virtual

◆ operator=()

ServerSocketIODevice & nidas::core::ServerSocketIODevice::operator= ( const ServerSocketIODevice & )
private

No assignment.

◆ read() [1/2]

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

Read from the sensor.

Exceptions
nidas::util::IOException

Implements nidas::core::IODevice.

References _socket, len, and nidas::util::Socket::recv().

◆ read() [2/2]

size_t nidas::core::ServerSocketIODevice::read ( void * buf,
size_t len,
int msecTimeout )
inlinevirtual

Read from the sensor with a timeout in milliseconds.

Exceptions
nidas::util::IOException

Implements nidas::core::IODevice.

References _socket, nidas::core::getSample(), len, nidas::util::Socket::recv(), and nidas::util::Socket::setTimeout().

◆ 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 nidas::core::UnixIODevice::UnixIODevice().

◆ setTcpNoDelay()

void nidas::core::ServerSocketIODevice::setTcpNoDelay ( bool val)
inline

◆ write()

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

Write to the sensor.

Exceptions
nidas::util::IOException

Implements nidas::core::IODevice.

References _socket, len, and nidas::util::Socket::send().

Member Data Documentation

◆ _addrtype

int nidas::core::ServerSocketIODevice::_addrtype
private

The type of the destination address, AF_INET or AF_UNIX.

Referenced by open().

◆ _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().

◆ _serverSocket

nidas::util::ServerSocket* nidas::core::ServerSocketIODevice::_serverSocket
private

The listen socket.

This isn't in an auto_ptr because one must close the socket prior to deleting it. The nidas::util::Socket destructor does not close the file descriptor.

Referenced by closeServerSocket(), and open().

◆ _sockAddr

nidas::util::auto_ptr<nidas::util::SocketAddress> nidas::core::ServerSocketIODevice::_sockAddr
private

The destination socket address.

Referenced by open().

◆ _socket

nidas::util::Socket* nidas::core::ServerSocketIODevice::_socket
private

◆ _sockPort

int nidas::core::ServerSocketIODevice::_sockPort
private

Port number that is parsed from sensor name.

Referenced by open().

◆ _tcpNoDelay

bool nidas::core::ServerSocketIODevice::_tcpNoDelay
private

Referenced by getTcpNoDelay(), and setTcpNoDelay().

◆ _unixPath

std::string nidas::core::ServerSocketIODevice::_unixPath
private

Path name of AF_UNIX socket.

Referenced by open().


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