wibble  1.1
server.h
Go to the documentation of this file.
1 #ifndef WIBBLE_NET_SERVER_H
2 #define WIBBLE_NET_SERVER_H
3 
4 /*
5  * net/server - Network server infrastructure
6  *
7  * Copyright (C) 2010 Enrico Zini <enrico@enricozini.org>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */
23 
24 #include <string>
25 #include <vector>
26 #include <signal.h>
27 
28 namespace wibble {
29 namespace net {
30 
34 struct Server
35 {
36  // Human readable server hostname
37  std::string host;
38  // Human readable server port
39  std::string port;
40  // Type of server socket (default SOCK_STREAM)
41  int socktype;
42  // Server socket
43  int sock;
44 
45  // Saved signal handlers before accept
46  struct sigaction *old_signal_actions;
47  // Signal handlers in use during accept
48  struct sigaction *signal_actions;
49 
50  Server();
51  ~Server();
52 
53  // Bind to a given port (and optionally interface hostname)
54  void bind(const char* port, const char* host=NULL);
55 
56  // Set socket to listen, with given backlog
57  void listen(int backlog = 16);
58 
59  // Set FD_CLOEXEC option on master socket, so it does not propagate to
60  // children. The master socket is not FD_CLOEXEC by default.
61  void set_sock_cloexec();
62 };
63 
64 struct TCPServer : public Server
65 {
66  // Signals used to stop the server's accept loop
67  std::vector<int> stop_signals;
68 
69  TCPServer();
70  virtual ~TCPServer();
71 
77  int accept_loop();
78 
79  virtual void handle_client(int sock, const std::string& peer_hostname, const std::string& peer_hostaddr, const std::string& peer_port) = 0;
80 
81 protected:
82  static void signal_handler(int sig);
83  static int last_signal;
84 
85  // Initialize signal handling structures
86  void signal_setup();
87  void signal_install();
88  void signal_uninstall();
89 };
90 
91 }
92 }
93 
94 // vim:set ts=4 sw=4:
95 #endif
void bind(const char *port, const char *host=NULL)
Generic bind/listen/accept internet server.
Definition: server.h:34
std::string host
Definition: server.h:37
std::vector< int > stop_signals
Definition: server.h:67
int sock
Definition: server.h:43
std::string port
Definition: server.h:39
int socktype
Definition: server.h:41
struct sigaction * signal_actions
Definition: server.h:48
Definition: amorph.h:17
static int last_signal
Definition: server.h:83
void listen(int backlog=16)
Definition: server.h:64
struct sigaction * old_signal_actions
Definition: server.h:46