#include <socket.h>
Public Types | |
typedef ConexusPointer< Socket > | pointer |
Public Member Functions | |
virtual | ~Socket () throw () |
virtual void | open () throw ( open_exception ) |
Creates the socket; similar to the socket() call. | |
virtual void | close (bool force=false) throw ( close_exception ) |
Overloads the parent FileDescriptor class to ensure that the final state clears the BOUND, CONNECTED, LISTENING and ACCEPTED flags. | |
virtual void | bind () throw ( bind_exception ) |
Binding without an address (autobinding) is a child specific action. | |
virtual void | bind (const Conexus::Address &a) throw ( bind_exception ) |
Binds the socket to the specified address. | |
virtual void | connect () throw ( connect_exception ) |
Connecting without an address (autoconnecting) is a child specific action. | |
virtual void | connect (const Address &a) throw ( connect_exception ) |
Connects the socket to the provided address. | |
virtual void | listen (int backlog=0) |
Places the socket in a listening mode; nearly identical to calling listen() on the socket. | |
int | domain () throw () |
Returns the communication domain which specifies the protocol family of the socket. | |
void | set_domain (int) throw () |
Sets the communication domain which specifies the protocol family of the socket; this will only set the domain internally, but will not have an actual effect until the socket is created, or if the socket is already created it will not have an effect until the socket is closed and recreated. | |
int | type () throw () |
Returns the socket type, which defines the communication mechanism of the socket. | |
void | set_type (int) throw () |
Sets the socket type, which defines the communication mechanism of the socket. | |
int | protocol () throw () |
Returns the specific protocol within the socket's protocol family. | |
void | set_protocol (int) throw () |
Sets the specific protocol within the socket's protocol family, but is normally 0. | |
virtual size_t | writeto (Address &a, const void *data, size_t size, Timeout timeout=Timeout(-1)) throw ( write_exception ) |
virtual size_t | writeto (Address &a, const Data data, Timeout timeout=Timeout(-1)) throw ( write_exception ) |
virtual void | set_option (int option, bool b, long required_state=SOCKET_BOUND) |
template<typename T > | |
void | set_option (int level, int optname, T &value, long required_state=SOCKET_BOUND) |
template<typename T > | |
void | option (int level, int optname, T &value, long required_state=SOCKET_BOUND) |
void | enable_reuse_address (bool enable=true) |
virtual void | change_state (long states) throw ( state_exception ) |
sigc::signal< void > | signal_bound () |
sigc::signal< void > | signal_connected () |
sigc::signal< void > | signal_listening () |
bool | is_bound () |
bool | is_connected () |
bool | is_listening () |
bool | is_accepted () |
Static Public Member Functions | |
static pointer | create (int domain=-1, int type=-1, int protocol=0) throw () |
Protected Member Functions | |
Socket (int domain=-1, int type=-1, int protocol=0) throw () | |
This default constructor is primarily for children to specify a domain, type and socket protocol. | |
virtual void | read_thread_main () |
virtual void | set_state_closed () |
virtual void | set_state_bound () |
virtual void | set_state_connected () |
virtual void | set_state_listening () |
virtual size_t | write_data (const Data data, Timeout timeout) throw ( write_exception ) |
virtual Data | read_data (size_t size, Timeout timeout) throw ( read_exception ) |
Reimplements Endpoint::read_data() using FileDescriptor::read_data(). | |
Protected Attributes | |
int | m_domain |
int | m_type |
int | m_protocol |
sigc::signal< void > | m_signal_bound |
sigc::signal< void > | m_signal_connected |
sigc::signal< void > | m_signal_listening |
Related Functions | |
(Note that these are not member functions.) | |
enum | SocketState { SOCKET_BOUND = ENDPOINT_LAST_STATE << 1, SOCKET_CONNECTED = ENDPOINT_LAST_STATE << 2, SOCKET_LISTENING = ENDPOINT_LAST_STATE << 3, SOCKET_ACCEPTED = ENDPOINT_LAST_STATE << 4, SOCKET_LAST_STATE = SOCKET_ACCEPTED } |
This class provides the following propertymm properties:
typedef ConexusPointer<Socket> Conexus::Socket::pointer |
Reimplemented from Conexus::FileDescriptor.
Reimplemented in Conexus::IP, Conexus::IPv4::IP, Conexus::IPv4::TCP, Conexus::IPv4::UDP, Conexus::IPv4::UDPPoset, Conexus::IPv6::IP, Conexus::IPv6::TCP, Conexus::IPv6::UDP, Conexus::IPv6::UDPPoset, Conexus::LL::Packet, Conexus::SSL::IPv4::TCP, Conexus::SSL::IPv4::UDP, and Conexus::SSL::IPv6::TCP.
Conexus::Socket::Socket | ( | int | domain = -1 , |
|
int | type = -1 , |
|||
int | protocol = 0 | |||
) | throw () [protected] |
This default constructor is primarily for children to specify a domain, type and socket protocol.
Conexus::Socket::~Socket | ( | ) | throw () [virtual] |
void Conexus::Socket::bind | ( | const Conexus::Address & | a | ) | throw ( bind_exception ) [virtual] |
Binds the socket to the specified address.
The socket should already be in the OPENED state before this call.
If the socket is in the CLOSED state, then set_state(OPENED) will be automatically called.
Reimplemented in Conexus::IPv4::IP, Conexus::IPv4::UDP, Conexus::IPv4::UDPPoset, Conexus::IPv6::IP, Conexus::IPv6::UDP, and Conexus::IPv6::UDPPoset.
References Conexus::ENDPOINT_OPENED, and Conexus::throw_bind_exception().
void Conexus::Socket::bind | ( | ) | throw ( bind_exception ) [virtual] |
Binding without an address (autobinding) is a child specific action.
By default an attempt to bind without providing an address will result in a thrown exception condition. Therefore children should modify this behavior if they wish to provide autobinding.
Reimplemented in Conexus::IPv4::IP, Conexus::IPv4::UDP, Conexus::IPv4::UDPPoset, Conexus::IPv6::IP, Conexus::IPv6::UDP, Conexus::IPv6::UDPPoset, and Conexus::LL::Packet.
Referenced by Conexus::LL::Packet::bind(), Conexus::IPv6::IP::bind(), and Conexus::IPv4::IP::bind().
void Conexus::Socket::change_state | ( | long | states | ) | throw ( state_exception ) [virtual] |
Reimplemented from Conexus::Endpoint.
References Conexus::Endpoint::change_state(), Conexus::ENDPOINT_CLOSED, Conexus::SOCKET_ACCEPTED, Conexus::SOCKET_BOUND, Conexus::SOCKET_CONNECTED, and Conexus::SOCKET_LISTENING.
Referenced by Conexus::LL::Packet::interface_index(), Conexus::LL::Packet::interface_name(), Conexus::LL::Packet::interface_names(), listen(), open(), option(), read_thread_main(), Conexus::LL::Packet::set_interface(), and set_option().
void Conexus::Socket::close | ( | bool | force = false |
) | throw ( close_exception ) [virtual] |
Overloads the parent FileDescriptor class to ensure that the final state clears the BOUND, CONNECTED, LISTENING and ACCEPTED flags.
Reimplemented from Conexus::FileDescriptor.
References Conexus::throw_close_exception().
void Conexus::Socket::connect | ( | const Address & | a | ) | throw ( connect_exception ) [virtual] |
Connects the socket to the provided address.
If the socket is in the CLOSED state, then set_state(OPENED) will be automatically called.
If the provided address is a broadcast address, will also set the broadcast socket option.
References Conexus::ENDPOINT_OPENED, and Conexus::throw_connect_exception().
void Conexus::Socket::connect | ( | ) | throw ( connect_exception ) [virtual] |
Connecting without an address (autoconnecting) is a child specific action.
By default an attempt to connect without providing an address will result in a thrown exception condition. Therefore children should modify this behavior if they wish to provide autoconnection.
Reimplemented in Conexus::IPv4::IP, Conexus::IPv4::UDP, Conexus::IPv4::UDPPoset, Conexus::IPv6::IP, Conexus::IPv6::UDP, Conexus::IPv6::UDPPoset, Conexus::SSL::IPv4::TCP, Conexus::SSL::IPv4::UDP, and Conexus::SSL::IPv6::TCP.
Referenced by Conexus::IPv6::IP::connect(), and Conexus::IPv4::IP::connect().
Socket::pointer Conexus::Socket::create | ( | int | domain = -1 , |
|
int | type = -1 , |
|||
int | protocol = 0 | |||
) | throw () [static] |
int Conexus::Socket::domain | ( | ) | throw () |
Returns the communication domain which specifies the protocol family of the socket.
References m_domain.
void Conexus::Socket::enable_reuse_address | ( | bool | enable = true |
) |
References Conexus::ENDPOINT_OPENED, and set_option().
bool Conexus::Socket::is_accepted | ( | ) |
bool Conexus::Socket::is_bound | ( | ) |
References Conexus::Endpoint::m_state, and Conexus::SOCKET_BOUND.
Referenced by Conexus::IPv6::UDP::add_multicast_interface(), Conexus::IPv4::UDP::add_multicast_interface(), Conexus::IPv4::UDP::enable_multicast_loopback(), Conexus::IPv4::UDP::is_multicast_loopback_enabled(), Conexus::IPv6::UDP::join(), Conexus::IPv4::UDP::join(), Conexus::IPv6::UDP::leave(), Conexus::IPv4::UDP::leave(), listen(), Conexus::IPv4::UDP::multicast_hop_limit(), Conexus::IPv6::UDPPoset::on_local_address_changed(), Conexus::IPv6::UDP::on_local_address_changed(), Conexus::IPv6::TCP::on_local_address_changed(), Conexus::IPv4::UDPPoset::on_local_address_changed(), Conexus::IPv4::UDP::on_local_address_changed(), Conexus::IPv4::TCP::on_local_address_changed(), read_thread_main(), Conexus::IPv6::UDP::remove_multicast_interface(), Conexus::IPv4::UDP::remove_multicast_interface(), Conexus::IPv4::UDP::set_multicast_hop_limit(), and set_state_bound().
bool Conexus::Socket::is_connected | ( | ) |
References Conexus::Endpoint::m_state, and Conexus::SOCKET_CONNECTED.
Referenced by Conexus::IPv6::UDP::on_remote_address_changed(), Conexus::IPv6::TCP::on_remote_address_changed(), Conexus::IPv4::UDP::on_remote_address_changed(), Conexus::IPv4::TCP::on_remote_address_changed(), set_state_connected(), Conexus::IPv6::TCP::shutdown(), and Conexus::IPv4::TCP::shutdown().
bool Conexus::Socket::is_listening | ( | ) |
References Conexus::Endpoint::m_state, and Conexus::SOCKET_LISTENING.
Referenced by set_state_listening().
void Conexus::Socket::listen | ( | int | backlog = 0 |
) | [virtual] |
Places the socket in a listening mode; nearly identical to calling listen() on the socket.
The socket should already be in the BOUND state before this call.
If the socket is in the CLOSED or OPENED states, then set_state(BOUND) will be automatically called.
References change_state(), is_bound(), Conexus::FileDescriptor::m_fd, set_state_listening(), Conexus::SOCKET_BOUND, and Conexus::throw_listen_exception().
void Conexus::Socket::open | ( | ) | throw ( open_exception ) [virtual] |
Creates the socket; similar to the socket() call.
Ideally, the socket will be in the CLOSED state before this call.
If the socket is in the OPENED state (not BOUND, CONNECTED, LISTENING, ACCEPTED...) this method will return without performing any action.
If the socket is in any state other than CLOSED or OPENED the socket will be closed and reopened.
Implements Conexus::Endpoint.
Reimplemented in Conexus::SSL::IPv4::TCP, Conexus::SSL::IPv4::UDP, and Conexus::SSL::IPv6::TCP.
References change_state(), Conexus::ENDPOINT_CLOSED, Conexus::Endpoint::is_open(), m_domain, Conexus::FileDescriptor::m_fd, m_protocol, m_type, Conexus::Endpoint::set_state_opened(), and Conexus::throw_open_exception().
void Conexus::Socket::option | ( | int | level, | |
int | optname, | |||
T & | value, | |||
long | required_state = SOCKET_BOUND | |||
) | [inline] |
References change_state(), and Conexus::FileDescriptor::m_fd.
Referenced by Conexus::IPv6::UDP::is_multicast_loopback_enabled(), Conexus::IPv4::UDP::is_multicast_loopback_enabled(), Conexus::IPv6::UDP::multicast_hop_limit(), Conexus::IPv4::UDP::multicast_hop_limit(), Conexus::IP::tos(), and Conexus::IP::ttl().
int Conexus::Socket::protocol | ( | ) | throw () |
Data Conexus::Socket::read_data | ( | size_t | size, | |
Timeout | timeout | |||
) | throw ( read_exception ) [protected, virtual] |
Reimplements Endpoint::read_data() using FileDescriptor::read_data().
Changes from FileDescriptor::read_data()
Reimplemented from Conexus::FileDescriptor.
Reimplemented in Conexus::SSL::IPv4::TCP, Conexus::SSL::IPv4::UDP, and Conexus::SSL::IPv6::TCP.
References Conexus::FileDescriptor::read_data(), Conexus::Data::size(), and Conexus::SOCKET_BOUND.
void Conexus::Socket::read_thread_main | ( | ) | [protected, virtual] |
Reimplemented from Conexus::Endpoint.
References change_state(), is_bound(), and Conexus::SOCKET_BOUND.
void Conexus::Socket::set_domain | ( | int | d | ) | throw () |
Sets the communication domain which specifies the protocol family of the socket; this will only set the domain internally, but will not have an actual effect until the socket is created, or if the socket is already created it will not have an effect until the socket is closed and recreated.
Linux currently defines the following protocol families:
Name | Purpose | Man page |
PF_UNIX, PF_LOCAL | Local communication | unix(7) |
PF_INET | IPv4 Internet protocols | ip(7) |
PF_INET6 | IPv6 Internet protocols | |
PF_IPX | IPX - Novell protocols | |
PF_NETLINK | Kernel user interface device | netlink(7) |
PF_X25 | ITU-T X.25 / ISO-8208 protocol | x25(7) |
PF_AX25 | Amateur radio AX.25 protocol | |
PF_ATMPVC | Access to raw ATM PVCs | |
PF_APPLETALK | Appletalk | ddp(7) |
PF_PACKET | Low level packet interface | packet(7) |
void Conexus::Socket::set_option | ( | int | level, | |
int | optname, | |||
T & | value, | |||
long | required_state = SOCKET_BOUND | |||
) | [inline] |
References change_state(), Conexus::FileDescriptor::m_fd, and Conexus::throw_socket_exception().
void Conexus::Socket::set_option | ( | int | option, | |
bool | b, | |||
long | required_state = SOCKET_BOUND | |||
) | [virtual] |
References change_state(), Conexus::FileDescriptor::m_fd, and Conexus::throw_socket_exception().
Referenced by Conexus::IPv6::UDP::bind(), Conexus::IPv4::UDP::bind(), Conexus::IPv6::UDP::enable_multicast_loopback(), Conexus::IPv4::UDP::enable_multicast_loopback(), enable_reuse_address(), Conexus::IPv6::UDP::join(), Conexus::IPv6::UDP::leave(), Conexus::IPv4::UDP::leave(), Conexus::IPv6::UDP::set_multicast_hop_limit(), Conexus::IPv4::UDP::set_multicast_hop_limit(), Conexus::IP::set_tos(), and Conexus::IP::set_ttl().
void Conexus::Socket::set_protocol | ( | int | p | ) | throw () |
Sets the specific protocol within the socket's protocol family, but is normally 0.
void Conexus::Socket::set_state_bound | ( | ) | [protected, virtual] |
References is_bound(), m_signal_bound, Conexus::Endpoint::m_state, and Conexus::SOCKET_BOUND.
void Conexus::Socket::set_state_closed | ( | ) | [protected, virtual] |
Reimplemented from Conexus::Endpoint.
References Conexus::Endpoint::is_closed(), Conexus::Endpoint::m_state, Conexus::SOCKET_ACCEPTED, Conexus::SOCKET_BOUND, Conexus::SOCKET_CONNECTED, and Conexus::SOCKET_LISTENING.
void Conexus::Socket::set_state_connected | ( | ) | [protected, virtual] |
References is_connected(), m_signal_connected, Conexus::Endpoint::m_state, and Conexus::SOCKET_CONNECTED.
void Conexus::Socket::set_state_listening | ( | ) | [protected, virtual] |
References is_listening(), m_signal_listening, Conexus::Endpoint::m_state, and Conexus::SOCKET_LISTENING.
Referenced by listen().
void Conexus::Socket::set_type | ( | int | t | ) | throw () |
Sets the socket type, which defines the communication mechanism of the socket.
Linux currently defines the following communication types:
Some socket types may not be implemented by all protocol families; for example, SOCK_SEQPACKET is not implemented for AF_INET.
sigc::signal< void > Conexus::Socket::signal_bound | ( | ) |
References m_signal_bound.
sigc::signal< void > Conexus::Socket::signal_connected | ( | ) |
References m_signal_connected.
sigc::signal< void > Conexus::Socket::signal_listening | ( | ) |
References m_signal_listening.
int Conexus::Socket::type | ( | ) | throw () |
Returns the socket type, which defines the communication mechanism of the socket.
References m_type.
size_t Conexus::Socket::write_data | ( | const Data | data, | |
Timeout | timeout | |||
) | throw ( write_exception ) [protected, virtual] |
Reimplemented from Conexus::FileDescriptor.
Reimplemented in Conexus::IPv4::TCP, Conexus::IPv4::UDP, Conexus::IPv4::UDPPoset, Conexus::IPv6::TCP, Conexus::IPv6::UDP, Conexus::IPv6::UDPPoset, Conexus::LL::Packet, Conexus::SSL::IPv4::TCP, Conexus::SSL::IPv4::UDP, and Conexus::SSL::IPv6::TCP.
References Conexus::SOCKET_CONNECTED, and Conexus::FileDescriptor::write_data().
Referenced by Conexus::IPv6::UDP::write_data(), Conexus::IPv6::TCP::write_data(), Conexus::IPv4::UDP::write_data(), and Conexus::IPv4::TCP::write_data().
size_t Conexus::Socket::writeto | ( | Address & | a, | |
const Data | data, | |||
Timeout | timeout = Timeout(-1) | |||
) | throw ( write_exception ) [virtual] |
size_t Conexus::Socket::writeto | ( | Address & | a, | |
const void * | data, | |||
size_t | size, | |||
Timeout | timeout = Timeout(-1) | |||
) | throw ( write_exception ) [virtual] |
enum SocketState [related] |
These enumerations are used in the socket class methods, and use is also encouraged in children.
int Conexus::Socket::m_domain [protected] |
int Conexus::Socket::m_protocol [protected] |
sigc::signal<void> Conexus::Socket::m_signal_bound [protected] |
Referenced by set_state_bound(), and signal_bound().
sigc::signal<void> Conexus::Socket::m_signal_connected [protected] |
Referenced by set_state_connected(), and signal_connected().
sigc::signal<void> Conexus::Socket::m_signal_listening [protected] |
Referenced by set_state_listening(), and signal_listening().
int Conexus::Socket::m_type [protected] |