nidas v1.2.3
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
nidas::util::SocketImpl Class Reference

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)
 
 SocketImpl (int fd, const SocketAddress &remoteaddr)
 
 SocketImpl (const SocketImpl &x)
 Copy constructor.
 
SocketImploperator= (const SocketImpl &rhs)
 Assignment operator.
 
 ~SocketImpl ()
 
int getFd () const
 
int getDomain () const
 Get the domain of this socket: AF_UNIX, AF_INET, etc, from sys/socket.h.
 
void setBacklog (int val)
 
const SocketAddressgetLocalSocketAddress () const
 Get local socket address of this socket.
 
int getLocalPort () const
 Get local port number of this socket.
 
const SocketAddressgetRemoteSocketAddress () const
 Get remote socket address of this socket.
 
int getRemotePort () const
 Get remote port number of this socket.
 
void setReuseAddress (bool val)
 
void setNonBlocking (bool val)
 
bool isNonBlocking () const
 
void setTcpNoDelay (bool val)
 Set the TCP_NODELAY (man 7 tcp) option on the socket.
 
bool getTcpNoDelay ()
 Get the TCP_NODELAY (man 7 tcp) option on the socket.
 
void setTimeout (int val)
 Set the timeout for receive(), recv(), and recvfrom() methods.
 
int getTimeout () const
 
void setKeepAlive (bool val)
 Set or unset the SO_KEEPALIVE socket option.
 
bool getKeepAlive () const
 Get the current value of the SO_KEEPALIVE socket option.
 
void setKeepAliveIdleSecs (int val)
 Set the number of seconds a connection needs to be idle before TCP begins sending out keep-alive probes (TCP_KEEPIDLE).
 
int getKeepAliveIdleSecs () const
 Get the current value of TCP_KEEPIDLE on this socket.
 
int getInQueueSize () const
 Get number of bytes currently unread in the local input queue.
 
int getOutQueueSize () const
 Get number of bytes currently unsent in the local output queue.
 
void close ()
 
void bind (int port)
 
void bind (const Inet4Address &addr, int port)
 
void bind (const SocketAddress &sockaddr)
 
void listen ()
 
Socketaccept ()
 
void connect (const std::string &dest, int port)
 
void connect (const Inet4Address &addr, int port)
 
void connect (const SocketAddress &addr)
 
void receive (DatagramPacketBase &packet)
 
void receive (DatagramPacketBase &packet, Inet4PacketInfo &info, int flags=0)
 
size_t recv (void *buf, size_t len, int flags=0)
 Receive data on a socket.
 
size_t recvfrom (void *buf, size_t len, int flags, SocketAddress &from)
 
void send (const DatagramPacketBase &packet, int flags=0)
 
size_t send (const void *buf, size_t len, int flags=0)
 send data on socket.
 
size_t send (const struct iovec *iov, int iovcnt, int flags=0)
 
size_t sendto (const void *buf, size_t len, int flags, const SocketAddress &to)
 
size_t sendto (const struct iovec *iov, int iovcnt, int flags, const SocketAddress &to)
 
void sendall (const void *buf, size_t len, int flags=0)
 send all data in buffer on socket, repeating send() as necessary, until all data is sent (or an exception occurs).
 
void joinGroup (Inet4Address groupAddr)
 Join a multicast group on all interfaces.
 
void joinGroup (Inet4Address groupAddr, const Inet4NetworkInterface &)
 Join a multicast group on a specific interface.
 
void leaveGroup (Inet4Address groupAddr)
 Leave a multicast group on all interfaces.
 
void leaveGroup (Inet4Address groupAddr, const Inet4NetworkInterface &iaddr)
 Leave a multicast group on a given interface.
 
void setReceiveBufferSize (int size)
 
int getReceiveBufferSize ()
 
void setSendBufferSize (int size)
 
int getSendBufferSize ()
 
void setTimeToLive (int val)
 
int getTimeToLive () const
 
void setPktInfo (bool val)
 Control whether a IP_PKTINFO ancillary message is received with each datagram.
 
bool getPktInfo () const
 
void setMulticastLoop (bool val)
 Whether to set the IP_MULTICAST_LOOP socket option.
 
bool getMulticastLoop () const
 
void setInterface (Inet4Address maddr, const Inet4NetworkInterface &iaddr)
 
void setInterface (Inet4Address iaddr)
 
Inet4NetworkInterface getInterface () const
 
Inet4NetworkInterface getInterface (const std::string &name) const
 
Inet4NetworkInterface findInterface (const Inet4Address &) const
 
std::list< Inet4NetworkInterfacegetInterfaces () const
 
void setBroadcastEnable (bool val)
 Enable or disable SO_BROADCAST.
 
bool getBroadcastEnable () const
 

Protected Member Functions

void getLocalAddr ()
 Do system call to determine local address of this socket.
 
void getRemoteAddr ()
 Do system call to determine address of remote end.
 

Protected Attributes

int _sockdomain
 
int _socktype
 
SocketAddress_localaddr
 
SocketAddress_remoteaddr
 
int _fd
 
int _backlog
 
bool _reuseaddr
 
bool _hasTimeout
 
struct timespec _timeout
 
bool _pktInfo
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ SocketImpl() [1/3]

SocketImpl::SocketImpl ( int domain,
int type )

◆ SocketImpl() [2/3]

SocketImpl::SocketImpl ( int fd,
const SocketAddress & remoteaddr )
Exceptions
IOException

References getLocalAddr().

◆ SocketImpl() [3/3]

SocketImpl::SocketImpl ( const SocketImpl & x)

Copy constructor.

◆ ~SocketImpl()

SocketImpl::~SocketImpl ( )

References _localaddr, and _remoteaddr.

Member Function Documentation

◆ accept()

Socket * SocketImpl::accept ( )

◆ bind() [1/3]

void SocketImpl::bind ( const Inet4Address & addr,
int port )
Exceptions
IOException

References bind(), and port.

◆ bind() [2/3]

void SocketImpl::bind ( const SocketAddress & sockaddr)

◆ bind() [3/3]

void SocketImpl::bind ( int port)

◆ close()

void SocketImpl::close ( )

◆ connect() [1/3]

void SocketImpl::connect ( const Inet4Address & addr,
int port )
Exceptions
IOException

References connect(), and port.

◆ connect() [2/3]

void SocketImpl::connect ( const SocketAddress & addr)

◆ connect() [3/3]

void SocketImpl::connect ( const std::string & dest,
int port )

◆ findInterface()

Inet4NetworkInterface SocketImpl::findInterface ( const Inet4Address & iaddr) const

◆ getBroadcastEnable()

bool SocketImpl::getBroadcastEnable ( ) const

◆ getDomain()

int nidas::util::SocketImpl::getDomain ( ) const
inline

◆ getFd()

int nidas::util::SocketImpl::getFd ( ) const
inline

◆ getInQueueSize()

int SocketImpl::getInQueueSize ( ) const

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.

Exceptions
IOException

References _fd, _localaddr, getDomain(), and nidas::util::SocketAddress::toAddressString().

Referenced by nidas::util::Socket::getInQueueSize().

◆ getInterface() [1/2]

Inet4NetworkInterface SocketImpl::getInterface ( ) const

◆ getInterface() [2/2]

Inet4NetworkInterface SocketImpl::getInterface ( const std::string & name) const
Exceptions
IOException

References getFd(), and VLOG.

◆ getInterfaces()

list< Inet4NetworkInterface > SocketImpl::getInterfaces ( ) const

◆ getKeepAlive()

bool SocketImpl::getKeepAlive ( ) const

Get the current value of the SO_KEEPALIVE socket option.

Exceptions
IOException

References _fd, _localaddr, getDomain(), len, and nidas::util::SocketAddress::toAddressString().

Referenced by nidas::util::Socket::getKeepAlive(), and setKeepAliveIdleSecs().

◆ getKeepAliveIdleSecs()

int SocketImpl::getKeepAliveIdleSecs ( ) const

Get the current value of TCP_KEEPIDLE on this socket.

Returns
Number of seconds a connection needs to be idle before TCP begins sending out keep-alive probes (TCP_KEEPIDLE).
Exceptions
IOException

References _fd, _localaddr, getDomain(), len, and nidas::util::SocketAddress::toAddressString().

Referenced by nidas::util::Socket::getKeepAliveIdleSecs().

◆ getLocalAddr()

void SocketImpl::getLocalAddr ( )
protected

Do system call to determine local address of this socket.

Exceptions
IOException

References _fd, _localaddr, _sockdomain, and VLOG.

Referenced by bind(), connect(), SocketImpl(), and SocketImpl().

◆ getLocalPort()

int nidas::util::SocketImpl::getLocalPort ( ) const
inline

◆ getLocalSocketAddress()

const SocketAddress & nidas::util::SocketImpl::getLocalSocketAddress ( ) const
inline

◆ getMulticastLoop()

bool SocketImpl::getMulticastLoop ( ) const

◆ getOutQueueSize()

int SocketImpl::getOutQueueSize ( ) const

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.

Exceptions
IOException

References _fd, _localaddr, getDomain(), and nidas::util::SocketAddress::toAddressString().

Referenced by nidas::util::Socket::getOutQueueSize().

◆ getPktInfo()

bool nidas::util::SocketImpl::getPktInfo ( ) const
inline

References _pktInfo.

Referenced by receive().

◆ getReceiveBufferSize()

int SocketImpl::getReceiveBufferSize ( )

◆ getRemoteAddr()

void SocketImpl::getRemoteAddr ( )
protected

Do system call to determine address of remote end.

Exceptions
IOException

References _fd, _remoteaddr, _sockdomain, and VLOG.

Referenced by connect().

◆ getRemotePort()

int nidas::util::SocketImpl::getRemotePort ( ) const
inline

Get remote port number of this socket.

Exceptions

)

References _remoteaddr, and nidas::util::SocketAddress::getPort().

Referenced by nidas::util::Socket::getRemotePort().

◆ getRemoteSocketAddress()

const SocketAddress & nidas::util::SocketImpl::getRemoteSocketAddress ( ) const
inline

Get remote socket address of this socket.

Exceptions

)

References _remoteaddr.

Referenced by close(), and nidas::util::Socket::getRemoteSocketAddress().

◆ getSendBufferSize()

int SocketImpl::getSendBufferSize ( )

◆ getTcpNoDelay()

bool SocketImpl::getTcpNoDelay ( )

Get the TCP_NODELAY (man 7 tcp) option on the socket.

Returns
true if TCP_NODELAY is set on the socket.
Exceptions
IOException

References _fd, _localaddr, getDomain(), len, and nidas::util::SocketAddress::toAddressString().

Referenced by nidas::util::Socket::getTcpNoDelay().

◆ getTimeout()

int SocketImpl::getTimeout ( ) const

◆ getTimeToLive()

int SocketImpl::getTimeToLive ( ) const

◆ isNonBlocking()

bool SocketImpl::isNonBlocking ( ) const

◆ joinGroup() [1/2]

void SocketImpl::joinGroup ( Inet4Address groupAddr)

Join a multicast group on all interfaces.

Exceptions
IOException

References ELOG, getInterfaces(), and joinGroup().

Referenced by joinGroup(), nidas::util::MulticastSocket::joinGroup(), and nidas::util::MulticastSocket::joinGroup().

◆ joinGroup() [2/2]

void SocketImpl::joinGroup ( Inet4Address groupAddr,
const Inet4NetworkInterface & iface )

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.

Exceptions
IOException

References _fd, and VLOG.

◆ leaveGroup() [1/2]

void SocketImpl::leaveGroup ( Inet4Address groupAddr)

Leave a multicast group on all interfaces.

Exceptions
IOException

References getInterfaces(), and leaveGroup().

Referenced by leaveGroup(), nidas::util::MulticastSocket::leaveGroup(), and nidas::util::MulticastSocket::leaveGroup().

◆ leaveGroup() [2/2]

void SocketImpl::leaveGroup ( Inet4Address groupAddr,
const Inet4NetworkInterface & iaddr )

Leave a multicast group on a given interface.

Exceptions
IOException

References _fd.

◆ listen()

void SocketImpl::listen ( )

◆ operator=()

SocketImpl & SocketImpl::operator= ( const SocketImpl & rhs)

◆ receive() [1/2]

void SocketImpl::receive ( DatagramPacketBase & packet)

◆ receive() [2/2]

void SocketImpl::receive ( DatagramPacketBase & packet,
Inet4PacketInfo & info,
int flags = 0 )

◆ recv()

size_t SocketImpl::recv ( void * buf,
size_t len,
int flags = 0 )

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.

Exceptions
IOException

References _fd, _hasTimeout, _localaddr, _timeout, ILOG, len, recv(), and nidas::util::SocketAddress::toAddressString().

Referenced by recv(), nidas::util::Socket::recv(), and nidas::util::DatagramSocket::recv().

◆ recvfrom()

size_t SocketImpl::recvfrom ( void * buf,
size_t len,
int flags,
SocketAddress & from )

◆ send() [1/3]

void SocketImpl::send ( const DatagramPacketBase & packet,
int flags = 0 )

◆ send() [2/3]

size_t SocketImpl::send ( const struct iovec * iov,
int iovcnt,
int flags = 0 )

◆ send() [3/3]

size_t SocketImpl::send ( const void * buf,
size_t len,
int flags = 0 )

send data on socket.

See send UNIX man page.

Parameters
bufpointer to buffer.
lennumber of bytes to send. @flags bitwise OR of flags for send. Default: 0.
Returns
Number of bytes written to socket. If using non-blocking IO, either via setNonBlocking(true), or by setting the MSG_DONTWAIT in flags, and the system function returns EAGAIN or EWOULDBLOCK, then the return value will be 0, and no IOException is thrown.
Exceptions
IOException

References _fd, _remoteaddr, len, send(), and nidas::util::SocketAddress::toAddressString().

◆ sendall()

void SocketImpl::sendall ( const void * buf,
size_t len,
int flags = 0 )

send all data in buffer on socket, repeating send() as necessary, until all data is sent (or an exception occurs).

Parameters
bufpointer to buffer.
lennumber of bytes to send. @flags bitwise OR of flags for send. Default: 0.
Returns
Number of bytes written to socket. It is not recommended to use this method if using non-blocking IO.
Exceptions
IOException

References _fd, _remoteaddr, len, send(), and nidas::util::SocketAddress::toAddressString().

Referenced by nidas::util::Socket::sendall().

◆ sendto() [1/2]

size_t SocketImpl::sendto ( const struct iovec * iov,
int iovcnt,
int flags,
const SocketAddress & to )

◆ sendto() [2/2]

size_t SocketImpl::sendto ( const void * buf,
size_t len,
int flags,
const SocketAddress & to )

◆ setBacklog()

void nidas::util::SocketImpl::setBacklog ( int val)
inline

◆ setBroadcastEnable()

void SocketImpl::setBroadcastEnable ( bool val)

Enable or disable SO_BROADCAST.

Note that broadcasting is generally not advised, best to use multicast instead.

Exceptions
IOException

References _fd, _localaddr, and nidas::util::SocketAddress::toAddressString().

Referenced by nidas::util::DatagramSocket::setBroadcastEnable().

◆ setInterface() [1/2]

void SocketImpl::setInterface ( Inet4Address iaddr)

◆ setInterface() [2/2]

void SocketImpl::setInterface ( Inet4Address maddr,
const Inet4NetworkInterface & iaddr )

◆ setKeepAlive()

void SocketImpl::setKeepAlive ( bool val)

Set or unset the SO_KEEPALIVE socket option.

Exceptions
IOException

References _fd, _localaddr, getDomain(), len, and nidas::util::SocketAddress::toAddressString().

Referenced by nidas::util::Socket::setKeepAlive(), and setKeepAliveIdleSecs().

◆ setKeepAliveIdleSecs()

void SocketImpl::setKeepAliveIdleSecs ( int val)

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.

Parameters
valNumber of seconds.
Exceptions
IOException

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 _fd, _localaddr, getDomain(), getKeepAlive(), len, setKeepAlive(), and nidas::util::SocketAddress::toAddressString().

Referenced by nidas::util::Socket::setKeepAliveIdleSecs().

◆ setMulticastLoop()

void SocketImpl::setMulticastLoop ( bool val)

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.

Exceptions
IOException

References _fd, _localaddr, and nidas::util::SocketAddress::toAddressString().

◆ setNonBlocking()

void SocketImpl::setNonBlocking ( bool val)

◆ setPktInfo()

void SocketImpl::setPktInfo ( bool val)

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.

Parameters
valif true enable the IP_PKTINFO message, if false, disable.
Exceptions
IOException

References _fd, _localaddr, _pktInfo, len, and nidas::util::SocketAddress::toAddressString().

Referenced by receive(), and nidas::util::DatagramSocket::setPktInfo().

◆ setReceiveBufferSize()

void SocketImpl::setReceiveBufferSize ( int size)

◆ setReuseAddress()

void nidas::util::SocketImpl::setReuseAddress ( bool val)
inline

References _reuseaddr.

◆ setSendBufferSize()

void SocketImpl::setSendBufferSize ( int size)

◆ setTcpNoDelay()

void SocketImpl::setTcpNoDelay ( bool val)

Set the TCP_NODELAY (man 7 tcp) option on the socket.

This option is only appropriate for TCP sockets.

Parameters
valIf true, set TCP_NODELAY. If false unset it.
Exceptions
IOException

References _fd, _localaddr, getDomain(), ILOG, len, and nidas::util::SocketAddress::toAddressString().

Referenced by nidas::util::Socket::setTcpNoDelay().

◆ setTimeout()

void SocketImpl::setTimeout ( int val)

Set the timeout for receive(), recv(), and recvfrom() methods.

Parameters
valtimeout 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=(), nidas::util::Socket::setTimeout(), and nidas::util::DatagramSocket::setTimeout().

◆ setTimeToLive()

void SocketImpl::setTimeToLive ( int val)

Member Data Documentation

◆ _backlog

int nidas::util::SocketImpl::_backlog
protected

Referenced by listen(), operator=(), and setBacklog().

◆ _fd

int nidas::util::SocketImpl::_fd
protected

◆ _hasTimeout

bool nidas::util::SocketImpl::_hasTimeout
protected

◆ _localaddr

SocketAddress* nidas::util::SocketImpl::_localaddr
protected

◆ _pktInfo

bool nidas::util::SocketImpl::_pktInfo
protected

Referenced by getPktInfo(), operator=(), and setPktInfo().

◆ _remoteaddr

SocketAddress* nidas::util::SocketImpl::_remoteaddr
protected

◆ _reuseaddr

bool nidas::util::SocketImpl::_reuseaddr
protected

Referenced by bind(), operator=(), and setReuseAddress().

◆ _sockdomain

int nidas::util::SocketImpl::_sockdomain
protected

◆ _socktype

int nidas::util::SocketImpl::_socktype
protected

◆ _timeout

struct timespec nidas::util::SocketImpl::_timeout
protected

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