24#ifndef _NETCOMM_SOCKET_SOCKET_H_
25#define _NETCOMM_SOCKET_SOCKET_H_
27#include <core/exception.h>
28#include <core/exceptions/software.h>
29#include <netinet/in.h>
30#include <sys/socket.h>
33#include <sys/signal.h>
93 virtual void connect(
const char *hostname,
const unsigned short int port);
94 virtual void connect(
const struct ::sockaddr_storage &addr_port);
95 virtual void connect(
const struct sockaddr *addr_port, socklen_t struct_size);
97 virtual void bind(
const unsigned short int port);
98 virtual void bind(
const unsigned short int port,
const char *ipaddr);
100 virtual void listen(
int backlog = 1);
102 virtual void close();
105 virtual size_t read(
void *buf,
size_t count,
bool read_all =
true);
106 virtual void write(
const void *buf,
size_t count);
107 virtual void send(
void *buf,
size_t buf_len);
108 virtual void send(
void *buf,
size_t buf_len,
const struct sockaddr *to_addr, socklen_t addr_len);
109 virtual size_t recv(
void *buf,
size_t buf_len);
110 virtual size_t recv(
void *buf,
size_t buf_len,
struct sockaddr *from_addr, socklen_t *addr_len);
122 virtual unsigned int mtu();
129 template <
class SocketTypeC>
143 int socket_addr_family_;
145 int socket_protocol_;
150template <
class SocketTypeC>
155 if (SocketTypeC *ts =
dynamic_cast<SocketTypeC *
>(s)) {
Base class for exceptions in Fawkes.
int _errno
Error number, should be used if the error was caused by a method that supplies errno.
SocketException(int _errno, const char *msg)
Constructor.
virtual Socket * clone()=0
Clone socket.
virtual bool available()
Check if data is available.
virtual void bind(const unsigned short int port)
Bind socket.
static const short POLL_RDHUP
Stream socket peer closed connection, or shut down writing half of connection.
int sock_fd
Socket file descriptor.
static const short POLL_NVAL
Invalid request.
AddrType
Address type specification.
@ UNSPECIFIED
Yet unknown address type.
virtual unsigned int mtu()
Maximum Transfer Unit (MTU) of socket.
virtual void connect(const char *hostname, const unsigned short int port)
Connect socket.
virtual size_t recv(void *buf, size_t buf_len)
Read from socket.
@ UDP
UDP datagram socket.
virtual size_t read(void *buf, size_t count, bool read_all=true)
Read from socket.
static const short POLL_HUP
Hang up.
virtual bool listening()
Is socket listening for connections?
Socket & operator=(Socket &socket)
Copy constructor.
static const short POLL_IN
Data can be read.
virtual void send(void *buf, size_t buf_len)
Write to the socket.
AddrType addr_type
Address type/family of socket.
static const short POLL_OUT
Writing will not block.
virtual short poll(int timeout=-1, short what=POLL_IN|POLL_HUP|POLL_PRI|POLL_RDHUP)
Wait for some event on socket.
static const short POLL_PRI
There is urgent data to read (e.g., out-of-band data on TCP socket; pseudo-terminal master in packet ...
virtual ~Socket()
Destructor.
float timeout
Timeout in seconds for various operations.
struct::sockaddr_storage * client_addr
Client address, set if connected.
virtual Socket * accept()
Accept connection.
static const short POLL_ERR
Error condition.
virtual void listen(int backlog=1)
Listen on socket.
unsigned int client_addr_len
length in bytes of client address.
virtual void close()
Close socket.
virtual void write(const void *buf, size_t count)
Write to the socket.
Fawkes library namespace.