fawkes::Socket Class Reference
[Fawkes Network Communication]
Socket base class.
More...
#include <netcomm/socket/socket.h>

Public Member Functions | |
| Socket (int domain, int type, int protocol, float timeout=0.f) | |
| Constructor similar to syscall. | |
| Socket (Socket &socket) | |
| Copy constructor. | |
| virtual | ~Socket () |
| Destructor. | |
| virtual void | connect (const char *hostname, const unsigned short int port) |
| Connect socket. | |
| virtual void | connect (struct sockaddr *addr_port, unsigned int struct_size) |
| Connect socket. | |
| virtual void | bind (const unsigned short int port) |
| Bind socket. | |
| virtual void | bind (const unsigned short int port, const char *hostname) |
| Bind socket to a specific address. | |
| virtual void | listen (int backlog=1) |
| Listen on socket. | |
| virtual Socket * | accept () |
| Accept connection. | |
| virtual void | close () |
| Close socket. | |
| virtual bool | available () |
| Check if data is available. | |
| virtual size_t | read (void *buf, size_t count, bool read_all=true) |
| Read from socket. | |
| virtual void | write (const void *buf, size_t count) |
| Write to the socket. | |
| virtual void | send (void *buf, size_t buf_len) |
| Write to the socket. | |
| virtual void | send (void *buf, size_t buf_len, const struct sockaddr *to_addr, socklen_t addr_len) |
| Send message. | |
| virtual size_t | recv (void *buf, size_t buf_len) |
| Read from socket. | |
| virtual size_t | recv (void *buf, size_t buf_len, struct sockaddr *from_addr, socklen_t *addr_len) |
| Receive data. | |
| virtual Socket * | clone ()=0 |
| Clone socket. | |
| virtual short | poll (int timeout=-1, short what=POLL_IN|POLL_HUP|POLL_PRI|POLL_RDHUP) |
| Wait for some event on socket. | |
| virtual bool | listening () |
| Is socket listening for connections? | |
| virtual unsigned int | mtu () |
| Maximum Transfer Unit (MTU) of socket. | |
| template<class SocketType > | |
| SocketType * | accept () |
| Accept connection. | |
Static Public Attributes | |
| static const short | POLL_IN = POLLIN |
| Data can be read. | |
| static const short | POLL_OUT = POLLOUT |
| Writing will not block. | |
| 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). | |
| static const short | POLL_RDHUP = 0 |
| Stream socket peer closed connection, or shut down writing half of connection. | |
| static const short | POLL_ERR = POLLERR |
| Error condition. | |
| static const short | POLL_HUP = POLLHUP |
| Hang up. | |
| static const short | POLL_NVAL = POLLNVAL |
| Invalid request. | |
Protected Member Functions | |
| Socket () | |
| Constructor. | |
Protected Attributes | |
| int | sock_fd |
| Socket file descriptor. | |
| float | timeout |
| Timeout in seconds for various operations. | |
| struct::sockaddr_in * | client_addr |
| Client address, set if connected. | |
| unsigned int | client_addr_len |
| length in bytes of client address. | |
Detailed Description
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.
Definition at line 43 of file socket.h.
Constructor & Destructor Documentation
| fawkes::Socket::Socket | ( | int | domain, | |
| int | type, | |||
| int | protocol, | |||
| 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.
- Parameters:
-
domain communication domain, selects the protocol type type of the sockets which specifies communication semantics protocol protocol to use, most types support only one and protocol should be 0 timeout See Socket::timeout.
- Exceptions:
-
SocketException thrown if socket cannot be opened, check errno for cause
Definition at line 155 of file socket.cpp.
References client_addr, client_addr_len, and sock_fd.
| fawkes::Socket::Socket | ( | Socket & | socket | ) |
Copy constructor.
- Parameters:
-
socket socket to copy
Definition at line 191 of file socket.cpp.
References client_addr, client_addr_len, sock_fd, and timeout.
| fawkes::Socket::~Socket | ( | ) | [virtual] |
| fawkes::Socket::Socket | ( | ) | [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 179 of file socket.cpp.
References client_addr, client_addr_len, sock_fd, and timeout.
Member Function Documentation
| SocketType* fawkes::Socket::accept | ( | ) | [inline] |
Accept connection.
This method works like accept() but it ensures that the returned socket is of the given type.
- Returns:
- socket to client
| SocketType * fawkes::Socket::accept | ( | ) | [inline, virtual] |
Accept connection.
Accepts a connection waiting in the queue.
- Returns:
- new socket used to communicate with the remote part
- Exceptions:
-
SocketException thrown if socket cannot accept, check errno for cause
Definition at line 371 of file socket.cpp.
References client_addr, client_addr_len, clone(), and sock_fd.
Referenced by fawkes::NetworkAcceptorThread::loop().
| bool fawkes::Socket::available | ( | ) | [virtual] |
Check if data is available.
Use this to check if data is available on the socket for reading.
- Returns:
- true, if data can be read, false otherwise
Definition at line 411 of file socket.cpp.
References sock_fd.
Referenced by fawkes::WorldInfoTransceiver::recv(), fawkes::FawkesNetworkTransceiver::recv(), and FuseNetworkTransceiver::recv().
| void fawkes::Socket::bind | ( | const unsigned short int | port, | |
| const char * | hostname | |||
| ) | [virtual] |
Bind socket to a specific address.
- Parameters:
-
port port to bind hostname hostname or textual IP address of a local interface to bind to.
- Exceptions:
-
SocketException thrown if socket cannot bind, check errno for cause
Definition at line 323 of file socket.cpp.
| void fawkes::Socket::bind | ( | const unsigned short int | port | ) | [virtual] |
Bind socket.
Can only be called on stream sockets.
- Parameters:
-
port port to bind
- Exceptions:
-
SocketException thrown if socket cannot bind, check errno for cause
Definition at line 298 of file socket.cpp.
References sock_fd.
Referenced by bind(), fawkes::NetworkAcceptorThread::NetworkAcceptorThread(), SplRefBoxProcessor::SplRefBoxProcessor(), and SplRefBoxRepeater::SplRefBoxRepeater().
| virtual Socket* fawkes::Socket::clone | ( | ) | [pure virtual] |
Clone socket.
This method has to be implemented by subclass to correctly clone the instance.
- Returns:
- cloned socket
Implemented in fawkes::DatagramSocket, fawkes::MulticastDatagramSocket, and fawkes::StreamSocket.
Referenced by accept().
| void fawkes::Socket::close | ( | ) | [virtual] |
Close socket.
Definition at line 219 of file socket.cpp.
References sock_fd.
Referenced by FuseServerClientThread::recv(), MidsizeRefBoxRepeater::~MidsizeRefBoxRepeater(), Msl2008RefBoxProcessor::~Msl2008RefBoxProcessor(), Msl2008RefBoxRepeater::~Msl2008RefBoxRepeater(), ~Socket(), SplRefBoxProcessor::~SplRefBoxProcessor(), and SplRefBoxRepeater::~SplRefBoxRepeater().
| void fawkes::Socket::connect | ( | struct sockaddr * | addr_port, | |
| unsigned int | struct_size | |||
| ) | [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.
- Parameters:
-
addr_port struct containing address and port to connect to struct_size size of addr_port struct
- Exceptions:
-
SocketException thrown if socket cannot connect, check errno for cause
Definition at line 237 of file socket.cpp.
References connect(), sock_fd, fawkes::time_diff_sec(), and timeout.
| void fawkes::Socket::connect | ( | const char * | hostname, | |
| const unsigned short int | port | |||
| ) | [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.
- Parameters:
-
hostname hostname or textual represenation of IP address to connect to port port to connect to
- Exceptions:
-
SocketException thrown if socket cannot connect, check errno for cause
Definition at line 270 of file socket.cpp.
References sock_fd.
Referenced by connect(), fawkes::FawkesNetworkClient::connect(), and FuseClient::connect().
| void fawkes::Socket::listen | ( | int | backlog = 1 |
) | [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.
- Parameters:
-
backlog maximum number of waiting connections
- Exceptions:
-
SocketException thrown if socket cannot listen, check errno for cause
Definition at line 357 of file socket.cpp.
References sock_fd.
Referenced by fawkes::NetworkAcceptorThread::NetworkAcceptorThread().
| bool fawkes::Socket::listening | ( | ) | [virtual] |
Is socket listening for connections?
- Returns:
- true if socket is listening for incoming connections, false otherwise
Definition at line 709 of file socket.cpp.
References sock_fd.
| unsigned int fawkes::Socket::mtu | ( | ) | [virtual] |
Maximum Transfer Unit (MTU) of socket.
Note that this can only be retrieved of connected sockets!
- Returns:
- MTU in bytes
Definition at line 727 of file socket.cpp.
References sock_fd.
| short fawkes::Socket::poll | ( | int | timeout = -1, |
|
| short | what = POLL_IN | POLL_HUP | POLL_PRI | POLL_RDHUP | |||
| ) | [virtual] |
Wait for some event on socket.
- Parameters:
-
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.
- Returns:
- Returns a flag value. Use bit-wise AND with the POLL_* constants in this class.
- Exceptions:
-
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.
- See also:
- Socket::POLL_IN
Definition at line 452 of file socket.cpp.
References POLL_ERR, and sock_fd.
Referenced by fawkes::FawkesNetworkServerClientThread::loop(), fawkes::FawkesNetworkClientRecvThread::loop(), and FuseServerClientThread::loop().
| size_t fawkes::Socket::read | ( | void * | buf, | |
| size_t | count, | |||
| bool | read_all = true | |||
| ) | [virtual] |
Read from socket.
Read from the socket. This method can only be used on streams.
- Parameters:
-
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.
- See also:
- write
- Exceptions:
-
SocketException thrown for any error during reading
Definition at line 524 of file socket.cpp.
References sock_fd, fawkes::time_diff_sec(), and timeout.
Referenced by fawkes::FawkesNetworkTransceiver::recv(), FuseNetworkTransceiver::recv(), Msl2008RefBoxProcessor::refbox_process(), MidsizeRefBoxRepeater::run(), Msl2008RefBoxRepeater::run(), and Msl2008RefBoxProcessor::run().
| size_t fawkes::Socket::recv | ( | void * | buf, | |
| size_t | buf_len, | |||
| struct sockaddr * | addr, | |||
| socklen_t * | addr_len | |||
| ) | [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.
- Parameters:
-
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 690 of file socket.cpp.
References sock_fd.
| size_t fawkes::Socket::recv | ( | void * | buf, | |
| size_t | buf_len | |||
| ) | [virtual] |
Read from socket.
Read from the socket. This method can only be used on streams. Usage of read() is recommended.
- Parameters:
-
buf buffer to read data into buf_len length of buffer, number of bytes to read from stream
- Exceptions:
-
SocketException thrown if an error occurs or the other side has closed the connection.
Definition at line 626 of file socket.cpp.
References sock_fd.
Referenced by fawkes::WorldInfoTransceiver::recv(), SplRefBoxProcessor::refbox_process(), SplRefBoxRepeater::run(), and SplRefBoxProcessor::run().
| void fawkes::Socket::send | ( | void * | buf, | |
| size_t | buf_len, | |||
| const struct sockaddr * | addr, | |||
| socklen_t | addr_len | |||
| ) | [virtual] |
Send message.
- Parameters:
-
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
Definition at line 645 of file socket.cpp.
References sock_fd, fawkes::time_diff_sec(), and timeout.
| void fawkes::Socket::send | ( | void * | buf, | |
| size_t | buf_len | |||
| ) | [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.
- Parameters:
-
buf buffer to write buf_len length of buffer, number of bytes to write to stream
- See also:
- write
Definition at line 607 of file socket.cpp.
References write().
| void fawkes::Socket::write | ( | const void * | buf, | |
| size_t | count | |||
| ) | [virtual] |
Write to the socket.
Write to the socket. This method can only be used on streams.
- Parameters:
-
buf buffer to write count number of bytes to write from buf
- Exceptions:
-
SocketException if the data could not be written or if a timeout occured.
Definition at line 481 of file socket.cpp.
References sock_fd, fawkes::time_diff_sec(), and timeout.
Referenced by send(), fawkes::FawkesNetworkTransceiver::send(), and FuseNetworkTransceiver::send().
Member Data Documentation
fawkes::Socket::client_addr [protected] |
fawkes::Socket::client_addr_len [protected] |
const short fawkes::Socket::POLL_ERR = POLLERR [static] |
Error condition.
Definition at line 51 of file socket.h.
Referenced by fawkes::FawkesNetworkServerClientThread::loop(), fawkes::FawkesNetworkClientRecvThread::loop(), and poll().
const short fawkes::Socket::POLL_HUP = POLLHUP [static] |
Hang up.
Definition at line 52 of file socket.h.
Referenced by fawkes::FawkesNetworkServerClientThread::loop(), and fawkes::FawkesNetworkClientRecvThread::loop().
const short fawkes::Socket::POLL_IN = POLLIN [static] |
Data can be read.
Definition at line 47 of file socket.h.
Referenced by fawkes::FawkesNetworkServerClientThread::loop(), and fawkes::FawkesNetworkClientRecvThread::loop().
const short fawkes::Socket::POLL_NVAL = POLLNVAL [static] |
const short fawkes::Socket::POLL_OUT = POLLOUT [static] |
const short fawkes::Socket::POLL_PRI = POLLPRI [static] |
const short fawkes::Socket::POLL_RDHUP = 0 [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 50 of file socket.h.
Referenced by fawkes::FawkesNetworkServerClientThread::loop(), and fawkes::FawkesNetworkClientRecvThread::loop().
fawkes::Socket::sock_fd [protected] |
Socket file descriptor.
Definition at line 103 of file socket.h.
Referenced by accept(), available(), bind(), fawkes::MulticastDatagramSocket::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().
fawkes::Socket::timeout [protected] |
The documentation for this class was generated from the following files:
- src/libs/netcomm/socket/socket.h
- src/libs/netcomm/socket/socket.cpp

