libDwm-0.6.0
Dwm::Socket Class Reference

This class abstracts a UNIX socket descriptor. More...

#include <DwmSocket.hh>

Inheritance diagram for Dwm::Socket:
Collaboration diagram for Dwm::Socket:

Public Member Functions

 Socket ()
 Constructor.
 
 Socket (const Socket &sock)
 Copy constructor. More...
 
Socketoperator= (const Socket &sock)
 operator = (assignment operator).
 
 operator bool () const
 
virtual bool Open (int domain, int type, int protocol)
 Opens the socket. More...
 
int Type () const
 
virtual Socket Accept (struct sockaddr_in &addr)
 Calls the UNIX accept(2) function. Returns the resulting socket.
 
virtual Socket Accept (Ipv4Address &addr, uint16_t &port)
 Calls accept() on the socket and returns the resulting socket. More...
 
virtual bool Accept (Socket &s, struct sockaddr_in &addr)
 Calls the UNIX accept() function for the given socket s and attaches *this to the return if successful. More...
 
virtual bool Accept (Socket &s, Ipv4Address &addr, uint16_t &port)
 Calls the UNIX accept() function for the given socket s and attaches *this to the return if successful. More...
 
virtual Socket Accept (struct sockaddr_un &addr)
 Calls the UNIX accept(2) function. Returns the resulting socket.
 
virtual bool Accept (Socket &s, struct sockaddr_un &addr)
 
virtual bool Listen (int backlog)
 Calls the UNIX listen(2) function. More...
 
virtual bool Bind (const struct sockaddr_in &addr)
 Calls the UNIX bind(2) function. More...
 
virtual bool Bind (const Ipv4Address &addr, uint16_t port)
 Bind to the given IPv4 address addr and port. More...
 
virtual bool Bind (const struct sockaddr_un &addr)
 Calls the UNIX bind(2) function. More...
 
virtual bool Connect (const struct sockaddr_in &name)
 Calls the UNIX connect(2) function. More...
 
virtual bool Connect (const Ipv4Address &dstAddr, uint16_t port)
 Connect to the given IPv4 address dstAddr at the given port. More...
 
virtual bool Connect (const struct sockaddr_un &name)
 Calls the UNIX connect(2) function. More...
 
virtual bool Getpeername (struct sockaddr *name, socklen_t &namelen)
 Calls the UNIX getpeername(2) function. Returns true on success,.
 
virtual bool Getpeername (Ipv4Address &addr, uint16_t &port)
 Calls getpeername() on the socket. More...
 
virtual bool Getsockname (struct sockaddr *name, socklen_t &namelen)
 Calls the UNIX getsockname(2) function. Returns true on success,.
 
virtual bool Getsockname (Ipv4Address &addr, uint16_t &port)
 Calls getsockname() on the socket. More...
 
virtual bool Setsockopt (int level, int optname, const void *optval, socklen_t optlen)
 Calls the UNIX setsockopt(2) function. Returns true on success,.
 
virtual bool Getsockopt (int level, int optname, void *optval, socklen_t &optlen)
 Calls the UNIX setsockopt(2) function. Returns true on success,.
 
virtual ssize_t RecvFrom (void *buf, size_t len, int flags, struct sockaddr_in &from)
 Calls the UNIX recvfrom(2) function.
 
virtual ssize_t RecvFrom (void *buf, size_t len, int flags, Ipv4Address &srcAddr, uint16_t &srcPort)
 Calls recvfrom() on the socket. More...
 
virtual ssize_t RecvFrom (std::string &s, int flags, Ipv4Address &srcAddr, uint16_t &srcPort)
 Calls recvfrom() on the socket. More...
 
virtual ssize_t SendTo (const void *buf, size_t len, int flags, const struct sockaddr_in &to)
 Calls the UNIX sendto(2) function.
 
virtual ssize_t SendTo (const void *buf, size_t len, int flags, const Ipv4Address &dstAddr, uint16_t dstPort)
 Calls sendto() on the socket, sending the given buffer buf of length len to the given destination address and port. More...
 
virtual ssize_t SendTo (const std::string &s, int flags, const Ipv4Address &dstAddr, uint16_t dstPort)
 Calls sendto() on the socket, sending the given string s to the given destination and port. More...
 
virtual bool JoinMulticastGroup (const Ipv4Address &groupAddr, const Ipv4Address &intfAddr)
 Join the given multicast group groupAddr on the given interface address intfAddr. More...
 
bool HeaderIncluded () const
 
- Public Member Functions inherited from Dwm::Descriptor
 Descriptor ()
 Constructor.
 
 Descriptor (const Descriptor &d)
 Copy constructor. Will dup() the descriptor of d.
 
 Descriptor (int d)
 
Descriptoroperator= (const Descriptor &d)
 operator = (assignment operator). More...
 
virtual ~Descriptor ()
 Destructor. Note this will close the underlying UNIX descriptor.
 
virtual bool Close ()
 Closes the Descriptor. Returns true on success, false on failure.
 
virtual bool Attach (int fd)
 Associates the Descriptor with an existing UNIX descriptor fd. More...
 
virtual bool Detach ()
 Disassociates the Descriptor from the underlying UNIX descriptor. More...
 
virtual bool SetNonBlocking ()
 Sets the Descriptor to non-blocking. More...
 
virtual bool SetBlocking ()
 Sets the soccket to blocking. More...
 
virtual operator int () const
 This completely blows encapsulation, but is needed so that Descriptor may be used in place of a UNIX descriptor in retrofitted code. More...
 
virtual bool FdSet (fd_set &fdSet)
 If Descriptor is valid, sets it in fdSet and returns true. More...
 
virtual bool FdClr (fd_set &fdSet)
 If Descriptor is valid, clears it in fdSet and returns true. More...
 
virtual bool FdIsSet (fd_set &fdSet)
 If Descriptor is valid and is set in fdSet, returns true. More...
 
virtual bool ReadWouldBlock ()
 

Protected Attributes

int _type
 
bool _headerIncluded
 
- Protected Attributes inherited from Dwm::Descriptor
int _fd
 

Detailed Description

This class abstracts a UNIX socket descriptor.

Constructor & Destructor Documentation

◆ Socket()

Dwm::Socket::Socket ( const Socket sock)

Copy constructor.

Note we use dup() to duplicate the underlying UNIX socket descriptor, which allows us to close the underlying UNIX socket descriptor in our destructor.

Member Function Documentation

◆ Accept() [1/3]

virtual Socket Dwm::Socket::Accept ( Ipv4Address addr,
uint16_t &  port 
)
virtual

Calls accept() on the socket and returns the resulting socket.

If successful, addr will be set to the peer's address and port will be set to the peer's port.

◆ Accept() [2/3]

virtual bool Dwm::Socket::Accept ( Socket s,
struct sockaddr_in &  addr 
)
virtual

Calls the UNIX accept() function for the given socket s and attaches *this to the return if successful.

Returns true on success, false on failure. addr is a result parameter and will contain the address of the remote connection on success.

◆ Accept() [3/3]

virtual bool Dwm::Socket::Accept ( Socket s,
Ipv4Address addr,
uint16_t &  port 
)
virtual

Calls the UNIX accept() function for the given socket s and attaches *this to the return if successful.

Returns true on success, false on failure. addr and port are result parameters and will contain the address and port of the remote connection on success.

◆ Bind() [1/3]

virtual bool Dwm::Socket::Bind ( const struct sockaddr_in &  addr)
virtual

Calls the UNIX bind(2) function.

Returns true on success, false on failure.

◆ Bind() [2/3]

virtual bool Dwm::Socket::Bind ( const Ipv4Address addr,
uint16_t  port 
)
virtual

Bind to the given IPv4 address addr and port.

Returns true on success, false on failure.

◆ Bind() [3/3]

virtual bool Dwm::Socket::Bind ( const struct sockaddr_un &  addr)
virtual

Calls the UNIX bind(2) function.

Returns true on success, false on failure.

◆ Connect() [1/3]

virtual bool Dwm::Socket::Connect ( const struct sockaddr_in &  name)
virtual

Calls the UNIX connect(2) function.

Returns true on success, false on failure.

◆ Connect() [2/3]

virtual bool Dwm::Socket::Connect ( const Ipv4Address dstAddr,
uint16_t  port 
)
virtual

Connect to the given IPv4 address dstAddr at the given port.

Returns true on success, false on failure.

◆ Connect() [3/3]

virtual bool Dwm::Socket::Connect ( const struct sockaddr_un &  name)
virtual

Calls the UNIX connect(2) function.

Returns true on success, false on failure.

◆ Getpeername()

virtual bool Dwm::Socket::Getpeername ( Ipv4Address addr,
uint16_t &  port 
)
virtual

Calls getpeername() on the socket.

Returns true on success, false on failure. addr and port are result parameters, and will contain the address and port of the peer on success.

◆ Getsockname()

virtual bool Dwm::Socket::Getsockname ( Ipv4Address addr,
uint16_t &  port 
)
virtual

Calls getsockname() on the socket.

Returns true on success, false on failure. addr and port are result parameters and will contain the address and port of the socket on success. Note this only works on IPv4 sockets, not UNIX domain sockets.

◆ JoinMulticastGroup()

virtual bool Dwm::Socket::JoinMulticastGroup ( const Ipv4Address groupAddr,
const Ipv4Address intfAddr 
)
virtual

Join the given multicast group groupAddr on the given interface address intfAddr.

Returns true on success, false on failure.

◆ Listen()

virtual bool Dwm::Socket::Listen ( int  backlog)
virtual

Calls the UNIX listen(2) function.

Returns true on success, false on failure.

◆ Open()

virtual bool Dwm::Socket::Open ( int  domain,
int  type,
int  protocol 
)
virtual

Opens the socket.

Note this just calls the UNIX socket(2) function, and takes the same arguments. Returns true on success, false on failure.

◆ RecvFrom() [1/2]

virtual ssize_t Dwm::Socket::RecvFrom ( void *  buf,
size_t  len,
int  flags,
Ipv4Address srcAddr,
uint16_t &  srcPort 
)
virtual

Calls recvfrom() on the socket.

A buffer buf of length len must be provided by the caller to receive the data. flags is passed to the recvfrom() function (see the recvfrom manpage). On success, returns the number of bytes received and sets srcAddr and srcPort to the source address and port of the received packet.

◆ RecvFrom() [2/2]

virtual ssize_t Dwm::Socket::RecvFrom ( std::string &  s,
int  flags,
Ipv4Address srcAddr,
uint16_t &  srcPort 
)
virtual

Calls recvfrom() on the socket.

On success, stores the received data in s, the source address in srcAddr and the source port in srcPort and returns the number of bytes received. On failure, returns -1. Note that may contain embedded nulls and should NOT be treated like a C string; use s.size() to determine how much data was stored.

◆ SendTo() [1/2]

virtual ssize_t Dwm::Socket::SendTo ( const void *  buf,
size_t  len,
int  flags,
const Ipv4Address dstAddr,
uint16_t  dstPort 
)
virtual

Calls sendto() on the socket, sending the given buffer buf of length len to the given destination address and port.

Returns the number of bytes sent on success, -1 on failure.

◆ SendTo() [2/2]

virtual ssize_t Dwm::Socket::SendTo ( const std::string &  s,
int  flags,
const Ipv4Address dstAddr,
uint16_t  dstPort 
)
virtual

Calls sendto() on the socket, sending the given string s to the given destination and port.

flags is passed on to sendto() (see the sendto() manpage). On success, returns the number of bytes sent which should be the same as s.size(). On failure, returns -1. Note that s can contain binary data, including nulls, but can not be longer than 65535 bytes (the maximum size of an IP packet) minus the length of the transport header and any IP header options.


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