Yate
|
A generic socket class. More...
#include <yateclass.h>
Public Types | |
enum | TOS { LowDelay = IPTOS_LOWDELAY, MaxThroughput = IPTOS_THROUGHPUT, MaxReliability = IPTOS_RELIABILITY, MinCost = IPTOS_MINCOST } |
Public Member Functions | |
Socket () | |
Socket (SOCKET handle) | |
Socket (int domain, int type, int protocol=0) | |
virtual | ~Socket () |
virtual bool | create (int domain, int type, int protocol=0) |
virtual bool | terminate () |
void | attach (SOCKET handle) |
SOCKET | detach () |
SOCKET | handle () const |
virtual bool | canRetry () const |
virtual bool | inProgress () const |
virtual bool | valid () const |
virtual bool | setOption (int level, int name, const void *value=0, socklen_t length=0) |
virtual bool | getOption (int level, int name, void *buffer, socklen_t *length) |
virtual bool | setParams (const NamedList ¶ms) |
virtual bool | setTOS (int tos) |
virtual bool | setBlocking (bool block=true) |
virtual bool | setReuse (bool reuse=true, bool exclusive=false) |
virtual bool | setLinger (int seconds=-1) |
virtual bool | bind (struct sockaddr *addr, socklen_t addrlen) |
bool | bind (const SocketAddr &addr) |
virtual bool | listen (unsigned int backlog=0) |
virtual Socket * | accept (struct sockaddr *addr=0, socklen_t *addrlen=0) |
Socket * | accept (SocketAddr &addr) |
SOCKET | acceptHandle (struct sockaddr *addr=0, socklen_t *addrlen=0) |
bool | updateError () |
virtual bool | canSelect () const |
virtual bool | connect (struct sockaddr *addr, socklen_t addrlen) |
bool | connect (const SocketAddr &addr) |
virtual bool | shutdown (bool stopReads, bool stopWrites) |
virtual bool | getSockName (struct sockaddr *addr, socklen_t *addrlen) |
bool | getSockName (SocketAddr &addr) |
virtual bool | getPeerName (struct sockaddr *addr, socklen_t *addrlen) |
bool | getPeerName (SocketAddr &addr) |
virtual int | sendTo (const void *buffer, int length, const struct sockaddr *addr, socklen_t adrlen, int flags=0) |
int | sendTo (const void *buffer, int length, const SocketAddr &addr, int flags=0) |
virtual int | send (const void *buffer, int length, int flags=0) |
virtual int | writeData (const void *buffer, int length) |
virtual int | recvFrom (void *buffer, int length, struct sockaddr *addr=0, socklen_t *adrlen=0, int flags=0) |
int | recvFrom (void *buffer, int length, SocketAddr &addr, int flags=0) |
virtual int | recv (void *buffer, int length, int flags=0) |
virtual int | readData (void *buffer, int length) |
virtual bool | select (bool *readok, bool *writeok, bool *except, struct timeval *timeout=0) |
bool | select (bool *readok, bool *writeok, bool *except, int64_t timeout) |
bool | installFilter (SocketFilter *filter) |
void | removeFilter (SocketFilter *filter, bool delobj=false) |
void | clearFilters () |
virtual void | timerTick (const Time &when) |
Static Public Member Functions | |
static SOCKET | invalidHandle () |
static int | socketError () |
static bool | efficientSelect () |
static bool | canSelect (SOCKET handle) |
static bool | createPair (Socket &sock1, Socket &sock2, int domain=AF_UNIX) |
Protected Member Functions | |
void | copyError () |
bool | checkError (int retcode, bool strict=false) |
bool | applyFilters (void *buffer, int length, int flags, const struct sockaddr *addr=0, socklen_t adrlen=0) |
Protected Attributes | |
SOCKET | m_handle |
ObjList | m_filters |
A generic socket class.
This class encapsulates a system dependent socket in a system independent abstraction
enum TOS |
Types of service
Socket | ( | ) |
Default constructor, creates an invalid socket
Socket | ( | SOCKET | handle | ) | [explicit] |
Constructor from an existing handle
handle | Operating system handle to an existing socket |
Socket | ( | int | domain, |
int | type, | ||
int | protocol = 0 |
||
) |
Constructor that also creates the socket handle
domain | Communication domain for the socket (protocol family) |
type | Type specification of the socket |
protocol | Specific protocol for the domain, 0 to use default |
virtual ~Socket | ( | ) | [virtual] |
Destructor - closes the handle if still open
Create a new socket for an incoming connection attempt on a listening socket
addr | Address to fill in with the address of the incoming connection |
addrlen | Length of the address structure on input, length of address data on return |
Socket* accept | ( | SocketAddr & | addr | ) |
Create a new socket for an incoming connection attempt on a listening socket
addr | Address to fill in with the address of the incoming connection |
Reimplemented in SctpSocket.
SOCKET acceptHandle | ( | struct sockaddr * | addr = 0 , |
socklen_t * | addrlen = 0 |
||
) |
Create a new socket for an incoming connection attempt on a listening socket
addr | Address to fill in with the address of the incoming connection |
addrlen | Length of the address structure on input, length of address data on return |
bool applyFilters | ( | void * | buffer, |
int | length, | ||
int | flags, | ||
const struct sockaddr * | addr = 0 , |
||
socklen_t | adrlen = 0 |
||
) | [protected] |
Apply installed filters to a received block of data
buffer | Buffer for received data |
length | Length of the data in buffer |
flags | Operating system specific bit flags of the operation |
addr | Address of the incoming data, may be NULL |
adrlen | Length of the valid data in address structure |
void attach | ( | SOCKET | handle | ) |
Attach an existing handle to the socket, closes any existing first
handle | Operating system handle to an existing socket |
virtual bool bind | ( | struct sockaddr * | addr, |
socklen_t | addrlen | ||
) | [virtual] |
Associates the socket with a local address
addr | Address to assign to this socket |
addrlen | Length of the address structure |
bool bind | ( | const SocketAddr & | addr | ) | [inline] |
Associates the socket with a local address
addr | Address to assign to this socket |
References SocketAddr::address(), Socket::bind(), and SocketAddr::length().
Referenced by Socket::bind().
virtual bool canRetry | ( | ) | const [virtual] |
Check if the last error code indicates a retryable condition
Reimplemented from Stream.
static bool canSelect | ( | SOCKET | handle | ) | [static] |
Check if a socket handle can be used in select
handle | The socket handle to check |
virtual bool canSelect | ( | ) | const [virtual] |
Check if this socket object can be used in a select
bool checkError | ( | int | retcode, |
bool | strict = false |
||
) | [protected] |
Copy the last error code from the operating system if an error occured, clear if not
retcode | Operation return code to check, 0 for success |
strict | True to consider errors only return codes of socketError() |
void clearFilters | ( | ) |
Removes and destroys all packet filters
virtual bool connect | ( | struct sockaddr * | addr, |
socklen_t | addrlen | ||
) | [virtual] |
Connects the socket to a remote address
addr | Address to connect to |
addrlen | Length of the address structure |
bool connect | ( | const SocketAddr & | addr | ) | [inline] |
Connects the socket to a remote address
addr | Socket address to connect to |
References SocketAddr::address(), Socket::connect(), and SocketAddr::length().
Referenced by Socket::connect().
void copyError | ( | ) | [protected] |
Copy the last error code from the operating system
virtual bool create | ( | int | domain, |
int | type, | ||
int | protocol = 0 |
||
) | [virtual] |
Creates a new socket handle,
domain | Communication domain for the socket (protocol family) |
type | Type specification of the socket |
protocol | Specific protocol for the domain, 0 to use default |
static bool createPair | ( | Socket & | sock1, |
Socket & | sock2, | ||
int | domain = AF_UNIX |
||
) | [static] |
SOCKET detach | ( | ) |
Detaches the object from the socket handle
static bool efficientSelect | ( | ) | [static] |
virtual bool getOption | ( | int | level, |
int | name, | ||
void * | buffer, | ||
socklen_t * | length | ||
) | [virtual] |
Get socket options
level | Level of the option to set |
name | Socket option for which the value is to be set |
buffer | Pointer to a buffer to return the value for the requested option |
length | Pointer to size of the supplied buffer, will be filled on return |
virtual bool getPeerName | ( | struct sockaddr * | addr, |
socklen_t * | addrlen | ||
) | [virtual] |
Retrieve the address of the remote socket of a connection
addr | Address to fill in with the address of the remote socket |
addrlen | Length of the address structure on input, length of address data on return |
bool getPeerName | ( | SocketAddr & | addr | ) |
Retrieve the address of the remote socket of a connection
addr | Address to fill in with the address of the remote socket |
virtual bool getSockName | ( | struct sockaddr * | addr, |
socklen_t * | addrlen | ||
) | [virtual] |
Retrieve the address of the local socket of a connection
addr | Address to fill in with the address of the local socket |
addrlen | Length of the address structure on input, length of address data on return |
bool getSockName | ( | SocketAddr & | addr | ) |
Retrieve the address of the local socket of a connection
addr | Address to fill in with the address of the local socket |
SOCKET handle | ( | ) | const [inline] |
Get the operating system handle to the socket
virtual bool inProgress | ( | ) | const [virtual] |
Check if the last error code indicates a non blocking operation in progress
Reimplemented from Stream.
bool installFilter | ( | SocketFilter * | filter | ) |
Install a new packet filter in the socket
filter | Pointer to the packet filter to install |
static SOCKET invalidHandle | ( | ) | [static] |
Get the operating system specific handle value for an invalid socket
virtual bool listen | ( | unsigned int | backlog = 0 | ) | [virtual] |
Start listening for incoming connections on the socket
backlog | Maximum length of the queue of pending connections, 0 for system maximum |
virtual int readData | ( | void * | buffer, |
int | length | ||
) | [virtual] |
Receive data from a connected stream socket
buffer | Buffer for data transfer |
length | Length of the buffer |
Implements Stream.
virtual int recv | ( | void * | buffer, |
int | length, | ||
int | flags = 0 |
||
) | [virtual] |
Receive a message from a connected socket
buffer | Buffer for data transfer |
length | Length of the buffer |
flags | Operating system specific bit flags that change the behaviour |
virtual int recvFrom | ( | void * | buffer, |
int | length, | ||
struct sockaddr * | addr = 0 , |
||
socklen_t * | adrlen = 0 , |
||
int | flags = 0 |
||
) | [virtual] |
Receive a message from a connected or unconnected socket
buffer | Buffer for data transfer |
length | Length of the buffer |
addr | Address to fill in with the address of the incoming data |
adrlen | Length of the address structure on input, length of address data on return |
flags | Operating system specific bit flags that change the behaviour |
int recvFrom | ( | void * | buffer, |
int | length, | ||
SocketAddr & | addr, | ||
int | flags = 0 |
||
) |
Receive a message from a connected or unconnected socket
buffer | Buffer for data transfer |
length | Length of the buffer |
addr | Address to fill in with the address of the incoming data |
flags | Operating system specific bit flags that change the behaviour |
void removeFilter | ( | SocketFilter * | filter, |
bool | delobj = false |
||
) |
Removes a packet filter and optionally destroys it
filter | Pointer to the packet filter to remove from socket |
delobj | Set to true to also delete the filter |
virtual bool select | ( | bool * | readok, |
bool * | writeok, | ||
bool * | except, | ||
struct timeval * | timeout = 0 |
||
) | [virtual] |
Determines the availability to perform synchronous I/O of the socket
readok | Address of a boolean variable to fill with readability status |
writeok | Address of a boolean variable to fill with writeability status |
except | Address of a boolean variable to fill with exceptions status |
timeout | Maximum time until the method returns, NULL for blocking |
bool select | ( | bool * | readok, |
bool * | writeok, | ||
bool * | except, | ||
int64_t | timeout | ||
) |
Determines the availability to perform synchronous I/O of the socket
readok | Address of a boolean variable to fill with readability status |
writeok | Address of a boolean variable to fill with writeability status |
except | Address of a boolean variable to fill with exceptions status |
timeout | Maximum time until the method returns, -1 for blocking |
virtual int send | ( | const void * | buffer, |
int | length, | ||
int | flags = 0 |
||
) | [virtual] |
Send a message over a connected socket
buffer | Buffer for data transfer |
length | Length of the buffer |
flags | Operating system specific bit flags that change the behaviour |
virtual int sendTo | ( | const void * | buffer, |
int | length, | ||
const struct sockaddr * | addr, | ||
socklen_t | adrlen, | ||
int | flags = 0 |
||
) | [virtual] |
Send a message over a connected or unconnected socket
buffer | Buffer for data transfer |
length | Length of the buffer |
addr | Address to send the message to, if NULL will behave like send() |
adrlen | Length of the address structure |
flags | Operating system specific bit flags that change the behaviour |
int sendTo | ( | const void * | buffer, |
int | length, | ||
const SocketAddr & | addr, | ||
int | flags = 0 |
||
) | [inline] |
Send a message over a connected or unconnected socket
buffer | Buffer for data transfer |
length | Length of the buffer |
addr | Address to send the message to |
flags | Operating system specific bit flags that change the behaviour |
References SocketAddr::address(), SocketAddr::length(), and Socket::sendTo().
Referenced by Socket::sendTo().
virtual bool setBlocking | ( | bool | block = true | ) | [virtual] |
Set the blocking or non-blocking operation mode of the socket
block | True if I/O operations should block, false for non-blocking |
Reimplemented from Stream.
virtual bool setLinger | ( | int | seconds = -1 | ) | [virtual] |
Set the way closing a socket is handled
seconds | How much to block waiting for socket to close, negative to no wait (close in background), zero to reset connection |
virtual bool setOption | ( | int | level, |
int | name, | ||
const void * | value = 0 , |
||
socklen_t | length = 0 |
||
) | [virtual] |
Set socket options
level | Level of the option to set |
name | Socket option for which the value is to be set |
value | Pointer to a buffer holding the value for the requested option |
length | Size of the supplied buffer |
Set specific socket parameters.
params | List of parameters |
virtual bool setReuse | ( | bool | reuse = true , |
bool | exclusive = false |
||
) | [virtual] |
Set the local address+port reuse flag of the socket. This method should be called before bind() or it will have no effect.
reuse | True if other sockets may listen on same address+port |
exclusive | Grant exclusive access to the address |
virtual bool setTOS | ( | int | tos | ) | [virtual] |
Set the Type of Service on the IP level of this socket
tos | New TOS bits to set |
virtual bool shutdown | ( | bool | stopReads, |
bool | stopWrites | ||
) | [virtual] |
Shut down one or both directions of a full-duplex socket.
stopReads | Request to shut down the read side of the socket |
stopWrites | Request to shut down the write side of the socket |
static int socketError | ( | ) | [static] |
Get the operating system specific return value of a failed operation
virtual bool terminate | ( | ) | [virtual] |
Closes the socket handle, terminates the connection
Implements Stream.
Run whatever actions required on idle thread runs. The default implementation calls SocketFilter::timerTick() for all installed filters.
when | Time when the idle run started |
bool updateError | ( | ) |
Update socket error from socket options. This method should be called when select() indicates a non blocking operation completed. Note: if false is returned, the socket error is the reason of getOption() failure
virtual bool valid | ( | ) | const [virtual] |
Check if this socket is valid
Implements Stream.
virtual int writeData | ( | const void * | buffer, |
int | length | ||
) | [virtual] |
Write data to a connected stream socket
buffer | Buffer for data transfer |
length | Length of the buffer |
Implements Stream.