nidas v1.2.3
|
A IODevice providing support for UDP and TCP sockets. More...
#include <SocketIODevice.h>
Public Member Functions | |
SocketIODevice () | |
Create a SocketIODevice. | |
~SocketIODevice () | |
void | open (int flags) |
Prepare to open the socket. | |
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 int | getReadFd () const =0 |
The file descriptor used when reading from this sensor. | |
virtual int | getWriteFd () const =0 |
The file descriptor used when writing to this sensor. | |
virtual size_t | read (void *buf, size_t len)=0 |
Read from the sensor. | |
virtual size_t | read (void *buf, size_t len, int msecTimeout)=0 |
Read from the sensor with a millisecond timeout. | |
virtual size_t | getBytesAvailable () const |
Return how many bytes are available to read on this IODevice. | |
virtual size_t | write (const void *buf, size_t len)=0 |
Write to the sensor. | |
virtual void | close ()=0 |
Close the device. | |
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 Attributes | |
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. | |
A IODevice providing support for UDP and TCP sockets.
SocketIODevice::SocketIODevice | ( | ) |
Create a SocketIODevice.
No IO operations are performed in the constructor, hence no IOExceptions.
SocketIODevice::~SocketIODevice | ( | ) |
Close the device.
nidas::util::IOException |
Implemented in nidas::core::ServerSocketIODevice, nidas::core::TCPSocketIODevice, nidas::core::UDPSocketIODevice, and nidas::core::UnixIODevice.
Referenced by nidas::core::DSMSensor::close().
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.
nidas::util::IOException |
References err, nidas::core::IODevice::getName(), nidas::core::IODevice::getReadFd(), and nidas::core::IODevice::ioctl().
Referenced by nidas::core::DSMSensor::getBytesAvailable().
References nidas::core::IODevice::_devname.
Referenced by nidas::core::SerialPortIODevice::applyTermios(), nidas::core::ServerSocketIODevice::close(), nidas::core::TCPSocketIODevice::close(), nidas::core::UDPSocketIODevice::close(), nidas::core::UnixIODevice::close(), nidas::core::IODevice::getBytesAvailable(), nidas::core::UnixIODevice::ioctl(), nidas::core::ServerSocketIODevice::ioctl(), ioctl(), nidas::core::SerialPortIODevice::open(), nidas::core::ServerSocketIODevice::open(), open(), nidas::core::UnixIODevice::open(), nidas::core::UnixIODevice::read(), nidas::core::UnixIODevice::read(), nidas::core::SerialPortIODevice::setRTS485(), nidas::core::SerialPortIODevice::write(), and nidas::core::UnixIODevice::write().
The file descriptor used when reading from this sensor.
Implemented in nidas::core::ServerSocketIODevice, nidas::core::TCPSocketIODevice, nidas::core::UDPSocketIODevice, and nidas::core::UnixIODevice.
Referenced by nidas::core::IODevice::getBytesAvailable(), and nidas::core::DSMSensor::getReadFd().
The file descriptor used when writing to this sensor.
Implemented in nidas::core::ServerSocketIODevice, nidas::core::TCPSocketIODevice, nidas::core::UDPSocketIODevice, and nidas::core::UnixIODevice.
Referenced by nidas::core::DSMSensor::getWriteFd().
Implements nidas::core::IODevice.
References nidas::core::IODevice::getName().
Prepare to open the socket.
This actually just parses the address from the device name, and doesn't actually do a socket connection or bind. See parseAddress(). The connect or bind must be done in the TCPSocketIODevice or UDPSocketIODevice open method. If the name contains a IP hostname and the IP address of that host is not available, then an InvalidParameterException is thrown.
Implements nidas::core::IODevice.
Reimplemented in nidas::core::TCPSocketIODevice, and nidas::core::UDPSocketIODevice.
References _addrtype, _bindAddr, _desthost, _port, _sockAddr, nidas::core::IODevice::getName(), and parseAddress().
Referenced by nidas::core::TCPSocketIODevice::open(), and nidas::core::UDPSocketIODevice::open().
|
static |
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.
nidas::util::ParseException |
References nidas::core::getSample(), and port.
Referenced by nidas::core::ServerSocketIODevice::open(), open(), and nidas::dynld::raf::PSI9116_Sensor::open().
Read from the sensor.
nidas::util::IOException |
Implemented in nidas::core::ServerSocketIODevice, nidas::core::TCPSocketIODevice, nidas::core::UDPSocketIODevice, and nidas::core::UnixIODevice.
Referenced by nidas::core::DSMSensor::read(), and nidas::core::DSMSensor::read().
|
pure virtualinherited |
Read from the sensor with a millisecond timeout.
nidas::util::IOException |
Implemented in nidas::core::ServerSocketIODevice, nidas::core::TCPSocketIODevice, nidas::core::UDPSocketIODevice, and nidas::core::UnixIODevice.
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().
Write to the sensor.
nidas::util::IOException |
Implemented in nidas::core::SerialPortIODevice, nidas::core::ServerSocketIODevice, nidas::core::TCPSocketIODevice, nidas::core::UDPSocketIODevice, and nidas::core::UnixIODevice.
Referenced by nidas::core::DSMSensor::write().
|
protected |
The type of the destination address, AF_INET or AF_UNIX.
Referenced by open().
|
protected |
The local bind socket address.
Referenced by open().
|
protected |
Destination host name from sensor name.
Referenced by open().
|
privateinherited |
Whether to reopen this sensor on an IOException.
Referenced by nidas::core::IODevice::getName(), and nidas::core::IODevice::setName().
|
protected |
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 open().
|
protected |
The destination socket address.
Referenced by open(), nidas::core::TCPSocketIODevice::open(), and nidas::core::UDPSocketIODevice::open().