1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
pub mod read_small_buf;

use xwt_core::prelude::*;

#[derive(Debug, thiserror::Error)]
pub enum EchoError<Endpoint>
where
    Endpoint: xwt_core::EndpointConnect + std::fmt::Debug,
    Endpoint::Connecting: std::fmt::Debug,
    EndpointConnectConnectionFor<Endpoint>: xwt_core::OpenBiStream + std::fmt::Debug,
{
    Connect(xwt_error::Connect<Endpoint>),
    Open(xwt_error::OpenBi<EndpointConnectConnectionFor<Endpoint>>),
    Send(WriteErrorFor<SendStreamFor<EndpointConnectConnectionFor<Endpoint>>>),
    Recv(ReadErrorFor<RecvStreamFor<EndpointConnectConnectionFor<Endpoint>>>),
    NoResponse,
    BadData(Vec<u8>),
}

pub async fn echo<Endpoint>(endpoint: Endpoint) -> Result<(), EchoError<Endpoint>>
where
    Endpoint: xwt_core::EndpointConnect + std::fmt::Debug,
    Endpoint::Connecting: std::fmt::Debug,
    EndpointConnectConnectionFor<Endpoint>: xwt_core::OpenBiStream + std::fmt::Debug,
{
    let connection = crate::utils::connect(endpoint, "https://echo.webtransport.day")
        .await
        .map_err(EchoError::Connect)?;

    let (mut send_stream, mut recv_stream) = crate::utils::open_bi(connection)
        .await
        .map_err(EchoError::Open)?;

    let mut to_write = &b"hello"[..];
    loop {
        let written = send_stream.write(to_write).await.map_err(EchoError::Send)?;
        to_write = &to_write[written..];
        if to_write.is_empty() {
            break;
        }
    }

    let mut read_buf = vec![0u8; 1024];

    let Some(read) = recv_stream
        .read(&mut read_buf[..])
        .await
        .map_err(EchoError::Recv)?
    else {
        return Err(EchoError::NoResponse);
    };
    read_buf.truncate(read);

    if read_buf != b"hello" {
        return Err(EchoError::BadData(read_buf));
    }

    Ok(())
}

#[derive(Debug, thiserror::Error)]
pub enum EchoChunksError<Endpoint, WriteChunk, ReadChunk>
where
    Endpoint: xwt_core::EndpointConnect + std::fmt::Debug,
    Endpoint::Connecting: std::fmt::Debug,
    EndpointConnectConnectionFor<Endpoint>: xwt_core::OpenBiStream + std::fmt::Debug,

    WriteChunk: xwt_core::WriteableChunk + std::fmt::Debug,
    ReadChunk: xwt_core::ReadableChunk + std::fmt::Debug,

    SendStreamFor<EndpointConnectConnectionFor<Endpoint>>: xwt_core::WriteChunk<WriteChunk>,
    RecvStreamFor<EndpointConnectConnectionFor<Endpoint>>: xwt_core::ReadChunk<ReadChunk>,
{
    Connect(xwt_error::Connect<Endpoint>),
    Open(xwt_error::OpenBi<EndpointConnectConnectionFor<Endpoint>>),
    Send(WriteChunkErrorFor<SendStreamFor<EndpointConnectConnectionFor<Endpoint>>, WriteChunk>),
    Recv(ReadChunkErrorFor<RecvStreamFor<EndpointConnectConnectionFor<Endpoint>>, ReadChunk>),
    NoResponse,
    BadData(Vec<u8>),
}

pub async fn echo_chunks<Endpoint, WriteChunk, ReadChunk>(
    endpoint: Endpoint,
) -> Result<(), EchoChunksError<Endpoint, WriteChunk, ReadChunk>>
where
    Endpoint: xwt_core::EndpointConnect + std::fmt::Debug,
    Endpoint::Connecting: std::fmt::Debug,
    EndpointConnectConnectionFor<Endpoint>: xwt_core::OpenBiStream + std::fmt::Debug,

    WriteChunk: xwt_core::WriteableChunk + std::fmt::Debug,
    ReadChunk: xwt_core::ReadableChunk + std::fmt::Debug,

    <WriteChunk as xwt_core::WriteableChunk>::Data<'static>: From<&'static [u8]>,
    for<'a> <ReadChunk as xwt_core::ReadableChunk>::Data<'a>: AsRef<[u8]>,

    SendStreamFor<EndpointConnectConnectionFor<Endpoint>>: xwt_core::WriteChunk<WriteChunk>,
    RecvStreamFor<EndpointConnectConnectionFor<Endpoint>>: xwt_core::ReadChunk<ReadChunk>,
{
    let connection = crate::utils::connect(endpoint, "https://echo.webtransport.day")
        .await
        .map_err(EchoChunksError::Connect)?;

    let (mut send_stream, mut recv_stream) = crate::utils::open_bi(connection)
        .await
        .map_err(EchoChunksError::Open)?;

    let write_data: WriteChunk::Data<'static> = (&b"hello"[..]).into();
    send_stream
        .write_chunk(write_data)
        .await
        .map_err(EchoChunksError::Send)?;

    let maybe_read_chunk = recv_stream
        .read_chunk(1024, false)
        .await
        .map_err(EchoChunksError::Recv)?;
    let Some(read_chunk) = maybe_read_chunk else {
        return Err(EchoChunksError::NoResponse);
    };
    let read_data = read_chunk.data.as_ref();

    if read_data != b"hello" {
        return Err(EchoChunksError::BadData(read_data.into()));
    }

    Ok(())
}

#[derive(Debug, thiserror::Error)]
pub enum EchoDatagrmsError<Endpoint>
where
    Endpoint: xwt_core::EndpointConnect + std::fmt::Debug,
    Endpoint::Connecting: std::fmt::Debug,
    EndpointConnectConnectionFor<Endpoint>: xwt_core::datagram::Datagrams + std::fmt::Debug,
    ReceiveDatagramFor<EndpointConnectConnectionFor<Endpoint>>: std::fmt::Debug,
{
    Connect(xwt_error::Connect<Endpoint>),
    Send(SendErrorFor<EndpointConnectConnectionFor<Endpoint>>),
    Recv(ReceiveErrorFor<EndpointConnectConnectionFor<Endpoint>>),
    BadData(ReceiveDatagramFor<EndpointConnectConnectionFor<Endpoint>>),
}

pub async fn echo_datagrams<Endpoint>(endpoint: Endpoint) -> Result<(), EchoDatagrmsError<Endpoint>>
where
    Endpoint: xwt_core::EndpointConnect + std::fmt::Debug,
    Endpoint::Connecting: std::fmt::Debug,
    EndpointConnectConnectionFor<Endpoint>: xwt_core::datagram::Datagrams + std::fmt::Debug,
    ReceiveDatagramFor<EndpointConnectConnectionFor<Endpoint>>: std::fmt::Debug,
{
    let connection = crate::utils::connect(endpoint, "https://echo.webtransport.day")
        .await
        .map_err(EchoDatagrmsError::Connect)?;

    connection
        .send_datagram(&b"hello"[..])
        .await
        .map_err(EchoDatagrmsError::Send)?;

    let read = connection
        .receive_datagram()
        .await
        .map_err(EchoDatagrmsError::Recv)?;

    if read.as_ref() != b"hello" {
        return Err(EchoDatagrmsError::BadData(read));
    }

    Ok(())
}