| 1 | /* |
| 2 | Copyright (C) 2006 by Intra2net AG - Gerd v. Egidy |
| 3 | |
| 4 | The software in this package is distributed under the GNU General |
| 5 | Public License version 2 (with a special exception described below). |
| 6 | |
| 7 | A copy of GNU General Public License (GPL) is included in this distribution, |
| 8 | in the file COPYING.GPL. |
| 9 | |
| 10 | As a special exception, if other files instantiate templates or use macros |
| 11 | or inline functions from this file, or you compile this file and link it |
| 12 | with other works to produce a work based on this file, this file |
| 13 | does not by itself cause the resulting work to be covered |
| 14 | by the GNU General Public License. |
| 15 | |
| 16 | However the source code for this file must still be made available |
| 17 | in accordance with section (3) of the GNU General Public License. |
| 18 | |
| 19 | This exception does not invalidate any other reasons why a work based |
| 20 | on this file might be covered by the GNU General Public License. |
| 21 | */ |
| 22 | #ifndef __LIBT2N_SOCKET_SERVER |
| 23 | #define __LIBT2N_SOCKET_SERVER |
| 24 | |
| 25 | #include <sys/types.h> |
| 26 | #include <string> |
| 27 | |
| 28 | #include "server.hxx" |
| 29 | #include "socket_handler.hxx" |
| 30 | #include "types.hxx" |
| 31 | |
| 32 | namespace libt2n |
| 33 | { |
| 34 | |
| 35 | class socket_server_connection; |
| 36 | |
| 37 | /** @brief Socket based server class |
| 38 | |
| 39 | Use this class to instantiate a server listening for client connections. |
| 40 | Call fill_buffer() to read data from the network and get_packet() to retrieve |
| 41 | this data. Don't forget to call cleanup() from time to time to remove closed |
| 42 | connections and close idle ones. |
| 43 | */ |
| 44 | class socket_server : public socket_handler, public server |
| 45 | { |
| 46 | friend class socket_server_connection; |
| 47 | |
| 48 | private: |
| 49 | fd_set connection_set; |
| 50 | std::string unix_path; |
| 51 | |
| 52 | void start_listening(); |
| 53 | |
| 54 | void new_connection(); |
| 55 | |
| 56 | bool fill_connection_buffers(); |
| 57 | void remove_connection_socket(int sock); |
| 58 | |
| 59 | protected: |
| 60 | std::ostream* get_logstream(log_level_values level) |
| 61 | { return server::get_logstream(level); } |
| 62 | |
| 63 | public: |
| 64 | socket_server(int port, const std::string& ip="0.0.0.0"); |
| 65 | socket_server(const std::string& path, mode_t filemode=00770, const std::string& user="", const std::string& group=""); |
| 66 | |
| 67 | ~socket_server(); |
| 68 | |
| 69 | bool fill_buffer(long long usec_timeout=-1,long long* usec_timeout_remaining=NULL); |
| 70 | }; |
| 71 | |
| 72 | /** @brief Socket based connection |
| 73 | |
| 74 | This class is used within a socket_server to represent the connection to each client. |
| 75 | */ |
| 76 | class socket_server_connection : public socket_handler, public server_connection |
| 77 | { |
| 78 | friend class socket_server; |
| 79 | |
| 80 | private: |
| 81 | socket_server_connection(int _sock, socket_type_value _stype, int _timeout) |
| 82 | : socket_handler(_sock,_stype), server_connection(_timeout) |
| 83 | { } |
| 84 | |
| 85 | ~socket_server_connection(); |
| 86 | |
| 87 | std::ostream* get_logstream(log_level_values level) |
| 88 | { return server_connection::get_logstream(level); } |
| 89 | |
| 90 | void real_write(const std::string& data) |
| 91 | { socket_write(data); } |
| 92 | |
| 93 | public: |
| 94 | bool fill_buffer(long long usec_timeout=-1,long long* usec_timeout_remaining=NULL) |
| 95 | { return socket_handler::fill_buffer(buffer,usec_timeout,usec_timeout_remaining); } |
| 96 | |
| 97 | virtual void close(); |
| 98 | }; |
| 99 | |
| 100 | } |
| 101 | |
| 102 | #endif |