Skip to main content

volans_stream/server/
handler.rs

1use std::{
2    convert::Infallible,
3    sync::Arc,
4    task::{Context, Poll},
5};
6
7use parking_lot::Mutex;
8use volans_core::PeerId;
9use volans_swarm::{
10    ConnectionHandler, ConnectionHandlerEvent, ConnectionId, InboundStreamHandler,
11    InboundUpgradeSend, SubstreamProtocol,
12};
13
14use crate::Upgrade;
15
16use super::shared::Shared;
17
18pub struct Handler {
19    remote_peer_id: PeerId,
20    connection_id: ConnectionId,
21    shared: Arc<Mutex<Shared>>,
22}
23
24impl Handler {
25    pub(crate) fn new(
26        remote_peer_id: PeerId,
27        connection_id: ConnectionId,
28        shared: Arc<Mutex<Shared>>,
29    ) -> Self {
30        Self {
31            remote_peer_id,
32            connection_id,
33            shared,
34        }
35    }
36}
37
38impl ConnectionHandler for Handler {
39    type Action = Infallible;
40    type Event = Infallible;
41
42    fn handle_action(&mut self, action: Self::Action) {
43        unreachable!("Handler does not support actions, got: {:?}", action);
44    }
45
46    fn poll(&mut self, _cx: &mut Context<'_>) -> Poll<ConnectionHandlerEvent<Self::Event>> {
47        Poll::Pending
48    }
49}
50
51impl InboundStreamHandler for Handler {
52    type InboundUpgrade = Upgrade;
53    type InboundUserData = ();
54
55    fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundUpgrade, Self::InboundUserData> {
56        SubstreamProtocol::new(
57            Upgrade {
58                supported_protocols: Shared::lock(&self.shared).supported_protocols(),
59            },
60            (),
61        )
62    }
63
64    fn on_fully_negotiated(
65        &mut self,
66        _user_data: Self::InboundUserData,
67        (stream, protocol): <Self::InboundUpgrade as InboundUpgradeSend>::Output,
68    ) {
69        Shared::lock(&self.shared).on_inbound_stream(
70            self.remote_peer_id,
71            self.connection_id,
72            stream,
73            protocol,
74        );
75    }
76
77    fn on_upgrade_error(
78        &mut self,
79        _user_data: Self::InboundUserData,
80        _error: <Self::InboundUpgrade as InboundUpgradeSend>::Error,
81    ) {
82    }
83}