sockets::Socket Class Reference

Base class for all sockets. More...

#include <socket.h>

Inheritance diagram for sockets::Socket:

sockets::IPSocket sockets::TCPClient sockets::TCPSocket List of all members.

Public Types

enum  {
  ssCreated = 0x00000001, ssLocalInfo = 0x00000002, ssRemoteInfo = 0x00000004, ssBound = 0x00000008,
  ssConnecting = 0x00000010, ssConnected = 0x00000020, ssListening = 0x00000040, ssMonitored = 0x00000080,
  ssReadable = 0x00000100, ssWritable = 0x00000200, ssCloseFlags, ssCopyFlags = (ssLocalInfo | ssRemoteInfo)
}
 Possible flags for socket status. More...

Public Member Functions

 Socket (int family=PF_UNSPEC, int type=0, int protocol=0)
 Constructor.
 Socket (Socket const &s)
 Copy constructor.
virtual Socketoperator= (Socket const &right)
 Assignment operator.
virtual ~Socket ()
 Destructor.
virtual bool assign (int fd, uint32_t status)
 Assigns a socket file descriptor.
virtual bool read_event ()
 Read event -- For call by the Monitor-derived classes.
virtual bool write_event ()
 Write event -- For call by the Monitor and derived classes.
int family () const
 Returns the socket protocol family.
int type () const
 Returns the socket type.
int protocol () const
 Returns the socket protocol.
uint32_t status () const
 Returns the socket status.
std::string const & monitor () const
 Returns the monitor name.
bool monitor (std::string const &name)
 Sets the monitor used by this socket.

Static Public Member Functions

static char const * hostname ()
 Returns the hostname of the current processor.

Protected Member Functions

virtual bool create ()
 Creates the socket.
virtual bool close ()
 Closes the socket.
virtual bool watch ()
 Monitors a socket.
virtual void unwatch ()
 Unmonitors a socket.
int fd () const
 Returns the socket file descriptor.
int family (int family)
 Sets the socket protocol family.
int type (int type)
 Sets the socket type.
int protocol (int protocol)
 Sets the socket protocol.
uint32_t status_add (uint32_t flags)
 Adds a flag to the socket status.
uint32_t status_remove (uint32_t flags)
 Removes a flag from the socket status.

Detailed Description

This is the base class for all sockets class. It must not be instantiated directly; rather, use a derived class or create your own.

It handles socket creation and destruction, and provides means to track socket status.


Member Enumeration Documentation

anonymous enum
 

Enumerator:
ssCreated  The socket has been created.
ssLocalInfo  Local info has been set.
ssRemoteInfo  Remote info has been set.
ssBound  Socket is bound.
ssConnecting  Connection in progress.
ssConnected  Socket is connected.
ssListening  Socket is listening.
ssMonitored  Socket is monitored.
ssReadable  Socket is readable.
ssWritable  Socket is writable.
ssCloseFlags  Flags removed by the close() function.
ssCopyFlags  Flags that can be copied.


Constructor & Destructor Documentation

sockets::Socket::Socket int  family = PF_UNSPEC,
int  type = 0,
int  protocol = 0
[explicit]
 

Constructor.

Parameters:
[in] family The socket protocol family (one of the PF_... constants defined in the system header files). May be set to PF_UNSPEC if you want to set it later.
[in] type The socket type (one of the SOCK_... constants defined in the system header files). May be set to 0 if you want to set it later.
[in] protocol The socket protocol that needs to be used. This is seldom used, as there is generally only one protocol for a given family/type combination; use the value 0 to use the default protocol.

sockets::Socket::Socket Socket const &  s  )  [inline]
 

This copy constructor copies the socket family, type, protocol, monitor and part of the status only.

Parameters:
[in] s The socket to copy.

virtual sockets::Socket::~Socket  )  [virtual]
 

This destructor calls the close() function.


Member Function Documentation

virtual bool sockets::Socket::assign int  fd,
uint32_t  status
[virtual]
 

Assigns a socket file descriptor to the socket object, that is then used by the object for all operations on the socket.

The ssCreated status flag must not be set prior to calling this function; it is set once the function returns successfully.

Parameters:
[in] fd The socket file descriptor to assign.
[in] status The status flags that are added to the socket. If the ssMonitored flag is among them, the watch() function is automatically called.
Returns:
true if successful, false otherwise. errno is set accordingly in the latter case.

Reimplemented in sockets::IPSocket, and sockets::TCPClient.

virtual bool sockets::Socket::close  )  [protected, virtual]
 

Closes the socket, if it has been created. The ssCloseFlags flags are removed from the socket status on success.

If the socket is monitored, the unwatch() function is called as well.

Returns:
true if successful or the socket wasn't created, false otherwise. errno is set accordingly in the latter case (see the close(2) manpage for possible errors).

Reimplemented in sockets::TCPClient.

virtual bool sockets::Socket::create  )  [protected, virtual]
 

Creates the socket. The socket protocol family, type and protocol must have been set prior to calling this function. Upon successful creation, the ssCreated flag is added to the socket status.

This function may be overriden to change the socket options, etc. as needed.

Returns:
true if the creation succeeded or the socket was already created, false otherwise. errno is set accordingly in the latter case (see the socket(2) manpage for possible errors).

Reimplemented in sockets::IPSocket.

int sockets::Socket::family int  family  )  [protected]
 

Sets the socket protocol family. No change is done when the ssCreated status flag is set on the socket.

Parameters:
[in] family The socket protocol family.
Returns:
The new socket protocol family.

int sockets::Socket::fd  )  const [inline, protected]
 

Returns the socket file descriptor.

Returns:
The socket file descriptor, or -1 if the socket creation has not been done yet or failed.

bool sockets::Socket::monitor std::string const &  name  ) 
 

Sets the monitor used by this socket. This can't be changed once the ssMonitored status flag is set on the socket.

Parameters:
[in] name The name of the monitor to use for this socket.
Returns:
true if successful, false if no monitor with this name is registered.

virtual Socket& sockets::Socket::operator= Socket const &  right  )  [virtual]
 

This assignment operator copies the socket family, type, protocol, monitor and part of the status only.

Parameters:
[in] right The socket to be assigned.

int sockets::Socket::protocol int  protocol  )  [protected]
 

Sets the socket protocol. No change is done when the ssCreated status flag is set on the socket.

Parameters:
[in] protocol The socket protocol.
Returns:
The new socket protocol.

virtual bool sockets::Socket::read_event  )  [inline, virtual]
 

This function is called by the Monitor-derived classes when a read event happens.

Returns:
false if the socket has been closed or is no longer valid, true otherwise.

Reimplemented in sockets::TCPClient.

uint32_t sockets::Socket::status  )  const [inline]
 

Returns the socket status, which is an or'ed set of the ss* flags defined in the Socket class.

Returns:
The socket status.

uint32_t sockets::Socket::status_add uint32_t  flags  )  [inline, protected]
 

Adds a flag to the socket status. Possible flags include all ss* flags defined in the Socket class.

Parameters:
[in] flags The flags to add.
Returns:
The new socket status.

uint32_t sockets::Socket::status_remove uint32_t  flags  )  [inline, protected]
 

Removes a flag from the socket status. Possible flags include all ss* flags defined in the Socket class.

Parameters:
[in] flags The flags to remove.
Returns:
The new socket status.

int sockets::Socket::type int  type  )  [protected]
 

Sets the socket type. No change is done when the ssCreated status flag is set on the socket.

Parameters:
[in] type The socket type.
Returns:
The new socket type.

virtual void sockets::Socket::unwatch  )  [protected, virtual]
 

Unmonitors a socket.

The ssMonitored flag is always unset after this function returns.

virtual bool sockets::Socket::watch  )  [protected, virtual]
 

Monitors a socket. The Monitor::add(), Monitor::update() function is called with an appropriate event set depending on whether the ssMonitored, ssReadable and ssWritable status flags are set.

The ssMonitored flag is always set after this function returns successfully.

Returns:
true if successful, false otherwise.

virtual bool sockets::Socket::write_event  )  [inline, virtual]
 

This function is called by the Monitor-derived classes when a write event happens.

Returns:
false if the socket has been closed or is no longer valid, true otherwise.

Reimplemented in sockets::TCPClient.


The documentation for this class was generated from the following file:
Generated on Sun May 20 21:32:23 2007 for Epona API by  doxygen 1.4.6