libt2n: (gerd) add ip communication
[libt2n] / src / socket_server.cpp
index 372960a..b807fdd 100644 (file)
@@ -43,12 +43,55 @@ using namespace std;
 namespace libt2n
 {
 
+/** @brief create a new tcp-based server
+    @param port tcp port you want to listen on
+    @param ip the local ip you want to listen on. "0.0.0.0" means all local ips (default).
+*/
 socket_server::socket_server(int port, const std::string& ip)
     : server(), socket_handler(0,tcp_s)
 {
-    // TODO
+    /* Create the socket. */
+    sock = socket (PF_INET, SOCK_STREAM, 0);
+    if (sock < 0)
+    {
+        string err="error opening socket: ";
+        err+=strerror(errno);
+        log(error, err);
+        throw t2n_server_error(err);
+    }
+
+    set_socket_options(sock);
+
+    /* Give the socket a name. */
+    struct sockaddr_in sockaddr;
+    sockaddr.sin_family = AF_INET;
+    sockaddr.sin_port = htons(port);
+
+    if (inet_aton(ip.c_str(),&sockaddr.sin_addr) == 0)
+    {
+        string err="failed listening on invalid ip ";
+        err+=ip;
+        log(error, err);
+        throw t2n_server_error(err);
+    }
+
+    if (bind (sock, (struct sockaddr *) &sockaddr, sizeof (sockaddr)) < 0)
+    {
+        string err="error binding socket: ";
+        err+=strerror(errno);
+        log(error, err);
+        throw t2n_server_error(err);
+    }
+
+    start_listening();
 }
 
+/** @brief create a new unix-socked-based server
+    @param path path of the socket
+    @param filemode permissions you want to open the socket with
+    @param user local username for the socket
+    @param group local groupname for the socket
+*/
 socket_server::socket_server(const std::string& path, mode_t filemode, const std::string& user, const std::string& group)
     : server(), socket_handler(0,unix_s)
 {
@@ -122,6 +165,20 @@ socket_server::socket_server(const std::string& path, mode_t filemode, const std
         }
     }
 
+    start_listening();
+}
+
+socket_server::~socket_server()
+{
+    socket_handler::close();
+
+    if (get_type()==unix_s)
+        unlink(unix_path.c_str());
+}
+
+/// start listening on a new server socket (called by the constructors)
+void socket_server::start_listening()
+{
     if (listen (sock, 5) < 0)
     {
         string err="error listening to socket: ";
@@ -135,14 +192,7 @@ socket_server::socket_server(const std::string& path, mode_t filemode, const std
     FD_SET (sock, &connection_set);
 }
 
-socket_server::~socket_server()
-{
-    socket_handler::close();
-
-    if (get_type()==unix_s)
-        unlink(unix_path.c_str());
-}
-
+/// handle a new connection from a client
 void socket_server::new_connection()
 {
     struct sockaddr_un clientname;
@@ -174,7 +224,7 @@ void socket_server::new_connection()
     return;
 }
 
-void socket_server::fill_buffer(long long usec_timeout)
+bool socket_server::fill_buffer(long long usec_timeout)
 {
     fd_set used_fdset=connection_set;
 
@@ -222,20 +272,27 @@ void socket_server::fill_buffer(long long usec_timeout)
         }
 
         // check all connections for pending data
-        fill_connection_buffers();
+        return fill_connection_buffers();
     }
 
-    return;
+    return false;
 }
 
-void socket_server::fill_connection_buffers()
+/// call fill_buffer() on all connections, called from fill_buffer()
+bool socket_server::fill_connection_buffers()
 {
+    bool data_found;
+
     std::map<unsigned int, server_connection*>::iterator ie=connections.end();
     for(std::map<unsigned int, server_connection*>::iterator i=connections.begin(); i != ie; i++)
         if (!i->second->server_connection::is_closed())
-            i->second->fill_buffer();
+            if (i->second->fill_buffer(0))
+                data_found=true;
+
+    return data_found;
 }
 
+/// remove the socket of a connection after the connection has been closed
 void socket_server::remove_connection_socket(int sock)
 {
     FD_CLR(sock, &connection_set);
@@ -251,6 +308,7 @@ void socket_server_connection::log(log_level_values level, const char* message)
     }
 }
 
+/// close this connection. complete data waiting in the buffer can still be retrieved.
 void socket_server_connection::close()
 {
     if (!server_connection::is_closed())