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
28namespace wibble {
29namespace net {
30
34struct 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)
42 // Server socket
43 int sock;
44
45 // Saved signal handlers before accept
47 // Signal handlers in use during accept
49
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.
62};
63
64struct TCPServer : public Server
65{
66 // Signals used to stop the server's accept loop
67 std::vector<int> stop_signals;
68
70 virtual ~TCPServer();
71
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
81protected:
82 static void signal_handler(int sig);
83 static int last_signal;
84
85 // Initialize signal handling structures
89};
90
91}
92}
93
94// vim:set ts=4 sw=4:
95#endif
Definition amorph.h:17
Definition amorph.h:30
Generic bind/listen/accept internet server.
Definition server.h:35
struct sigaction * signal_actions
Definition server.h:48
void listen(int backlog=16)
std::string host
Definition server.h:37
struct sigaction * old_signal_actions
Definition server.h:46
std::string port
Definition server.h:39
int socktype
Definition server.h:41
int sock
Definition server.h:43
void bind(const char *port, const char *host=NULL)
Definition server.h:65
static int last_signal
Definition server.h:83
static void signal_handler(int sig)
virtual void handle_client(int sock, const std::string &peer_hostname, const std::string &peer_hostaddr, const std::string &peer_port)=0
std::vector< int > stop_signals
Definition server.h:67
int accept_loop()
Loop accepting connections on the socket, until interrupted by a signal in stop_signals.