Skip to content

Commit dc66ca3

Browse files
committed
Add override for flush()
1 parent 3388c66 commit dc66ca3

2 files changed

Lines changed: 88 additions & 2 deletions

File tree

src/NimBLEStream.cpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,49 @@ size_t NimBLEStreamServer::write(const uint8_t* data, size_t len) {
701701
return NimBLEStream::write(data, len);
702702
}
703703

704+
/**
705+
* @brief Flush all pending TX data by attempting immediate sends.
706+
* @details This blocks while trying to drain the TX buffer. If send cannot make
707+
* progress (e.g. disconnected or persistent failure), queued TX/RX data is cleared.
708+
*/
709+
void NimBLEStreamServer::flush() {
710+
if (!m_txBuf || m_txBuf->size() == 0) {
711+
return;
712+
}
713+
714+
const uint32_t timeoutMs = static_cast<uint32_t>(std::min<unsigned long>(getTimeout(), 0xFFFFFFFFUL));
715+
const uint32_t retryDelay = std::max<uint32_t>(1, ble_npl_time_ms_to_ticks32(5));
716+
uint32_t waitStart = ble_npl_time_get();
717+
while (m_txBuf->size() > 0) {
718+
size_t before = m_txBuf->size();
719+
bool retry = send();
720+
size_t after = m_txBuf->size();
721+
722+
if (after == 0) {
723+
return;
724+
}
725+
726+
if (after < before) {
727+
waitStart = ble_npl_time_get();
728+
continue;
729+
}
730+
731+
if (retry && timeoutMs > 0) {
732+
const uint32_t elapsed = ble_npl_time_get() - waitStart;
733+
if (elapsed < ble_npl_time_ms_to_ticks32(timeoutMs)) {
734+
ble_npl_time_delay(retryDelay);
735+
continue;
736+
}
737+
}
738+
739+
m_txBuf->drop(m_txBuf->size());
740+
if (m_rxBuf) {
741+
m_rxBuf->drop(m_rxBuf->size());
742+
}
743+
return;
744+
}
745+
}
746+
704747
/**
705748
* @brief Attempt to send data from the TX buffer as BLE notifications.
706749
* @return true if a retry should be scheduled, false otherwise.
@@ -905,6 +948,49 @@ void NimBLEStreamClient::end() {
905948
NimBLEStream::end();
906949
}
907950

951+
/**
952+
* @brief Flush all pending TX data by attempting immediate writes.
953+
* @details This blocks while trying to drain the TX buffer. If send cannot make
954+
* progress (e.g. disconnected or persistent failure), queued TX/RX data is cleared.
955+
*/
956+
void NimBLEStreamClient::flush() {
957+
if (!m_txBuf || m_txBuf->size() == 0) {
958+
return;
959+
}
960+
961+
const uint32_t timeoutMs = static_cast<uint32_t>(std::min<unsigned long>(getTimeout(), 0xFFFFFFFFUL));
962+
const uint32_t retryDelay = std::max<uint32_t>(1, ble_npl_time_ms_to_ticks32(5));
963+
uint32_t waitStart = ble_npl_time_get();
964+
while (m_txBuf->size() > 0) {
965+
size_t before = m_txBuf->size();
966+
bool retry = send();
967+
size_t after = m_txBuf->size();
968+
969+
if (after == 0) {
970+
return;
971+
}
972+
973+
if (after < before) {
974+
waitStart = ble_npl_time_get();
975+
continue;
976+
}
977+
978+
if (retry && timeoutMs > 0) {
979+
const uint32_t elapsed = ble_npl_time_get() - waitStart;
980+
if (elapsed < ble_npl_time_ms_to_ticks32(timeoutMs)) {
981+
ble_npl_time_delay(retryDelay);
982+
continue;
983+
}
984+
}
985+
986+
m_txBuf->drop(m_txBuf->size());
987+
if (m_rxBuf) {
988+
m_rxBuf->drop(m_rxBuf->size());
989+
}
990+
return;
991+
}
992+
}
993+
908994
/**
909995
* @brief Write data to the stream, which will be sent as BLE writes to the remote characteristic.
910996
* @return True if a retry should be scheduled due to lack of BLE buffers, false otherwise.

src/NimBLEStream.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,6 @@ class NimBLEStream : public Stream {
8282
return write(static_cast<uint8_t>(data));
8383
}
8484

85-
virtual void flush() override {}
86-
8785
size_t availableForWrite() const;
8886

8987
// Read up to len bytes into buffer (non-blocking)
@@ -159,6 +157,7 @@ class NimBLEStreamServer : public NimBLEStream {
159157
uint16_t getPeerHandle() const { return m_charCallbacks.m_peerHandle; }
160158
void setCallbacks(NimBLECharacteristicCallbacks* pCallbacks) { m_charCallbacks.m_userCallbacks = pCallbacks; }
161159
bool ready() const override;
160+
virtual void flush() override;
162161

163162
using NimBLEStream::write; // Inherit template write overloads
164163

@@ -211,6 +210,7 @@ class NimBLEStreamClient : public NimBLEStream {
211210
void end() override;
212211
void setNotifyCallback(NimBLERemoteCharacteristic::notify_callback cb) { m_userNotifyCallback = cb; }
213212
bool ready() const override;
213+
virtual void flush() override;
214214

215215
using NimBLEStream::write; // Inherit template write overloads
216216

0 commit comments

Comments
 (0)