libt2n: (gerd) add ip communication
[libt2n] / src / socket_server.hxx
1 /***************************************************************************
2  *   Copyright (C) 2006 by Gerd v. Egidy                                   *
3  *   gve@intra2net.com                                                     *
4  *                                                                         *
5  *   This library is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU Lesser General Public License version   *
7  *   2.1 as published by the Free Software Foundation.                     *
8  *                                                                         *
9  *   This library is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU Lesser General Public License for more details.                   *
13  *                                                                         *
14  *   You should have received a copy of the GNU Lesser General Public      *
15  *   License along with this program; if not, write to the                 *
16  *   Free Software Foundation, Inc.,                                       *
17  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
18  ***************************************************************************/
19 #ifndef __LIBT2N_SOCKET_SERVER
20 #define __LIBT2N_SOCKET_SERVER
21
22 #include <sys/types.h>
23 #include <string>
24
25 #include "server.hxx"
26 #include "socket_handler.hxx"
27 #include "types.hxx"
28
29 namespace libt2n
30 {
31
32 class socket_server_connection;
33
34 /** @brief Socket based server class
35
36     Use this class to instantiate a server listening for client connections.
37     Call fill_buffer() to read data from the network and get_packet() to retrieve
38     this data. Don't forget to call cleanup() from time to time to remove closed
39     connections and close idle ones.
40 */
41 class socket_server : public socket_handler, public server
42 {
43     friend class socket_server_connection;
44
45     private:
46         fd_set connection_set;
47         std::string unix_path;
48
49         void start_listening();
50
51         void new_connection();
52
53         bool fill_connection_buffers();
54         void remove_connection_socket(int sock);
55
56     protected:
57         void log(log_level_values level, const std::string& message)
58             { log(level,message.c_str()); }
59         void log(log_level_values level, const char* message)
60             { server::log(level,message); }
61
62     public:
63         socket_server(int port, const std::string& ip="0.0.0.0");
64         socket_server(const std::string& path, mode_t filemode=00770, const std::string& user="", const std::string& group="");
65
66         ~socket_server();
67
68         bool fill_buffer(long long usec_timeout=-1);
69 };
70
71 /** @brief Socket based connection
72
73     This class is used within a socket_server to represent the connection to each client.
74 */
75 class socket_server_connection : public socket_handler, public server_connection
76 {
77     friend class socket_server;
78
79     private:
80         socket_server_connection(int _sock, socket_type_value _stype, int _timeout)
81            : server_connection(_timeout), socket_handler(_sock,_stype)
82            { }
83
84         void log(log_level_values level, const char* message);
85
86         void real_write(const std::string& data)
87             { socket_write(data); }
88
89     public:
90         bool fill_buffer(long long usec_timeout=-1)
91             { return socket_handler::fill_buffer(buffer,usec_timeout); }
92
93         void close();
94 };
95
96 }
97
98 #endif