muxing 0.1.1

A simple muxing library for Rust
Documentation
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
use crate::{
    Config, ConnectionError, Result, StreamId,
    frame::{Codec, Flags, Frame, Header},
};
use asynchronous_codec::Framed;
use bytes::Bytes;
use cleanup::Cleanup;
use closing::Closing;
use futures::{
    AsyncRead, AsyncWrite, FutureExt, SinkExt, StreamExt,
    channel::mpsc,
    stream::{Fuse, SelectAll},
};
use parking_lot::Mutex;
use std::{
    collections::{HashMap, VecDeque},
    fmt,
    sync::{Arc, atomic::AtomicUsize},
    task::{Context, Poll, Waker},
};
use stream::{Shared, State, TaggedStream};
use tracing::{debug, error, trace, warn};

mod cleanup;
mod closing;
mod stream;

pub use stream::Stream;

#[allow(dead_code)]
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
pub enum Endpoint {
    Client,
    Server,
}

#[allow(dead_code)]
const NEXT_CONNECTION_ID: AtomicUsize = AtomicUsize::new(1);

#[derive(Clone, Copy)]
pub(crate) struct ConnectionId(usize);

impl ConnectionId {
    pub(crate) fn new() -> Self {
        Self(NEXT_CONNECTION_ID.fetch_add(1, std::sync::atomic::Ordering::SeqCst))
    }
}

impl fmt::Debug for ConnectionId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:08x}", self.0)
    }
}

impl fmt::Display for ConnectionId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:08x}", self.0)
    }
}

#[derive(Debug)]
pub(crate) enum StreamCommand {
    SendFrame(Frame),
    CloseStream { ack: bool },
}

#[derive(Debug)]
pub(crate) enum Action {
    None,
    New(Stream),
    Terminate,
}

enum ConnectionState<T> {
    Active(Active<T>),
    Closing(Closing<T>),
    Closed,
    Cleanup(Cleanup),
    Poisoned,
}

impl<T> fmt::Debug for ConnectionState<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ConnectionState::Active(_) => write!(f, "Active"),
            ConnectionState::Closing(_) => write!(f, "Closing"),
            ConnectionState::Cleanup(_) => write!(f, "Cleanup"),
            ConnectionState::Closed => write!(f, "Closed"),
            ConnectionState::Poisoned => write!(f, "Poisoned"),
        }
    }
}

#[derive(Debug)]
pub struct Connection<T> {
    inner: ConnectionState<T>,
}

impl<T: AsyncRead + AsyncWrite + Unpin> Connection<T> {
    pub fn new(socket: T, endpoint: Endpoint, config: Config) -> Self {
        Self {
            inner: ConnectionState::Active(Active::new(socket, endpoint, config)),
        }
    }

    pub fn poll_new_outbound(&mut self, cx: &mut Context<'_>) -> Poll<Result<Stream>> {
        loop {
            match std::mem::replace(&mut self.inner, ConnectionState::Poisoned) {
                ConnectionState::Active(mut active) => match active.poll_new_outbound(cx) {
                    Poll::Ready(Ok(stream)) => {
                        self.inner = ConnectionState::Active(active);
                        return Poll::Ready(Ok(stream));
                    }
                    Poll::Ready(Err(e)) => {
                        self.inner = ConnectionState::Cleanup(active.cleanup(e));
                        continue;
                    }
                    Poll::Pending => {
                        self.inner = ConnectionState::Active(active);
                        return Poll::Pending;
                    }
                },
                ConnectionState::Closing(mut closing) => match closing.poll_unpin(cx) {
                    Poll::Ready(Ok(())) => {
                        self.inner = ConnectionState::Closed;
                        return Poll::Ready(Err(ConnectionError::Closed));
                    }
                    Poll::Ready(Err(e)) => {
                        self.inner = ConnectionState::Closed;
                        return Poll::Ready(Err(e));
                    }
                    Poll::Pending => {
                        self.inner = ConnectionState::Closing(closing);
                        return Poll::Pending;
                    }
                },
                ConnectionState::Cleanup(mut cleanup) => match cleanup.poll_unpin(cx) {
                    Poll::Ready(e) => {
                        self.inner = ConnectionState::Closed;
                        return Poll::Ready(Err(e));
                    }
                    Poll::Pending => {
                        self.inner = ConnectionState::Cleanup(cleanup);
                        return Poll::Pending;
                    }
                },
                ConnectionState::Closed => {
                    self.inner = ConnectionState::Closed;
                    return Poll::Ready(Err(ConnectionError::Closed));
                }
                ConnectionState::Poisoned => unreachable!(),
            }
        }
    }

    pub fn poll_next_inbound(&mut self, cx: &mut Context<'_>) -> Poll<Option<Result<Stream>>> {
        loop {
            match std::mem::replace(&mut self.inner, ConnectionState::Poisoned) {
                ConnectionState::Active(mut active) => match active.poll(cx) {
                    Poll::Ready(Ok(stream)) => {
                        self.inner = ConnectionState::Active(active);
                        return Poll::Ready(Some(Ok(stream)));
                    }
                    Poll::Ready(Err(e)) => {
                        self.inner = ConnectionState::Cleanup(active.cleanup(e));
                        continue;
                    }
                    Poll::Pending => {
                        self.inner = ConnectionState::Active(active);
                        return Poll::Pending;
                    }
                },
                ConnectionState::Closing(mut closing) => match closing.poll_unpin(cx) {
                    Poll::Ready(Ok(())) => {
                        // 关闭连接成功
                        self.inner = ConnectionState::Closed;
                        return Poll::Ready(None);
                    }
                    Poll::Ready(Err(e)) => {
                        self.inner = ConnectionState::Closed;
                        return Poll::Ready(Some(Err(e)));
                    }
                    Poll::Pending => {
                        self.inner = ConnectionState::Closing(closing);
                        return Poll::Pending;
                    }
                },
                ConnectionState::Cleanup(mut cleanup) => match cleanup.poll_unpin(cx) {
                    Poll::Ready(ConnectionError::Closed) => {
                        // 关闭连接成功
                        self.inner = ConnectionState::Closed;
                        return Poll::Ready(None);
                    }
                    Poll::Ready(e) => {
                        self.inner = ConnectionState::Closed;
                        return Poll::Ready(Some(Err(e)));
                    }
                    Poll::Pending => {
                        self.inner = ConnectionState::Cleanup(cleanup);
                        return Poll::Pending;
                    }
                },
                ConnectionState::Closed => {
                    self.inner = ConnectionState::Closed;
                    return Poll::Ready(None);
                }
                ConnectionState::Poisoned => unreachable!(),
            }
        }
    }
    pub fn poll_close(&mut self, cx: &mut Context<'_>) -> Poll<Result<()>> {
        loop {
            match std::mem::replace(&mut self.inner, ConnectionState::Poisoned) {
                ConnectionState::Active(active) => {
                    self.inner = ConnectionState::Closing(active.close());
                }
                ConnectionState::Closing(mut closing) => match closing.poll_unpin(cx) {
                    Poll::Ready(Ok(())) => {
                        self.inner = ConnectionState::Closed;
                        return Poll::Ready(Ok(()));
                    }
                    Poll::Ready(Err(e)) => {
                        warn!("Failure while closing connection: {}", e);
                        self.inner = ConnectionState::Closed;
                        return Poll::Ready(Err(e));
                    }
                    Poll::Pending => {
                        self.inner = ConnectionState::Closing(closing);
                        return Poll::Pending;
                    }
                },
                ConnectionState::Cleanup(mut cleanup) => match cleanup.poll_unpin(cx) {
                    Poll::Ready(e) => {
                        warn!("Failure while closing connection: {}", e);
                        self.inner = ConnectionState::Closed;
                        return Poll::Ready(Err(e));
                    }
                    Poll::Pending => {
                        self.inner = ConnectionState::Cleanup(cleanup);
                        return Poll::Pending;
                    }
                },
                ConnectionState::Closed => {
                    self.inner = ConnectionState::Closed;
                    return Poll::Ready(Ok(()));
                }
                ConnectionState::Poisoned => unreachable!(),
            }
        }
    }
}

impl<T> Drop for Connection<T> {
    fn drop(&mut self) {
        match &mut self.inner {
            ConnectionState::Active(active) => active.drop_all_streams(),
            ConnectionState::Closing(_) => {}
            ConnectionState::Cleanup(_) => {}
            ConnectionState::Closed => {}
            ConnectionState::Poisoned => {}
        }
    }
}

struct Active<T> {
    id: ConnectionId,
    endpoint: Endpoint,
    config: Arc<Config>,
    next_id: u32,
    socket: Fuse<Framed<T, Codec>>,
    streams: HashMap<StreamId, Arc<Mutex<Shared>>>,
    stream_receivers: SelectAll<TaggedStream<StreamId, mpsc::Receiver<StreamCommand>>>,
    no_streams_waker: Option<Waker>,

    pending_read_frame: Option<Frame>,
    pending_write_frame: Option<Frame>,
    new_outbound_stream_waker: Option<Waker>,
}

impl<T> fmt::Debug for Active<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("Connection")
            .field("id", &self.id)
            .field("endpoint", &self.endpoint)
            .field("streams", &self.streams.len())
            .field("next_id", &self.next_id)
            .finish()
    }
}

impl<T> fmt::Display for Active<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "(Connection {} {:?} (streams {}))",
            self.id,
            self.endpoint,
            self.streams.len()
        )
    }
}

impl<T> Active<T>
where
    T: AsyncRead + AsyncWrite + Unpin,
{
    fn new(socket: T, endpoint: Endpoint, config: Config) -> Self {
        let id = ConnectionId::new();
        let socket = Framed::new(socket, Codec::new(config.max_frame_size)).fuse();
        let next_id = if endpoint == Endpoint::Client { 1 } else { 2 };
        Self {
            id,
            endpoint,
            config: Arc::new(config),
            next_id,
            socket,
            streams: HashMap::new(),
            stream_receivers: SelectAll::default(),
            no_streams_waker: None,
            pending_read_frame: None,
            pending_write_frame: None,
            new_outbound_stream_waker: None,
        }
    }

    fn poll_new_outbound(&mut self, cx: &mut Context<'_>) -> Poll<Result<Stream>> {
        if self.streams.len() >= self.config.max_num_streams {
            error!(
                "{}: maximum({}) number of streams reached",
                self.id, self.config.max_num_streams
            );
            return Poll::Ready(Err(ConnectionError::TooManyStreams));
        }
        if self.ack_backlog() >= self.config.max_ack_backlog {
            debug!(
                "{}: streams({}) waiting for ACK, task for wake-up",
                self.id, self.config.max_ack_backlog
            );
            self.new_outbound_stream_waker = Some(cx.waker().clone());
            return Poll::Ready(Err(ConnectionError::TooManyStreams));
        }
        trace!("{}: creating new outbound stream", self.id);
        let id = self.next_stream_id()?;
        let stream = self.make_new_outbound_stream(id);
        debug!("{}: new outbound {} of {}", self.id, id, self);
        Poll::Ready(Ok(stream))
    }

    fn make_new_outbound_stream(&mut self, id: StreamId) -> Stream {
        let config = self.config.clone();
        let (sender, receiver) = mpsc::channel(10);
        self.stream_receivers.push(TaggedStream::new(id, receiver));
        if let Some(waker) = self.no_streams_waker.take() {
            waker.wake();
        }
        Stream::new_outbound(id, self.id, config, sender)
    }

    fn make_new_inbound_stream(&mut self, id: StreamId) -> Stream {
        let config = self.config.clone();
        let (sender, receiver) = mpsc::channel(10);
        self.stream_receivers.push(TaggedStream::new(id, receiver));
        if let Some(waker) = self.no_streams_waker.take() {
            waker.wake();
        }
        Stream::new_inbound(id, self.id, config, sender)
    }

    fn next_stream_id(&mut self) -> Result<StreamId> {
        let proposed = StreamId::new(self.next_id);
        self.next_id = self
            .next_id
            .checked_add(2)
            .ok_or(ConnectionError::NoMoreStreamIds)?;
        match self.endpoint {
            Endpoint::Client => assert!(proposed.is_client()),
            Endpoint::Server => assert!(proposed.is_server()),
        }
        Ok(proposed)
    }

    fn ack_backlog(&mut self) -> usize {
        self.streams
            .iter()
            .filter(|(id, _)| match self.endpoint {
                Endpoint::Client => id.is_client(),
                Endpoint::Server => id.is_server(),
            })
            .filter(|(_, s)| s.lock().is_pending_ack())
            .count()
    }

    fn is_valid_remote_id(&self, id: StreamId) -> bool {
        match self.endpoint {
            Endpoint::Client => id.is_server(),
            Endpoint::Server => id.is_client(),
        }
    }

    fn close(self) -> Closing<T> {
        let pending_frames = self
            .pending_read_frame
            .into_iter()
            .chain(self.pending_write_frame)
            .collect::<VecDeque<Frame>>();
        Closing::new(self.stream_receivers, pending_frames, self.socket)
    }

    fn cleanup(mut self, error: ConnectionError) -> Cleanup {
        self.drop_all_streams();
        Cleanup::new(self.stream_receivers, error)
    }

    fn poll(&mut self, cx: &mut Context<'_>) -> Poll<Result<Stream>> {
        loop {
            if self.socket.poll_ready_unpin(cx).is_ready() {
                //Socket is ready to send
                if let Some(frame) = self
                    .pending_read_frame
                    .take()
                    .or_else(|| self.pending_write_frame.take())
                {
                    self.socket.start_send_unpin(frame)?;
                    continue;
                }
            }
            // Flush the socket
            match self.socket.poll_flush_unpin(cx)? {
                Poll::Ready(()) => {}
                Poll::Pending => {}
            }

            if self.pending_write_frame.is_none() {
                match self.stream_receivers.poll_next_unpin(cx) {
                    Poll::Ready(Some((_, Some(StreamCommand::SendFrame(frame))))) => {
                        trace!("{}/{}: sending frame", self.id, frame.header().stream_id());
                        self.pending_write_frame.replace(frame);
                        continue;
                    }
                    Poll::Ready(Some((id, Some(StreamCommand::CloseStream { ack })))) => {
                        trace!("{}/{}: sending close", self.id, id);
                        self.pending_write_frame.replace(Frame::new_close(id, ack));
                    }
                    Poll::Ready(Some((id, None))) => {
                        if let Some(frame) = self.on_drop_stream(id) {
                            self.pending_write_frame.replace(frame);
                        }
                        continue;
                    }
                    Poll::Ready(None) => {
                        self.new_outbound_stream_waker = Some(cx.waker().clone());
                    }
                    Poll::Pending => {}
                }
            }

            if self.pending_read_frame.is_none() {
                match self.socket.poll_next_unpin(cx) {
                    Poll::Ready(Some(frame)) => match self.on_frame(frame?)? {
                        Action::New(stream) => {
                            trace!("{}: new inbound {} of {}", self.id, stream, self);
                            return Poll::Ready(Ok(stream));
                        }
                        Action::Terminate => {
                            trace!("{}: sending close", self.id);
                            return Poll::Ready(Err(ConnectionError::Closed));
                        }
                        Action::None => {}
                    },
                    Poll::Ready(None) => {
                        return Poll::Ready(Err(ConnectionError::Closed));
                    }
                    Poll::Pending => {}
                }
            }
            return Poll::Pending;
        }
    }

    fn on_frame(&mut self, frame: Frame) -> Result<Action> {
        trace!("{}: received: {}", self.id, frame.header());
        let stream_id = frame.header().stream_id();
        if frame.header().flags().contains(Flags::ACK) {
            // ACK Frame
            if let Some(stream) = self.streams.get(&stream_id) {
                stream
                    .lock()
                    .update_state(self.id, stream_id, State::Open { ack: true });
            }
            if let Some(waker) = self.new_outbound_stream_waker.take() {
                waker.wake();
            }
        }
        if frame.header().flags().contains(Flags::RST) {
            if let Some(s) = self.streams.get_mut(&stream_id) {
                let mut shared = s.lock();
                shared.update_state(self.id, stream_id, State::Closed);
                if let Some(w) = shared.reader.take() {
                    w.wake()
                }
                if let Some(w) = shared.writer.take() {
                    w.wake()
                }
            }
            return Ok(Action::None);
        }
        let is_finish = frame.header().flags().contains(Flags::FIN); // 流半关闭
        if frame.header().flags().contains(Flags::SYN) {
            // 新建流
            if !self.is_valid_remote_id(stream_id) {
                error!("{}: invalid stream id {}", self.id, stream_id);
                return Ok(Action::Terminate);
            }
            if self.streams.contains_key(&stream_id) {
                error!("{}: stream {} already exists", self.id, stream_id);
                return Ok(Action::Terminate);
            }
            if self.streams.len() == self.config.max_num_streams {
                error!(
                    "{}: maximum({}) number of streams reached",
                    self.id, self.config.max_num_streams
                );
                return Ok(Action::Terminate);
            }
            let stream = self.make_new_inbound_stream(stream_id);
            {
                let mut shared = stream.shared();
                if is_finish {
                    shared.update_state(self.id, stream_id, State::RecvClosed);
                }
                shared.buffer.extend_from_slice(&frame.into_data()[..]);
            };
            self.streams.insert(stream_id, stream.clone_shared());
            return Ok(Action::New(stream));
        }
        if let Some(s) = self.streams.get_mut(&stream_id) {
            let mut shared = s.lock();
            if is_finish {
                shared.update_state(self.id, stream_id, State::RecvClosed);
            }
            shared.buffer.extend_from_slice(&frame.into_data()[..]);
            if let Some(w) = shared.reader.take() {
                w.wake()
            }
        } else {
            trace!(
                "{}/{}: data frame for unknown stream, possibly dropped earlier: {:?}",
                self.id, stream_id, frame
            );
        }
        Ok(Action::None)
    }

    fn on_drop_stream(&mut self, stream_id: StreamId) -> Option<Frame> {
        let s = self.streams.get(&stream_id).expect("stream should exist");
        trace!("{}: dropping stream {}", self.id, stream_id);
        let mut shared = s.lock();
        let state = shared.update_state(self.id, stream_id, State::Closed);
        match state {
            State::Open { .. } => {
                let mut header = Header::new(stream_id, 0);
                header.rst();
                Some(Frame::new(header, Bytes::new()))
            }
            State::RecvClosed => {
                let mut header = Header::new(stream_id, 0);
                header.fin();
                Some(Frame::new(header, Bytes::new()))
            }
            State::SendClosed | State::Closed => None,
        }
    }
}

impl<T> Active<T> {
    fn drop_all_streams(&mut self) {
        self.streams.drain().for_each(|(sid, s)| {
            let mut shared = s.lock();
            shared.update_state(self.id, sid, State::Closed);
            if let Some(waker) = shared.reader.take() {
                waker.wake();
            }
            if let Some(waker) = shared.writer.take() {
                waker.wake();
            }
        });
    }
}