fn on_transmit()

in quic/s2n-quic-core/src/datagram/default.rs [466:513]


    fn on_transmit<P: Packet>(&mut self, packet: &mut P) {
        // Cede space to stream data when datagrams are not prioritized
        if packet.has_pending_streams() && !packet.datagrams_prioritized() {
            return;
        }

        self.record_capacity_stats(packet.remaining_capacity());

        let mut has_written = false;

        while packet.remaining_capacity() > 0 {
            let Some(datagram) = self.queue.pop_front() else {
                break;
            };

            // Ensure there is enough space in the packet to send a datagram
            if packet.remaining_capacity() < datagram.data.len() {
                // This check keeps us from popping all the datagrams off the
                // queue when packet space remaining is smaller than the datagram.
                if has_written {
                    self.queue.push_front(datagram);
                    break;
                }

                // the datagram is too large for the current packet and unlikely to ever fit so
                // record a metric and try the next datagram in the queue
                self.dropped_datagrams += 1;
                continue;
            }

            match packet.write_datagram(&datagram.data) {
                Ok(()) => has_written = true,
                Err(_error) => {
                    // TODO log this
                    self.dropped_datagrams += 1;
                    continue;
                }
            }
        }

        // If we now have additional capacity wake the stored waker if we have one to
        // let the application know that there is space on the queue for more datagrams.
        if self.capacity > self.queue.len() {
            if let Some(w) = self.waker.take() {
                w.wake();
            }
        }
    }