diff options
author | Tim Oesterreich <[email protected]> | 2019-03-06 16:34:07 +0100 |
---|---|---|
committer | Hannes Rantzsch <[email protected]> | 2019-04-16 10:48:13 +0200 |
commit | b1acdbfe4f6ed49691dc16e74bd8906bedf3c625 (patch) | |
tree | b9ce31b3c41723e28bd02847076f3495e712e4a0 /src | |
parent | e2ed1d7af6302d2d4f5da9460be85e2f99f7b6d0 (diff) |
async ops will now call operator() from their constructor -> initiating functions do not have to call async_read/write anymore
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/tls/asio/asio_async_base.h | 2 | ||||
-rw-r--r-- | src/lib/tls/asio/asio_async_handshake_op.h | 17 | ||||
-rw-r--r-- | src/lib/tls/asio/asio_async_read_op.h | 20 | ||||
-rw-r--r-- | src/lib/tls/asio/asio_async_write_op.h | 14 | ||||
-rw-r--r-- | src/lib/tls/asio/asio_stream.h | 4 |
5 files changed, 25 insertions, 32 deletions
diff --git a/src/lib/tls/asio/asio_async_base.h b/src/lib/tls/asio/asio_async_base.h index 1d41cefaa..f7410e62a 100644 --- a/src/lib/tls/asio/asio_async_base.h +++ b/src/lib/tls/asio/asio_async_base.h @@ -46,7 +46,7 @@ struct AsyncBase : boost::asio::coroutine } template<class... Args> - void invoke_now(Args&& ... args) + void complete_now(Args&& ... args) { m_handler(std::forward<Args>(args)...); m_work_guard_1.reset(); diff --git a/src/lib/tls/asio/asio_async_handshake_op.h b/src/lib/tls/asio/asio_async_handshake_op.h index eee553424..78adea386 100644 --- a/src/lib/tls/asio/asio_async_handshake_op.h +++ b/src/lib/tls/asio/asio_async_handshake_op.h @@ -39,20 +39,16 @@ struct AsyncHandshakeOperation : public AsyncBase<Handler, typename Stream::exec stream.get_executor()) , m_stream(stream) , m_core(core) - , m_ec(ec) { + this->operator()(ec, std::size_t(0), false); } AsyncHandshakeOperation(AsyncHandshakeOperation&&) = default; - using typename AsyncBase<Handler, typename Stream::executor_type, Allocator>::allocator_type; - using typename AsyncBase<Handler, typename Stream::executor_type, Allocator>::executor_type; - void operator()(boost::system::error_code ec, std::size_t bytesTransferred, bool isContinuation = true) { reenter(this) { - if(ec) { m_ec = ec; } // process tls packets from socket first if(bytesTransferred > 0) { @@ -63,12 +59,12 @@ struct AsyncHandshakeOperation : public AsyncBase<Handler, typename Stream::exec } catch(const std::exception&) { - m_ec = convertException(); + ec = convertException(); } } // send tls packets - if(m_core.hasDataToSend() && !m_ec) + if(m_core.hasDataToSend() && !ec) { // \note: we construct `AsyncWriteOperation` with 0 as its last parameter (`plainBytesTransferred`). // This operation will eventually call `*this` as its own handler, passing the 0 back to this call @@ -79,12 +75,11 @@ struct AsyncHandshakeOperation : public AsyncBase<Handler, typename Stream::exec Stream, Allocator> op{std::move(*this), m_stream, m_core, 0}; - boost::asio::async_write(m_stream.next_layer(), m_core.sendBuffer(), std::move(op)); return; } // we need more tls data from the socket - if(!m_stream.native_handle()->is_active() && !m_ec) + if(!m_stream.native_handle()->is_active() && !ec) { m_stream.next_layer().async_read_some(m_core.input_buffer, std::move(*this)); return; @@ -95,10 +90,12 @@ struct AsyncHandshakeOperation : public AsyncBase<Handler, typename Stream::exec // this 0 byte read completes immediately. `yield` causes the coroutine to reenter the function after // this read, enabling us to call the handler, while respecting asios guarantee that the handler will not // be called without an intermediate initiating function + m_ec = ec; yield m_stream.next_layer().async_read_some(boost::asio::mutable_buffer(), std::move(*this)); + ec = m_ec; } - this->invoke_now(m_ec); + this->complete_now(ec); } } diff --git a/src/lib/tls/asio/asio_async_read_op.h b/src/lib/tls/asio/asio_async_read_op.h index eaaf8ea33..5c653c2ba 100644 --- a/src/lib/tls/asio/asio_async_read_op.h +++ b/src/lib/tls/asio/asio_async_read_op.h @@ -41,21 +41,17 @@ struct AsyncReadOperation : public AsyncBase<Handler, typename Stream::executor_ , m_core(core) , m_buffers(buffers) , m_decodedBytes(0) - , m_ec(ec) { + this->operator()(ec, m_decodedBytes, false); } AsyncReadOperation(AsyncReadOperation&&) = default; - using typename AsyncBase<Handler, typename Stream::executor_type, Allocator>::allocator_type; - using typename AsyncBase<Handler, typename Stream::executor_type, Allocator>::executor_type; - void operator()(boost::system::error_code ec, std::size_t bytes_transferred, bool isContinuation = true) { reenter(this) { - if(ec) { m_ec = ec; } - if(bytes_transferred > 0 && !m_ec) + if(bytes_transferred > 0 && !ec) { boost::asio::const_buffer read_buffer{m_core.input_buffer.data(), bytes_transferred}; try @@ -65,29 +61,31 @@ struct AsyncReadOperation : public AsyncBase<Handler, typename Stream::executor_ } catch(const std::exception&) { - m_ec = convertException(); + ec = convertException(); } } - if(!m_core.hasReceivedData() && !m_ec) + if(!m_core.hasReceivedData() && !ec) { // we need more tls packets from the socket m_stream.next_layer().async_read_some(m_core.input_buffer, std::move(*this)); return; } - if(m_core.hasReceivedData() && !m_ec) + if(m_core.hasReceivedData() && !ec) { m_decodedBytes = m_core.copyReceivedData(m_buffers); - m_ec = {}; + ec = {}; } if(!isContinuation) { + m_ec = ec; yield m_stream.next_layer().async_read_some(boost::asio::mutable_buffer(), std::move(*this)); + ec = m_ec; } - this->invoke_now(m_ec, m_decodedBytes); + this->complete_now(ec, m_decodedBytes); } } diff --git a/src/lib/tls/asio/asio_async_write_op.h b/src/lib/tls/asio/asio_async_write_op.h index e8cda3e76..ac1d96857 100644 --- a/src/lib/tls/asio/asio_async_write_op.h +++ b/src/lib/tls/asio/asio_async_write_op.h @@ -39,31 +39,33 @@ struct AsyncWriteOperation : public AsyncBase<Handler, typename Stream::executor , m_stream(stream) , m_core(core) , m_plainBytesTransferred(plainBytesTransferred) - , m_ec(ec) { + this->operator()(ec, std::size_t(0), false); } AsyncWriteOperation(AsyncWriteOperation&&) = default; - using typename AsyncBase<Handler, typename Stream::executor_type, Allocator>::allocator_type; - using typename AsyncBase<Handler, typename Stream::executor_type, Allocator>::executor_type; - void operator()(boost::system::error_code ec, std::size_t bytes_transferred, bool isContinuation = true) { reenter(this) { m_core.consumeSendBuffer(bytes_transferred); - if(ec) { m_ec = ec; } + if(m_core.hasDataToSend() && !ec){ + boost::asio::async_write(m_stream.next_layer(), m_core.sendBuffer(), std::move(*this)); + return; + } if(!isContinuation) { + m_ec = ec; yield m_stream.next_layer().async_write_some(boost::asio::const_buffer(), std::move(*this)); + ec = m_ec; } // the size of the sent TLS record can differ from the size of the payload due to TLS encryption. We need to tell // the handler how many bytes of the original data we already processed. - this->invoke_now(m_ec, m_ec ? 0 : m_plainBytesTransferred); + this->complete_now(ec, ec ? 0 : m_plainBytesTransferred); } } diff --git a/src/lib/tls/asio/asio_stream.h b/src/lib/tls/asio/asio_stream.h index 7a77844a7..e52684bfa 100644 --- a/src/lib/tls/asio/asio_stream.h +++ b/src/lib/tls/asio/asio_stream.h @@ -198,7 +198,6 @@ class Stream : public StreamBase<Channel> AsyncHandshakeOperation<typename std::decay<HandshakeHandler>::type, Stream> op{std::move(init.completion_handler), *this, this->m_core}; - op({}, std::size_t(0), false); return init.result.get(); } @@ -386,7 +385,6 @@ class Stream : public StreamBase<Channel> this->m_core, std::size_t(0), Botan::TLS::convertException()}; - boost::asio::async_write(m_nextLayer, this->m_core.sendBuffer(), std::move(op)); return init.result.get(); } @@ -395,7 +393,6 @@ class Stream : public StreamBase<Channel> *this, this->m_core, sent}; - boost::asio::async_write(m_nextLayer, this->m_core.sendBuffer(), std::move(op)); return init.result.get(); } @@ -414,7 +411,6 @@ class Stream : public StreamBase<Channel> *this, this->m_core, buffers}; - op({}, std::size_t(0), false); return init.result.get(); } |