fn try_fill_write(&mut self) {
        if !self.write_buffer.is_empty() {
            return;
        }

        // TODO add a test to make sure we only ever send the same request once
        let requests = self
            .requests
            .iter_mut()
            .filter(|r| match r.state {
                RequestState::NotSerialized => true,
                RequestState::Serialized(_) | RequestState::Sent(_) => false,
            })
            .collect::<Vec<_>>();

        // TODO add a test to make sure we don't send empty data
        if requests.is_empty() {
            return;
        }

        // TODO make mtp itself use BytesMut to avoid copies
        let mut msg_ids = Vec::new();
        for request in requests.iter() {
            if let Some(msg_id) = self.mtp.push(&request.body) {
                msg_ids.push(msg_id);
            } else {
                break;
            }
        }
        let temp_vec = self.mtp.finalize();
        self.mtp_buffer = temp_vec[..].into();
        self.write_buffer.clear();
        self.transport
            .pack(&self.mtp_buffer, &mut self.write_buffer);

        // NOTE: we have to use the FILTERED requests, not the saved ones.
        // The key to finding this was printing the old and new state (but took ~2h to find).
        // Otherwise we will likely change from Sent to Serialized and enter an infinite loop.
        // This will very easily cause transport flood (using self, trying to upload two files at once).
        // TODO add a test for this
        requests
            .into_iter()
            .zip(msg_ids.into_iter())
            .for_each(|(req, msg_id)| {
                assert!(req.body.len() >= 4);
                let req_id =
                    u32::from_le_bytes([req.body[0], req.body[1], req.body[2], req.body[3]]);
                debug!(
                    "serialized request {:x} ({}) with {:?}",
                    req_id,
                    tl::name_for_id(req_id),
                    msg_id
                );
                req.state = RequestState::Serialized(msg_id);
            });
    }
/// Setup the write buffer for the transport, unless a write is already pending.
Setup the write buffer for the transport, unless a write is already pending.
Setup the write buffer for the transport unless a write is already pending .
fn try_fill_write(&mut self) {
        if !self.write_buffer.is_empty() {
            return;
        }
        // TODO add a test to make sure we only ever send the same request once
        let requests = self
            .requests
            .iter_mut()
            .filter(|r| match r.state {
                RequestState::NotSerialized => true,
                RequestState::Serialized(_) | RequestState::Sent(_) => false,
            })
            .collect::<Vec<_>>();
        // TODO add a test to make sure we don't send empty data
        if requests.is_empty() {
            return;
        }
        // TODO make mtp itself use BytesMut to avoid copies
        let mut msg_ids = Vec::new();
        for request in requests.iter() {
            if let Some(msg_id) = self.mtp.push(&request.body) {
                msg_ids.push(msg_id);
            } else {
                break;
            }
        }
        let temp_vec = self.mtp.finalize();
        self.mtp_buffer = temp_vec[..].into();
        self.write_buffer.clear();
        self.transport
            .pack(&self.mtp_buffer, &mut self.write_buffer);
        // NOTE: we have to use the FILTERED requests, not the saved ones.
        // The key to finding this was printing the old and new state (but took ~2h to find).
        // Otherwise we will likely change from Sent to Serialized and enter an infinite loop.
        // This will very easily cause transport flood (using self, trying to upload two files at once).
        // TODO add a test for this
        requests
            .into_iter()
            .zip(msg_ids.into_iter())
            .for_each(|(req, msg_id)| {
                assert!(req.body.len() >= 4);
                let req_id =
                    u32::from_le_bytes([req.body[0], req.body[1], req.body[2], req.body[3]]);
                debug!(
                    "serialized request {:x} ({}) with {:?}",
                    req_id,
                    tl::name_for_id(req_id),
                    msg_id
                );
                req.state = RequestState::Serialized(msg_id);
            });
    }
fn try_fill_write ( & mut self ) { if ! self . write_buffer . is_empty ( ) { return ; } // TODO add a test to make sure we only ever send the same request once let requests = self . requests . iter_mut ( ) . filter ( | r | match r . state { RequestState :: NotSerialized => true , RequestState :: Serialized ( _ ) | RequestState :: Sent ( _ ) => false , } ) . collect :: < Vec < _ > > ( ) ; // TODO add a test to make sure we don't send empty data if requests . is_empty ( ) { return ; } // TODO make mtp itself use BytesMut to avoid copies let mut msg_ids = Vec :: new ( ) ; for request in requests . iter ( ) { if let Some ( msg_id ) = self . mtp . push ( & request . body ) { msg_ids . push ( msg_id ) ; } else { break ; } } let temp_vec = self . mtp . finalize ( ) ; self . mtp_buffer = temp_vec [ .. ] . into ( ) ; self . write_buffer . clear ( ) ; self . transport . pack ( & self . mtp_buffer , & mut self . write_buffer ) ; // NOTE: we have to use the FILTERED requests, not the saved ones. // The key to finding this was printing the old and new state (but took ~2h to find). // Otherwise we will likely change from Sent to Serialized and enter an infinite loop. // This will very easily cause transport flood (using self, trying to upload two files at once). // TODO add a test for this requests . into_iter ( ) . zip ( msg_ids . into_iter ( ) ) . for_each ( | ( req , msg_id ) | { assert ! ( req body len ( ) 4 ) ; let req_id = u32 :: from_le_bytes ( [ req . body [ 0 ] , req . body [ 1 ] , req . body [ 2 ] , req . body [ 3 ] ] ) ; debug ! ( " " req_id tl name_for_id ( req_id ) msg_id ) ; req . state = RequestState :: Serialized ( msg_id ) ; } ) ; }
Setup the write buffer for the transport, unless a write is already pending.
Setup the write buffer for the transport unless a write is already pending .
&mut self
&mut self