nidas v1.2.3
|
An IODevice supporting a TCP or UNIX server socket. More...
#include <ServerSocketIODevice.h>
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. | |
ServerSocketIODevice & | operator= (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. | |
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.
ServerSocketIODevice::ServerSocketIODevice | ( | ) |
Create a SocketIODevice.
No IO operations to the sensor are performed in the constructor (hence no IOExceptions).
|
virtual |
References close(), and closeServerSocket().
|
private |
No copying.
|
virtual |
close the sensor (and any associated FIFOs).
nidas::util::IOException |
Implements nidas::core::IODevice.
References _socket, nidas::util::Socket::close(), nidas::util::Socket::getFd(), nidas::core::IODevice::getName(), nidas::core::getSample(), and LOG_INFO.
Referenced by open(), and ~ServerSocketIODevice().
|
protected |
nidas::util::IOException |
References _serverSocket, nidas::util::ServerSocket::close(), and nidas::util::ServerSocket::getFd().
Referenced by ~ServerSocketIODevice().
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(), close(), nidas::core::TCPSocketIODevice::close(), nidas::core::UDPSocketIODevice::close(), nidas::core::UnixIODevice::close(), nidas::core::IODevice::getBytesAvailable(), nidas::core::UnixIODevice::ioctl(), ioctl(), nidas::core::SocketIODevice::ioctl(), nidas::core::SerialPortIODevice::open(), open(), nidas::core::SocketIODevice::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().
|
inlinevirtual |
The file descriptor used when reading from this SocketIODevice.
Implements nidas::core::IODevice.
References _socket, and nidas::util::Socket::getFd().
|
inline |
|
inlinevirtual |
The file descriptor used when writing to this sensor.
Implements nidas::core::IODevice.
References _socket, and nidas::util::Socket::getFd().
Perform an ioctl on the device.
Not necessary for a socket, and will throw an IOException.
nidas::util::IOException |
Implements nidas::core::IODevice.
References nidas::core::IODevice::getName().
Open the socket.
Implements nidas::core::IODevice.
References _addrtype, _serverSocket, _sockAddr, _socket, _sockPort, _unixPath, nidas::util::ServerSocket::accept(), close(), nidas::core::IODevice::getName(), nidas::core::getSample(), getTcpNoDelay(), nidas::core::SocketIODevice::parseAddress(), nidas::util::Socket::setNonBlocking(), and nidas::util::Socket::setTcpNoDelay().
|
private |
No assignment.
Read from the sensor.
nidas::util::IOException |
Implements nidas::core::IODevice.
References _socket, len, and nidas::util::Socket::recv().
|
inlinevirtual |
Read from the sensor with a timeout in milliseconds.
nidas::util::IOException |
Implements nidas::core::IODevice.
References _socket, nidas::core::getSample(), len, nidas::util::Socket::recv(), and nidas::util::Socket::setTimeout().
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().
nidas::util::IOException |
References _tcpNoDelay, and nidas::core::getSample().
Referenced by nidas::dynld::raf::CVI_LV_Input::buildIODevice().
Write to the sensor.
nidas::util::IOException |
Implements nidas::core::IODevice.
References _socket, len, and nidas::util::Socket::send().
|
private |
The type of the destination address, AF_INET or AF_UNIX.
Referenced by open().
|
privateinherited |
Whether to reopen this sensor on an IOException.
Referenced by nidas::core::IODevice::getName(), and nidas::core::IODevice::setName().
|
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().
|
private |
The destination socket address.
Referenced by open().
|
private |
Referenced by close(), getReadFd(), getWriteFd(), open(), read(), read(), and write().
|
private |
Port number that is parsed from sensor name.
Referenced by open().
|
private |
Referenced by getTcpNoDelay(), and setTcpNoDelay().
|
private |
Path name of AF_UNIX socket.
Referenced by open().