mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2026-07-25 01:52:24 +00:00
moved BoostAsyncStream to BoostStream.h
This commit is contained in:
@@ -438,63 +438,6 @@ namespace stream
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//-------------------------------------------------
|
||||
|
||||
class BoostAsyncStream // for boost::beast and boost::asio
|
||||
{
|
||||
public:
|
||||
|
||||
using executor_type = boost::asio::any_io_executor;
|
||||
|
||||
BoostAsyncStream (std::shared_ptr<Stream> stream): m_Stream (stream) {}
|
||||
|
||||
// AsyncStream
|
||||
executor_type get_executor() noexcept { return m_Stream->GetService ().get_executor(); }
|
||||
|
||||
// AsyncReadStream
|
||||
template<typename MutableBufferSequence, typename ReadHandler>
|
||||
void async_read_some(const MutableBufferSequence& bufs, ReadHandler&& handler)
|
||||
{
|
||||
size_t received = 0;
|
||||
for (auto it = boost::asio::buffer_sequence_begin (bufs); it != boost::asio::buffer_sequence_end (bufs); it++)
|
||||
{
|
||||
auto len = m_Stream->ReadSome ((uint8_t *)it->data (), it->size ());
|
||||
received += len;
|
||||
if (received < it->size ()) break;
|
||||
}
|
||||
if (received > 0) // we have some data
|
||||
handler (boost::system::error_code (), received);
|
||||
else if (bufs.size () > 0) // wait for incoming data
|
||||
m_Stream->AsyncReceive (*boost::asio::buffer_sequence_begin (bufs),
|
||||
[handler = std::move (handler)](const boost::system::error_code& ecode, size_t bytes_transferred) mutable
|
||||
{
|
||||
handler (boost::system::error_code (), bytes_transferred);
|
||||
});
|
||||
else
|
||||
handler (boost::system::error_code (), 0);
|
||||
}
|
||||
|
||||
// AsyncWriteStream
|
||||
template<typename ConstBufferSequence, typename WriteHandler>
|
||||
void async_write_some(const ConstBufferSequence& bufs, WriteHandler&& handler)
|
||||
{
|
||||
size_t sent = 0;
|
||||
for (auto it = boost::asio::buffer_sequence_begin (bufs); it != boost::asio::buffer_sequence_end (bufs); it++)
|
||||
{
|
||||
const auto& buf = *it;
|
||||
sent += buf.size ();
|
||||
m_Stream->Send ((const uint8_t *)buf.data (), buf.size ());
|
||||
// TODO: AsyncSend wiht callback for last buf, but not possible below C++23
|
||||
}
|
||||
handler (boost::system::error_code (), sent);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
std::shared_ptr<Stream> m_Stream;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* Copyright (c) 2026, The PurpleI2P Project
|
||||
*
|
||||
* This file is part of Purple i2pd project and licensed under BSD3
|
||||
*
|
||||
* See full license text in LICENSE file at top of project tree
|
||||
*/
|
||||
|
||||
#ifndef BOOST_STREAM_H_
|
||||
#define BOOST_STREAM_H_
|
||||
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/beast.hpp>
|
||||
#include "Streaming.h"
|
||||
|
||||
namespace i2p
|
||||
{
|
||||
namespace client
|
||||
{
|
||||
class BoostAsyncStream // for boost::beast and boost::asio
|
||||
{
|
||||
public:
|
||||
|
||||
using executor_type = boost::asio::any_io_executor;
|
||||
|
||||
BoostAsyncStream (std::shared_ptr<i2p::stream::Stream> stream): m_Stream (stream) {}
|
||||
|
||||
std::shared_ptr<i2p::stream::Stream> GetStream () const { return m_Stream; }
|
||||
|
||||
// AsyncStream
|
||||
executor_type get_executor() noexcept { return m_Stream->GetService ().get_executor(); }
|
||||
|
||||
// AsyncReadStream
|
||||
template<typename MutableBufferSequence, typename ReadHandler>
|
||||
void async_read_some(const MutableBufferSequence& bufs, ReadHandler&& handler)
|
||||
{
|
||||
size_t received = 0;
|
||||
for (auto it = boost::asio::buffer_sequence_begin (bufs); it != boost::asio::buffer_sequence_end (bufs); it++)
|
||||
{
|
||||
auto len = m_Stream->ReadSome ((uint8_t *)it->data (), it->size ());
|
||||
received += len;
|
||||
if (received < it->size ()) break;
|
||||
}
|
||||
if (received > 0) // we have some data
|
||||
handler (boost::system::error_code (), received);
|
||||
else if (bufs.size () > 0) // wait for incoming data
|
||||
m_Stream->AsyncReceive (*boost::asio::buffer_sequence_begin (bufs),
|
||||
[handler = std::move (handler)](const boost::system::error_code& ecode, size_t bytes_transferred) mutable
|
||||
{
|
||||
handler (boost::system::error_code (), bytes_transferred);
|
||||
});
|
||||
else
|
||||
handler (boost::system::error_code (), 0);
|
||||
}
|
||||
|
||||
// AsyncWriteStream
|
||||
template<typename ConstBufferSequence, typename WriteHandler>
|
||||
void async_write_some(const ConstBufferSequence& bufs, WriteHandler&& handler)
|
||||
{
|
||||
size_t sent = 0;
|
||||
for (auto it = boost::asio::buffer_sequence_begin (bufs); it != boost::asio::buffer_sequence_end (bufs); it++)
|
||||
{
|
||||
const auto& buf = *it;
|
||||
sent += buf.size ();
|
||||
m_Stream->Send ((const uint8_t *)buf.data (), buf.size ());
|
||||
// TODO: AsyncSend wiht callback for last buf, but not possible below C++23
|
||||
}
|
||||
handler (boost::system::error_code (), sent);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
std::shared_ptr<i2p::stream::Stream> m_Stream;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
namespace boost::beast
|
||||
{
|
||||
// for websockets over BoostAsyncStream
|
||||
template<typename TeardownHandler>
|
||||
void async_teardown (boost::beast::role_type role, i2p::client::BoostAsyncStream& stream, TeardownHandler&& handler)
|
||||
{
|
||||
auto s = stream.GetStream ();
|
||||
if (s) s->Close ();
|
||||
handler (boost::system::error_code ()); // TODO: check stream's status
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -854,7 +854,7 @@ namespace torrents
|
||||
{
|
||||
if (stream)
|
||||
{
|
||||
auto httpStream = std::make_shared<i2p::stream::BoostAsyncStream>(stream);
|
||||
auto httpStream = std::make_shared<i2p::client::BoostAsyncStream>(stream);
|
||||
boost::beast::http::async_write (*httpStream, *req,
|
||||
std::bind (&TorrentsTunnel::TrackerRequestSent, this, std::placeholders::_1, std::placeholders::_2, httpStream, torrent, req));
|
||||
}
|
||||
@@ -862,7 +862,7 @@ namespace torrents
|
||||
}
|
||||
|
||||
void TorrentsTunnel::TrackerRequestSent (const boost::beast::error_code& ecode, size_t bytes_transferred,
|
||||
std::shared_ptr<i2p::stream::BoostAsyncStream> httpStream, std::shared_ptr<Torrent> torrent,
|
||||
std::shared_ptr<i2p::client::BoostAsyncStream> httpStream, std::shared_ptr<Torrent> torrent,
|
||||
std::shared_ptr<boost::beast::http::request<boost::beast::http::string_body> > req)
|
||||
{
|
||||
if (!ecode)
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
#include "HTTP.h"
|
||||
#include "I2PService.h"
|
||||
#include "AddressBook.h"
|
||||
#include "BoostStream.h"
|
||||
|
||||
namespace i2p
|
||||
{
|
||||
@@ -201,7 +202,7 @@ namespace torrents
|
||||
|
||||
void RequestTracker (std::shared_ptr<Torrent> torrent);
|
||||
void TrackerRequestSent (const boost::beast::error_code& ecode, size_t bytes_transferred,
|
||||
std::shared_ptr<i2p::stream::BoostAsyncStream> httpStream, std::shared_ptr<Torrent> torrent,
|
||||
std::shared_ptr<i2p::client::BoostAsyncStream> httpStream, std::shared_ptr<Torrent> torrent,
|
||||
std::shared_ptr<boost::beast::http::request<boost::beast::http::string_body> > req);
|
||||
|
||||
void ScheduleTrackerRequestsCheck ();
|
||||
|
||||
Reference in New Issue
Block a user