X-Git-Url: http://developer.intra2net.com/git/?p=libt2n;a=blobdiff_plain;f=src%2Fsocket_server.hxx;h=4c928c5f020026d392d60ce64ae086b99830ad22;hp=605613682d18a8ab893ff3782106539c8ec5e1d0;hb=2f90dcbf75fbafbefde8916c6012f0023a90b527;hpb=aa499d2034964a4c125794b7e8ea768cb7471411 diff --git a/src/socket_server.hxx b/src/socket_server.hxx index 6056136..4c928c5 100644 --- a/src/socket_server.hxx +++ b/src/socket_server.hxx @@ -20,61 +20,76 @@ #define __LIBT2N_SOCKET_SERVER #include +#include #include "server.hxx" +#include "socket_handler.hxx" +#include "types.hxx" namespace libt2n { -/** - Socket based server class +class socket_server_connection; + +/** @brief Socket based server class + + Use this class to instantiate a server listening for client connections. + Call fill_buffer() to read data from the network and get_packet() to retrieve + this data. Don't forget to call cleanup() from time to time to remove closed + connections and close idle ones. */ -class socket_server : public server +class socket_server : public socket_handler, public server { - public: - enum socket_type_value { tcp_s, unix_s }; + friend class socket_server_connection; private: - int sock; fd_set connection_set; - socket_type_value socket_type; std::string unix_path; - void set_socket_options(int sock); + void start_listening(); + void new_connection(); + bool fill_connection_buffers(); + void remove_connection_socket(int sock); + + protected: + std::ostream* get_logstream(log_level_values level) + { return server::get_logstream(level); } + public: - socket_server(int port, const char* ip="0.0.0.0"); - socket_server(const char* path, mode_t filemode, const char* user="", const char* group=""); + socket_server(int port, const std::string& ip="0.0.0.0"); + socket_server(const std::string& path, mode_t filemode=00770, const std::string& user="", const std::string& group=""); ~socket_server(); - socket_type_value get_type() - { return socket_type; } - - void fill_buffer(long long usec_timeout=-1); - void fill_connection_buffers(); + bool fill_buffer(long long usec_timeout=-1,long long* usec_timeout_remaining=NULL); }; -/** - Socket based connection class +/** @brief Socket based connection + + This class is used within a socket_server to represent the connection to each client. */ -class socket_connection : public connection +class socket_server_connection : public socket_handler, public server_connection { friend class socket_server; private: - static const int recv_buffer_size=2048; + socket_server_connection(int _sock, socket_type_value _stype, int _timeout) + : server_connection(_timeout), socket_handler(_sock,_stype) + { } - int sock; + std::ostream* get_logstream(log_level_values level) + { return server_connection::get_logstream(level); } - socket_connection(int _sock, int _timeout); + void real_write(const std::string& data) + { socket_write(data); } public: + bool fill_buffer(long long usec_timeout=-1,long long* usec_timeout_remaining=NULL) + { return socket_handler::fill_buffer(buffer,usec_timeout,usec_timeout_remaining); } void close(); - void fill_buffer(fd_set &cur_fdset); - void write(const std::string& data); }; }