From a35dd6da7a5ccf4561418d097ac1491fff92faef Mon Sep 17 00:00:00 2001 From: Davis King Date: Wed, 13 Aug 2008 01:32:23 +0000 Subject: [PATCH] Added some stuff to encourage the use of scoped_ptr with the connection objects. --HG-- extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%402467 --- dlib/sockets/sockets_extensions.cpp | 17 +++++-- dlib/sockets/sockets_extensions.h | 8 ++++ dlib/sockstreambuf/sockstreambuf_kernel_1.h | 8 ++++ dlib/sockstreambuf/sockstreambuf_kernel_2.h | 44 +++++++++++++------ .../sockstreambuf_kernel_abstract.h | 12 +++++ 5 files changed, 72 insertions(+), 17 deletions(-) diff --git a/dlib/sockets/sockets_extensions.cpp b/dlib/sockets/sockets_extensions.cpp index 705a0e202..e7baa7011 100644 --- a/dlib/sockets/sockets_extensions.cpp +++ b/dlib/sockets/sockets_extensions.cpp @@ -200,11 +200,22 @@ namespace dlib connection* con, unsigned long timeout ) + { + scoped_ptr ptr(con); + close_gracefully(ptr,timeout); + } + +// ---------------------------------------------------------------------------------------- + + void close_gracefully ( + scoped_ptr& con, + unsigned long timeout + ) { if(con->shutdown_outgoing()) { // there was an error so just close it now and return - delete con; + con.reset(); return; } @@ -218,11 +229,11 @@ namespace dlib } catch (...) { - delete con; + con.reset(); throw; } - delete con; + con.reset(); } // ---------------------------------------------------------------------------------------- diff --git a/dlib/sockets/sockets_extensions.h b/dlib/sockets/sockets_extensions.h index e52be3dfd..4248f871d 100644 --- a/dlib/sockets/sockets_extensions.h +++ b/dlib/sockets/sockets_extensions.h @@ -6,6 +6,7 @@ #include #include "../sockets.h" #include "sockets_extensions_abstract.h" +#include "../smart_pointers.h" namespace dlib { @@ -38,6 +39,13 @@ namespace dlib unsigned long timeout = 500 ); +// ---------------------------------------------------------------------------------------- + + void close_gracefully ( + scoped_ptr& con, + unsigned long timeout = 500 + ); + // ---------------------------------------------------------------------------------------- } diff --git a/dlib/sockstreambuf/sockstreambuf_kernel_1.h b/dlib/sockstreambuf/sockstreambuf_kernel_1.h index 2926103d2..d882e88e3 100644 --- a/dlib/sockstreambuf/sockstreambuf_kernel_1.h +++ b/dlib/sockstreambuf/sockstreambuf_kernel_1.h @@ -44,6 +44,14 @@ namespace dlib lastread_next(false) {} + sockstreambuf_kernel_1 ( + const scoped_ptr& con_ + ) : + con(*con_), + peek(EOF), + lastread_next(false) + {} + connection* get_connection ( ) { return &con; } diff --git a/dlib/sockstreambuf/sockstreambuf_kernel_2.h b/dlib/sockstreambuf/sockstreambuf_kernel_2.h index 06a8a0d9c..25ab8af53 100644 --- a/dlib/sockstreambuf/sockstreambuf_kernel_2.h +++ b/dlib/sockstreambuf/sockstreambuf_kernel_2.h @@ -35,20 +35,17 @@ namespace dlib out_buffer(0), in_buffer(0) { - try - { - out_buffer = new char[out_buffer_size]; - in_buffer = new char[in_buffer_size]; - } - catch (...) - { - if (out_buffer) delete [] out_buffer; - throw; - } - setp(out_buffer, out_buffer + (out_buffer_size-1)); - setg(in_buffer+max_putback, - in_buffer+max_putback, - in_buffer+max_putback); + init(); + } + + sockstreambuf_kernel_2 ( + const scoped_ptr& con_ + ) : + con(*con_), + out_buffer(0), + in_buffer(0) + { + init(); } virtual ~sockstreambuf_kernel_2 ( @@ -65,6 +62,25 @@ namespace dlib protected: + void init ( + ) + { + try + { + out_buffer = new char[out_buffer_size]; + in_buffer = new char[in_buffer_size]; + } + catch (...) + { + if (out_buffer) delete [] out_buffer; + throw; + } + setp(out_buffer, out_buffer + (out_buffer_size-1)); + setg(in_buffer+max_putback, + in_buffer+max_putback, + in_buffer+max_putback); + } + int flush_out_buffer ( ) { diff --git a/dlib/sockstreambuf/sockstreambuf_kernel_abstract.h b/dlib/sockstreambuf/sockstreambuf_kernel_abstract.h index dd6a483f1..ae6c33be3 100644 --- a/dlib/sockstreambuf/sockstreambuf_kernel_abstract.h +++ b/dlib/sockstreambuf/sockstreambuf_kernel_abstract.h @@ -57,6 +57,18 @@ namespace dlib - std::bad_alloc !*/ + sockstreambuf ( + const scoped_ptr& con + ); + /*! + requires + - con == a valid connection object + ensures + - *this will read from and write to con + throws + - std::bad_alloc + !*/ + ~sockstreambuf ( ); /*!