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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// MIT/Apache2 License
use super::{create_setup, AsyncConnection};
use crate::{
auth_info::AuthInfo,
auto::{xproto::Setup, AsByteSequence},
display::StaticSetup,
xid::XidGenerator,
};
use alloc::{boxed::Box, vec};
use core::{
future::Future,
iter, mem,
pin::Pin,
task::{Context, Poll},
};
use tinyvec::TinyVec;
/// Future returned by `establish_async`.
#[must_use = "futures do nothing unless polled or .awaited"]
pub enum EstablishConnectionFuture<'a, C: ?Sized> {
/// We are currently trying to resolve for the `AuthInfo`.
#[doc(hidden)]
ResolvingAuthInfo {
conn: &'a mut C,
auth_info_get_future: Pin<Box<dyn Future<Output = AuthInfo> + Send + Sync>>,
},
/// We are currently sending packets for the setup request.
#[doc(hidden)]
SendSetupRequest {
conn: &'a mut C,
bytes: TinyVec<[u8; 32]>,
},
/// We are currently reading the setup.
#[doc(hidden)]
ReadSetupBytes {
conn: &'a mut C,
buffer: TinyVec<[u8; 32]>,
cursor: usize,
initial_eight_bytes: bool,
},
/// Completed.
#[doc(hidden)]
Complete,
}
impl<'a, C: ?Sized> EstablishConnectionFuture<'a, C> {
#[inline]
pub(crate) fn run(conn: &'a mut C, auth_info: Option<AuthInfo>) -> Self {
match auth_info {
None => EstablishConnectionFuture::ResolvingAuthInfo {
conn,
auth_info_get_future: Box::pin(AuthInfo::get_async()),
},
Some(auth) => EstablishConnectionFuture::SendSetupRequest {
conn,
bytes: setup_bytes(auth),
},
}
}
}
#[inline]
fn setup_bytes(auth_info: AuthInfo) -> TinyVec<[u8; 32]> {
let setup = create_setup(auth_info);
let mut bytes: TinyVec<[u8; 32]> = iter::repeat(0).take(setup.size()).collect();
let len = setup.as_bytes(&mut bytes);
bytes.truncate(len);
bytes
}
impl<'a, C: AsyncConnection + Unpin + ?Sized> Future for EstablishConnectionFuture<'a, C> {
type Output = crate::Result<(StaticSetup, XidGenerator)>;
#[inline]
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
loop {
match mem::replace(&mut *self, EstablishConnectionFuture::Complete) {
EstablishConnectionFuture::Complete => {
panic!("Attempted to poll future after completion")
}
EstablishConnectionFuture::ResolvingAuthInfo {
conn,
mut auth_info_get_future,
} => match auth_info_get_future.as_mut().poll(cx) {
Poll::Pending => {
*self = EstablishConnectionFuture::ResolvingAuthInfo {
conn,
auth_info_get_future,
};
return Poll::Pending;
}
Poll::Ready(auth_info) => {
*self = EstablishConnectionFuture::SendSetupRequest {
conn,
bytes: setup_bytes(auth_info),
};
}
},
EstablishConnectionFuture::SendSetupRequest { conn, mut bytes } => {
let mut bytes_sent = 0;
let mut _fds = vec![];
let res = conn.poll_send_packet(&mut bytes, &mut _fds, cx, &mut bytes_sent);
bytes = bytes.split_off(bytes_sent);
match res {
Poll::Ready(Err(e)) => return Poll::Ready(Err(e)),
Poll::Pending => {
*self = EstablishConnectionFuture::SendSetupRequest { conn, bytes };
return Poll::Pending;
}
Poll::Ready(Ok(())) => {
bytes.truncate(8);
*self = EstablishConnectionFuture::ReadSetupBytes {
conn,
buffer: iter::repeat(0).take(8).collect(),
cursor: 0,
initial_eight_bytes: true,
};
}
}
}
EstablishConnectionFuture::ReadSetupBytes {
conn,
mut buffer,
mut cursor,
initial_eight_bytes,
} => {
let mut _fds = vec![];
let mut bytes_read = 0;
let res = conn.poll_read_packet(
&mut buffer[cursor..],
&mut _fds,
cx,
&mut bytes_read,
);
cursor += bytes_read;
match res {
Poll::Pending => {
*self = EstablishConnectionFuture::ReadSetupBytes {
conn,
buffer,
cursor,
initial_eight_bytes,
};
return Poll::Pending;
}
Poll::Ready(Err(e)) => return Poll::Ready(Err(e)),
Poll::Ready(Ok(())) => {
if initial_eight_bytes {
// figure out whether or not it succeeded
match buffer[0] {
0 => {
return Poll::Ready(Err(crate::BreadError::FailedToConnect))
}
2 => {
return Poll::Ready(Err(
crate::BreadError::FailedToAuthorize,
))
}
_ => (),
}
// read in the rest of the setup
let length =
u16::from_ne_bytes([buffer[6], buffer[7]]) as usize * 4;
buffer.extend(iter::repeat(0).take(length));
*self = EstablishConnectionFuture::ReadSetupBytes {
conn,
buffer,
cursor,
initial_eight_bytes: false,
};
} else {
let (setup, _) = match Setup::from_bytes(&buffer) {
Some(s) => s,
None => {
return Poll::Ready(Err(crate::BreadError::BadObjectRead(
Some("Setup"),
)))
}
};
let xid = XidGenerator::new(
setup.resource_id_base,
setup.resource_id_mask,
);
return Poll::Ready(Ok((setup, xid)));
}
}
}
}
}
}
}
}