24 #ifndef __NETCOMM_SOCKET_SOCKET_H_
25 #define __NETCOMM_SOCKET_SOCKET_H_
27 #include <core/exception.h>
28 #include <core/exceptions/software.h>
30 #include <sys/socket.h>
31 #include <sys/types.h>
32 #include <netinet/in.h>
34 #include <sys/signal.h>
77 Socket(
int domain,
int type,
int protocol,
float timeout = 0.f);
81 virtual void connect(
const char *hostname,
const unsigned short int port);
82 virtual void connect(
struct sockaddr *addr_port,
unsigned int struct_size);
84 virtual void bind(
const unsigned short int port);
85 virtual void bind(
const unsigned short int port,
86 const char *hostname);
88 virtual void listen(
int backlog = 1);
93 virtual size_t read(
void *buf,
size_t count,
bool read_all =
true);
94 virtual void write(
const void *buf,
size_t count);
95 virtual void send(
void *buf,
size_t buf_len);
96 virtual void send(
void *buf,
size_t buf_len,
97 const struct sockaddr *to_addr, socklen_t addr_len);
98 virtual size_t recv(
void *buf,
size_t buf_len);
99 virtual size_t recv(
void *buf,
size_t buf_len,
100 struct sockaddr *from_addr, socklen_t *addr_len);
112 virtual unsigned int mtu();
119 template <
class SocketType>
133 template <
class SocketType>
138 if (SocketType *ts = dynamic_cast<SocketType *>(s)) {
142 throw TypeMismatchException(
"Socket types do not match");
virtual void connect(const char *hostname, const unsigned short int port)
Connect socket.
static const short POLL_ERR
Error condition.
virtual void close()
Close socket.
virtual void write(const void *buf, size_t count)
Write to the 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 ...
SocketException(const char *msg, int _errno)
Constructor.
virtual size_t recv(void *buf, size_t buf_len)
Read from socket.
static const short POLL_IN
Data can be read.
struct::sockaddr_in * client_addr
Client address, set if connected.
int _errno
Error number, should be used if the error was caused by a method that supplies errno.
unsigned int client_addr_len
length in bytes of client address.
virtual bool available()
Check if data is available.
virtual Socket * clone()=0
Clone socket.
Base class for exceptions in Fawkes.
virtual size_t read(void *buf, size_t count, bool read_all=true)
Read from socket.
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.
virtual ~Socket()
Destructor.
virtual bool listening()
Is socket listening for connections?
virtual unsigned int mtu()
Maximum Transfer Unit (MTU) of socket.
virtual short poll(int timeout=-1, short what=POLL_IN|POLL_HUP|POLL_PRI|POLL_RDHUP)
Wait for some event on socket.
int sock_fd
Socket file descriptor.
virtual void send(void *buf, size_t buf_len)
Write to the socket.
static const short POLL_HUP
Hang up.
virtual Socket * accept()
Accept connection.
static const short POLL_NVAL
Invalid request.
virtual void listen(int backlog=1)
Listen on socket.
float timeout
Timeout in seconds for various operations.
static const short POLL_OUT
Writing will not block.