hopr_transport/
socket.rs

1use futures::{Sink, Stream};
2use hopr_protocol_app::prelude::{ApplicationDataIn, ApplicationDataOut};
3use hopr_transport_session::DestinationRouting;
4
5/// Represents the socket behavior of the hopr-lib spawned [`Hopr`] object.
6///
7/// HOPR socket aims to mimic a simple socket like write and read behavior for unstructured
8/// communication without the advanced session properties.
9///
10/// Typical use cases might be low level UDP based protocols that can wrap the HOPR socket
11/// without needing the advanced session functionality.
12pub struct HoprSocket<T, U> {
13    rx: T,
14    tx: U,
15}
16
17impl<T, U> From<(T, U)> for HoprSocket<T, U>
18where
19    T: Stream<Item = ApplicationDataIn> + Send + 'static,
20    U: Sink<(DestinationRouting, ApplicationDataOut)> + Send + Clone + 'static,
21{
22    fn from(value: (T, U)) -> Self {
23        Self {
24            rx: value.0,
25            tx: value.1,
26        }
27    }
28}
29
30impl<T, U> HoprSocket<T, U>
31where
32    T: Stream<Item = ApplicationDataIn> + Send + 'static,
33    U: Sink<(DestinationRouting, ApplicationDataOut)> + Send + Clone + 'static,
34{
35    pub fn reader(self) -> T {
36        self.rx
37    }
38
39    pub fn writer(&self) -> U {
40        self.tx.clone()
41    }
42}