#include <socket.h>
Public Types | |
enum | { ssCreated = 0x00000001, ssLocalInfo = 0x00000002, ssRemoteInfo = 0x00000004, ssBound = 0x00000008, ssConnecting = 0x00000010, ssConnected = 0x00000020, ssListening = 0x00000040, ssMonitored = 0x00000080, ssReadable = 0x00000100, ssWritable = 0x00000200, ssSSL = 0x00000400, ssSSLHandshake = 0x00000800, ssCloseFlags, ssCopyFlags = (ssLocalInfo | ssRemoteInfo | ssSSL) } |
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 Socket & | operator= (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 void | init (size_t hint) |
Initializes the sockets library. | |
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. | |
Friends | |
class | SSLInfo |
Add SSLInfo as a friend. |
It handles socket creation and destruction, and provides means to track socket status.
anonymous enum |
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. |
ssSSL | SSL support requested. |
ssSSLHandshake | SSL handshake has been done. |
ssCloseFlags | Flags removed by the close() function. |
ssCopyFlags | Flags that can be copied. |
sockets::Socket::Socket | ( | int | family = PF_UNSPEC , |
|
int | type = 0 , |
|||
int | protocol = 0 | |||
) | [explicit] |
Constructor.
[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.
[in] | s | The socket to copy. |
virtual sockets::Socket::~Socket | ( | ) | [virtual] |
This destructor calls the close() function.
static void sockets::Socket::init | ( | size_t | hint | ) | [static] |
Initializes the sockets library.
This calls the setup_monitors() function and does any other required initialization.
[in] | hint | A hint passed to the setup_monitors() function. |
This assignment operator copies the socket family, type, protocol, monitor and part of the status only.
[in] | right | The socket to be assigned. |
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.
[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. |
Reimplemented in sockets::IPSocket, and sockets::TCPClient.
virtual bool sockets::Socket::read_event | ( | ) | [inline, virtual] |
This function is called by the Monitor-derived classes when a read event happens.
Reimplemented in sockets::TCPClient.
virtual bool sockets::Socket::write_event | ( | ) | [inline, virtual] |
This function is called by the Monitor-derived classes when a write event happens.
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.
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.
[in] | name | The name of the monitor to use for this socket. |
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.
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.
Reimplemented in sockets::TCPClient.
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.
virtual void sockets::Socket::unwatch | ( | ) | [protected, virtual] |
Unmonitors a socket.
The ssMonitored flag is always unset after this function returns.
int sockets::Socket::fd | ( | ) | const [inline, protected] |
Returns the socket file descriptor.
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.
[in] | family | The socket protocol family. |
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.
[in] | type | The socket type. |
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.
[in] | protocol | The socket protocol. |
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.
[in] | flags | The flags to add. |
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.
[in] | flags | The flags to remove. |