Added a shutdown() method to iosockstream.

This commit is contained in:
Davis King 2015-01-28 20:18:13 -05:00
parent 4b11f1ce57
commit 08711f9b91
2 changed files with 26 additions and 2 deletions

View File

@ -59,6 +59,7 @@ namespace dlib
const network_address& addr const network_address& addr
) )
{ {
auto_mutex lock(class_mutex);
close(); close();
con.reset(connect(addr)); con.reset(connect(addr));
buf.reset(new sockstreambuf(con.get())); buf.reset(new sockstreambuf(con.get()));
@ -79,6 +80,7 @@ namespace dlib
unsigned long timeout unsigned long timeout
) )
{ {
auto_mutex lock(class_mutex);
close(timeout); close(timeout);
con.reset(connect(addr.host_address, addr.port, timeout)); con.reset(connect(addr.host_address, addr.port, timeout));
buf.reset(new sockstreambuf(con.get())); buf.reset(new sockstreambuf(con.get()));
@ -91,6 +93,7 @@ namespace dlib
unsigned long timeout = 10000 unsigned long timeout = 10000
) )
{ {
auto_mutex lock(class_mutex);
rdbuf(0); rdbuf(0);
try try
{ {
@ -126,12 +129,21 @@ namespace dlib
unsigned long timeout unsigned long timeout
) )
{ {
auto_mutex lock(class_mutex);
if (con) if (con)
{ {
con_timeout.reset(new dlib::timeout(*this,&iosockstream::terminate_connection,timeout,con)); con_timeout.reset(new dlib::timeout(*this,&iosockstream::terminate_connection,timeout,con));
} }
} }
void shutdown (
)
{
auto_mutex lock(class_mutex);
if (con)
con->shutdown();
}
private: private:
void terminate_connection( void terminate_connection(
@ -142,6 +154,7 @@ namespace dlib
} }
scoped_ptr<timeout> con_timeout; scoped_ptr<timeout> con_timeout;
rmutex class_mutex;
shared_ptr_thread_safe<connection> con; shared_ptr_thread_safe<connection> con;
scoped_ptr<sockstreambuf> buf; scoped_ptr<sockstreambuf> buf;

View File

@ -22,8 +22,10 @@ namespace dlib
stream's output buffers. stream's output buffers.
THREAD SAFETY THREAD SAFETY
It is not safe to touch this object from more than one thread at a time. It is not safe for multiple threads to make concurrent accesses to the same
Therefore, you should mutex lock it if you need to do so. instance of this object (except for calls to shutdown() which are always
threadsafe). Therefore, you should mutex lock an instance of this object
if you need to touch it from multiple threads.
!*/ !*/
public: public:
@ -147,6 +149,15 @@ namespace dlib
- This function has no effect on this object. - This function has no effect on this object.
!*/ !*/
void shutdown (
);
/*!
ensures
- Immediately closes the TCP connection and causes all I/O operations on
this object to return an error.
- It is safe to call this function from any thread, therefore, you can use
it to signal when you want a connection to terminate from another thread.
!*/
}; };
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------