From 96767ff85614974ff8c31686bce19001ec5cccd2 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Wed, 20 May 2009 17:00:33 +0200 Subject: waitWhile-Methoden f?r Connection hinzugef?gt --- src/Net/Connection.h | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) (limited to 'src/Net/Connection.h') diff --git a/src/Net/Connection.h b/src/Net/Connection.h index 303485d..625fc94 100644 --- a/src/Net/Connection.h +++ b/src/Net/Connection.h @@ -37,9 +37,14 @@ namespace Net { class ThreadManager; class Connection : boost::noncopyable { - private: + protected: friend class ThreadManager; + enum State { + DISCONNECTED, CONNECT, CONNECTED, DISCONNECT + }; + + private: class Buffer { public: Buffer(const uint8_t *data0, std::size_t length) : data(new std::vector(data0, data0+length)), buffer(boost::asio::buffer(*data)) {} @@ -55,6 +60,9 @@ class Connection : boost::noncopyable { boost::asio::const_buffer buffer; }; + boost::condition_variable_any stateChanged; + + State state; std::vector receiveBuffer; std::size_t received; @@ -86,11 +94,6 @@ class Connection : boost::noncopyable { boost::shared_mutex connectionLock; - enum State { - DISCONNECTED, CONNECT, CONNECTED, DISCONNECT - } state; - - boost::asio::ssl::stream socket; boost::asio::ip::tcp::endpoint peer; @@ -105,10 +108,15 @@ class Connection : boost::noncopyable { return (state == DISCONNECT); } + void _setState(State newState) { + state = newState; + stateChanged.notify_all(); + } + void doDisconnect(); Connection(boost::asio::ssl::context &sslContext) : - receiveBuffer(1024*1024), state(DISCONNECTED), socket(ioService, sslContext) {} + state(DISCONNECTED), receiveBuffer(1024*1024), socket(ioService, sslContext) {} public: virtual ~Connection(); @@ -128,6 +136,20 @@ class Connection : boost::noncopyable { return _isDisconnecting(); } + void waitWhileConnecting() { + boost::shared_lock lock(connectionLock); + + while(_isConnecting()) + stateChanged.wait(lock); + } + + void waitWhileConnected() { + boost::shared_lock lock(connectionLock); + + while(_isConnected()) + stateChanged.wait(lock); + } + /*const gnutls_datum_t* getCertificate() const { // TODO Thread-safeness return gnutls_certificate_get_ours(session); -- cgit v1.2.3