Fawkes API
Fawkes Development Version
|
#include <netcomm/socket/socket.h>
Public Types | |
enum | AddrType { UNSPECIFIED, IPv4, IPv6 } |
Address type specification. More... | |
enum | SocketType { TCP, UDP } |
Socket type. More... | |
Public Member Functions | |
Socket (AddrType addr_type, SocketType sock_type, float timeout=0.f) | |
Constructor similar to syscall. More... | |
Socket (Socket &socket) | |
Copy constructor. More... | |
virtual | ~Socket () |
Destructor. More... | |
virtual void | connect (const char *hostname, const unsigned short int port) |
Connect socket. More... | |
virtual void | connect (const struct ::sockaddr_storage &addr_port) |
Connect socket. More... | |
virtual void | connect (const struct sockaddr *addr_port, socklen_t struct_size) |
Connect socket. More... | |
virtual void | bind (const unsigned short int port) |
Bind socket. More... | |
virtual void | bind (const unsigned short int port, const char *ipaddr) |
Bind socket to a specific address. More... | |
virtual void | listen (int backlog=1) |
Listen on socket. More... | |
virtual Socket * | accept () |
Accept connection. More... | |
virtual void | close () |
Close socket. More... | |
virtual bool | available () |
Check if data is available. More... | |
virtual size_t | read (void *buf, size_t count, bool read_all=true) |
Read from socket. More... | |
virtual void | write (const void *buf, size_t count) |
Write to the socket. More... | |
virtual void | send (void *buf, size_t buf_len) |
Write to the socket. More... | |
virtual void | send (void *buf, size_t buf_len, const struct sockaddr *to_addr, socklen_t addr_len) |
Send message. More... | |
virtual size_t | recv (void *buf, size_t buf_len) |
Read from socket. More... | |
virtual size_t | recv (void *buf, size_t buf_len, struct sockaddr *from_addr, socklen_t *addr_len) |
Receive data. More... | |
virtual Socket * | clone ()=0 |
Clone socket. More... | |
virtual short | poll (int timeout=-1, short what=POLL_IN|POLL_HUP|POLL_PRI|POLL_RDHUP) |
Wait for some event on socket. More... | |
virtual bool | listening () |
Is socket listening for connections? More... | |
virtual unsigned int | mtu () |
Maximum Transfer Unit (MTU) of socket. More... | |
template<class SocketTypeC > | |
SocketTypeC * | accept () |
Accept connection. More... | |
Static Public Attributes | |
static const short | POLL_IN = POLLIN |
Data can be read. More... | |
static const short | POLL_OUT = POLLOUT |
Writing will not block. More... | |
static const short | POLL_PRI = POLLPRI |
There is urgent data to read (e.g., out-of-band data on TCP socket; pseudo-terminal master in packet mode has seen state change in slave). More... | |
static const short | POLL_RDHUP = 0 |
Stream socket peer closed connection, or shut down writing half of connection. More... | |
static const short | POLL_ERR = POLLERR |
Error condition. More... | |
static const short | POLL_HUP = POLLHUP |
Hang up. More... | |
static const short | POLL_NVAL = POLLNVAL |
Invalid request. More... | |
Protected Member Functions | |
Socket (SocketType sock_type, float timeout=0.f) | |
IPv4 Constructor. More... | |
Socket () | |
Constructor. More... | |
Protected Attributes | |
AddrType | addr_type |
Address type/family of socket. More... | |
int | sock_fd |
Socket file descriptor. More... | |
float | timeout |
Timeout in seconds for various operations. More... | |
struct ::sockaddr_storage * | client_addr |
Client address, set if connected. More... | |
unsigned int | client_addr_len |
length in bytes of client address. More... | |
Socket base class.
This is the base class for all sockets. You cannot use it directly but you have to use one of the derivatives like StreamSocket or DatagramSocket.
fawkes::Socket::Socket | ( | AddrType | addr_type, |
SocketType | sock_type, | ||
float | timeout = 0.f |
||
) |
Constructor similar to syscall.
This creates a new socket. This is a plain pass-through constructor to the socket() syscall. In most cases this should only be used by a derivate.
addr_type | Specify IPv4 or IPv6 |
sock_type | socket type, either TCP or UDP |
timeout | See Socket::timeout. |
SocketException | thrown if socket cannot be opened, check errno for cause |
Definition at line 163 of file socket.cpp.
fawkes::Socket::Socket | ( | Socket & | socket | ) |
Copy constructor.
socket | socket to copy |
Definition at line 220 of file socket.cpp.
References client_addr, client_addr_len, sock_fd, and timeout.
|
virtual |
|
protected |
IPv4 Constructor.
This creates a new socket. This is a plain pass-through constructor to the socket() syscall. In most cases this should only be used by a derivate.
sock_type | socket type, either TCP or UDP |
timeout | See Socket::timeout. |
SocketException | thrown if socket cannot be opened, check errno for cause |
Definition at line 191 of file socket.cpp.
|
protected |
Constructor.
Plain constructor. The socket will not be opened. This may only be called by sub-classes and you must ensure that the socket file descriptor is initialized properly.
Definition at line 210 of file socket.cpp.
|
virtual |
Accept connection.
Accepts a connection waiting in the queue.
SocketException | thrown if socket cannot accept, check errno for cause |
Definition at line 565 of file socket.cpp.
References client_addr, clone(), and sock_fd.
Referenced by fawkes::NetworkAcceptorThread::loop().
SocketTypeC * fawkes::Socket::accept | ( | ) |
|
virtual |
Check if data is available.
Use this to check if data is available on the socket for reading.
Definition at line 611 of file socket.cpp.
References sock_fd.
Referenced by firevision::FuseClient::disconnect(), fawkes::FawkesNetworkTransceiver::recv(), and firevision::FuseNetworkTransceiver::recv().
|
virtual |
Bind socket.
Can only be called on stream sockets.
port | port to bind |
SocketException | thrown if socket cannot bind, check errno for cause |
Reimplemented in fawkes::MulticastDatagramSocket, and fawkes::BroadcastDatagramSocket.
Definition at line 419 of file socket.cpp.
References addr_type, IPv4, IPv6, and sock_fd.
Referenced by bind(), and fawkes::NetworkAcceptorThread::NetworkAcceptorThread().
|
virtual |
Bind socket to a specific address.
port | port to bind |
ipaddr | textual IP address of a local interface to bind to, must match the address type passed to the constructor. |
SocketException | thrown if socket cannot bind, check errno for cause |
Reimplemented in fawkes::MulticastDatagramSocket, and fawkes::BroadcastDatagramSocket.
Definition at line 479 of file socket.cpp.
|
pure virtual |
Clone socket.
This method has to be implemented by subclass to correctly clone the instance.
Implemented in fawkes::MulticastDatagramSocket, fawkes::BroadcastDatagramSocket, fawkes::StreamSocket, and fawkes::DatagramSocket.
Referenced by accept().
|
virtual |
Close socket.
Definition at line 274 of file socket.cpp.
References sock_fd.
Referenced by connect(), firevision::FuseClient::disconnect(), firevision::FuseServerClientThread::recv(), fawkes::FawkesNetworkServerClientThread::set_clid(), Msl2010RefBoxProcessor::~Msl2010RefBoxProcessor(), and ~Socket().
|
virtual |
Connect socket.
If called for a stream socket this will connect to the remote address. If you call this on a datagram socket you will tune in to a specific sender and receiver.
hostname | hostname or textual represenation of IP address to connect to |
port | port to connect to |
SocketException | thrown if socket cannot connect, check errno for cause |
Definition at line 340 of file socket.cpp.
References close(), sock_fd, and fawkes::StringConversions::to_string().
Referenced by firevision::FuseClient::connect(), and connect().
|
virtual |
Connect socket.
If called for a stream socket this will connect to the remote address. If you call this on a datagram socket you will tune in to a specific sender and receiver.
addr_port | struct containing address and port to connect to |
SocketException | thrown if socket cannot connect, check errno for cause |
Definition at line 291 of file socket.cpp.
References connect().
|
virtual |
Connect socket.
If called for a stream socket this will connect to the remote address. If you call this on a datagram socket you will tune in to a specific sender and receiver.
addr_port | struct containing address and port to connect to |
struct_size | size of addr_port struct |
SocketException | thrown if socket cannot connect, check errno for cause |
Definition at line 305 of file socket.cpp.
References connect(), sock_fd, fawkes::time_diff_sec(), and timeout.
|
virtual |
Listen on socket.
This waits for new connections on a bound socket. The backlog is the maximum number of connections waiting for being accepted.
backlog | maximum number of waiting connections |
SocketException | thrown if socket cannot listen, check errno for cause |
Definition at line 547 of file socket.cpp.
References sock_fd.
Referenced by fawkes::NetworkAcceptorThread::NetworkAcceptorThread().
|
virtual |
Is socket listening for connections?
Definition at line 932 of file socket.cpp.
References sock_fd.
|
virtual |
Maximum Transfer Unit (MTU) of socket.
Note that this can only be retrieved of connected sockets!
Definition at line 950 of file socket.cpp.
References sock_fd.
|
virtual |
Wait for some event on socket.
timeout | timeout in miliseconds to wait. A negative value means to wait forever until an event occurs, zero means just check, don't wait. |
what | what to wait for, a bitwise OR'ed combination of POLL_IN, POLL_OUT and POLL_PRI. |
InterruptedException | thrown, if poll is interrupted by a signal |
SocketException | thrown for any other error the poll() syscall can cause, see Exception::errno() for the cause of the error. |
Definition at line 652 of file socket.cpp.
References POLL_ERR, and sock_fd.
Referenced by firevision::FuseClient::enqueue_and_wait(), fawkes::FawkesNetworkServerClientThread::loop(), firevision::FuseServerClientThread::loop(), and Msl2010RefBoxProcessor::refbox_process().
|
virtual |
Read from socket.
Read from the socket. This method can only be used on streams.
buf | buffer to write from |
count | length of buffer, number of bytes to write to stream |
read_all | setting this to true causes a call to read() loop until exactly count bytes have been read, if false it will return after the first successful read with the number of bytes available then. |
SocketException | thrown for any error during reading |
Definition at line 729 of file socket.cpp.
References sock_fd, fawkes::time_diff_sec(), and timeout.
Referenced by fawkes::FawkesNetworkTransceiver::recv(), firevision::FuseNetworkTransceiver::recv(), and Msl2010RefBoxProcessor::refbox_process().
|
virtual |
Read from socket.
Read from the socket. This method can only be used on streams. Usage of read() is recommended.
buf | buffer to read data into |
buf_len | length of buffer, number of bytes to read from stream |
SocketException | thrown if an error occurs or the other side has closed the connection. |
Definition at line 836 of file socket.cpp.
References sock_fd.
|
virtual |
Receive data.
This will use recvfrom() to read data from the socket and returns the number of bytes actually read. It will not wait until the requested number of bytes has been read. Use read() if you need this.
buf | buffer that read data shall be stored in. |
buf_len | length of buffer and number of bytes to be read |
addr | return parameter, contains address of sender |
addr_len | initially has to contain size of address, on return contains the actual bytes used. |
Definition at line 909 of file socket.cpp.
References sock_fd.
|
virtual |
Write to the socket.
Write to the socket. This method can be used on streams or on datagram sockets which have been tuned to a specific receiver by using connect(). For streams usage of write() is recommended as it is the more intuitive way to deal with a stream.
buf | buffer to write |
buf_len | length of buffer, number of bytes to write to stream |
Reimplemented in fawkes::MulticastDatagramSocket, and fawkes::BroadcastDatagramSocket.
Definition at line 816 of file socket.cpp.
References write().
Referenced by fawkes::BroadcastDatagramSocket::send(), and fawkes::MulticastDatagramSocket::send().
|
virtual |
Send message.
buf | buffer with data to send |
buf_len | length of buffer, all data will be send. |
addr | addr to send data to. |
addr_len | length of address |
Reimplemented in fawkes::MulticastDatagramSocket, and fawkes::BroadcastDatagramSocket.
Definition at line 859 of file socket.cpp.
References sock_fd, fawkes::time_diff_sec(), and timeout.
|
virtual |
Write to the socket.
Write to the socket. This method can only be used on streams.
buf | buffer to write |
count | number of bytes to write from buf |
SocketException | if the data could not be written or if a timeout occured. |
Definition at line 681 of file socket.cpp.
References sock_fd, fawkes::time_diff_sec(), and timeout.
Referenced by fawkes::FawkesNetworkTransceiver::send(), firevision::FuseNetworkTransceiver::send(), and send().
|
protected |
|
protected |
|
protected |
|
static |
Error condition.
Definition at line 73 of file socket.h.
Referenced by fawkes::FawkesNetworkServerClientThread::loop(), fawkes::FawkesNetworkClientRecvThread::loop(), poll(), and fawkes::SocketException::SocketException().
|
static |
Hang up.
Definition at line 74 of file socket.h.
Referenced by fawkes::FawkesNetworkServerClientThread::loop(), fawkes::FawkesNetworkClientRecvThread::loop(), and fawkes::SocketException::SocketException().
|
static |
Data can be read.
Definition at line 69 of file socket.h.
Referenced by fawkes::FawkesNetworkServerClientThread::loop(), fawkes::FawkesNetworkClientRecvThread::loop(), and fawkes::SocketException::SocketException().
|
static |
Invalid request.
Definition at line 75 of file socket.h.
Referenced by fawkes::SocketException::SocketException().
|
static |
Writing will not block.
Definition at line 70 of file socket.h.
Referenced by fawkes::SocketException::SocketException().
|
static |
There is urgent data to read (e.g., out-of-band data on TCP socket; pseudo-terminal master in packet mode has seen state change in slave).
Definition at line 71 of file socket.h.
Referenced by fawkes::SocketException::SocketException().
|
static |
Stream socket peer closed connection, or shut down writing half of connection.
The _GNU_SOURCE feature test macro must be defined in order to obtain this definition (since Linux 2.6.17).
Definition at line 72 of file socket.h.
Referenced by fawkes::FawkesNetworkServerClientThread::loop(), fawkes::FawkesNetworkClientRecvThread::loop(), and fawkes::SocketException::SocketException().
|
protected |
Socket file descriptor.
Definition at line 140 of file socket.h.
Referenced by accept(), available(), fawkes::BroadcastDatagramSocket::bind(), fawkes::MulticastDatagramSocket::bind(), bind(), close(), connect(), listen(), listening(), mtu(), fawkes::StreamSocket::nodelay(), poll(), read(), recv(), send(), fawkes::MulticastDatagramSocket::set_loop(), fawkes::StreamSocket::set_nodelay(), fawkes::MulticastDatagramSocket::set_ttl(), Socket(), and write().
|
protected |