1
//! Declares a type to configure new streams.
2

            
3
use tor_cell::relaycell::msg::{BeginFlags, IpVersionPreference};
4

            
5
/// A set of preferences used to declare how a new stream should be opened.
6
8
#[derive(Clone, Debug, Default)]
7
pub struct StreamParameters {
8
    /// Preferred IP version to use.
9
    ip_version: IpVersionPreference,
10
    /// True if we are requesting an optimistic stream.
11
    optimistic: bool,
12
}
13

            
14
impl StreamParameters {
15
    /// Create a new [`StreamParameters`] using default settings.
16
    pub fn new() -> Self {
17
        Self::default()
18
    }
19

            
20
    /// Configure which IP version (IPv4 or IPv6) you'd like to request,
21
    /// if you're connecting to a hostname.
22
    ///
23
    /// The default is to allow either version, but to prefer IPv4.
24
    pub fn ip_version(&mut self, preference: IpVersionPreference) -> &mut Self {
25
        self.ip_version = preference;
26
        self
27
    }
28

            
29
    /// Configure whether the stream should be opened "optimistically."
30
    ///
31
    /// By default, streams are not "optimistic". When you call
32
    /// [`ClientCirc::begin_stream()`](crate::circuit::ClientCirc::begin_stream),
33
    /// the function won't give you a stream until the exit node has
34
    /// confirmed that it has successfully opened a connection to your
35
    /// target address.  It's safer to wait in this way, but it is slower:
36
    /// it takes an entire round trip to get your confirmation.
37
    ///
38
    /// If a stream _is_ configured to be "optimistic", then
39
    /// `ClientCirc::begin_stream()` will return the stream
40
    /// immediately, without waiting for an answer from the exit.  You
41
    /// can start sending data on the stream right away, though of
42
    /// course this data will be lost if the connection is not
43
    /// actually successful.
44
    pub fn optimistic(&mut self, optimistic: bool) -> &mut Self {
45
        self.optimistic = optimistic;
46
        self
47
    }
48

            
49
    /// Crate-internal: Return true if the stream is optimistic.
50
8
    pub(crate) fn is_optimistic(&self) -> bool {
51
8
        self.optimistic
52
8
    }
53

            
54
    /// Crate-internal: Get a set of [`BeginFlags`] for this stream.
55
8
    pub(crate) fn begin_flags(&self) -> BeginFlags {
56
8
        self.ip_version.into()
57
8
    }
58
}