У меня есть класс, который реализует чтение и написание в сокет SSL в Boost :: asio . Я также использую модель, в которой есть только один поток. Тем не менее, Boost :: asio иногда вызывает Boost_assert , как будто я вызову следующую асинхронную операцию, не ожидая завершения предыдущей. Тем не менее, мой код гарантирует, что операции чтения и записи выполняются последовательно. Отладчик указывает на этот раздел Boost :: Asio
void assign(bool& b)
{
BOOST_ASSERT(!b_);
BOOST_ASSERT(clear_);
b_ = &b;
// If this assert goes off, it means you are attempting
// to issue two of the same asynchronous I/O operation
// at the same time, without waiting for the first one
// to complete. For example, attempting two simultaneous
// calls to async_read_some. Only one pending call of
// each I/O type (read and write) is permitted.
//
BOOST_ASSERT(! *b_);
*b_ = true;
}
Специально для строки boost_assert (! *b _);
Что может быть причиной? Вот мой код < /p>
template
class io : public std::enable_shared_from_this
{
using read_callback_t = std::function;
public:
io(wrapper_t stream_wrapper) : m_stream_wrapper(std::move(stream_wrapper)) {}
void start(read_callback_t cb)
{
m_read_callback = std::move(cb);
read();
}
void write(std::string data)
{
m_write_queue.push(std::move(data));
if (m_write_queue.size() == 1)
{
write_impl();
}
}
void close()
{
auto self = this->shared_from_this();
m_stream_wrapper.get_stream().async_shutdown([this, self](const boost::system::error_code& ec)
{
m_stream_wrapper.close();
});
}
protected:
wrapper_t m_stream_wrapper;
read_callback_t m_read_callback;
std::array m_buffer;
std::queue m_write_queue;
void read()
{
auto self = this->shared_from_this();
m_stream_wrapper.get_stream().async_read_some(boost::asio::buffer(m_buffer),
[this, self](boost::system::error_code ec, std::size_t bytes_transferred)
{
if (!ec)
{
std::string received(m_buffer.data(), bytes_transferred);
m_read_callback(received);
read();
}
});
}
void write_impl()
{
auto self = this->shared_from_this();
boost::asio::async_write(m_stream_wrapper.get_stream(), boost::asio::buffer(m_write_queue.front()),
[this, self](boost::system::error_code ec, std::size_t /*length*/)
{
if (!ec)
{
m_write_queue.pop();
if (!m_write_queue.empty())
{
write_impl();
}
}
});
}
};
< /code>
stream_wrapper - просто удобная обертка вокруг SSL :: Stream . Объект io создается через make_shared , а метод начала называется один раз. async_write называется непоследовательно. Тогда почему BOOST_ASSERT происходит?class stream_wrapper
{
using stream_t = boost::beast::basic_stream;
public:
stream_wrapper(boost::asio::ssl::stream stream, boost::asio::ssl::context context)
: m_stream(std::move(stream)),
m_context(std::move(context)) {}
boost::asio::ssl::stream& get_stream()
{
return m_stream;
}
bool is_closed()
{
return m_is_closed;
}
boost::asio::ip::tcp::socket& get_socket()
{
return m_stream.next_layer().socket();
}
boost::system::error_code cancel_async_operations()
{
boost::system::error_code ec;
get_socket().cancel(ec);
return ec;
}
void close()
{
if (m_is_closed)
{
return;
}
m_is_closed = true;
boost::system::error_code ignored_ec;
get_socket().close(ignored_ec);
}
private:
boost::asio::ssl::stream m_stream;
boost::asio::ssl::context m_context;
bool m_is_closed = false;
};
Подробнее здесь: https://stackoverflow.com/questions/797 ... ent-reason
Boost_assert (! *B_); триггеры без видимой причины ⇐ C++
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Последний элемент цикла не выполняется должным образом без видимой причины.
Anonymous » » в форуме Jquery - 0 Ответы
- 19 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Последний элемент цикла не выполняется должным образом без видимой причины.
Anonymous » » в форуме Javascript - 0 Ответы
- 11 Просмотры
-
Последнее сообщение Anonymous
-