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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
use core::fmt::Debug;

pub trait ErrorType {
    type Error: Debug;
}

impl<E> ErrorType for &E
where
    E: ErrorType,
{
    type Error = E::Error;
}

impl<E> ErrorType for &mut E
where
    E: ErrorType,
{
    type Error = E::Error;
}

pub type Fragmented = bool;
pub type Final = bool;

#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum FrameType {
    Text(Fragmented),
    Binary(Fragmented),
    Ping,
    Pong,
    Close,
    SocketClose,
    Continue(Final),
}

impl FrameType {
    pub fn is_fragmented(&self) -> bool {
        match self {
            Self::Text(fragmented) | Self::Binary(fragmented) => *fragmented,
            Self::Continue(_) => true,
            _ => false,
        }
    }

    pub fn is_final(&self) -> bool {
        match self {
            Self::Text(fragmented) | Self::Binary(fragmented) => !*fragmented,
            Self::Continue(final_) => *final_,
            _ => true,
        }
    }
}

pub trait Receiver: ErrorType {
    fn recv(&mut self, frame_data_buf: &mut [u8]) -> Result<(FrameType, usize), Self::Error>;
}

impl<R> Receiver for &mut R
where
    R: Receiver,
{
    fn recv(&mut self, frame_data_buf: &mut [u8]) -> Result<(FrameType, usize), Self::Error> {
        (*self).recv(frame_data_buf)
    }
}

pub trait Sender: ErrorType {
    fn send(&mut self, frame_type: FrameType, frame_data: &[u8]) -> Result<(), Self::Error>;
}

impl<S> Sender for &mut S
where
    S: Sender,
{
    fn send(&mut self, frame_type: FrameType, frame_data: &[u8]) -> Result<(), Self::Error> {
        (*self).send(frame_type, frame_data)
    }
}

pub mod server {
    pub use super::*;

    pub trait Acceptor: ErrorType {
        type Connection: Sender<Error = Self::Error> + Receiver<Error = Self::Error>;

        fn accept(&self) -> Result<Self::Connection, Self::Error>;
    }

    impl<A> Acceptor for &A
    where
        A: Acceptor,
    {
        type Connection = A::Connection;

        fn accept(&self) -> Result<Self::Connection, Self::Error> {
            (*self).accept()
        }
    }
}

pub mod callback_server {
    pub use super::*;

    pub trait SessionProvider {
        type Session: Clone + Send + PartialEq + Debug;

        fn session(&self) -> Self::Session;

        fn is_new(&self) -> bool;
        fn is_closed(&self) -> bool;
    }

    pub trait SenderFactory: ErrorType {
        type Sender: Sender<Error = Self::Error>;

        fn create(&self) -> Result<Self::Sender, Self::Error>;
    }
}

#[cfg(feature = "nightly")]
pub mod asynch {
    use core::future::Future;

    use crate::executor::asynch::{Blocker, Blocking, TrivialUnblocking};

    pub use super::{ErrorType, Fragmented, FrameType};

    pub trait Receiver: ErrorType {
        type ReceiveFuture<'a>: Future<Output = Result<(FrameType, usize), Self::Error>>
        where
            Self: 'a;

        fn recv<'a>(&'a mut self, frame_data_buf: &'a mut [u8]) -> Self::ReceiveFuture<'a>;
    }

    impl<R> Receiver for &mut R
    where
        R: Receiver,
    {
        type ReceiveFuture<'a>
        = R::ReceiveFuture<'a> where Self: 'a;

        fn recv<'a>(&'a mut self, frame_data_buf: &'a mut [u8]) -> Self::ReceiveFuture<'a> {
            (*self).recv(frame_data_buf)
        }
    }

    pub trait Sender: ErrorType {
        type SendFuture<'a>: Future<Output = Result<(), Self::Error>>
        where
            Self: 'a;

        fn send<'a>(
            &'a mut self,
            frame_type: FrameType,
            frame_data: &'a [u8],
        ) -> Self::SendFuture<'a>;
    }

    impl<S> Sender for &mut S
    where
        S: Sender,
    {
        type SendFuture<'a>
        = S::SendFuture<'a> where Self: 'a;

        fn send<'a>(
            &'a mut self,
            frame_type: FrameType,
            frame_data: &'a [u8],
        ) -> Self::SendFuture<'a> {
            (*self).send(frame_type, frame_data)
        }
    }

    impl<B, E> ErrorType for Blocking<B, E>
    where
        E: ErrorType,
    {
        type Error = E::Error;
    }

    impl<B, S> super::Sender for Blocking<B, S>
    where
        B: Blocker,
        S: Sender,
    {
        fn send(&mut self, frame_type: FrameType, frame_data: &[u8]) -> Result<(), Self::Error> {
            self.blocker.block_on(self.api.send(frame_type, frame_data))
        }
    }

    impl<B, R> super::Receiver for Blocking<B, R>
    where
        B: Blocker,
        R: Receiver,
    {
        fn recv(&mut self, frame_data_buf: &mut [u8]) -> Result<(FrameType, usize), Self::Error> {
            self.blocker.block_on(self.api.recv(frame_data_buf))
        }
    }

    impl<E> ErrorType for TrivialUnblocking<E>
    where
        E: ErrorType,
    {
        type Error = E::Error;
    }

    impl<S> Sender for TrivialUnblocking<S>
    where
        S: super::Sender + Send,
    {
        type SendFuture<'a>
        = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;

        fn send<'a>(
            &'a mut self,
            frame_type: FrameType,
            frame_data: &'a [u8],
        ) -> Self::SendFuture<'a> {
            async move { self.api.send(frame_type, frame_data) }
        }
    }

    impl<R> Receiver for TrivialUnblocking<R>
    where
        R: super::Receiver + Send,
    {
        type ReceiveFuture<'a>
        = impl Future<Output = Result<(FrameType, usize), Self::Error>> + 'a where Self: 'a;

        fn recv<'a>(&'a mut self, frame_data_buf: &'a mut [u8]) -> Self::ReceiveFuture<'a> {
            async move { self.api.recv(frame_data_buf) }
        }
    }

    pub mod server {
        pub use super::*;

        pub trait Acceptor: ErrorType {
            type Sender<'a>: Sender<Error = Self::Error>
            where
                Self: 'a;
            type Receiver<'a>: Receiver<Error = Self::Error>
            where
                Self: 'a;

            type AcceptFuture<'a>: Future<
                Output = Result<(Self::Sender<'a>, Self::Receiver<'a>), Self::Error>,
            >
            where
                Self: 'a;

            fn accept(&self) -> Self::AcceptFuture<'_>;
        }

        impl<A> Acceptor for &A
        where
            A: Acceptor,
        {
            type Sender<'a> = A::Sender<'a> where Self: 'a;
            type Receiver<'a> = A::Receiver<'a> where Self: 'a;

            type AcceptFuture<'a>
            = A::AcceptFuture<'a> where Self: 'a;

            fn accept(&self) -> Self::AcceptFuture<'_> {
                (*self).accept()
            }
        }

        impl<A> Acceptor for &mut A
        where
            A: Acceptor,
        {
            type Sender<'a> = A::Sender<'a> where Self: 'a;
            type Receiver<'a> = A::Receiver<'a> where Self: 'a;

            type AcceptFuture<'a>
            = A::AcceptFuture<'a> where Self: 'a;

            fn accept(&self) -> Self::AcceptFuture<'_> {
                (**self).accept()
            }
        }
    }
}