Fawkes API  Fawkes Development Version
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
socket.h
1 
2 /***************************************************************************
3  * socket.h - Fawkes socket base class
4  *
5  * Created: Thu Nov 09 12:55:25 2006
6  * Copyright 2006 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #ifndef __NETCOMM_SOCKET_SOCKET_H_
25 #define __NETCOMM_SOCKET_SOCKET_H_
26 
27 #include <core/exception.h>
28 #include <core/exceptions/software.h>
29 
30 #include <sys/socket.h>
31 #include <sys/types.h>
32 #include <netinet/in.h>
33 // just to be safe nobody else can do it
34 #include <sys/signal.h>
35 
36 #ifdef POLL_IN
37 # undef POLL_IN
38 #endif
39 #ifdef POLL_OUT
40 # undef POLL_OUT
41 #endif
42 #ifdef POLL_PRI
43 # undef POLL_PRI
44 #endif
45 #ifdef POLL_RDHUP
46 # undef POLL_RDHUP
47 #endif
48 #ifdef POLL_ERR
49 # undef POLL_ERR
50 #endif
51 #ifdef POLL_HUP
52 # undef POLL_HUP
53 #endif
54 
55 
56 namespace fawkes {
57 
58 class SocketException : public Exception
59 {
60  public:
61  SocketException(const char *msg, int _errno);
62  SocketException(const char *msg);
63 };
64 
65 class Socket
66 {
67  public:
68 
69  static const short POLL_IN;
70  static const short POLL_OUT;
71  static const short POLL_PRI;
72  static const short POLL_RDHUP;
73  static const short POLL_ERR;
74  static const short POLL_HUP;
75  static const short POLL_NVAL;
76 
77  Socket(int domain, int type, int protocol, float timeout = 0.f);
78  Socket(Socket &socket);
79  virtual ~Socket();
80 
81  virtual void connect(const char *hostname, const unsigned short int port);
82  virtual void connect(struct sockaddr *addr_port, unsigned int struct_size);
83 
84  virtual void bind(const unsigned short int port);
85  virtual void bind(const unsigned short int port,
86  const char *hostname);
87 
88  virtual void listen(int backlog = 1);
89  virtual Socket * accept();
90  virtual void close();
91  virtual bool available();
92 
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);
101 
102  /** Clone socket.
103  * This method has to be implemented by subclass to correctly clone the instance.
104  * @return cloned socket
105  */
106  virtual Socket * clone() = 0;
107 
108  virtual short poll(int timeout = -1, short what = POLL_IN | POLL_HUP | POLL_PRI | POLL_RDHUP);
109 
110  virtual bool listening();
111 
112  virtual unsigned int mtu();
113 
114  /** Accept connection.
115  * This method works like accept() but it ensures that the returned socket is of
116  * the given type.
117  * @return socket to client
118  */
119  template <class SocketType>
120  SocketType * accept();
121 
122  protected:
123  Socket();
124 
125  int sock_fd;
126  float timeout;
127  struct ::sockaddr_in *client_addr;
128  unsigned int client_addr_len;
129 
130 };
131 
132 
133 template <class SocketType>
134 SocketType *
136 {
137  Socket *s = accept();
138  if (SocketType *ts = dynamic_cast<SocketType *>(s)) {
139  return ts;
140  } else {
141  delete s;
142  throw TypeMismatchException("Socket types do not match");
143  }
144 }
145 
146 } // end namespace fawkes
147 
148 #endif
virtual void connect(const char *hostname, const unsigned short int port)
Connect socket.
Definition: socket.cpp:270
static const short POLL_ERR
Error condition.
Definition: socket.h:73
virtual void close()
Close socket.
Definition: socket.cpp:219
virtual void write(const void *buf, size_t count)
Write to the socket.
Definition: socket.cpp:481
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 ...
Definition: socket.h:71
SocketException(const char *msg, int _errno)
Constructor.
Definition: socket.cpp:82
virtual size_t recv(void *buf, size_t buf_len)
Read from socket.
Definition: socket.cpp:628
static const short POLL_IN
Data can be read.
Definition: socket.h:69
struct::sockaddr_in * client_addr
Client address, set if connected.
Definition: socket.h:127
Socket()
Constructor.
Definition: socket.cpp:179
int _errno
Error number, should be used if the error was caused by a method that supplies errno.
Definition: exception.h:110
Socket base class.
Definition: socket.h:65
unsigned int client_addr_len
length in bytes of client address.
Definition: socket.h:128
virtual bool available()
Check if data is available.
Definition: socket.cpp:411
virtual Socket * clone()=0
Clone socket.
Base class for exceptions in Fawkes.
Definition: exception.h:36
virtual size_t read(void *buf, size_t count, bool read_all=true)
Read from socket.
Definition: socket.cpp:525
virtual void bind(const unsigned short int port)
Bind socket.
Definition: socket.cpp:298
static const short POLL_RDHUP
Stream socket peer closed connection, or shut down writing half of connection.
Definition: socket.h:72
virtual ~Socket()
Destructor.
Definition: socket.cpp:207
virtual bool listening()
Is socket listening for connections?
Definition: socket.cpp:712
virtual unsigned int mtu()
Maximum Transfer Unit (MTU) of socket.
Definition: socket.cpp:730
virtual short poll(int timeout=-1, short what=POLL_IN|POLL_HUP|POLL_PRI|POLL_RDHUP)
Wait for some event on socket.
Definition: socket.cpp:452
int sock_fd
Socket file descriptor.
Definition: socket.h:125
virtual void send(void *buf, size_t buf_len)
Write to the socket.
Definition: socket.cpp:608
static const short POLL_HUP
Hang up.
Definition: socket.h:74
virtual Socket * accept()
Accept connection.
Definition: socket.cpp:371
static const short POLL_NVAL
Invalid request.
Definition: socket.h:75
virtual void listen(int backlog=1)
Listen on socket.
Definition: socket.cpp:357
float timeout
Timeout in seconds for various operations.
Definition: socket.h:126
Socket exception.
Definition: socket.h:58
static const short POLL_OUT
Writing will not block.
Definition: socket.h:70