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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
pub mod framer;
pub mod messenger;
pub mod protocol;

use links_core::core::conid::ConnectionId;
use mio::{Interest, Registry, Token};
use std::{
    fmt::{Debug, Display},
    io::Error,
    time::{Duration, Instant},
};

// ---- Acceptor ----

/// Represents the state of a non-blocking accept operation on a pool
///
/// # Variants
///  * [PoolAcceptStatus::Accepted] - indicates that accept was successful
///  * [PoolAcceptStatus::WouldBlock] - indicates that no connection was accepted
#[derive(Debug, PartialEq)]
pub enum PoolAcceptStatus {
    Accepted,
    Rejected,
    WouldBlock,
}
impl PoolAcceptStatus {
    /// Unwraps to [()] if the variant is [PoolAcceptStatus::Accepted], otherwise panics
    #[track_caller]
    pub fn unwrap_accepted(self) {
        match self {
            PoolAcceptStatus::Accepted => (),
            PoolAcceptStatus::Rejected => panic!("PoolAcceptStatus::Rejected"),
            PoolAcceptStatus::WouldBlock => panic!("PoolAcceptStatus::WouldBlock"),
        }
    }
    #[track_caller]
    pub fn unwrap_rejected(self) {
        match self {
            PoolAcceptStatus::Accepted => panic!("PoolAcceptStatus::Accepted"),
            PoolAcceptStatus::Rejected => (),
            PoolAcceptStatus::WouldBlock => panic!("PoolAcceptStatus::WouldBlock"),
        }
    }
    pub fn is_accepted(&self) -> bool {
        match self {
            PoolAcceptStatus::Accepted => true,
            PoolAcceptStatus::Rejected => false,
            PoolAcceptStatus::WouldBlock => false,
        }
    }
    pub fn is_wouldblock(&self) -> bool {
        match self {
            PoolAcceptStatus::Accepted => false,
            PoolAcceptStatus::Rejected => false,
            PoolAcceptStatus::WouldBlock => true,
        }
    }
    pub fn is_rejected(&self) -> bool {
        match self {
            PoolAcceptStatus::Accepted => false,
            PoolAcceptStatus::Rejected => true,
            PoolAcceptStatus::WouldBlock => false,
        }
    }
}
pub trait PoolSvcAcceptorOfCltNonBlocking {
    fn accept_into_pool(&mut self) -> Result<PoolAcceptStatus, Error>;
    /// Will call [PoolSvcAcceptorOfCltNonBlocking::accept_into_pool] until it returns [PoolAcceptStatus::Accepted] or [PoolAcceptStatus::WouldBlock] after the timeout.
    fn accept_into_pool_busywait_timeout(&mut self, timeout: Duration) -> Result<PoolAcceptStatus, Error> {
        use PoolAcceptStatus::{Accepted, Rejected, WouldBlock};
        let start = Instant::now();
        loop {
            match self.accept_into_pool()? {
                Accepted => return Ok(Accepted),
                Rejected => return Ok(Rejected),
                WouldBlock => {
                    if start.elapsed() > timeout {
                        return Ok(WouldBlock);
                    }
                }
            }
        }
    }
    /// Will call [PoolSvcAcceptorOfCltNonBlocking::accept_into_pool] until it returns [PoolAcceptStatus::Accepted]
    fn accept_into_pool_busywait(&mut self) -> Result<PoolAcceptStatus, Error> {
        use PoolAcceptStatus::{Accepted, Rejected, WouldBlock};
        loop {
            match self.accept_into_pool()? {
                Accepted => return Ok(Accepted),
                Rejected => return Ok(Rejected),
                WouldBlock => continue,
            }
        }
    }
}

/// Represents the state of a non-blocking accept operation
/// # Variants
/// * [AcceptStatus::Accepted(T)] - indicates that accept was successful and `T` contains the value accepted
/// * [AcceptStatus::WouldBlock] - indicates that no connection was accepted and the caller should try again
#[derive(Debug, PartialEq)]
pub enum AcceptStatus<T> {
    Accepted(T),
    Rejected,
    WouldBlock,
}
impl<T> AcceptStatus<T> {
    /// Unwraps into `T` if the variant is [AcceptStatus::Accepted], otherwise panics
    #[track_caller]
    pub fn unwrap_accepted(self) -> T {
        match self {
            AcceptStatus::Accepted(t) => t,
            AcceptStatus::Rejected => panic!("AcceptStatus::Rejected"),
            AcceptStatus::WouldBlock => panic!("AcceptStatus::WouldBlock"),
        }
    }
    pub fn is_accepted(&self) -> bool {
        match self {
            AcceptStatus::Accepted(_) => true,
            AcceptStatus::Rejected => false,
            AcceptStatus::WouldBlock => false,
        }
    }
    pub fn is_wouldblock(&self) -> bool {
        match self {
            AcceptStatus::Accepted(_) => false,
            AcceptStatus::Rejected => false,
            AcceptStatus::WouldBlock => true,
        }
    }
    pub fn is_rejected(&self) -> bool {
        match self {
            AcceptStatus::Accepted(_) => false,
            AcceptStatus::Rejected => true,
            AcceptStatus::WouldBlock => false,
        }
    }
}
pub trait SvcAcceptorOfCltNonBlocking<T> {
    fn accept(&self) -> Result<AcceptStatus<T>, Error>;

    fn accept_busywait_timeout(&self, timeout: Duration) -> Result<AcceptStatus<T>, Error> {
        use AcceptStatus::{Accepted, Rejected, WouldBlock};
        let start = Instant::now();
        loop {
            match self.accept()? {
                Accepted(t) => return Ok(Accepted(t)),
                Rejected => return Ok(Rejected),
                WouldBlock => {
                    if start.elapsed() > timeout {
                        return Ok(WouldBlock);
                    }
                }
            }
        }
    }

    fn accept_busywait(&self) -> Result<T, Error> {
        use AcceptStatus::{Accepted, Rejected, WouldBlock};
        loop {
            match self.accept()? {
                Accepted(clt) => return Ok(clt),
                Rejected => continue,
                WouldBlock => continue,
            }
        }
    }
}

// ---- Recver ----

/// Represents the state of a non-blocking read operation
///
/// # Variants
/// * [RecvStatus::Completed(Some(T))] - indicates that read was successful and `T` contains the value read
/// * [RecvStatus::Completed(None)] - indicates that connection was closed by the peer cleanly and all data was read
/// * [RecvStatus::WouldBlock] - indicates that no data was read and the caller should try again
#[derive(Debug, PartialEq)]
pub enum RecvStatus<T> {
    Completed(Option<T>),
    WouldBlock,
}
impl<T> RecvStatus<T> {
    /// Will panic if the variant is [RecvStatus::WouldBlock], otherwise unwraps into [`Option<T>`] from [RecvStatus::Completed(`Option<T>`)]
    #[track_caller]
    pub fn unwrap_completed_none(self) {
        match self {
            RecvStatus::Completed(Some(_)) => panic!("ReadStatus::Completed(Some(_))"),
            RecvStatus::Completed(None) => (),
            RecvStatus::WouldBlock => panic!("ReadStatus::WouldBlock"),
        }
    }
    /// Will panic if the variant is [RecvStatus::WouldBlock] or [RecvStatus::Completed(None)],  otherwise unwraps into `T` from [RecvStatus::Completed(Some(T))]
    #[track_caller]
    pub fn unwrap_completed_some(self) -> T {
        match self {
            RecvStatus::Completed(Some(t)) => t,
            RecvStatus::Completed(None) => panic!("ReadStatus::Completed(None)"),
            RecvStatus::WouldBlock => panic!("ReadStatus::WouldBlock"),
        }
    }
    #[track_caller]
    pub fn unwrap_wouldblock(self) {
        match self {
            RecvStatus::Completed(_) => panic!("ReadStatus::Completed(_)"),
            RecvStatus::WouldBlock => {}
        }
    }
    pub fn is_completed(&self) -> bool {
        match self {
            RecvStatus::Completed(_) => true,
            RecvStatus::WouldBlock => false,
        }
    }
    pub fn is_completed_none(&self) -> bool {
        match self {
            RecvStatus::Completed(Some(_)) => false,
            RecvStatus::Completed(None) => true,
            RecvStatus::WouldBlock => false,
        }
    }
    pub fn is_completed_some(&self) -> bool {
        match self {
            RecvStatus::Completed(Some(_)) => true,
            RecvStatus::Completed(None) => false,
            RecvStatus::WouldBlock => false,
        }
    }
    pub fn is_wouldblock(&self) -> bool {
        match self {
            RecvStatus::Completed(_) => false,
            RecvStatus::WouldBlock => true,
        }
    }
}

pub trait RecvNonBlocking<T>: Debug + Display {
    /// Will attempt to read a message from the stream. Each call to this method will
    /// attempt to read data from the stream via system call and if sufficient number of bytes were read to
    /// make a single frame it will attempt to deserialize it into a message and return it
    fn recv(&mut self) -> Result<RecvStatus<T>, Error>;

    /// Will call [Self::recv] until it returns [RecvStatus::Completed] or [RecvStatus::WouldBlock] after the timeout.
    fn recv_busywait_timeout(&mut self, timeout: Duration) -> Result<RecvStatus<T>, Error> {
        use RecvStatus::{Completed, WouldBlock};
        let start = Instant::now();
        loop {
            match self.recv()? {
                Completed(o) => return Ok(Completed(o)),
                WouldBlock => {
                    if start.elapsed() > timeout {
                        return Ok(WouldBlock);
                    }
                    std::hint::spin_loop()
                }
            }
        }
    }
    /// Will busywait block on [Self::recv] until it returns [RecvStatus::Completed]
    fn recv_busywait(&mut self) -> Result<Option<T>, Error> {
        use RecvStatus::{Completed, WouldBlock};
        loop {
            match self.recv()? {
                Completed(o) => return Ok(o),
                WouldBlock => std::hint::spin_loop(),
            }
        }
    }
}

// ---- Sender ----

/// Represents the state of the write operation
///
/// # Variants
///    * [SendStatus::Completed] - indicates that all bytes were written to the underlying stream
///    * [SendStatus::WouldBlock] - indicates that zero bytes were written to the underlying stream
#[derive(Debug, PartialEq)]
pub enum SendStatus {
    Completed,
    WouldBlock,
}
impl SendStatus {
    /// Will panic if the variant is [SendStatus::WouldBlock], otherwise unwraps into [()] from [SendStatus::Completed]
    #[inline(always)]
    #[track_caller]
    pub fn unwrap_completed(self) {
        match self {
            SendStatus::Completed => {}
            SendStatus::WouldBlock => panic!("SendStatus::WouldBlock"),
        }
    }
    pub fn is_completed(&self) -> bool {
        match self {
            SendStatus::Completed => true,
            SendStatus::WouldBlock => false,
        }
    }
    pub fn is_wouldblock(&self) -> bool {
        !self.is_completed()
    }
}

pub trait SendNonBlocking<T>: Debug + Display {
    /// The call will internally serialize the `T` and attempt to write the resulting bytes into a stream.
    /// If there was a successfull attempt which wrote some, not all, bytes from serialized message
    /// into the stream and hence the write was only partial, the call will busy wait until all of
    /// remaining bytes are written before returning [SendStatus::Completed].
    /// [SendStatus::WouldBlock] is returned only if the attempt did not write any bytes to the stream
    /// after the first attempt
    ///
    /// # Important
    /// * The implementation will trigger [crate::prelude::Protocol] & [links_core::prelude::CallbackSend] hooks
    fn send(&mut self, msg: &mut T) -> Result<SendStatus, Error>;

    /// Will call [Self::send] until it returns [SendStatus::Completed] or [SendStatus::WouldBlock] after the timeout,
    /// while propagating all errors from [Self::send]
    ///
    /// # Warning
    /// Consider overriding this default implementation if your [Self::send] implementation issues callback functions
    /// calls which must be called once and only once.
    #[inline(always)]
    fn send_busywait_timeout(&mut self, msg: &mut T, timeout: Duration) -> Result<SendStatus, Error> {
        use SendStatus::{Completed, WouldBlock};
        let start = Instant::now();
        loop {
            match self.send(msg)? {
                Completed => return Ok(Completed),
                WouldBlock => {
                    if start.elapsed() > timeout {
                        return Ok(WouldBlock);
                    }
                    std::hint::spin_loop();
                }
            }
        }
    }
    /// Will call [Self::send] until it returns [SendStatus::Completed]
    ///
    /// # Warning
    /// Consider overriding this default implementation if your [Self::send] implementation issues callback functions
    /// calls which must be called once and only once.
    #[inline(always)]
    fn send_busywait(&mut self, msg: &mut T) -> Result<(), Error> {
        use SendStatus::{Completed, WouldBlock};
        loop {
            match self.send(msg)? {
                Completed => return Ok(()),
                WouldBlock => std::hint::spin_loop(),
            }
        }
    }
}

pub trait SendNonBlockingNonMut<T> {
    /// The call will internally serialize the msg and attempt to write the resulting bytes into a stream.
    /// If there was a successfull attempt which wrote some bytes from serialized message
    /// into the stream but the write was only partial then the call will busy wait until all of
    /// remaining bytes were written before returning [SendStatus::Completed].
    /// [SendStatus::WouldBlock] is returned only if the attempt did not write any bytes to the stream
    /// after the first attempt
    fn send(&mut self, msg: &T) -> Result<SendStatus, Error>;

    /// Will call [`Self::send`] until it returns [SendStatus::Completed] or [SendStatus::WouldBlock] after the timeout,
    #[inline(always)]
    fn send_busywait_timeout(&mut self, msg: &T, timeout: Duration) -> Result<SendStatus, Error> {
        let start = Instant::now();
        loop {
            match self.send(msg)? {
                SendStatus::Completed => return Ok(SendStatus::Completed),
                SendStatus::WouldBlock => {
                    if start.elapsed() > timeout {
                        return Ok(SendStatus::WouldBlock);
                    }
                    std::hint::spin_loop()
                }
            }
        }
    }
    /// Will call [`Self::send`] until it returns [SendStatus::Completed]
    #[inline(always)]
    fn send_busywait(&mut self, msg: &T) -> Result<(), Error> {
        use SendStatus::{Completed, WouldBlock};
        loop {
            match self.send(msg)? {
                Completed => return Ok(()),
                WouldBlock => std::hint::spin_loop(),
            }
        }
    }
}

pub trait ReSendNonBlocking<T> {
    /// The call will internally serialize the msg and attempt to write the resulting bytes into a stream.
    /// If there was a successfull attempt which wrote some bytes from serialized message
    /// into the stream but the write was only partial then the call will busy wait until all of
    /// remaining bytes were written before returning [SendStatus::Completed].
    /// [SendStatus::WouldBlock] is returned only if the attempt did not write any bytes to the stream
    /// after the first attempt
    ///
    /// # Important
    /// * The implementation will Not issue [crate::prelude::Protocol] & [links_core::prelude::CallbackSend] hooks
    fn re_send(&mut self, msg: &T) -> Result<SendStatus, Error>;

    /// Will call [`Self::re_send`] until it returns [SendStatus::Completed] or [SendStatus::WouldBlock] after the timeout,
    #[inline(always)]
    fn re_send_busywait_timeout(&mut self, msg: &T, timeout: Duration) -> Result<SendStatus, Error> {
        let start = Instant::now();
        loop {
            match self.re_send(msg)? {
                SendStatus::Completed => return Ok(SendStatus::Completed),
                SendStatus::WouldBlock => {
                    if start.elapsed() > timeout {
                        return Ok(SendStatus::WouldBlock);
                    }
                    std::hint::spin_loop()
                }
            }
        }
    }
    /// Will call [`Self::re_send`] until it returns [SendStatus::Completed]
    #[inline(always)]
    fn re_send_busywait(&mut self, msg: &T) -> Result<(), Error> {
        use SendStatus::{Completed, WouldBlock};
        loop {
            match self.re_send(msg)? {
                Completed => return Ok(()),
                WouldBlock => std::hint::spin_loop(),
            }
        }
    }
}

// ---- Pool ----

#[derive(Debug)]
pub enum PollEventStatus {
    Completed,
    WouldBlock,
    Terminate,
}
pub trait PollAble: ConnectionId + Display + Send + 'static {
    /// this function exists as a hook in case you need to perform resource locking prior to registering the source
    ///
    /// # Warning
    /// [PollAble::source] usage will depend on your override implementation and may not used
    fn register(&mut self, registry: &Registry, token: Token, interests: Interest) -> Result<(), Error> {
        registry.register(*self.source(), token, interests)?;
        Ok(())
    }
    /// this function exists as a hook in case you need to perform resource locking prior to de-registering the source
    ///
    /// # Warning
    /// [PollAble::source] usage will depend on your override implementation and may not used
    fn deregister(&mut self, registry: &Registry) -> Result<(), Error> {
        registry.deregister(*self.source())?;
        Ok(())
    }
    /// represents the source of the event, typically implementing this function is sufficient as [PollAble::register] and [PollAble::deregister] functions
    /// are implemented using it to get the source for the poll. However You can choose to override [PollAble::register] and [PollAble::deregister] functions
    /// when for example you require to lock a mutes to get access to the source in which case source function will not be used.
    fn source(&mut self) -> Box<&mut dyn mio::event::Source>;
}
/// A trait to be implemented for a type that can be registered with a [mio::Poll] instance, it helps to
/// abstract away details of [Token] generation registration and de-registration
/// The source will get automatically deregistered from the poll instance when [PollRead::on_readable_event]
/// returns [Ok(PollEventStatus::Terminate)] or [Err(_)]
pub trait PollRead: PollAble {
    // /// this function exists as a hook in case you need to perform resource locking prior to registering the source
    // ///
    // /// # Warning
    // /// [PollRead::source] usage will depend on your override implementation and may not used
    // fn register(&mut self, registry: &Registry, token: Token, interests: Interest) -> Result<(), Error> {
    //     registry.register(*self.source(), token, interests)?;
    //     Ok(())
    // }
    // /// this function exists as a hook in case you need to perform resource locking prior to de-registering the source
    // ///
    // /// # Warning
    // /// [PollRead::source] usage will depend on your override implementation and may not used
    // fn deregister(&mut self, registry: &Registry) -> Result<(), Error> {
    //     registry.deregister(*self.source())?;
    //     Ok(())
    // }
    // /// represents the source of the event, typically implementing this function is sufficient as [PollRead::register] and [PollRead::deregister] functions
    // /// are implemented using it to get the source for the poll. However You can choose to override [PollRead::register] and [PollRead::deregister] functions
    // /// when for example you require to lock a mutes to get access to the source in which case source function will not be used.
    // fn source(&mut self) -> Box<&mut dyn mio::event::Source>;
    /// Will be called when OS signals that the source is readable
    fn on_readable_event(&mut self) -> Result<PollEventStatus, Error>;
}

/// A trait to be implemented for a type that can be registered with a [mio::Poll] instance, it helps to
/// abstract away details of [Token] generation registration and de-registration specifically monitoring
pub trait PollAccept<R: PollRead>: PollAble {
    fn poll_accept(&mut self) -> Result<AcceptStatus<R>, Error>;
}