Skip to main content

http2_proto/transport/
mod.rs

1//! Transport layer abstraction.
2//!
3//! This module provides a `Transport` trait that abstracts over raw TCP
4//! and TLS-encrypted connections, allowing protocol implementations
5//! to work with either.
6
7mod plain;
8
9#[cfg(feature = "tls")]
10mod tls;
11
12pub use plain::PlainTransport;
13
14#[cfg(feature = "tls")]
15pub use tls::{TlsConfig, TlsTransport};
16
17use std::io;
18
19/// Transport state.
20#[derive(Debug, Clone, Copy, PartialEq, Eq)]
21pub enum TransportState {
22    /// Transport is performing handshake (TLS only).
23    Handshaking,
24    /// Transport is ready for application data.
25    Ready,
26    /// Transport encountered an error.
27    Error,
28    /// Transport is closed.
29    Closed,
30}
31
32/// Abstraction over raw TCP and TLS transports.
33///
34/// This trait provides a completion-based interface for sending and
35/// receiving data.
36pub trait Transport {
37    /// Get the current transport state.
38    fn state(&self) -> TransportState;
39
40    /// Check if the transport is ready for application data.
41    fn is_ready(&self) -> bool {
42        self.state() == TransportState::Ready
43    }
44
45    /// Queue data to be sent.
46    ///
47    /// Returns the number of bytes queued, or `WouldBlock` if the
48    /// send buffer is full.
49    fn send(&mut self, data: &[u8]) -> io::Result<usize>;
50
51    /// Read available decrypted data.
52    ///
53    /// Returns the number of bytes read, or `WouldBlock` if no data
54    /// is available.
55    fn recv(&mut self, buf: &mut [u8]) -> io::Result<usize>;
56
57    /// Process raw data received from the socket.
58    ///
59    /// For TLS, this decrypts the data. For plain, this just buffers it.
60    fn on_recv(&mut self, data: &[u8]) -> io::Result<()>;
61
62    /// Get data that needs to be sent on the socket.
63    ///
64    /// For TLS, this returns encrypted data. For plain, this returns
65    /// the queued application data.
66    fn pending_send(&self) -> &[u8];
67
68    /// Mark bytes as sent on the socket.
69    fn advance_send(&mut self, n: usize);
70
71    /// Check if there's pending data to send.
72    fn has_pending_send(&self) -> bool {
73        !self.pending_send().is_empty()
74    }
75
76    /// Initiate shutdown.
77    fn shutdown(&mut self) -> io::Result<()>;
78}
79
80/// Result of processing incoming data.
81#[derive(Debug, Clone, Copy, PartialEq, Eq)]
82pub enum ProcessResult {
83    /// Need more data to continue.
84    NeedMoreData,
85    /// Have application data ready to read.
86    DataReady,
87    /// Handshake completed (TLS only).
88    HandshakeComplete,
89    /// Connection is closing.
90    Closing,
91}