wibble
1.1
wibble
net
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
wibble
Definition
amorph.h:17
signal.h
wibble::SanitizeReturn
Definition
amorph.h:30
wibble::net::Server
Generic bind/listen/accept internet server.
Definition
server.h:35
wibble::net::Server::signal_actions
struct sigaction * signal_actions
Definition
server.h:48
wibble::net::Server::listen
void listen(int backlog=16)
wibble::net::Server::host
std::string host
Definition
server.h:37
wibble::net::Server::Server
Server()
wibble::net::Server::old_signal_actions
struct sigaction * old_signal_actions
Definition
server.h:46
wibble::net::Server::set_sock_cloexec
void set_sock_cloexec()
wibble::net::Server::port
std::string port
Definition
server.h:39
wibble::net::Server::socktype
int socktype
Definition
server.h:41
wibble::net::Server::~Server
~Server()
wibble::net::Server::sock
int sock
Definition
server.h:43
wibble::net::Server::bind
void bind(const char *port, const char *host=NULL)
wibble::net::TCPServer
Definition
server.h:65
wibble::net::TCPServer::last_signal
static int last_signal
Definition
server.h:83
wibble::net::TCPServer::signal_install
void signal_install()
wibble::net::TCPServer::signal_uninstall
void signal_uninstall()
wibble::net::TCPServer::signal_handler
static void signal_handler(int sig)
wibble::net::TCPServer::signal_setup
void signal_setup()
wibble::net::TCPServer::handle_client
virtual void handle_client(int sock, const std::string &peer_hostname, const std::string &peer_hostaddr, const std::string &peer_port)=0
wibble::net::TCPServer::TCPServer
TCPServer()
wibble::net::TCPServer::~TCPServer
virtual ~TCPServer()
wibble::net::TCPServer::stop_signals
std::vector< int > stop_signals
Definition
server.h:67
wibble::net::TCPServer::accept_loop
int accept_loop()
Loop accepting connections on the socket, until interrupted by a signal in stop_signals.
Generated by
1.9.8