nidas
v1.2-1520
|
Implementation of a socket, providing a C++ interface to system socket calls: socket,bind,listen,accept,setsockopt, etc. More...
#include <Socket.h>
Public Member Functions | |
SocketImpl (int domain, int type) throw (IOException) | |
SocketImpl (int fd, const SocketAddress &remoteaddr) throw (IOException) | |
SocketImpl (const SocketImpl &x) | |
Copy constructor. More... | |
SocketImpl & | operator= (const SocketImpl &rhs) |
Assignment operator. More... | |
~SocketImpl () | |
int | getFd () const |
int | getDomain () const |
Get the domain of this socket: AF_UNIX, AF_INET, etc, from sys/socket.h. More... | |
void | setBacklog (int val) |
const SocketAddress & | getLocalSocketAddress () const throw () |
Get local socket address of this socket. More... | |
int | getLocalPort () const throw () |
Get local port number of this socket. More... | |
const SocketAddress & | getRemoteSocketAddress () const throw () |
Get remote socket address of this socket. More... | |
int | getRemotePort () const throw () |
Get remote port number of this socket. More... | |
void | setReuseAddress (bool val) |
void | setNonBlocking (bool val) throw (IOException) |
bool | isNonBlocking () const throw (IOException) |
void | setTcpNoDelay (bool val) throw (IOException) |
Set the TCP_NODELAY (man 7 tcp) option on the socket. More... | |
bool | getTcpNoDelay () throw (IOException) |
Get the TCP_NODELAY (man 7 tcp) option on the socket. More... | |
void | setTimeout (int val) |
Set the timeout for receive(), recv(), and recvfrom() methods. More... | |
int | getTimeout () const |
void | setKeepAlive (bool val) throw (IOException) |
Set or unset the SO_KEEPALIVE socket option. More... | |
bool | getKeepAlive () const throw (IOException) |
Get the current value of the SO_KEEPALIVE socket option. More... | |
void | setKeepAliveIdleSecs (int val) throw (IOException) |
Set the number of seconds a connection needs to be idle before TCP begins sending out keep-alive probes (TCP_KEEPIDLE). More... | |
int | getKeepAliveIdleSecs () const throw (IOException) |
Get the current value of TCP_KEEPIDLE on this socket. More... | |
int | getInQueueSize () const throw (IOException) |
Get number of bytes currently unread in the local input queue. More... | |
int | getOutQueueSize () const throw (IOException) |
Get number of bytes currently unsent in the local output queue. More... | |
void | close () throw (IOException) |
void | bind (int port) throw (IOException) |
void | bind (const Inet4Address &addr, int port) throw (IOException) |
void | bind (const SocketAddress &sockaddr) throw (IOException) |
void | listen () throw (IOException) |
Socket * | accept () throw (IOException) |
void | connect (const std::string &dest, int port) throw (UnknownHostException,IOException) |
void | connect (const Inet4Address &addr, int port) throw (IOException) |
void | connect (const SocketAddress &addr) throw (IOException) |
void | receive (DatagramPacketBase &packet) throw (IOException) |
void | receive (DatagramPacketBase &packet, Inet4PacketInfo &info, int flags=0) throw (IOException) |
size_t | recv (void *buf, size_t len, int flags=0) throw (IOException) |
Receive data on a socket. More... | |
size_t | recvfrom (void *buf, size_t len, int flags, SocketAddress &from) throw (IOException) |
void | send (const DatagramPacketBase &packet, int flags=0) throw (IOException) |
size_t | send (const void *buf, size_t len, int flags=0) throw (IOException) |
send data on socket. More... | |
size_t | send (const struct iovec *iov, int iovcnt, int flags=0) throw (IOException) |
size_t | sendto (const void *buf, size_t len, int flags, const SocketAddress &to) throw (IOException) |
size_t | sendto (const struct iovec *iov, int iovcnt, int flags, const SocketAddress &to) throw (IOException) |
void | sendall (const void *buf, size_t len, int flags=0) throw (IOException) |
send all data in buffer on socket, repeating send() as necessary, until all data is sent (or an exception occurs). More... | |
void | joinGroup (Inet4Address groupAddr) throw (IOException) |
Join a multicast group on all interfaces. More... | |
void | joinGroup (Inet4Address groupAddr, const Inet4NetworkInterface &) throw (IOException) |
Join a multicast group on a specific interface. More... | |
void | leaveGroup (Inet4Address groupAddr) throw (IOException) |
Leave a multicast group on all interfaces. More... | |
void | leaveGroup (Inet4Address groupAddr, const Inet4NetworkInterface &iaddr) throw (IOException) |
Leave a multicast group on a given interface. More... | |
void | setReceiveBufferSize (int size) throw (IOException) |
int | getReceiveBufferSize () throw (IOException) |
void | setSendBufferSize (int size) throw (IOException) |
int | getSendBufferSize () throw (IOException) |
void | setTimeToLive (int val) throw (IOException) |
int | getTimeToLive () const throw (IOException) |
void | setPktInfo (bool val) throw (IOException) |
Control whether a IP_PKTINFO ancillary message is received with each datagram. More... | |
bool | getPktInfo () const |
void | setMulticastLoop (bool val) throw (IOException) |
Whether to set the IP_MULTICAST_LOOP socket option. More... | |
bool | getMulticastLoop () const throw (IOException) |
void | setInterface (Inet4Address maddr, const Inet4NetworkInterface &iaddr) throw (IOException) |
void | setInterface (Inet4Address iaddr) throw (IOException) |
Inet4NetworkInterface | getInterface () const throw (IOException) |
Inet4NetworkInterface | getInterface (const std::string &name) const throw (IOException) |
Inet4NetworkInterface | findInterface (const Inet4Address &) const throw (IOException) |
std::list< Inet4NetworkInterface > | getInterfaces () const throw (IOException) |
void | setBroadcastEnable (bool val) throw (IOException) |
Enable or disable SO_BROADCAST. More... | |
bool | getBroadcastEnable () const throw (IOException) |
Protected Member Functions | |
void | getLocalAddr () throw (IOException) |
Do system call to determine local address of this socket. More... | |
void | getRemoteAddr () throw (IOException) |
Do system call to determine address of remote end. More... | |
Protected Attributes | |
int | _sockdomain |
int | _socktype |
SocketAddress * | _localaddr |
SocketAddress * | _remoteaddr |
int | _fd |
int | _backlog |
bool | _reuseaddr |
bool | _hasTimeout |
struct timespec | _timeout |
bool | _pktInfo |
Implementation of a socket, providing a C++ interface to system socket calls: socket,bind,listen,accept,setsockopt, etc.
This is patterned after java.net.SocketImpl. This class includes methods for both stream (TCP) and datagram (UDP) sockets. We also haven't implemented the socket implementation factory from Java.
This class provides the public copy constructors and assignment operators. Objects of this class can be copied and assigned to without restriction. However, because of this, the destructor does not close the socket file descriptor, so, in general, you should make sure that the socket is closed once at some point.
SocketImpl::SocketImpl | ( | int | domain, |
int | type | ||
) | |||
throw | ( | IOException | |
) |
SocketImpl::SocketImpl | ( | int | fd, |
const SocketAddress & | remoteaddr | ||
) | |||
throw | ( | IOException | |
) |
SocketImpl::SocketImpl | ( | const SocketImpl & | x | ) |
Copy constructor.
SocketImpl::~SocketImpl | ( | ) |
References _localaddr, and _remoteaddr.
Socket * SocketImpl::accept | ( | ) | ||
throw | ( | IOException | ||
) |
References _fd, _localaddr, _sockdomain, _socktype, NLOG, nidas::util::SocketAddress::toAddressString(), and VLOG.
void SocketImpl::bind | ( | int | port | ) | |
throw | ( | IOException | |||
) |
References port.
void SocketImpl::bind | ( | const Inet4Address & | addr, |
int | port | ||
) | |||
throw | ( | IOException | |
) |
References port.
void SocketImpl::bind | ( | const SocketAddress & | sockaddr | ) | |
throw | ( | IOException | |||
) |
References WLOG.
void SocketImpl::close | ( | ) | ||
throw | ( | IOException | ||
) |
References _fd, fd, getLocalSocketAddress(), getRemoteSocketAddress(), and VLOG.
Referenced by nidas::util::ServerSocket::close().
void SocketImpl::connect | ( | const std::string & | dest, |
int | port | ||
) | |||
throw | ( | UnknownHostException, | |
IOException | |||
) |
References nidas::util::Inet4Address::getByName(), and port.
void SocketImpl::connect | ( | const Inet4Address & | addr, |
int | port | ||
) | |||
throw | ( | IOException | |
) |
References port.
void SocketImpl::connect | ( | const SocketAddress & | addr | ) | |
throw | ( | IOException | |||
) |
Inet4NetworkInterface SocketImpl::findInterface | ( | const Inet4Address & | iaddr | ) | const |
throw | ( | IOException | |||
) |
bool SocketImpl::getBroadcastEnable | ( | ) | const | |
throw | ( | IOException | ||
) |
References _fd, _localaddr, and nidas::util::SocketAddress::toAddressString().
|
inline |
Get the domain of this socket: AF_UNIX, AF_INET, etc, from sys/socket.h.
References _sockdomain.
Referenced by getInQueueSize(), getKeepAlive(), getKeepAliveIdleSecs(), getOutQueueSize(), and getTcpNoDelay().
|
inline |
References _fd.
Referenced by getInterface(), and getInterfaces().
int SocketImpl::getInQueueSize | ( | ) | const | |
throw | ( | IOException | ||
) |
Get number of bytes currently unread in the local input queue.
This does a call to ioctl(fd, SIOCINQ, &num); See man 7 tcp or udp.
References _fd, _localaddr, getDomain(), and nidas::util::SocketAddress::toAddressString().
Inet4NetworkInterface SocketImpl::getInterface | ( | ) | const | |
throw | ( | IOException | ||
) |
Inet4NetworkInterface SocketImpl::getInterface | ( | const std::string & | name | ) | const |
throw | ( | IOException | |||
) |
list< Inet4NetworkInterface > SocketImpl::getInterfaces | ( | ) | const | |
throw | ( | IOException | ||
) |
References getFd(), getInterface(), and VLOG.
Referenced by getInterface().
bool SocketImpl::getKeepAlive | ( | ) | const | |
throw | ( | IOException | ||
) |
Get the current value of the SO_KEEPALIVE socket option.
References _fd, _localaddr, getDomain(), len, and nidas::util::SocketAddress::toAddressString().
int SocketImpl::getKeepAliveIdleSecs | ( | ) | const | |
throw | ( | IOException | ||
) |
Get the current value of TCP_KEEPIDLE on this socket.
References _fd, _localaddr, getDomain(), len, and nidas::util::SocketAddress::toAddressString().
|
protected |
Do system call to determine local address of this socket.
References _fd, _localaddr, _sockdomain, nidas::util::SocketAddress::toString(), and VLOG.
|
inline |
Get local port number of this socket.
References _localaddr, and nidas::util::SocketAddress::getPort().
|
inline |
bool SocketImpl::getMulticastLoop | ( | ) | const | |
throw | ( | IOException | ||
) |
References _fd, _localaddr, and nidas::util::SocketAddress::toAddressString().
int SocketImpl::getOutQueueSize | ( | ) | const | |
throw | ( | IOException | ||
) |
Get number of bytes currently unsent in the local output queue.
This does a call to ioctl(fd, SIOCOUTQ, &num); See man 7 tcp or udp.
References _fd, _localaddr, getDomain(), and nidas::util::SocketAddress::toAddressString().
|
inline |
References _pktInfo.
int SocketImpl::getReceiveBufferSize | ( | ) | ||
throw | ( | IOException | ||
) |
References _fd, _localaddr, nidas::util::SocketAddress::toAddressString(), and VLOG.
|
protected |
Do system call to determine address of remote end.
References _fd, _remoteaddr, _sockdomain, nidas::util::SocketAddress::toString(), and VLOG.
|
inline |
Get remote port number of this socket.
References _remoteaddr, and nidas::util::SocketAddress::getPort().
|
inline |
int SocketImpl::getSendBufferSize | ( | ) | ||
throw | ( | IOException | ||
) |
References _fd, _localaddr, nidas::util::SocketAddress::toAddressString(), and VLOG.
bool SocketImpl::getTcpNoDelay | ( | ) | ||
throw | ( | IOException | ||
) |
Get the TCP_NODELAY (man 7 tcp) option on the socket.
References _fd, _localaddr, getDomain(), len, and nidas::util::SocketAddress::toAddressString().
int SocketImpl::getTimeout | ( | ) | const |
References _timeout, MSECS_PER_SEC, and NSECS_PER_MSEC.
Referenced by operator=().
int SocketImpl::getTimeToLive | ( | ) | const | |
throw | ( | IOException | ||
) |
References _fd, _localaddr, and nidas::util::SocketAddress::toAddressString().
bool SocketImpl::isNonBlocking | ( | ) | const | |
throw | ( | IOException | ||
) |
References _fd, _localaddr, and nidas::util::SocketAddress::toAddressString().
void SocketImpl::joinGroup | ( | Inet4Address | groupAddr | ) | |
throw | ( | IOException | |||
) |
Join a multicast group on all interfaces.
References ELOG, nidas::util::Exception::getErrno(), nidas::util::Inet4NetworkInterface::getFlags(), and nidas::util::Exception::toString().
void SocketImpl::joinGroup | ( | Inet4Address | groupAddr, |
const Inet4NetworkInterface & | iface | ||
) | |||
throw | ( | IOException | |
) |
Join a multicast group on a specific interface.
According to "man 7 ip", if the interface adddress "is equal to INADDR_ANY an appropriate interface is chosen by the system", which may not be what you want. This was eth0 on a system with lo,eth0 and eth1.
References VLOG.
void SocketImpl::leaveGroup | ( | Inet4Address | groupAddr | ) | |
throw | ( | IOException | |||
) |
Leave a multicast group on all interfaces.
void nidas::util::SocketImpl::leaveGroup | ( | Inet4Address | groupAddr, |
const Inet4NetworkInterface & | iaddr | ||
) | |||
throw | ( | IOException | |
) |
Leave a multicast group on a given interface.
void SocketImpl::listen | ( | ) | ||
throw | ( | IOException | ||
) |
References _backlog, _fd, _localaddr, _sockdomain, _socktype, and nidas::util::SocketAddress::toAddressString().
SocketImpl & SocketImpl::operator= | ( | const SocketImpl & | rhs | ) |
Assignment operator.
References _backlog, _fd, _localaddr, _pktInfo, _remoteaddr, _reuseaddr, _sockdomain, _socktype, nidas::util::SocketAddress::clone(), getTimeout(), and setTimeout().
void SocketImpl::receive | ( | DatagramPacketBase & | packet | ) | |
throw | ( | IOException | |||
) |
References ILOG.
void SocketImpl::receive | ( | DatagramPacketBase & | packet, |
Inet4PacketInfo & | info, | ||
int | flags = 0 |
||
) | |||
throw | ( | IOException | |
) |
References nidas::util::LogContext::active(), nidas::util::Inet4NetworkInterface::getAddress(), nidas::util::Inet4Address::getHostAddress(), nidas::util::Inet4NetworkInterface::getIndex(), nidas::util::Inet4NetworkInterface::getInterface(), nidas::util::Inet4NetworkInterface::getName(), ILOG, nidas::util::LogContext::log(), LOG_DEBUG, nidas::util::Exception::what(), and WLOG.
size_t SocketImpl::recv | ( | void * | buf, |
size_t | len, | ||
int | flags = 0 |
||
) | |||
throw | ( | IOException | |
) |
Receive data on a socket.
See "man 2 recv" for values of the flags parameter (none of which have been tested). An EOFException is returned if the remote host does an orderly shutdown of the socket.
size_t SocketImpl::recvfrom | ( | void * | buf, |
size_t | len, | ||
int | flags, | ||
SocketAddress & | from | ||
) | |||
throw | ( | IOException | |
) |
void SocketImpl::send | ( | const DatagramPacketBase & | packet, |
int | flags = 0 |
||
) | |||
throw | ( | IOException | |
) |
References VLOG.
size_t SocketImpl::send | ( | const void * | buf, |
size_t | len, | ||
int | flags = 0 |
||
) | |||
throw | ( | IOException | |
) |
send data on socket.
See send UNIX man page.
buf | pointer to buffer. |
len | number of bytes to send. bitwise OR of flags for send. Default: 0. |
References len.
size_t SocketImpl::send | ( | const struct iovec * | iov, |
int | iovcnt, | ||
int | flags = 0 |
||
) | |||
throw | ( | IOException | |
) |
void SocketImpl::sendall | ( | const void * | buf, |
size_t | len, | ||
int | flags = 0 |
||
) | |||
throw | ( | IOException | |
) |
send all data in buffer on socket, repeating send() as necessary, until all data is sent (or an exception occurs).
buf | pointer to buffer. |
len | number of bytes to send. bitwise OR of flags for send. Default: 0. |
References len.
size_t SocketImpl::sendto | ( | const void * | buf, |
size_t | len, | ||
int | flags, | ||
const SocketAddress & | to | ||
) | |||
throw | ( | IOException | |
) |
References len.
size_t SocketImpl::sendto | ( | const struct iovec * | iov, |
int | iovcnt, | ||
int | flags, | ||
const SocketAddress & | to | ||
) | |||
throw | ( | IOException | |
) |
|
inline |
References _backlog.
void SocketImpl::setBroadcastEnable | ( | bool | val | ) | |
throw | ( | IOException | |||
) |
Enable or disable SO_BROADCAST.
Note that broadcasting is generally not advised, best to use multicast instead.
void nidas::util::SocketImpl::setInterface | ( | Inet4Address | maddr, |
const Inet4NetworkInterface & | iaddr | ||
) | |||
throw | ( | IOException | |
) |
void SocketImpl::setInterface | ( | Inet4Address | iaddr | ) | |
throw | ( | IOException | |||
) |
void SocketImpl::setKeepAlive | ( | bool | val | ) | |
throw | ( | IOException | |||
) |
Set or unset the SO_KEEPALIVE socket option.
References len.
void SocketImpl::setKeepAliveIdleSecs | ( | int | val | ) | |
throw | ( | IOException | |||
) |
Set the number of seconds a connection needs to be idle before TCP begins sending out keep-alive probes (TCP_KEEPIDLE).
Only appropriate for stream (TCP) connections, not datagrams (UDP). Calls setKeepAlive(true) if necessary so that the SO_KEEPALIVE option is set.
val | Number of seconds. man 7 tcp: tcp_keepalive_time The number of seconds a connection needs to be idle before TCP begins send- ing out keep-alive probes. Keep-alives are only sent when the SO_KEEPALIVE socket option is enabled. The default value is 7200 seconds (2 hours). An idle connection is terminated after approximately an additional 11 minutes (9 probes an interval of 75 seconds apart) when keep-alive is enabled. |
Note that underlying connection tracking mechanisms and application timeouts may be much shorter.
References len.
void SocketImpl::setMulticastLoop | ( | bool | val | ) | |
throw | ( | IOException | |||
) |
Whether to set the IP_MULTICAST_LOOP socket option.
According to "man 7 ip", IP_MULTICAST_LOOP controls "whether sent multicast packets should be looped back to the local sockets." This behaviour seems to be the default in Linux in that setting this does not seem to be necessary for a process on a host to receive multicast packets that are sent out on one of its interfaces, providing the multicast reader has joined that interface, and a firewall is not blocking them.
void SocketImpl::setNonBlocking | ( | bool | val | ) | |
throw | ( | IOException | |||
) |
References ILOG.
void SocketImpl::setPktInfo | ( | bool | val | ) | |
throw | ( | IOException | |||
) |
Control whether a IP_PKTINFO ancillary message is received with each datagram.
Only supported on DatagramSockets. The IP_PKTINFO message is converted to an Inet4PacketInfo object which is available via the getInet4PacketInfo() method.
val,: | if true enable the IP_PKTINFO message, if false, disable. |
References len.
void SocketImpl::setReceiveBufferSize | ( | int | size | ) | |
throw | ( | IOException | |||
) |
|
inline |
References _reuseaddr.
void SocketImpl::setSendBufferSize | ( | int | size | ) | |
throw | ( | IOException | |||
) |
void SocketImpl::setTcpNoDelay | ( | bool | val | ) | |
throw | ( | IOException | |||
) |
void SocketImpl::setTimeout | ( | int | val | ) |
Set the timeout for receive(), recv(), and recvfrom() methods.
val | timeout in milliseconds. 0=no timeout (infinite) The receive methods will return IOTimeoutException if an operation times out. |
References _hasTimeout, _timeout, MSECS_PER_SEC, and NSECS_PER_MSEC.
Referenced by operator=().
void SocketImpl::setTimeToLive | ( | int | val | ) | |
throw | ( | IOException | |||
) |
|
protected |
Referenced by listen(), operator=(), and setBacklog().
|
protected |
Referenced by accept(), close(), getBroadcastEnable(), getFd(), getInQueueSize(), getInterface(), getKeepAlive(), getKeepAliveIdleSecs(), getLocalAddr(), getMulticastLoop(), getOutQueueSize(), getReceiveBufferSize(), getRemoteAddr(), getSendBufferSize(), getTcpNoDelay(), getTimeToLive(), isNonBlocking(), listen(), and operator=().
|
protected |
Referenced by setTimeout().
|
protected |
Referenced by accept(), getBroadcastEnable(), getInQueueSize(), getInterface(), getKeepAlive(), getKeepAliveIdleSecs(), getLocalAddr(), getLocalPort(), getLocalSocketAddress(), getMulticastLoop(), getOutQueueSize(), getReceiveBufferSize(), getSendBufferSize(), getTcpNoDelay(), getTimeToLive(), isNonBlocking(), listen(), operator=(), and ~SocketImpl().
|
protected |
Referenced by getPktInfo(), and operator=().
|
protected |
Referenced by getRemoteAddr(), getRemotePort(), getRemoteSocketAddress(), operator=(), and ~SocketImpl().
|
protected |
Referenced by operator=(), and setReuseAddress().
|
protected |
Referenced by accept(), getDomain(), getLocalAddr(), getRemoteAddr(), listen(), and operator=().
|
protected |
Referenced by accept(), listen(), and operator=().
|
protected |
Referenced by getTimeout(), and setTimeout().