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
This commit is contained in:
Davis King 2008-08-13 01:32:23 +00:00
parent f2cd116fce
commit a35dd6da7a
5 changed files with 72 additions and 17 deletions

View File

@ -200,11 +200,22 @@ namespace dlib
connection* con,
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())
{
// 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();
}
// ----------------------------------------------------------------------------------------

View File

@ -6,6 +6,7 @@
#include <string>
#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<connection>& con,
unsigned long timeout = 500
);
// ----------------------------------------------------------------------------------------
}

View File

@ -44,6 +44,14 @@ namespace dlib
lastread_next(false)
{}
sockstreambuf_kernel_1 (
const scoped_ptr<connection>& con_
) :
con(*con_),
peek(EOF),
lastread_next(false)
{}
connection* get_connection (
) { return &con; }

View File

@ -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<connection>& 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 (
)
{

View File

@ -57,6 +57,18 @@ namespace dlib
- 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 (
);
/*!