1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use codec::RecvError;
use frame;
use proto::*;

#[derive(Debug)]
pub(crate) struct Settings {
    /// Received SETTINGS frame pending processing. The ACK must be written to
    /// the socket first then the settings applied **before** receiving any
    /// further frames.
    pending: Option<frame::Settings>,
}

impl Settings {
    pub fn new() -> Self {
        Settings {
            pending: None,
        }
    }

    pub fn recv_settings(&mut self, frame: frame::Settings) {
        if frame.is_ack() {
            debug!("received remote settings ack");
        // TODO: handle acks
        } else {
            assert!(self.pending.is_none());
            self.pending = Some(frame);
        }
    }

    pub fn send_pending_ack<T, B, C, P>(
        &mut self,
        dst: &mut Codec<T, B>,
        streams: &mut Streams<C, P>,
    ) -> Poll<(), RecvError>
    where
        T: AsyncWrite,
        B: Buf,
        C: Buf,
        P: Peer,
    {
        trace!("send_pending_ack; pending={:?}", self.pending);

        if let Some(ref settings) = self.pending {
            if !dst.poll_ready()?.is_ready() {
                trace!("failed to send ACK");
                return Ok(Async::NotReady);
            }

            // Create an ACK settings frame
            let frame = frame::Settings::ack();

            // Buffer the settings frame
            dst.buffer(frame.into())
                .ok()
                .expect("invalid settings frame");

            trace!("ACK sent; applying settings");

            if let Some(val) = settings.max_frame_size() {
                dst.set_max_send_frame_size(val as usize);
            }

            if let Some(val) = settings.header_table_size() {
                dst.set_max_send_header_table_size(val as usize);
            }

            streams.apply_remote_settings(settings)?;
        }

        self.pending = None;

        Ok(().into())
    }
}