#define __LIBT2N_SOCKET_SERVER
#include <sys/types.h>
+#include <string>
#include "server.hxx"
#include "socket_handler.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 socket_handler, server
+class socket_server : public socket_handler, public server
{
+ friend class socket_server_connection;
+
private:
fd_set connection_set;
std::string unix_path;
+ void start_listening();
+
void new_connection();
+ bool fill_connection_buffers();
+ void remove_connection_socket(int sock);
+
protected:
- void log(log_level_values level, const std::string& message)
- { log(level,message.c_str()); }
- void log(log_level_values level, const char* message)
- { server::log(level,message); }
+ 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();
- void fill_buffer(long long usec_timeout=-1);
- void fill_connection_buffers();
-
- void remove_connection_socket(int sock);
+ 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_server_connection : public socket_handler, server_connection
+class socket_server_connection : public socket_handler, public server_connection
{
friend class socket_server;
: server_connection(_timeout), socket_handler(_sock,_stype)
{ }
- void log(log_level_values level, const char* message)
- { if(my_server) my_server->log(level,message); }
+ std::ostream* get_logstream(log_level_values level)
+ { return server_connection::get_logstream(level); }
void real_write(const std::string& data)
- { socket_handler::write(data); }
+ { socket_write(data); }
public:
- void fill_buffer(long long usec_timeout=-1)
- { socket_handler::fill_buffer(buffer,usec_timeout); }
+ 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();
};