diff options
author | Matthias Schiffer <matthias@gamezock.de> | 2009-05-20 17:00:33 +0200 |
---|---|---|
committer | Matthias Schiffer <matthias@gamezock.de> | 2009-05-20 17:00:33 +0200 |
commit | 96767ff85614974ff8c31686bce19001ec5cccd2 (patch) | |
tree | e9a66ebde9e6bfd2db81011e63d56834a8b4e943 /src/Net/Connection.h | |
parent | 776377bb21ee1cfe0bcdbc000f7c6fa0be227226 (diff) | |
download | mad-96767ff85614974ff8c31686bce19001ec5cccd2.tar mad-96767ff85614974ff8c31686bce19001ec5cccd2.zip |
waitWhile-Methoden f?r Connection hinzugef?gt
Diffstat (limited to 'src/Net/Connection.h')
-rw-r--r-- | src/Net/Connection.h | 36 |
1 files changed, 29 insertions, 7 deletions
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<uint8_t>(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<boost::uint8_t> 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<boost::asio::ip::tcp::socket> 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<boost::shared_mutex> lock(connectionLock); + + while(_isConnecting()) + stateChanged.wait(lock); + } + + void waitWhileConnected() { + boost::shared_lock<boost::shared_mutex> lock(connectionLock); + + while(_isConnected()) + stateChanged.wait(lock); + } + /*const gnutls_datum_t* getCertificate() const { // TODO Thread-safeness return gnutls_certificate_get_ours(session); |