Commit 83ff1e3e authored by Davis King's avatar Davis King

Added a method to the server object that lets a user set the graceful close

timeout time to something other than the default of 500ms.
parent da72b576
...@@ -21,7 +21,8 @@ namespace dlib ...@@ -21,7 +21,8 @@ namespace dlib
thread_count(0), thread_count(0),
thread_count_signaler(thread_count_mutex), thread_count_signaler(thread_count_mutex),
max_connections(1000), 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 ...@@ -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:: int server::
get_max_connections ( get_max_connections (
) const ) const
...@@ -85,6 +108,7 @@ namespace dlib ...@@ -85,6 +108,7 @@ namespace dlib
listening_ip = ""; listening_ip = "";
listening_port = 0; listening_port = 0;
max_connections = 1000; max_connections = 1000;
graceful_close_timeout = 500;
listening_port_mutex.unlock(); listening_port_mutex.unlock();
listening_ip_mutex.unlock(); listening_ip_mutex.unlock();
max_connections_mutex.unlock(); max_connections_mutex.unlock();
...@@ -185,6 +209,7 @@ namespace dlib ...@@ -185,6 +209,7 @@ namespace dlib
listening_ip = ""; listening_ip = "";
listening_port = 0; listening_port = 0;
max_connections = 1000; max_connections = 1000;
graceful_close_timeout = 500;
listening_port_mutex.unlock(); listening_port_mutex.unlock();
listening_ip_mutex.unlock(); listening_ip_mutex.unlock();
max_connections_mutex.unlock(); max_connections_mutex.unlock();
...@@ -304,7 +329,7 @@ namespace dlib ...@@ -304,7 +329,7 @@ namespace dlib
try{cons.add(client_temp);} try{cons.add(client_temp);}
catch(...) catch(...)
{ {
sock.reset();; sock.reset();
delete client; delete client;
cons_mutex.unlock(); cons_mutex.unlock();
...@@ -326,7 +351,8 @@ namespace dlib ...@@ -326,7 +351,8 @@ namespace dlib
try{ try{
temp = new param ( temp = new param (
*this, *this,
*client *client,
get_graceful_close_timeout()
); );
} catch (...) } catch (...)
{ {
...@@ -545,10 +571,11 @@ namespace dlib ...@@ -545,10 +571,11 @@ namespace dlib
connection* temp; connection* temp;
if (p.the_server.cons.is_member(&p.new_connection)) if (p.the_server.cons.is_member(&p.new_connection))
p.the_server.cons.remove(&p.new_connection,temp); 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(); 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 // decrement the thread count and signal if it is now zero
p.the_server.thread_count_mutex.lock(); p.the_server.thread_count_mutex.lock();
--p.the_server.thread_count; --p.the_server.thread_count;
......
...@@ -45,7 +45,8 @@ namespace dlib ...@@ -45,7 +45,8 @@ namespace dlib
thread_count_signaler == a signaler associated with thread_count_mutex thread_count_signaler == a signaler associated with thread_count_mutex
thread_count_zero == a signaler associated with thread_count_mutex thread_count_zero == a signaler associated with thread_count_mutex
max_connections == 1000 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 CONVENTION
listening_port == get_listening_port() listening_port == get_listening_port()
...@@ -85,14 +86,17 @@ namespace dlib ...@@ -85,14 +86,17 @@ namespace dlib
{ {
param ( param (
server& server_, server& server_,
connection& new_connection_ connection& new_connection_,
unsigned long graceful_close_timeout_
) : ) :
the_server(server_), the_server(server_),
new_connection(new_connection_) new_connection(new_connection_),
graceful_close_timeout(graceful_close_timeout_)
{} {}
server& the_server; server& the_server;
connection& new_connection; connection& new_connection;
unsigned long graceful_close_timeout;
}; };
...@@ -146,6 +150,13 @@ namespace dlib ...@@ -146,6 +150,13 @@ namespace dlib
void start_async ( void start_async (
); );
void set_graceful_close_timeout (
unsigned long timeout
);
unsigned long get_graceful_close_timeout (
) const;
private: private:
void start_async_helper ( void start_async_helper (
...@@ -200,6 +211,7 @@ namespace dlib ...@@ -200,6 +211,7 @@ namespace dlib
signaler thread_count_zero; signaler thread_count_zero;
scoped_ptr<thread_function> async_start_thread; scoped_ptr<thread_function> async_start_thread;
scoped_ptr<listener> sock; scoped_ptr<listener> sock;
unsigned long graceful_close_timeout;
// restricted functions // restricted functions
......
...@@ -15,10 +15,11 @@ namespace dlib ...@@ -15,10 +15,11 @@ namespace dlib
/*! /*!
INITIAL VALUE INITIAL VALUE
get_listening_ip() == "" get_listening_ip() == ""
get_listening_port() == 0 get_listening_port() == 0
is_running() == false is_running() == false
get_max_connections() == 1000 get_max_connections() == 1000
get_graceful_close_timeout() == 500
CALLBACK FUNCTIONS CALLBACK FUNCTIONS
...@@ -242,6 +243,28 @@ namespace dlib ...@@ -242,6 +243,28 @@ namespace dlib
- std::bad_alloc - 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: private:
virtual void on_connect ( virtual void on_connect (
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment