nidas  v1.2-1520
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) throw (IOException)
 
 SocketImpl (int fd, const SocketAddress &remoteaddr) throw (IOException)
 
 SocketImpl (const SocketImpl &x)
 Copy constructor. More...
 
SocketImploperator= (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 SocketAddressgetLocalSocketAddress () const throw ()
 Get local socket address of this socket. More...
 
int getLocalPort () const throw ()
 Get local port number of this socket. More...
 
const SocketAddressgetRemoteSocketAddress () 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)
 
Socketaccept () 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< Inet4NetworkInterfacegetInterfaces () 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
 

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::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.

Member Function Documentation

Socket * SocketImpl::accept ( )
throw (IOException
)
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
)
void SocketImpl::connect ( const std::string &  dest,
int  port 
)
throw (UnknownHostException,
IOException
)
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
)
int nidas::util::SocketImpl::getDomain ( ) const
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().

int nidas::util::SocketImpl::getFd ( ) const
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.

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

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

void SocketImpl::getLocalAddr ( )
throw (IOException
)
protected

Do system call to determine local address of this socket.

References _fd, _localaddr, _sockdomain, nidas::util::SocketAddress::toString(), and VLOG.

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

Get local port number of this socket.

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

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

Get local socket address of this socket.

References _localaddr.

Referenced by close().

bool SocketImpl::getMulticastLoop ( ) const
throw (IOException
)
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().

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

References _pktInfo.

int SocketImpl::getReceiveBufferSize ( )
throw (IOException
)
void SocketImpl::getRemoteAddr ( )
throw (IOException
)
protected

Do system call to determine address of remote end.

References _fd, _remoteaddr, _sockdomain, nidas::util::SocketAddress::toString(), and VLOG.

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

Get remote port number of this socket.

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

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

Get remote socket address of this socket.

References _remoteaddr.

Referenced by close().

int SocketImpl::getSendBufferSize ( )
throw (IOException
)
bool SocketImpl::getTcpNoDelay ( )
throw (IOException
)

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

Returns
true if TCP_NODELAY is set 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
)
bool SocketImpl::isNonBlocking ( ) const
throw (IOException
)
void SocketImpl::joinGroup ( Inet4Address  groupAddr)
throw (IOException
)
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
)
SocketImpl & SocketImpl::operator= ( const SocketImpl rhs)
void SocketImpl::receive ( DatagramPacketBase packet)
throw (IOException
)

References ILOG.

void SocketImpl::receive ( DatagramPacketBase packet,
Inet4PacketInfo info,
int  flags = 0 
)
throw (IOException
)
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.

References ILOG, and len.

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

References ILOG, and len.

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.

Parameters
bufpointer to buffer.
lennumber of bytes to send. 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.

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

Parameters
bufpointer to buffer.
lennumber of bytes to send. 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.

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
)
void nidas::util::SocketImpl::setBacklog ( int  val)
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.

Parameters
valNumber 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.

Parameters
val,:if true enable the IP_PKTINFO message, if false, disable.

References len.

void SocketImpl::setReceiveBufferSize ( int  size)
throw (IOException
)
void nidas::util::SocketImpl::setReuseAddress ( bool  val)
inline

References _reuseaddr.

void SocketImpl::setSendBufferSize ( int  size)
throw (IOException
)
void SocketImpl::setTcpNoDelay ( bool  val)
throw (IOException
)

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

This option is only appropriate for TCP sockets.

Parameters
val,:If true, set TCP_NODELAY. If false unset it.

References ILOG, and len.

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

void SocketImpl::setTimeToLive ( int  val)
throw (IOException
)

Member Data Documentation

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

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

int nidas::util::SocketImpl::_fd
protected
bool nidas::util::SocketImpl::_hasTimeout
protected

Referenced by setTimeout().

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

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

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

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

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

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

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

Referenced by getTimeout(), and setTimeout().


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