atap 0.1.0

Threadsafe futureless async runtime for macOS
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
//! # TCP task
//! The tasks the `Tcp` constructors and a `Listener` return,
//! and everything they do once run

use crate::modules::input::{Token, token};
use crate::{
    RuntimeError,
    constants::INLINE_PAYLOAD,
    futures::{
        net::{
            address::{Target, family, from_raw, local_of, peer_of, to_raw},
            exchange::{self, Stage},
            socket::{Options, begin_connect, configure, finished_connecting, open, set_flag},
            step::{Progress, settle, wait_on},
        },
        task::{
            Nothing, Task,
            sealed::{self, Step},
        },
        tcp::connection::{Connection, Listener},
    },
    modules::{fd::Fd, int_check::IntCheck, park},
};
use std::{mem, net::SocketAddr, sync::Arc, time::Duration};

// Anything larger costs a page mapping per task
const _: () = assert!(mem::size_of::<Result<Connection, RuntimeError>>() <= INLINE_PAYLOAD);
const _: () = assert!(mem::size_of::<Result<Listener, RuntimeError>>() <= INLINE_PAYLOAD);
const _: () =
    assert!(mem::size_of::<Result<(Connection, SocketAddr), RuntimeError>>() <= INLINE_PAYLOAD);

/// Opens a connection
///
/// ## Returns
/// The connection. Each address a name looks up to is tried in
/// turn, and the error from the last one comes back if none of
/// them take
#[derive(Debug, Clone)]
#[must_use = "a task does nothing until it is run or spawned"]
pub struct ConnectTask {
    /// Where to connect
    target: Target,

    /// What the socket is set up with
    options: Options,

    /// How far this run has got
    progress: Progress<Connecting>,
}

/// How far a connect has got
#[derive(Default)]
struct Connecting {
    /// Addresses still to try, the next one last. `None` before
    /// they have been looked up
    left: Option<Vec<SocketAddr>>,

    /// A socket part way through connecting, and where to
    trying: Option<(Fd, SocketAddr)>,

    /// Why the last address tried didn't take
    failure: Option<RuntimeError>,
}

/// How a connect came back when it was started
enum Started {
    /// Connected at once, which a loopback address can do
    Connected(Fd),

    /// Under way, to be finished when the socket is writable
    Waiting(Fd),
}

impl ConnectTask {
    /// Connects to `target`
    pub(crate) fn new(target: Target) -> Self {
        Self {
            target,
            options: Options::default(),
            progress: Progress::default(),
        }
    }

    /// Sends small writes at once rather than waiting to batch them
    ///
    /// ## Returns
    /// The task. Calling it twice keeps the last
    pub fn nodelay(mut self, nodelay: bool) -> Self {
        self.options.nodelay = nodelay;
        self
    }

    /// Probes a quiet connection after `idle`, so a peer that has
    /// gone is noticed
    ///
    /// ## Returns
    /// The task. Calling it twice keeps the last
    pub fn keepalive(mut self, idle: Duration) -> Self {
        self.options.keepalive = Some(idle);
        self
    }

    /// Carries settings over from a task this one runs inside
    #[cfg_attr(not(feature = "tls"), allow(dead_code))]
    pub(crate) fn with_options(mut self, options: Options) -> Self {
        self.options = options;
        self
    }

    /// Does as much of the connect as can be done without waiting
    fn advance(&mut self) -> Result<Step<Result<Connection, RuntimeError>>, RuntimeError> {
        let state = &mut self.progress.0;

        loop {
            if let Some((fd, addr)) = state.trying.take() {
                match finished_connecting(fd.raw()) {
                    Ok(true) => return Ok(Step::Done(connected(fd, addr))),

                    Ok(false) => {
                        let step = wait_on(fd.raw(), libc::EVFILT_WRITE)?;
                        state.trying = Some((fd, addr));

                        return Ok(step);
                    }

                    // The socket goes, and the next address is tried
                    Err(error) => state.failure = Some(error),
                }

                continue;
            }

            if state.left.is_none() {
                let mut found = self.target.resolve()?;

                // Popped from the end, so reversed to keep the order
                found.reverse();
                state.left = Some(found);
            }

            let Some(addr) = state.left.as_mut().and_then(Vec::pop) else {
                return Err(state.failure.take().unwrap_or(RuntimeError::BadAddress));
            };

            match start_connect(&addr, &self.options) {
                Ok(Started::Connected(fd)) => return Ok(Step::Done(connected(fd, addr))),
                Ok(Started::Waiting(fd)) => state.trying = Some((fd, addr)),
                Err(error) => state.failure = Some(error),
            }
        }
    }
}

/// Starts a connect on a fresh socket
fn start_connect(addr: &SocketAddr, options: &Options) -> Result<Started, RuntimeError> {
    let fd = open(family(addr), libc::SOCK_STREAM)?;

    options.apply(fd.raw(), addr.is_ipv6())?;

    let (raw, len) = to_raw(addr);

    let at_once = begin_connect(
        fd.raw(),
        (&raw as *const libc::sockaddr_storage).cast::<libc::sockaddr>(),
        len,
    )?;

    match at_once {
        true => Ok(Started::Connected(fd)),
        false => Ok(Started::Waiting(fd)),
    }
}

/// Wraps a socket that has finished connecting
fn connected(fd: Fd, peer: SocketAddr) -> Result<Connection, RuntimeError> {
    let local = local_of(fd.raw())?;

    Ok(Connection::new(fd, local, peer))
}

/// Opens a socket that waits for connections
///
/// ## Returns
/// The listener, bound to the first address that takes
#[derive(Debug, Clone)]
#[must_use = "a task does nothing until it is run or spawned"]
pub struct ListenTask {
    /// Where to listen
    target: Target,

    /// What the socket is set up with
    options: Options,
}

impl ListenTask {
    /// Listens on `target`
    pub(crate) fn new(target: Target) -> Self {
        Self {
            target,
            options: Options::default(),
        }
    }

    /// How many connections may wait to be accepted
    ///
    /// ## Behaviour
    /// The kernel's own limit caps it
    ///
    /// ## Returns
    /// The task. Calling it twice keeps the last
    pub fn backlog(mut self, backlog: u32) -> Self {
        self.options.backlog = Some(backlog);
        self
    }

    /// Lets other sockets listen on the same port, each set up the
    /// same way
    ///
    /// ## Returns
    /// The task. Calling it twice keeps the last
    pub fn reuse_port(mut self, reuse: bool) -> Self {
        self.options.reuse_port = reuse;
        self
    }

    /// Takes only IPv6 connections on an IPv6 address
    ///
    /// ## Returns
    /// The task. Calling it twice keeps the last
    pub fn v6_only(mut self, only: bool) -> Self {
        self.options.v6_only = only;
        self
    }

    /// Carries settings over from a task this one runs inside
    #[cfg_attr(not(feature = "tls"), allow(dead_code))]
    pub(crate) fn with_options(mut self, options: Options) -> Self {
        self.options = options;
        self
    }

    /// The settings the socket is set up with
    #[cfg_attr(not(feature = "tls"), allow(dead_code))]
    pub(crate) fn options(&self) -> Options {
        self.options
    }

    /// Binds and listens
    fn listen(&self) -> Result<Listener, RuntimeError> {
        let found = self.target.resolve()?;

        let mut failure = RuntimeError::BadAddress;

        for addr in found {
            match bind_listen(&addr, &self.options) {
                Ok(listener) => return Ok(listener),
                Err(error) => failure = error,
            }
        }

        Err(failure)
    }
}

/// Binds a fresh socket to `addr` and starts it listening
fn bind_listen(addr: &SocketAddr, options: &Options) -> Result<Listener, RuntimeError> {
    let fd = open(family(addr), libc::SOCK_STREAM)?;

    options.apply(fd.raw(), addr.is_ipv6())?;

    // So a port that was just in use can be listened on again at
    // once, rather than after the old connections time out
    set_flag(fd.raw(), libc::SO_REUSEADDR)?;

    let (raw, len) = to_raw(addr);

    unsafe {
        libc::bind(
            fd.raw(),
            (&raw as *const libc::sockaddr_storage).cast::<libc::sockaddr>(),
            len,
        )
    }
    .check()?;

    unsafe { libc::listen(fd.raw(), options.backlog()) }.check()?;

    // Port 0 was a free one picked by the kernel, so this is the
    // only way to know which
    let local = local_of(fd.raw())?;

    Ok(Listener::new(fd, local))
}

/// Takes the next connection off a listener
///
/// ## Returns
/// The connection, and the address it came from
#[derive(Debug, Clone)]
#[must_use = "a task does nothing until it is run or spawned"]
pub struct AcceptTask {
    /// Where the connections come from
    listener: Listener,
}

impl AcceptTask {
    /// Accepts from `listener`
    pub(crate) fn new(listener: Listener) -> Self {
        Self { listener }
    }

    /// Takes a connection if one is waiting
    fn advance(
        &mut self,
    ) -> Result<Step<Result<(Connection, SocketAddr), RuntimeError>>, RuntimeError> {
        let fd = self.listener.fd();

        loop {
            let mut storage: libc::sockaddr_storage = unsafe { mem::zeroed() };
            let mut len = mem::size_of::<libc::sockaddr_storage>() as libc::socklen_t;

            let accepted = unsafe {
                libc::accept(
                    fd,
                    (&mut storage as *mut libc::sockaddr_storage).cast::<libc::sockaddr>(),
                    &mut len,
                )
            }
            .check();

            match accepted {
                Ok(raw) => return Ok(Step::Done(adopt(Fd::new(raw), &storage))),

                // Gone again before it could be taken, so the next one
                Err(RuntimeError::CheckError(Some(libc::EINTR | libc::ECONNABORTED))) => {}

                Err(RuntimeError::CheckError(Some(libc::EAGAIN))) => {
                    return wait_on(fd, libc::EVFILT_READ);
                }

                Err(error) => return Err(error),
            }
        }
    }
}

/// Wraps a socket fresh from `accept`
fn adopt(
    fd: Fd,
    storage: &libc::sockaddr_storage,
) -> Result<(Connection, SocketAddr), RuntimeError> {
    configure(fd.raw())?;

    let peer = match from_raw(storage) {
        Some(peer) => peer,
        None => peer_of(fd.raw())?,
    };

    let local = local_of(fd.raw())?;

    Ok((Connection::new(fd, local, peer), peer))
}

/// Connects, sends, and reads the answer to the end
///
/// ## Returns
/// Everything the other side sent before it closed the
/// connection
#[derive(Debug, Clone)]
#[must_use = "a task does nothing until it is run or spawned"]
pub struct RequestTask {
    /// How it connects
    connect: ConnectTask,

    /// What it sends
    data: Arc<[u8]>,

    /// How far this run has got
    stage: Progress<Stage>,
}

impl RequestTask {
    /// Sends `data` to `target` and reads what comes back
    pub(crate) fn new(target: Target, data: Arc<[u8]>) -> Self {
        Self {
            connect: ConnectTask::new(target),
            data,
            stage: Progress::default(),
        }
    }

    /// Takes the exchange as far as it can go without waiting
    fn advance(&mut self, reactor_id: i32, task_id: usize) -> Step<Result<Vec<u8>, RuntimeError>> {
        exchange::advance(
            &mut self.connect,
            &mut self.stage.0,
            &self.data,
            reactor_id,
            task_id,
        )
    }
}

impl sealed::Sealed for ConnectTask {}
impl sealed::Sealed for ListenTask {}
impl sealed::Sealed for AcceptTask {}
impl sealed::Sealed for RequestTask {}

impl Task for ConnectTask {
    type Output = Result<Connection, RuntimeError>;
    type Input = Nothing;

    /// Waits on this thread, for `Runtime::block`
    fn execute(&self, _token: Token, reactor_id: i32, task_id: usize) -> Self::Output {
        park::drive(self.clone(), reactor_id, task_id)
    }

    fn prepare(&mut self, _token: Token) {
        self.progress = Progress::default();
    }

    /// A name lookup blocks, so only a literal address keeps it on
    /// a worker
    fn blocking(&self, _token: Token) -> bool {
        self.target.needs_lookup()
    }

    fn step(&mut self, _token: Token, _reactor_id: i32, _task_id: usize) -> Step<Self::Output> {
        settle(self.advance())
    }
}

impl Task for ListenTask {
    type Output = Result<Listener, RuntimeError>;
    type Input = Nothing;

    /// Never waits on the socket, so this is the whole task
    fn execute(&self, _token: Token, _reactor_id: i32, _task_id: usize) -> Self::Output {
        self.listen()
    }

    /// A name lookup blocks, so only a literal address keeps it on
    /// a worker
    fn blocking(&self, _token: Token) -> bool {
        self.target.needs_lookup()
    }
}

impl Task for AcceptTask {
    type Output = Result<(Connection, SocketAddr), RuntimeError>;
    type Input = Nothing;

    /// Waits on this thread, for `Runtime::block`
    fn execute(&self, _token: Token, reactor_id: i32, task_id: usize) -> Self::Output {
        park::drive(self.clone(), reactor_id, task_id)
    }

    fn step(&mut self, _token: Token, _reactor_id: i32, _task_id: usize) -> Step<Self::Output> {
        settle(self.advance())
    }
}

impl Task for RequestTask {
    type Output = Result<Vec<u8>, RuntimeError>;
    type Input = Nothing;

    /// Waits on this thread, for `Runtime::block`
    fn execute(&self, _token: Token, reactor_id: i32, task_id: usize) -> Self::Output {
        park::drive(self.clone(), reactor_id, task_id)
    }

    fn prepare(&mut self, _token: Token) {
        self.connect.progress = Progress::default();
        self.stage = Progress::default();
    }

    /// Whatever the connect says
    fn blocking(&self, _token: Token) -> bool {
        self.connect.blocking(token())
    }

    fn step(&mut self, _token: Token, reactor_id: i32, task_id: usize) -> Step<Self::Output> {
        self.advance(reactor_id, task_id)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::futures::net::address::sealed::Sealed;
    use crate::modules::input::token;

    /// Only a task that has to look a name up asks for a sleep
    /// thread. The rest step on a worker and park
    #[test]
    fn only_a_name_lookup_blocks() {
        assert!(!ConnectTask::new("127.0.0.1:80".target()).blocking(token()));
        assert!(ConnectTask::new("localhost:80".target()).blocking(token()));
        assert!(!ListenTask::new("127.0.0.1:0".target()).blocking(token()));
        assert!(ListenTask::new("localhost:0".target()).blocking(token()));
        assert!(!RequestTask::new("[::1]:80".target(), Arc::from(&b""[..])).blocking(token()));
        assert!(RequestTask::new("localhost:80".target(), Arc::from(&b""[..])).blocking(token()));
    }
}