Base Framework
|
#include <base/net/Socket.h>
Inherits Resource, AsynchronousInputStream, and AsynchronousOutputStream.
Inherited by ServerSocket [protected]
, and StreamSocket.
Public Types | |
enum | Domain { IPV4, IPV6, DEFAULT_DOMAIN = IPV4 } |
enum | Kind { STREAM, DATAGRAM, RAW } |
Public Member Functions | |
Socket () noexcept | |
Domain | getDomain () const |
Kind | getKind () const |
bool | accept (Socket &socket) |
void | bind (const InetAddress &address, unsigned short port) |
void | bind (const InetEndPoint &endPoint) |
void | close () |
void | connect (const InetAddress &address, unsigned short port) |
void | connect (const InetEndPoint &endPoint) |
void | create (Kind kind, Domain domain=DEFAULT_DOMAIN) |
void | getName () noexcept |
const InetAddress & | getAddress () const noexcept |
unsigned short | getPort () const noexcept |
InetEndPoint | getEndPoint () const noexcept |
const InetAddress & | getLocalAddress () const noexcept |
unsigned short | getLocalPort () const noexcept |
InetEndPoint | getLocalEndPoint () const noexcept |
void | listen (unsigned int backlog) |
void | shutdownInputStream () |
void | shutdownOutputStream () |
int | getErrorState () const |
bool | getReuseAddress () const |
void | setReuseAddress (bool value) |
bool | getKeepAlive () const |
void | setKeepAlive (bool value) |
bool | getBroadcast () const |
void | setBroadcast (bool value) |
int | getLinger () const |
void | setLinger (int seconds) |
int | getReceiveBufferSize () const |
void | setReceiveBufferSize (int size) |
int | getSendBufferSize () const |
void | setSendBufferSize (int size) |
bool | getDontRoute () const |
void | setDontRoute (bool value) |
uint64 | getReceiveTimeout () const |
void | setReceiveTimeout (uint64 nanoseconds) |
uint64 | getSendTimeout () const |
void | setSendTimeout (uint64 nanoseconds) |
bool | getTcpNoDelay () const |
void | setTcpNoDelay (bool value) |
uint64 | getTcpDeferAccept () const |
void | setTcpDeferAccept (uint64 nanoseconds) |
unsigned int | getTimeToLive () const |
void | setTimeToLive (unsigned int value) |
uint8 | getMulticastHops () const |
void | setMulticastHops (uint8 value) |
bool | getMulticastLoopback () const |
void | setMulticastLoopback (bool value) |
InetAddress | getMulticastInterface () const |
void | setMulticastInterface (const InetAddress &interface) |
uint8 | getUnicastHops () const |
void | setUnicastHops (uint8 value) |
void | joinGroup (const InetAddress &group) |
void | joinGroup (const InetAddress &interface, const InetAddress &group) |
void | leaveGroup (const InetAddress &interface, const InetAddress &group) |
bool | getIPv6Restriction () const |
void | setIPv6Restriction (bool value) |
void | setNonBlocking (bool value) |
unsigned int | available () const |
void | flush () |
unsigned int | read (uint8 *buffer, unsigned int size, bool nonblocking=false) |
unsigned int | write (const uint8 *buffer, unsigned int size, bool nonblocking=false) |
unsigned int | sendTo (const uint8 *buffer, unsigned int size, const InetAddress &address, unsigned short port) |
unsigned int | receiveFrom (uint8 *buffer, unsigned int size, InetAddress &address, unsigned short &port) |
void | asyncCancel () |
AsynchronousReadOperation | read (uint8 *buffer, unsigned int bytesToRead, AsynchronousReadEventListener *listener) |
AsynchronousWriteOperation | write (const uint8 *buffer, unsigned int bytesToWrite, AsynchronousWriteEventListener *listener) |
void | wait () const |
bool | wait (unsigned int microseconds) const |
~Socket () | |
![]() | |
String | getDescription () const |
void | setDescription (const String &description) |
unsigned int | getResourceId () const |
unsigned int | getCreatedById () const |
AnyReference | getHandle () const noexcept |
template<class TYPE > | |
TYPE & | getInternalHandle () const |
operator bool () const noexcept | |
![]() | |
virtual | ~AsynchronousStream () noexcept(false) |
Static Public Member Functions | |
static const char * | toString (Domain domain) noexcept |
static const char * | toString (Kind kind) noexcept |
![]() | |
static bool | asyncTest () noexcept |
Protected Types | |
enum | { OPTION_DONT_ROUTE, OPTION_TCP_NO_DELAY, OPTION_TCP_DEFER_ACCEPT } |
Protected Member Functions | |
OperatingSystem::Handle | getHandle () const noexcept |
bool | getBooleanOption (int option) const |
void | setBooleanOption (int option, bool value) |
Additional Inherited Members | |
![]() | |
AnyReference | handle |
This class implements a socket. A socket is an endpoint for communication between two hosts on a network. MT-level is safe.
|
protected |
Socket options.
enum Socket::Domain |
enum Socket::Kind |
|
noexcept |
Initializes an invalidated socket object (ie. unconnected and unbound).
Socket::~Socket | ( | ) |
Releases the socket.
bool Socket::accept | ( | Socket & | socket | ) |
Accepts the first connection from the queue of pending connections on the specified socket. This function is used with a connection-oriented socket.
socket | Specifies the socket to accept a connection from. |
unsigned int Socket::available | ( | ) | const |
Returns the number of bytes that can be read or skipped over without blocking.
void Socket::bind | ( | const InetAddress & | address, |
unsigned short | port | ||
) |
Associates a local name (address and port) with this socket.
address | The IP address the socket should be bound to. If unspecified the assigned address may not be known until the socket has been connected with connect or accept. |
port | The port the socket should be bound to. If zero the socket is assigned to a unique port. |
|
inline |
Associates a local name (address and port) with this socket.
endPoint | The end point. |
void Socket::close | ( | ) |
Closes this socket.
void Socket::connect | ( | const InetAddress & | address, |
unsigned short | port | ||
) |
Connects this socket to the specified address and port.
address | The IP address to connect to. |
port | The port to connect to. |
|
inline |
Connects this socket to the specified address and port.
endPoint | The end point. |
void Socket::create | ( | Kind | kind, |
Domain | domain = DEFAULT_DOMAIN |
||
) |
Creates either a stream or a datagram socket.
kind | The socket type (e.g. STREAM). |
domain | The domain (the default is DEFAULT_DOMAIN). |
void Socket::flush | ( | ) |
Forces any buffered bytes to be written out.
|
noexcept |
Returns the IP address to which the socket is connected.
|
protected |
Get boolean socket option.
bool Socket::getBroadcast | ( | ) | const |
Returns true if broadcast datagrams allowed on this socket.
Domain Socket::getDomain | ( | ) | const |
Returns the domain.
bool Socket::getDontRoute | ( | ) | const |
Returns true if the standard routing facilities have been disabled.
|
inlinenoexcept |
Returns the connected end point.
int Socket::getErrorState | ( | ) | const |
Returns the error state of the socket.
|
protectednoexcept |
Returns the handle of the socket.
bool Socket::getIPv6Restriction | ( | ) | const |
Returns the IPv6 packet restriction flag.
bool Socket::getKeepAlive | ( | ) | const |
Returns true if connection is kept alive.
Kind Socket::getKind | ( | ) | const |
Returns the kind.
int Socket::getLinger | ( | ) | const |
Gets the linger interval.
|
noexcept |
Returns the local IP address to which the socket is bound.
|
inlinenoexcept |
Returns the local end point.
|
noexcept |
Returns the local port to which the socket is bound.
uint8 Socket::getMulticastHops | ( | ) | const |
Returns the maximum number of multicast hops (time to live).
InetAddress Socket::getMulticastInterface | ( | ) | const |
Returns the default interface for outgoing multicast packets.
bool Socket::getMulticastLoopback | ( | ) | const |
Returns the multicast loop-back value.
|
noexcept |
Caches the locally assigned address and port of the socket. This member function can be used after a succesful accept or connect to determine the locally assigned address and port if unspecified.
|
noexcept |
Returns the remote port to which the socket is connected.
int Socket::getReceiveBufferSize | ( | ) | const |
Gets the size of the receive buffer.
uint64 Socket::getReceiveTimeout | ( | ) | const |
Returns the timeout period in nanoseconds for receive.
bool Socket::getReuseAddress | ( | ) | const |
Returns true if 'bind' allows local addresses to be reused.
int Socket::getSendBufferSize | ( | ) | const |
Gets the size of the send buffer.
uint64 Socket::getSendTimeout | ( | ) | const |
Returns the timeout period in nanoseconds for send.
uint64 Socket::getTcpDeferAccept | ( | ) | const |
Returns the timeout period in nanoseconds for send.
bool Socket::getTcpNoDelay | ( | ) | const |
Returns true if the Nagle's algorithm is disabled.
unsigned int Socket::getTimeToLive | ( | ) | const |
Returns the current time to live (TTL) value.
uint8 Socket::getUnicastHops | ( | ) | const |
Returns the maximum number of unicast hops (time to live).
void Socket::joinGroup | ( | const InetAddress & | group | ) |
Joins the specified multicast group on the default interface.
void Socket::joinGroup | ( | const InetAddress & | interface, |
const InetAddress & | group | ||
) |
Joins the specified multicast group on the specified interface.
void Socket::leaveGroup | ( | const InetAddress & | interface, |
const InetAddress & | group | ||
) |
Leaves the specified multicast group of the specified interface.
void Socket::listen | ( | unsigned int | backlog | ) |
This function places the socket in a state where it is listening for incoming connections. It also sets the maximum length of the queue of pending connections. The backlog argument may be silently reduced. The socket must be bound and unconnected.
backlog | The maxium length of the queue. |
|
virtual |
Requests and asynchronous read operation.
buffer | The bytes to be read. |
bytesToRead | The number of bytes to be read. |
listener | The listener to be notified on completion. |
Implements AsynchronousInputStream.
unsigned int Socket::read | ( | uint8 * | buffer, |
unsigned int | size, | ||
bool | nonblocking = false |
||
) |
Fills the buffer with bytes from the socket input stream. Blocks if asked to read more bytes than available. Raises EndOfFile if end of stream has been reached.
buffer | The buffer. |
size | The size of the buffer. |
nonblocking | Select nonblocking mode. |
unsigned int Socket::receiveFrom | ( | uint8 * | buffer, |
unsigned int | size, | ||
InetAddress & | address, | ||
unsigned short & | port | ||
) |
Receives data from any address using an unconnected socket.
buffer | The buffer. |
size | The size of the buffer. |
address | The address. |
port | The port. |
unsigned int Socket::sendTo | ( | const uint8 * | buffer, |
unsigned int | size, | ||
const InetAddress & | address, | ||
unsigned short | port | ||
) |
Sends the contents of the buffer to the specified address using an unconnected socket.
buffer | The buffer. |
size | The size of the buffer. |
address | The Internet address of the remote host. |
port | The port of the remote host. |
|
protected |
Set boolean socket option.
void Socket::setBroadcast | ( | bool | value | ) |
Sets the broadcast datagram flag of this socket.
void Socket::setDontRoute | ( | bool | value | ) |
Disables/enables the standard routing facilities.
void Socket::setIPv6Restriction | ( | bool | value | ) |
Sets the IPv6 packet restriction flag.
void Socket::setKeepAlive | ( | bool | value | ) |
Sets the keep alive flag of this socket.
void Socket::setLinger | ( | int | seconds | ) |
Sets the linger interval. Negative time disables the linger.
void Socket::setMulticastHops | ( | uint8 | value | ) |
Sets the maximum number of multicast hops (time to live).
void Socket::setMulticastInterface | ( | const InetAddress & | interface | ) |
Sets the default interface for outgoing multicast packets.
void Socket::setMulticastLoopback | ( | bool | value | ) |
Sets the multicast loop-back value.
void Socket::setNonBlocking | ( | bool | value | ) |
Sets the blocking mode of the socket.
void Socket::setReceiveBufferSize | ( | int | size | ) |
Sets the size of the receive buffer.
void Socket::setReceiveTimeout | ( | uint64 | nanoseconds | ) |
Sets the timeout period for receive (clamped to one day).
void Socket::setReuseAddress | ( | bool | value | ) |
Sets the local address reuse flag of this socket.
void Socket::setSendBufferSize | ( | int | size | ) |
Sets the size of the send buffer.
void Socket::setSendTimeout | ( | uint64 | nanoseconds | ) |
Sets the timeout period for send (clamped to one day).
void Socket::setTcpDeferAccept | ( | uint64 | nanoseconds | ) |
Sets the timeout period for send (clamped to one day).
void Socket::setTcpNoDelay | ( | bool | value | ) |
Disables/enables the Nagle's algorithm.
void Socket::setTimeToLive | ( | unsigned int | value | ) |
Sets the time to live (TTL) value.
void Socket::setUnicastHops | ( | uint8 | value | ) |
Sets the maximum number of unicast hops (time to live).
void Socket::shutdownInputStream | ( | ) |
Disables the input stream for this socket.
void Socket::shutdownOutputStream | ( | ) |
Disables the output stream for this socket.
void Socket::wait | ( | ) | const |
Blocking wait for input to become available.
bool Socket::wait | ( | unsigned int | microseconds | ) | const |
Blocking wait for input to become available.
microseconds | The timeout periode in microseconds. |
|
virtual |
Requests and asynchronous write operation.
buffer | The bytes to be read. |
bytesToWrite | The number of bytes to be written. |
listener | The listener to be notified on completion. |
Implements AsynchronousOutputStream.
unsigned int Socket::write | ( | const uint8 * | buffer, |
unsigned int | size, | ||
bool | nonblocking = false |
||
) |
Writes bytes in buffer to stream.
buffer | The buffer containing the bytes to be written. |
size | The number of bytes to be written. |
nonblocking | Select nonblocking mode. |