bap_frame_batch.h 672 B

1234567891011121314151617
  1. #pragma once
  2. #include <cstddef>
  3. namespace sunrise::server::transport {
  4. // Drain coalesced TCP frames without overwriting an unsent response. The budget
  5. // keeps one busy connection from monopolizing the server thread.
  6. template<class Peer, class Drain, class Flush>
  7. bool drain_frame_batch(Peer& peer, Drain drain, Flush flush) {
  8. for (std::size_t count = 0; count < 16 && peer.outputSize == 0; ++count) {
  9. const auto before = peer.streamSize;
  10. if (!drain(peer)) return false;
  11. if (before == peer.streamSize) break; // Incomplete frame: wait for more bytes.
  12. if (peer.outputSize != 0 && !flush(peer)) return false;
  13. }
  14. return true;
  15. }
  16. }