From 78357baca4fdb2ec421e6dbf49f33b96f3bc99d6 Mon Sep 17 00:00:00 2001 From: orignal Date: Fri, 11 Nov 2022 13:31:54 -0500 Subject: [PATCH] sync AcceptStream --- libi2pd/Streaming.cpp | 20 ++++++++++++++++++++ libi2pd/Streaming.h | 3 ++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/libi2pd/Streaming.cpp b/libi2pd/Streaming.cpp index 63cf20a2..9ba236d7 100644 --- a/libi2pd/Streaming.cpp +++ b/libi2pd/Streaming.cpp @@ -1362,6 +1362,26 @@ namespace stream acceptor (stream); } + std::shared_ptr StreamingDestination::AcceptStream (int timeout) + { + std::shared_ptr stream; + std::condition_variable streamAccept; + std::mutex streamAcceptMutex; + std::unique_lock l(streamAcceptMutex); + AcceptOnce ( + [&streamAccept, &streamAcceptMutex, &stream](std::shared_ptr s) + { + stream = s; + std::unique_lock l(streamAcceptMutex); + streamAccept.notify_all (); + }); + if (timeout) + streamAccept.wait_for (l, std::chrono::seconds (timeout)); + else + streamAccept.wait (l); + return stream; + } + void StreamingDestination::HandlePendingIncomingTimer (const boost::system::error_code& ecode) { if (ecode != boost::asio::error::operation_aborted) diff --git a/libi2pd/Streaming.h b/libi2pd/Streaming.h index 1662f0e4..105977e8 100644 --- a/libi2pd/Streaming.h +++ b/libi2pd/Streaming.h @@ -279,7 +279,8 @@ namespace stream bool IsAcceptorSet () const { return m_Acceptor != nullptr; }; void AcceptOnce (const Acceptor& acceptor); void AcceptOnceAcceptor (std::shared_ptr stream, Acceptor acceptor, Acceptor prev); - + std::shared_ptr AcceptStream (int timeout = 0); // sync + std::shared_ptr GetOwner () const { return m_Owner; }; void SetOwner (std::shared_ptr owner) { m_Owner = owner; }; uint16_t GetLocalPort () const { return m_LocalPort; };