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,15 +148,19 @@ 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)
[this](const shared_const_buffer& buffer) { {
const auto& msg = buffer.message(); // don't remove the first message as it might beeing sent already
if (!msg.is_pcm_chunk) messages_.erase(std::remove_if(messages_.begin() + 1, messages_.end(),
return false; [this](const shared_const_buffer& buffer) {
auto age = chronos::clk::now() - msg.rec_time; const auto& msg = buffer.message();
return (age > std::chrono::milliseconds(bufferMs_) + 100ms); if (!msg.is_pcm_chunk)
}), return false;
messages_.end()); auto age = chronos::clk::now() - msg.rec_time;
return (age > std::chrono::milliseconds(bufferMs_) + 100ms);
}),
messages_.end());
}
if (send_now) if (send_now)
messages_.push_front(const_buf); messages_.push_front(const_buf);