Fix message queueing

This commit is contained in:
badaix 2020-02-27 20:44:13 +01:00
parent c445deb95a
commit 4cb67c7960

View file

@ -128,10 +128,10 @@ void StreamSession::stop()
void StreamSession::send_next() void StreamSession::send_next()
{ {
shared_const_buffer buffer = messages_.front(); auto& buffer = messages_.front();
messages_.pop_front();
boost::asio::async_write(socket_, buffer, boost::asio::async_write(socket_, buffer,
boost::asio::bind_executor(strand_, [ this, self = shared_from_this(), buffer ](boost::system::error_code ec, std::size_t length) { boost::asio::bind_executor(strand_, [ this, self = shared_from_this(), buffer ](boost::system::error_code ec, std::size_t length) {
messages_.pop_front();
if (ec) if (ec)
{ {
LOG(ERROR, LOG_TAG) << "StreamSession write error (msg length: " << length << "): " << ec.message() << "\n"; LOG(ERROR, LOG_TAG) << "StreamSession write error (msg length: " << length << "): " << ec.message() << "\n";
@ -148,7 +148,10 @@ void StreamSession::sendAsync(shared_const_buffer const_buf, bool send_now)
{ {
strand_.post([ this, self = shared_from_this(), const_buf, send_now ]() { strand_.post([ this, self = shared_from_this(), const_buf, send_now ]() {
// delete PCM chunks that are older than the overall buffer duration // delete PCM chunks that are older than the overall buffer duration
messages_.erase(std::remove_if(messages_.begin(), messages_.end(), if (messages_.size() > 1)
{
// don't remove the first message as it might beeing sent already
messages_.erase(std::remove_if(messages_.begin() + 1, messages_.end(),
[this](const shared_const_buffer& buffer) { [this](const shared_const_buffer& buffer) {
const auto& msg = buffer.message(); const auto& msg = buffer.message();
if (!msg.is_pcm_chunk) if (!msg.is_pcm_chunk)
@ -157,6 +160,7 @@ void StreamSession::sendAsync(shared_const_buffer const_buf, bool send_now)
return (age > std::chrono::milliseconds(bufferMs_) + 100ms); return (age > std::chrono::milliseconds(bufferMs_) + 100ms);
}), }),
messages_.end()); messages_.end());
}
if (send_now) if (send_now)
messages_.push_front(const_buf); messages_.push_front(const_buf);