Skip to main content

karyon_p2p/
queued_conn.rs

1use karyon_core::{async_runtime::lock::SemaphoreGuard, async_util::AsyncQueueRef};
2
3use karyon_net::{Endpoint, FramedConn, FramedReader, FramedWriter};
4
5#[cfg(feature = "quic")]
6use karyon_net::quic::QuicConn;
7
8use crate::{Error, PeerID, Result, codec::PeerNetMsgCodec, conn_direction::ConnDirection};
9
10/// Queue that hands connections from the listener and the connector to
11/// the PeerPool.
12pub(crate) type ConnQueueRef = AsyncQueueRef<Incoming>;
13
14/// Max connections waiting for the PeerPool. The pool takes each one
15/// right away, so the queue is rarely full.
16pub(crate) const CONN_QUEUE_SIZE: usize = 64;
17
18/// A connection for the PeerPool to handshake and turn into a
19/// `PeerSession`.
20/// Carries the raw split halves plus metadata.
21pub(crate) struct QueuedConn {
22    pub reader: FramedReader<PeerNetMsgCodec>,
23    pub writer: FramedWriter<PeerNetMsgCodec>,
24    pub direction: ConnDirection,
25    pub remote_endpoint: Endpoint,
26    /// PeerID derived from the secure transport (TLS cert).
27    /// `None` for unauthenticated transports (TCP, Unix).
28    /// The application handshake asserts `vermsg.peer_id == this` when
29    /// `Some`, so a peer can't claim a PeerID it can't prove.
30    pub verified_peer_id: Option<PeerID>,
31    /// PeerID an outbound dial was for, as discovery gave it. Not
32    /// verified; it only tells discovery which peer failed.
33    pub dialed_peer_id: Option<PeerID>,
34    #[cfg(feature = "quic")]
35    pub quic_conn: Option<QuicConn>,
36}
37
38impl QueuedConn {
39    /// Splits a stream connection. For QUIC, `conn` is the handshake
40    /// stream; add the connection with `with_quic`.
41    pub(crate) fn new(
42        conn: FramedConn<PeerNetMsgCodec>,
43        direction: ConnDirection,
44        verified_peer_id: Option<PeerID>,
45    ) -> Result<Self> {
46        let remote_endpoint = conn
47            .peer_endpoint()
48            .ok_or(Error::InvalidEndpoint("missing peer endpoint".into()))?;
49        let (reader, writer) = conn.split();
50        Ok(Self {
51            reader,
52            writer,
53            direction,
54            remote_endpoint,
55            verified_peer_id,
56            dialed_peer_id: None,
57            #[cfg(feature = "quic")]
58            quic_conn: None,
59        })
60    }
61
62    /// Records the PeerID an outbound dial was for.
63    pub(crate) fn with_dialed(mut self, peer_id: Option<PeerID>) -> Self {
64        self.dialed_peer_id = peer_id;
65        self
66    }
67
68    /// Adds the QUIC connection that carries the protocol streams.
69    #[cfg(feature = "quic")]
70    pub(crate) fn with_quic(mut self, quic_conn: QuicConn) -> Self {
71        self.quic_conn = Some(quic_conn);
72        self
73    }
74}
75
76/// A connection and the listener or connector slot it holds. The slot
77/// is freed once the connection is closed.
78pub(crate) struct Incoming {
79    pub conn: QueuedConn,
80    pub slot: SemaphoreGuard,
81}