Код: Выделить всё
SocketWrapperКласс Код: Выделить всё
#pragma once
#include
#include
#include
class SocketWrapper
{
private:
using SslStream = boost::asio::ssl::stream;
public:
template
boost::cobalt::promise asyncRead(uint8_t (&data)[read_length])
{
namespace asio = boost::asio;
namespace cobalt = boost::cobalt;
auto buf = asio::buffer(data, read_length);
if (isSSL()) {
co_return co_await stream().async_read_some(buf, cobalt::use_op);
}
else {
co_return co_await underlyingSocket().async_read_some(buf, cobalt::use_op);
}
}
private:
bool isSSL() const;
const boost::asio::ip::tcp::socket& underlyingSocket() const;
boost::asio::ip::tcp::socket& underlyingSocket();
const SslStream& stream() const;
SslStream& stream();
};
Код: Выделить всё
asyncReadКод: Выделить всё
cobalt::promise someRead(SocketWrapper& from)
{
try
{
uint8_t buf[8192];
while (true)
{
std::size_t n = co_await from.asyncRead(buf);
// other async operations with co_await
}
}
catch (std::exception& e)
{
std::cerr
Подробнее здесь: [url]https://stackoverflow.com/questions/79798736/using-an-analog-of-boostasioawaitable-in-boostcobalt[/url]