mirror of
https://github.com/badaix/snapcast.git
synced 2025-07-28 13:58:48 +02:00
Fix crash when messages are queued
This commit is contained in:
parent
fa0dbfc808
commit
2217595c6b
2 changed files with 14 additions and 7 deletions
|
@ -81,7 +81,7 @@ void ClientConnection::connect(const ResultHandler& handler)
|
||||||
}
|
}
|
||||||
LOG(NOTICE, LOG_TAG) << "Connected to " << socket_.remote_endpoint().address().to_string() << endl;
|
LOG(NOTICE, LOG_TAG) << "Connected to " << socket_.remote_endpoint().address().to_string() << endl;
|
||||||
handler(ec);
|
handler(ec);
|
||||||
// getNextMessage();
|
// getNextMessage();
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
resolver_.async_resolve(query, host_, cpt::to_string(port_), [this, handler](const boost::system::error_code& ec, tcp::resolver::results_type results) {
|
resolver_.async_resolve(query, host_, cpt::to_string(port_), [this, handler](const boost::system::error_code& ec, tcp::resolver::results_type results) {
|
||||||
|
@ -138,9 +138,14 @@ void ClientConnection::sendNext()
|
||||||
tv t;
|
tv t;
|
||||||
message.msg->sent = t;
|
message.msg->sent = t;
|
||||||
message.msg->serialize(stream);
|
message.msg->serialize(stream);
|
||||||
boost::asio::async_write(socket_, streambuf, boost::asio::bind_executor(strand_, [this](boost::system::error_code ec, std::size_t length) {
|
auto handler = message.handler;
|
||||||
std::ignore = length;
|
|
||||||
auto handler = messages_.front().handler;
|
boost::asio::async_write(socket_, streambuf, boost::asio::bind_executor(strand_, [this, handler](boost::system::error_code ec, std::size_t length) {
|
||||||
|
if (ec)
|
||||||
|
LOG(ERROR, LOG_TAG) << "Failed to send message, error: " << ec.message() << "\n";
|
||||||
|
else
|
||||||
|
LOG(TRACE, LOG_TAG) << "Wrote " << length << " bytes to socket\n";
|
||||||
|
|
||||||
messages_.pop_front();
|
messages_.pop_front();
|
||||||
if (handler)
|
if (handler)
|
||||||
handler(ec);
|
handler(ec);
|
||||||
|
@ -152,8 +157,8 @@ void ClientConnection::sendNext()
|
||||||
|
|
||||||
void ClientConnection::send(const msg::message_ptr& message, const ResultHandler& handler)
|
void ClientConnection::send(const msg::message_ptr& message, const ResultHandler& handler)
|
||||||
{
|
{
|
||||||
boost::asio::post(strand_, [this, message, handler]() {
|
strand_.post([this, message, handler]() {
|
||||||
messages_.push_back({message, handler});
|
messages_.emplace_back(message, handler);
|
||||||
if (messages_.size() > 1)
|
if (messages_.size() > 1)
|
||||||
{
|
{
|
||||||
LOG(DEBUG, LOG_TAG) << "outstanding async_write\n";
|
LOG(DEBUG, LOG_TAG) << "outstanding async_write\n";
|
||||||
|
|
|
@ -123,7 +123,6 @@ public:
|
||||||
/// @param message the message
|
/// @param message the message
|
||||||
/// @param timeout the send timeout
|
/// @param timeout the send timeout
|
||||||
/// @param handler async result handler with the response message or error
|
/// @param handler async result handler with the response message or error
|
||||||
//template <>
|
|
||||||
void sendRequest(const msg::message_ptr& message, const chronos::usec& timeout, const MessageHandler<msg::BaseMessage>& handler);
|
void sendRequest(const msg::message_ptr& message, const chronos::usec& timeout, const MessageHandler<msg::BaseMessage>& handler);
|
||||||
|
|
||||||
/// @sa sendRequest with templated response message
|
/// @sa sendRequest with templated response message
|
||||||
|
@ -161,6 +160,9 @@ protected:
|
||||||
boost::asio::io_context::strand strand_;
|
boost::asio::io_context::strand strand_;
|
||||||
struct PendingMessage
|
struct PendingMessage
|
||||||
{
|
{
|
||||||
|
PendingMessage(const msg::message_ptr& msg, ResultHandler handler) : msg(msg), handler(handler)
|
||||||
|
{
|
||||||
|
}
|
||||||
msg::message_ptr msg;
|
msg::message_ptr msg;
|
||||||
ResultHandler handler;
|
ResultHandler handler;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue