libp2p_webrtc_websys/
stream.rs1use std::{
3 pin::Pin,
4 task::{Context, Poll},
5};
6
7use futures::{AsyncRead, AsyncWrite};
8use send_wrapper::SendWrapper;
9use web_sys::RtcDataChannel;
10
11use self::poll_data_channel::PollDataChannel;
12
13mod poll_data_channel;
14
15pub struct Stream {
19 inner: SendWrapper<libp2p_webrtc_utils::Stream<PollDataChannel>>,
21}
22
23pub(crate) type DropListener = SendWrapper<libp2p_webrtc_utils::DropListener<PollDataChannel>>;
24
25impl Stream {
26 pub(crate) fn new(data_channel: RtcDataChannel) -> (Self, DropListener) {
27 let (inner, drop_listener) =
28 libp2p_webrtc_utils::Stream::new(PollDataChannel::new(data_channel));
29
30 (
31 Self {
32 inner: SendWrapper::new(inner),
33 },
34 SendWrapper::new(drop_listener),
35 )
36 }
37}
38
39impl AsyncRead for Stream {
40 fn poll_read(
41 self: Pin<&mut Self>,
42 cx: &mut Context<'_>,
43 buf: &mut [u8],
44 ) -> Poll<std::io::Result<usize>> {
45 Pin::new(&mut *self.get_mut().inner).poll_read(cx, buf)
46 }
47}
48
49impl AsyncWrite for Stream {
50 fn poll_write(
51 self: Pin<&mut Self>,
52 cx: &mut Context<'_>,
53 buf: &[u8],
54 ) -> Poll<std::io::Result<usize>> {
55 Pin::new(&mut *self.get_mut().inner).poll_write(cx, buf)
56 }
57
58 fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
59 Pin::new(&mut *self.get_mut().inner).poll_flush(cx)
60 }
61
62 fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
63 Pin::new(&mut *self.get_mut().inner).poll_close(cx)
64 }
65}