@@ -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.
0 commit comments