wibble 0.1.28
|
00001 #ifndef WIBBLE_NET_SERVER_H 00002 #define WIBBLE_NET_SERVER_H 00003 00004 /* 00005 * net/server - Network server infrastructure 00006 * 00007 * Copyright (C) 2010 Enrico Zini <enrico@enricozini.org> 00008 * 00009 * This library is free software; you can redistribute it and/or 00010 * modify it under the terms of the GNU Lesser General Public 00011 * License as published by the Free Software Foundation; either 00012 * version 2.1 of the License, or (at your option) any later version. 00013 * 00014 * This library is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 * Lesser General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU Lesser General Public 00020 * License along with this library; if not, write to the Free Software 00021 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00022 */ 00023 00024 #include <string> 00025 #include <vector> 00026 #include <signal.h> 00027 00028 namespace wibble { 00029 namespace net { 00030 00034 struct Server 00035 { 00036 // Human readable server hostname 00037 std::string host; 00038 // Human readable server port 00039 std::string port; 00040 // Type of server socket (default SOCK_STREAM) 00041 int socktype; 00042 // Server socket 00043 int sock; 00044 00045 // Saved signal handlers before accept 00046 struct sigaction *old_signal_actions; 00047 // Signal handlers in use during accept 00048 struct sigaction *signal_actions; 00049 00050 Server(); 00051 ~Server(); 00052 00053 // Bind to a given port (and optionally interface hostname) 00054 void bind(const char* port, const char* host=NULL); 00055 00056 // Set socket to listen, with given backlog 00057 void listen(int backlog = 16); 00058 00059 // Set FD_CLOEXEC option on master socket, so it does not propagate to 00060 // children. The master socket is not FD_CLOEXEC by default. 00061 void set_sock_cloexec(); 00062 }; 00063 00064 struct TCPServer : public Server 00065 { 00066 // Signals used to stop the server's accept loop 00067 std::vector<int> stop_signals; 00068 00069 TCPServer(); 00070 virtual ~TCPServer(); 00071 00077 int accept_loop(); 00078 00079 virtual void handle_client(int sock, const std::string& peer_hostname, const std::string& peer_hostaddr, const std::string& peer_port) = 0; 00080 00081 protected: 00082 static void signal_handler(int sig); 00083 static int last_signal; 00084 00085 // Initialize signal handling structures 00086 void signal_setup(); 00087 void signal_install(); 00088 void signal_uninstall(); 00089 }; 00090 00091 } 00092 } 00093 00094 // vim:set ts=4 sw=4: 00095 #endif