Robust discarding of stale data on new TCP connection

This commit is contained in:
Joris van Rantwijk 2024-09-21 17:08:42 +02:00
parent a7802d11e3
commit 14999db4cf
1 changed files with 27 additions and 2 deletions

View File

@ -15,6 +15,7 @@
#include <iomanip> #include <iomanip>
#include <stdexcept> #include <stdexcept>
#include <system_error> #include <system_error>
#include <thread>
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include <boost/system/error_code.hpp> #include <boost/system/error_code.hpp>
@ -202,6 +203,30 @@ public:
} }
private: private:
/**
* Re-initialize DMA stream and discard stale data from the DMA buffer.
*
* Do not call this while an async_send() operation is in progress.
*/
void discard_stale_data()
{
// Re-init DMA and clear buffer.
m_dma_stream.init();
// The DMA buffer and the internal RAM buffer in the FPGA are now empty.
// But the front-end logic in the FPGA may still contain some stale
// data as well as an overflow record. These will flow into the RAM
// buffer within a microsecond. Let's wait for that.
// Sleeping in an Asio handler is bad, but 1 microsecond is so short
// that it won't hurt.
std::this_thread::sleep_for(std::chrono::microseconds(1));
// Assuming no new data is being produced, the front-end buffer is
// now clean and overflow cleared. A little data may have moved into
// the FPGA RAM buffer. Re-init DMA again to discard that.
m_dma_stream.init();
}
/** Accept completion handler. */ /** Accept completion handler. */
void handle_accept(const boost::system::error_code& error, void handle_accept(const boost::system::error_code& error,
asio::ip::tcp::socket conn) asio::ip::tcp::socket conn)
@ -281,10 +306,10 @@ private:
} }
// Clear buffer, then enable DMA stream. // Clear buffer, then enable DMA stream.
m_dma_stream.init(); discard_stale_data();
m_dma_stream.set_enabled(true); m_dma_stream.set_enabled(true);
// Try to send some data. // Prepare to send data.
transmit_data(false); transmit_data(false);
} }