Commit a35dd6da authored by Davis King's avatar Davis King

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
parent f2cd116f
...@@ -200,11 +200,22 @@ namespace dlib ...@@ -200,11 +200,22 @@ namespace dlib
connection* con, connection* con,
unsigned long timeout unsigned long timeout
) )
{
scoped_ptr<connection> ptr(con);
close_gracefully(ptr,timeout);
}
// ----------------------------------------------------------------------------------------
void close_gracefully (
scoped_ptr<connection>& con,
unsigned long timeout
)
{ {
if(con->shutdown_outgoing()) if(con->shutdown_outgoing())
{ {
// there was an error so just close it now and return // there was an error so just close it now and return
delete con; con.reset();
return; return;
} }
...@@ -218,11 +229,11 @@ namespace dlib ...@@ -218,11 +229,11 @@ namespace dlib
} }
catch (...) catch (...)
{ {
delete con; con.reset();
throw; throw;
} }
delete con; con.reset();
} }
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include <string> #include <string>
#include "../sockets.h" #include "../sockets.h"
#include "sockets_extensions_abstract.h" #include "sockets_extensions_abstract.h"
#include "../smart_pointers.h"
namespace dlib namespace dlib
{ {
...@@ -38,6 +39,13 @@ namespace dlib ...@@ -38,6 +39,13 @@ namespace dlib
unsigned long timeout = 500 unsigned long timeout = 500
); );
// ----------------------------------------------------------------------------------------
void close_gracefully (
scoped_ptr<connection>& con,
unsigned long timeout = 500
);
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
} }
......
...@@ -44,6 +44,14 @@ namespace dlib ...@@ -44,6 +44,14 @@ namespace dlib
lastread_next(false) lastread_next(false)
{} {}
sockstreambuf_kernel_1 (
const scoped_ptr<connection>& con_
) :
con(*con_),
peek(EOF),
lastread_next(false)
{}
connection* get_connection ( connection* get_connection (
) { return &con; } ) { return &con; }
......
...@@ -35,20 +35,17 @@ namespace dlib ...@@ -35,20 +35,17 @@ namespace dlib
out_buffer(0), out_buffer(0),
in_buffer(0) in_buffer(0)
{ {
try init();
{ }
out_buffer = new char[out_buffer_size];
in_buffer = new char[in_buffer_size]; sockstreambuf_kernel_2 (
} const scoped_ptr<connection>& con_
catch (...) ) :
{ con(*con_),
if (out_buffer) delete [] out_buffer; out_buffer(0),
throw; in_buffer(0)
} {
setp(out_buffer, out_buffer + (out_buffer_size-1)); init();
setg(in_buffer+max_putback,
in_buffer+max_putback,
in_buffer+max_putback);
} }
virtual ~sockstreambuf_kernel_2 ( virtual ~sockstreambuf_kernel_2 (
...@@ -65,6 +62,25 @@ namespace dlib ...@@ -65,6 +62,25 @@ namespace dlib
protected: 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 ( int flush_out_buffer (
) )
{ {
......
...@@ -57,6 +57,18 @@ namespace dlib ...@@ -57,6 +57,18 @@ namespace dlib
- std::bad_alloc - std::bad_alloc
!*/ !*/
sockstreambuf (
const scoped_ptr<connection>& con
);
/*!
requires
- con == a valid connection object
ensures
- *this will read from and write to con
throws
- std::bad_alloc
!*/
~sockstreambuf ( ~sockstreambuf (
); );
/*! /*!
......
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