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

An IODevice consisting of a TCP socket. More...

#include <TCPSocketIODevice.h>

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

Public Member Functions

 TCPSocketIODevice ()
 Create a TCPSocketIODevice.
 
 ~TCPSocketIODevice ()
 
void open (int flags)
 Open the socket, which does a socket connect to the remote address which is parsed from the contents of getName().
 
int getReadFd () const
 The file descriptor used when reading from this SocketIODevice.
 
int getWriteFd () const
 The file descriptor used when writing to this 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 close ()
 close the device.
 
void setTcpNoDelay (bool val)
 
bool getTcpNoDelay ()
 
void setKeepAliveIdleSecs (int val)
 
int getKeepAliveIdleSecs () const
 
void ioctl (int, void *, size_t)
 
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.
 

Static Public Member Functions

static void parseAddress (const std::string &name, int &addrtype, std::string &hostname, int &port, std::string &bindaddr)
 Parse the getName() string to extract a socket type, destination address and optional port number.
 

Protected Member Functions

 TCPSocketIODevice (const TCPSocketIODevice &)
 No copy.
 
TCPSocketIODeviceoperator= (const TCPSocketIODevice &)
 No assignment.
 

Protected Attributes

nidas::util::Socket_socket
 The socket.
 
bool _tcpNoDelay
 
int _keepAliveIdleSecs
 
int _addrtype
 The type of the destination address, AF_INET or AF_UNIX.
 
std::string _desthost
 Destination host name from sensor name.
 
int _port
 Port number that is parsed from sensor name.
 
nidas::util::auto_ptr< nidas::util::SocketAddress_sockAddr
 The destination socket address.
 
std::string _bindAddr
 The local bind socket address.
 

Private Attributes

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

Detailed Description

An IODevice consisting of a TCP socket.

Constructor & Destructor Documentation

◆ TCPSocketIODevice() [1/2]

TCPSocketIODevice::TCPSocketIODevice ( )

Create a TCPSocketIODevice.

No IO operations are performed in the constructor, hence no IOExceptions.

◆ ~TCPSocketIODevice()

TCPSocketIODevice::~TCPSocketIODevice ( )

References close().

◆ TCPSocketIODevice() [2/2]

nidas::core::TCPSocketIODevice::TCPSocketIODevice ( const TCPSocketIODevice & )
protected

No copy.

Member Function Documentation

◆ close()

void TCPSocketIODevice::close ( )
virtual

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

◆ getKeepAliveIdleSecs()

int nidas::core::TCPSocketIODevice::getKeepAliveIdleSecs ( ) const
inline
Exceptions
nidas::util::IOException

References _keepAliveIdleSecs.

Referenced by open().

◆ getName()

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

◆ getReadFd()

int nidas::core::TCPSocketIODevice::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::TCPSocketIODevice::getTcpNoDelay ( )
inline
Exceptions
nidas::util::IOException

References _tcpNoDelay.

Referenced by open().

◆ getWriteFd()

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

The file descriptor used when writing to this device.

Implements nidas::core::IODevice.

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

◆ ioctl()

void nidas::core::SocketIODevice::ioctl ( int ,
void * ,
size_t  )
inlinevirtualinherited

◆ open()

void TCPSocketIODevice::open ( int flags)
virtual

◆ operator=()

TCPSocketIODevice & nidas::core::TCPSocketIODevice::operator= ( const TCPSocketIODevice & )
protected

No assignment.

◆ parseAddress()

void SocketIODevice::parseAddress ( const std::string & name,
int & addrtype,
std::string & hostname,
int & port,
std::string & bindaddr )
staticinherited

Parse the getName() string to extract a socket type, destination address and optional port number.

The syntax of the set/getName() string is: ( "inet" | "sock" | "unix" | "blue" | "btspp" ) ':' address [:port] "inet" or "sock" indicate a TCP or UDP connection for use by derived classes implementing a TCP or UDP socket. The address field should then be a hostname which can be resolved to an IP address, or an IP address in dot notation. A port number is required for TCP/UDP socket connection. "unix" indicates a Unix socket connection, where address is the path name of the unix socket, for process to process communications. The port value is not used by unix sockets.

Exceptions
nidas::util::ParseException

References nidas::core::getSample(), and port.

Referenced by nidas::core::ServerSocketIODevice::open(), nidas::core::SocketIODevice::open(), and nidas::dynld::raf::PSI9116_Sensor::open().

◆ read() [1/2]

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

Read from the device.

Exceptions
nidas::util::IOException

Implements nidas::core::IODevice.

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

◆ read() [2/2]

size_t TCPSocketIODevice::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 _socket, nidas::core::getSample(), len, nidas::util::Socket::recv(), and nidas::util::Socket::setTimeout().

◆ setKeepAliveIdleSecs()

void nidas::core::TCPSocketIODevice::setKeepAliveIdleSecs ( int val)
inline

◆ 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::TCPSocketIODevice::setTcpNoDelay ( bool val)
inline

◆ write()

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

Write to the device.

Exceptions
nidas::util::IOException

Implements nidas::core::IODevice.

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

Member Data Documentation

◆ _addrtype

int nidas::core::SocketIODevice::_addrtype
protectedinherited

The type of the destination address, AF_INET or AF_UNIX.

Referenced by nidas::core::SocketIODevice::open().

◆ _bindAddr

std::string nidas::core::SocketIODevice::_bindAddr
protectedinherited

The local bind socket address.

Referenced by nidas::core::SocketIODevice::open().

◆ _desthost

std::string nidas::core::SocketIODevice::_desthost
protectedinherited

Destination host name from sensor name.

Referenced by nidas::core::SocketIODevice::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().

◆ _keepAliveIdleSecs

int nidas::core::TCPSocketIODevice::_keepAliveIdleSecs
protected

◆ _port

int nidas::core::SocketIODevice::_port
protectedinherited

Port number that is parsed from sensor name.

For a server socket this is the local port number to listen on. For a remote connection socket this is the remote port number.

Referenced by nidas::core::SocketIODevice::open().

◆ _sockAddr

nidas::util::auto_ptr<nidas::util::SocketAddress> nidas::core::SocketIODevice::_sockAddr
protectedinherited

The destination socket address.

Referenced by nidas::core::SocketIODevice::open(), open(), and nidas::core::UDPSocketIODevice::open().

◆ _socket

nidas::util::Socket* nidas::core::TCPSocketIODevice::_socket
protected

The 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 close(), getReadFd(), getWriteFd(), open(), read(), read(), and write().

◆ _tcpNoDelay

bool nidas::core::TCPSocketIODevice::_tcpNoDelay
protected

Referenced by getTcpNoDelay(), and setTcpNoDelay().


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