diff --git a/dlib/server/server_kernel.cpp b/dlib/server/server_kernel.cpp index 03ca82dbe..84ea2fab2 100644 --- a/dlib/server/server_kernel.cpp +++ b/dlib/server/server_kernel.cpp @@ -21,7 +21,8 @@ namespace dlib thread_count(0), thread_count_signaler(thread_count_mutex), max_connections(1000), - thread_count_zero(thread_count_mutex) + thread_count_zero(thread_count_mutex), + graceful_close_timeout(500) { } @@ -36,6 +37,28 @@ namespace dlib // ---------------------------------------------------------------------------------------- + unsigned long server:: + get_graceful_close_timeout ( + ) const + { + auto_mutex lock(max_connections_mutex); + return graceful_close_timeout; + } + +// ---------------------------------------------------------------------------------------- + + void server:: + set_graceful_close_timeout ( + unsigned long timeout + ) + { + auto_mutex lock(max_connections_mutex); + graceful_close_timeout = timeout; + } + +// ---------------------------------------------------------------------------------------- + + int server:: get_max_connections ( ) const @@ -85,6 +108,7 @@ namespace dlib listening_ip = ""; listening_port = 0; max_connections = 1000; + graceful_close_timeout = 500; listening_port_mutex.unlock(); listening_ip_mutex.unlock(); max_connections_mutex.unlock(); @@ -185,6 +209,7 @@ namespace dlib listening_ip = ""; listening_port = 0; max_connections = 1000; + graceful_close_timeout = 500; listening_port_mutex.unlock(); listening_ip_mutex.unlock(); max_connections_mutex.unlock(); @@ -304,7 +329,7 @@ namespace dlib try{cons.add(client_temp);} catch(...) { - sock.reset();; + sock.reset(); delete client; cons_mutex.unlock(); @@ -326,7 +351,8 @@ namespace dlib try{ temp = new param ( *this, - *client + *client, + get_graceful_close_timeout() ); } catch (...) { @@ -545,10 +571,11 @@ namespace dlib connection* temp; if (p.the_server.cons.is_member(&p.new_connection)) p.the_server.cons.remove(&p.new_connection,temp); - try{ close_gracefully(&p.new_connection); } - catch (...) { sdlog << LERROR << "close_gracefully() threw"; } p.the_server.cons_mutex.unlock(); + try{ close_gracefully(&p.new_connection, p.graceful_close_timeout); } + catch (...) { sdlog << LERROR << "close_gracefully() threw"; } + // decrement the thread count and signal if it is now zero p.the_server.thread_count_mutex.lock(); --p.the_server.thread_count; diff --git a/dlib/server/server_kernel.h b/dlib/server/server_kernel.h index 8a8cb35aa..2117e25cb 100644 --- a/dlib/server/server_kernel.h +++ b/dlib/server/server_kernel.h @@ -45,7 +45,8 @@ namespace dlib thread_count_signaler == a signaler associated with thread_count_mutex thread_count_zero == a signaler associated with thread_count_mutex max_connections == 1000 - max_connections_mutex == a mutex for max_connections + max_connections_mutex == a mutex for max_connections and graceful_close_timeout + graceful_close_timeout == 500 CONVENTION listening_port == get_listening_port() @@ -85,14 +86,17 @@ namespace dlib { param ( server& server_, - connection& new_connection_ + connection& new_connection_, + unsigned long graceful_close_timeout_ ) : the_server(server_), - new_connection(new_connection_) + new_connection(new_connection_), + graceful_close_timeout(graceful_close_timeout_) {} server& the_server; connection& new_connection; + unsigned long graceful_close_timeout; }; @@ -146,6 +150,13 @@ namespace dlib void start_async ( ); + void set_graceful_close_timeout ( + unsigned long timeout + ); + + unsigned long get_graceful_close_timeout ( + ) const; + private: void start_async_helper ( @@ -200,6 +211,7 @@ namespace dlib signaler thread_count_zero; scoped_ptr async_start_thread; scoped_ptr sock; + unsigned long graceful_close_timeout; // restricted functions diff --git a/dlib/server/server_kernel_abstract.h b/dlib/server/server_kernel_abstract.h index d8332dfce..f7860d26c 100644 --- a/dlib/server/server_kernel_abstract.h +++ b/dlib/server/server_kernel_abstract.h @@ -15,10 +15,11 @@ namespace dlib /*! INITIAL VALUE - get_listening_ip() == "" - get_listening_port() == 0 - is_running() == false - get_max_connections() == 1000 + get_listening_ip() == "" + get_listening_port() == 0 + is_running() == false + get_max_connections() == 1000 + get_graceful_close_timeout() == 500 CALLBACK FUNCTIONS @@ -242,6 +243,28 @@ namespace dlib - std::bad_alloc !*/ + void set_graceful_close_timeout ( + unsigned long timeout + ); + /*! + ensures + - #get_graceful_close_timeout() == timeout + !*/ + + unsigned long get_graceful_close_timeout ( + ) const; + /*! + ensures + - When on_connect() terminates, it will close the connection using + close_gracefully(). This is done so that any data still in the + operating system's output buffers gets a chance to be properly + transmitted to the remote host. Part of this involves waiting for + the remote host to close their end of the connection. Therefore, + get_graceful_close_timeout() returns the timeout, in milliseconds, + that we wait for the remote host to close their end of the + connection. This is the timeout value given to close_gracefully(). + !*/ + private: virtual void on_connect (