Commit e79fa3867f061c1103b3d8bf040663a2c6aecbbe
1 parent
0e4d5005
fix(connection): clear the send-queue on socket-error
Otherwise, on reconnect a whole bunch of older samples are sent out.
Showing
2 changed files
with
8 additions
and
2 deletions
src/Connection.cpp
| @@ -107,6 +107,12 @@ void TrueMQTT::Client::Impl::Connection::runRead() | @@ -107,6 +107,12 @@ void TrueMQTT::Client::Impl::Connection::runRead() | ||
| 107 | case State::SOCKET_ERROR: | 107 | case State::SOCKET_ERROR: |
| 108 | m_state = State::BACKOFF; | 108 | m_state = State::BACKOFF; |
| 109 | m_impl.connectionStateChange(false); | 109 | m_impl.connectionStateChange(false); |
| 110 | + | ||
| 111 | + // Clear send-queue, as we can't send anything anymore. | ||
| 112 | + { | ||
| 113 | + std::scoped_lock lock(m_send_queue_mutex); | ||
| 114 | + m_send_queue.clear(); | ||
| 115 | + } | ||
| 110 | break; | 116 | break; |
| 111 | 117 | ||
| 112 | case State::STOP: | 118 | case State::STOP: |
| @@ -166,7 +172,7 @@ void TrueMQTT::Client::Impl::Connection::runWrite() | @@ -166,7 +172,7 @@ void TrueMQTT::Client::Impl::Connection::runWrite() | ||
| 166 | 172 | ||
| 167 | default: | 173 | default: |
| 168 | // Sleep for a bit to avoid hogging the CPU. | 174 | // Sleep for a bit to avoid hogging the CPU. |
| 169 | - std::this_thread::sleep_for(std::chrono::milliseconds(1)); | 175 | + std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 170 | break; | 176 | break; |
| 171 | } | 177 | } |
| 172 | } | 178 | } |
src/Packet.cpp
| @@ -362,7 +362,7 @@ bool TrueMQTT::Client::Impl::Connection::sendConnect() | @@ -362,7 +362,7 @@ bool TrueMQTT::Client::Impl::Connection::sendConnect() | ||
| 362 | packet.write_string(m_impl.m_last_will_message); | 362 | packet.write_string(m_impl.m_last_will_message); |
| 363 | } | 363 | } |
| 364 | 364 | ||
| 365 | - return send(std::move(packet)); | 365 | + return send(std::move(packet), true); |
| 366 | } | 366 | } |
| 367 | 367 | ||
| 368 | bool TrueMQTT::Client::Impl::Connection::sendPingRequest() | 368 | bool TrueMQTT::Client::Impl::Connection::sendPingRequest() |