socket_server.hxx
Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 #ifndef __LIBT2N_SOCKET_SERVER
00023 #define __LIBT2N_SOCKET_SERVER
00024 
00025 #include <sys/types.h>
00026 #include <string>
00027 #include <set>
00028 
00029 #include "server.hxx"
00030 #include "socket_handler.hxx"
00031 #include "types.hxx"
00032 
00033 namespace libt2n
00034 {
00035 
00036 class socket_server_connection;
00037 
00045 class socket_server : public socket_handler, public server
00046 {
00047     friend class socket_server_connection;
00048 
00049     private:
00050         fd_set connection_set;
00051         std::string unix_path;
00052         std::set<int> sockets_set;
00053 
00054         void start_listening();
00055 
00056         void new_connection();
00057 
00058         bool fill_connection_buffers();
00059         void remove_connection_socket(int sock);
00060 
00061     protected:
00062         std::ostream* get_logstream(log_level_values level)
00063             { return server::get_logstream(level); }
00064 
00065     public:
00066         socket_server(int port, const std::string& ip="0.0.0.0");
00067         socket_server(const std::string& path, mode_t filemode=00770, const std::string& user="", const std::string& group="");
00068 
00069         ~socket_server();
00070 
00071         bool fill_buffer(long long usec_timeout=-1,long long* usec_timeout_remaining=NULL);
00072         std::set<int> get_sockets_set()
00073             { return sockets_set; };
00074 };
00075 
00080 class socket_server_connection : public socket_handler, public server_connection
00081 {
00082     friend class socket_server;
00083 
00084     private:
00085         socket_server_connection(int _sock, socket_type_value _stype, int _timeout)
00086            : socket_handler(_sock,_stype), server_connection(_timeout)
00087            { }
00088 
00089         ~socket_server_connection();
00090 
00091         std::ostream* get_logstream(log_level_values level)
00092             { return server_connection::get_logstream(level); }
00093 
00094         void real_write(const std::string& data)
00095             { socket_write(data); }
00096 
00097     public:
00098         bool fill_buffer(long long usec_timeout=-1,long long* usec_timeout_remaining=NULL);
00099 
00100         virtual void close();
00101 };
00102 
00103 }
00104 
00105 #endif