sfo-cmd-server 0.3.2

command server implement
Documentation
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
use crate::TunnelId;
use crate::errors::{CmdErrorCode, CmdResult, cmd_err, into_cmd_err};
use crate::peer_id::PeerId;
use bucky_raw_codec::{RawDecode, RawEncode};
use callback_result::SingleCallbackWaiter;
use futures_lite::ready;
use num::{FromPrimitive, ToPrimitive};
use sfo_split::RHalf;
use std::collections::HashMap;
use std::fmt::Debug;
use std::hash::Hash;
use std::ops::DerefMut;
use std::pin::Pin;
use std::sync::{Arc, Mutex};
use std::task::{Context, Poll};
use std::{fmt, io};
use tokio::io::{AsyncBufRead, AsyncRead, AsyncReadExt, AsyncWrite, ReadBuf};

#[derive(RawEncode, RawDecode)]
pub struct CmdHeader<LEN, CMD> {
    pkg_len: LEN,
    version: u8,
    cmd_code: CMD,
    is_resp: bool,
    seq: Option<u32>,
}

impl<
    LEN: RawEncode + for<'a> RawDecode<'a> + Copy + Send + Sync + 'static + FromPrimitive + ToPrimitive,
    CMD: RawEncode + for<'a> RawDecode<'a> + Copy + Send + Sync + 'static,
> CmdHeader<LEN, CMD>
{
    pub fn new(version: u8, is_resp: bool, seq: Option<u32>, cmd_code: CMD, pkg_len: LEN) -> Self {
        Self {
            pkg_len,
            version,
            seq,
            cmd_code,
            is_resp,
        }
    }

    pub fn pkg_len(&self) -> LEN {
        self.pkg_len
    }

    pub fn version(&self) -> u8 {
        self.version
    }

    pub fn seq(&self) -> Option<u32> {
        self.seq
    }

    pub fn is_resp(&self) -> bool {
        self.is_resp
    }

    pub fn cmd_code(&self) -> CMD {
        self.cmd_code
    }

    pub fn set_pkg_len(&mut self, pkg_len: LEN) {
        self.pkg_len = pkg_len;
    }
}

#[async_trait::async_trait]
pub trait CmdBodyReadAll: tokio::io::AsyncRead + Send + 'static {
    async fn read_all(&mut self) -> CmdResult<Vec<u8>>;
}

pub(crate) struct CmdBodyRead<
    R: AsyncRead + Send + 'static + Unpin,
    W: AsyncWrite + Send + 'static + Unpin,
> {
    recv: Option<RHalf<R, W>>,
    len: usize,
    offset: usize,
    waiter: Arc<SingleCallbackWaiter<CmdResult<RHalf<R, W>>>>,
}

impl<R: AsyncRead + Send + 'static + Unpin, W: AsyncWrite + Send + 'static + Unpin>
    CmdBodyRead<R, W>
{
    pub fn new(recv: RHalf<R, W>, len: usize) -> Self {
        Self {
            recv: Some(recv),
            len,
            offset: 0,
            waiter: Arc::new(SingleCallbackWaiter::new()),
        }
    }

    pub(crate) fn get_waiter(&self) -> Arc<SingleCallbackWaiter<CmdResult<RHalf<R, W>>>> {
        self.waiter.clone()
    }
}

#[async_trait::async_trait]
impl<R: AsyncRead + Send + 'static + Unpin, W: AsyncWrite + Send + 'static + Unpin> CmdBodyReadAll
    for CmdBodyRead<R, W>
{
    async fn read_all(&mut self) -> CmdResult<Vec<u8>> {
        if self.offset == self.len {
            return Ok(Vec::new());
        }
        let mut buf = vec![0u8; self.len - self.offset];
        let ret = self
            .recv
            .as_mut()
            .unwrap()
            .read_exact(&mut buf)
            .await
            .map_err(into_cmd_err!(CmdErrorCode::IoError));
        if ret.is_ok() {
            self.offset = self.len;
            self.waiter
                .set_result_with_cache(Ok(self.recv.take().unwrap()));
            Ok(buf)
        } else {
            self.recv.take();
            self.waiter
                .set_result_with_cache(Err(cmd_err!(CmdErrorCode::IoError, "read body error")));
            Err(ret.err().unwrap())
        }
    }
}

impl<R: AsyncRead + Send + 'static + Unpin, W: AsyncWrite + Send + 'static + Unpin> Drop
    for CmdBodyRead<R, W>
{
    fn drop(&mut self) {
        if self.recv.is_none() || (self.len == self.offset && self.len != 0) {
            return;
        }
        let mut recv = self.recv.take().unwrap();
        let len = self.len - self.offset;
        let waiter = self.waiter.clone();
        if len == 0 {
            waiter.set_result_with_cache(Ok(recv));
            return;
        }

        tokio::spawn(async move {
            let mut buf = vec![0u8; len];
            if let Err(e) = recv.read_exact(&mut buf).await {
                waiter.set_result_with_cache(Err(cmd_err!(
                    CmdErrorCode::IoError,
                    "read body error {}",
                    e
                )));
            } else {
                waiter.set_result_with_cache(Ok(recv));
            }
        });
    }
}

impl<R: AsyncRead + Send + 'static + Unpin, W: AsyncWrite + Send + 'static + Unpin>
    tokio::io::AsyncRead for CmdBodyRead<R, W>
{
    fn poll_read(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &mut ReadBuf<'_>,
    ) -> Poll<std::io::Result<()>> {
        let this = Pin::into_inner(self);
        let len = this.len - this.offset;
        if len == 0 {
            return Poll::Ready(Ok(()));
        }
        let recv = Pin::new(this.recv.as_mut().unwrap().deref_mut());
        let read_len = std::cmp::min(len, buf.remaining());
        let mut read_buf = ReadBuf::new(buf.initialize_unfilled_to(read_len));
        let fut = recv.poll_read(cx, &mut read_buf);
        match fut {
            Poll::Ready(Ok(())) => {
                let len = read_buf.filled().len();
                drop(read_buf);
                this.offset += len;
                buf.advance(len);
                if this.offset == this.len {
                    this.waiter
                        .set_result_with_cache(Ok(this.recv.take().unwrap()));
                }
                Poll::Ready(Ok(()))
            }
            Poll::Ready(Err(e)) => {
                this.recv.take();
                this.waiter
                    .set_result_with_cache(Err(cmd_err!(CmdErrorCode::IoError, "read body error")));
                Poll::Ready(Err(e))
            }
            Poll::Pending => Poll::Pending,
        }
    }
}

#[callback_trait::callback_trait]
pub trait CmdHandler<LEN, CMD>: Send + Sync + 'static
where
    LEN: RawEncode
        + for<'a> RawDecode<'a>
        + Copy
        + Send
        + Sync
        + 'static
        + FromPrimitive
        + ToPrimitive,
    CMD: RawEncode + for<'a> RawDecode<'a> + Copy + Send + Sync + 'static,
{
    async fn handle(
        &self,
        local_id: PeerId,
        peer_id: PeerId,
        tunnel_id: TunnelId,
        header: CmdHeader<LEN, CMD>,
        body: CmdBody,
    ) -> CmdResult<Option<CmdBody>>;
}

pub(crate) struct CmdHandlerMap<LEN, CMD> {
    map: Mutex<HashMap<CMD, Arc<dyn CmdHandler<LEN, CMD>>>>,
}

impl<LEN, CMD> CmdHandlerMap<LEN, CMD>
where
    LEN: RawEncode
        + for<'a> RawDecode<'a>
        + Copy
        + Send
        + Sync
        + 'static
        + FromPrimitive
        + ToPrimitive,
    CMD: RawEncode + for<'a> RawDecode<'a> + Copy + Send + Sync + 'static + Eq + Hash,
{
    pub fn new() -> Self {
        Self {
            map: Mutex::new(HashMap::new()),
        }
    }

    pub fn insert(&self, cmd: CMD, handler: impl CmdHandler<LEN, CMD>) {
        self.map.lock().unwrap().insert(cmd, Arc::new(handler));
    }

    pub fn get(&self, cmd: CMD) -> Option<Arc<dyn CmdHandler<LEN, CMD>>> {
        self.map.lock().unwrap().get(&cmd).map(|v| v.clone())
    }
}
pin_project_lite::pin_project! {
pub struct CmdBody {
        #[pin]
        reader: Box<dyn AsyncBufRead + Unpin + Send  + 'static>,
        length: u64,
        bytes_read: u64,
    }
}

impl CmdBody {
    pub fn empty() -> Self {
        Self {
            reader: Box::new(tokio::io::empty()),
            length: 0,
            bytes_read: 0,
        }
    }

    pub fn from_reader(reader: impl AsyncBufRead + Unpin + Send + 'static, length: u64) -> Self {
        Self {
            reader: Box::new(reader),
            length,
            bytes_read: 0,
        }
    }

    pub fn into_reader(self) -> Box<dyn AsyncBufRead + Unpin + Send + 'static> {
        self.reader
    }

    pub async fn read_all(&mut self) -> CmdResult<Vec<u8>> {
        let mut buf = Vec::with_capacity(1024);
        self.read_to_end(&mut buf)
            .await
            .map_err(into_cmd_err!(CmdErrorCode::Failed, "read to end failed"))?;
        Ok(buf)
    }

    pub fn from_bytes(bytes: Vec<u8>) -> Self {
        Self {
            length: bytes.len() as u64,
            reader: Box::new(io::Cursor::new(bytes)),
            bytes_read: 0,
        }
    }

    pub async fn into_bytes(mut self) -> CmdResult<Vec<u8>> {
        let mut buf = Vec::with_capacity(1024);
        self.read_to_end(&mut buf)
            .await
            .map_err(into_cmd_err!(CmdErrorCode::Failed, "read to end failed"))?;
        Ok(buf)
    }

    pub fn from_string(s: String) -> Self {
        Self {
            length: s.len() as u64,
            reader: Box::new(io::Cursor::new(s.into_bytes())),
            bytes_read: 0,
        }
    }

    pub async fn into_string(mut self) -> CmdResult<String> {
        let mut result = String::with_capacity(self.len() as usize);
        self.read_to_string(&mut result)
            .await
            .map_err(into_cmd_err!(CmdErrorCode::Failed, "read to string failed"))?;
        Ok(result)
    }

    pub async fn from_path<P>(path: P) -> io::Result<Self>
    where
        P: AsRef<std::path::Path>,
    {
        let path = path.as_ref();
        let file = tokio::fs::File::open(path).await?;
        Self::from_file(file).await
    }

    pub async fn from_file(file: tokio::fs::File) -> io::Result<Self> {
        let len = file.metadata().await?.len();

        Ok(Self {
            length: len,
            reader: Box::new(tokio::io::BufReader::new(file)),
            bytes_read: 0,
        })
    }

    pub fn len(&self) -> u64 {
        self.length
    }

    /// Returns `true` if the body has a length of zero, and `false` otherwise.
    pub fn is_empty(&self) -> bool {
        self.length == 0
    }

    pub fn chain(self, other: CmdBody) -> Self {
        let length = (self.length - self.bytes_read)
            .checked_add(other.length - other.bytes_read)
            .unwrap_or(0);
        Self {
            length,
            reader: Box::new(tokio::io::AsyncReadExt::chain(self, other)),
            bytes_read: 0,
        }
    }
}

impl Debug for CmdBody {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("CmdResponse")
            .field("reader", &"<hidden>")
            .field("length", &self.length)
            .field("bytes_read", &self.bytes_read)
            .finish()
    }
}

impl From<String> for CmdBody {
    fn from(s: String) -> Self {
        Self::from_string(s)
    }
}

impl<'a> From<&'a str> for CmdBody {
    fn from(s: &'a str) -> Self {
        Self::from_string(s.to_owned())
    }
}

impl From<Vec<u8>> for CmdBody {
    fn from(b: Vec<u8>) -> Self {
        Self::from_bytes(b)
    }
}

impl<'a> From<&'a [u8]> for CmdBody {
    fn from(b: &'a [u8]) -> Self {
        Self::from_bytes(b.to_owned())
    }
}

impl AsyncRead for CmdBody {
    #[allow(rustdoc::missing_doc_code_examples)]
    fn poll_read(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &mut ReadBuf<'_>,
    ) -> Poll<io::Result<()>> {
        let buf = if self.length == self.bytes_read {
            return Poll::Ready(Ok(()));
        } else {
            buf
        };

        ready!(Pin::new(&mut self.reader).poll_read(cx, buf))?;
        self.bytes_read += buf.filled().len() as u64;
        Poll::Ready(Ok(()))
    }
}

impl AsyncBufRead for CmdBody {
    #[allow(rustdoc::missing_doc_code_examples)]
    fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<&'_ [u8]>> {
        self.project().reader.poll_fill_buf(cx)
    }

    fn consume(mut self: Pin<&mut Self>, amt: usize) {
        Pin::new(&mut self.reader).consume(amt)
    }
}

#[cfg(test)]
mod tests {
    use super::{CmdBody, CmdBodyRead, CmdBodyReadAll, CmdHeader};
    use crate::{CmdTunnel, CmdTunnelRead, CmdTunnelWrite, PeerId};
    use std::pin::Pin;
    use std::task::{Context, Poll};
    use tokio::io::{
        AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, DuplexStream, ReadBuf, split,
    };

    struct TestRead {
        read: tokio::io::ReadHalf<DuplexStream>,
    }

    impl AsyncRead for TestRead {
        fn poll_read(
            mut self: Pin<&mut Self>,
            cx: &mut Context<'_>,
            buf: &mut ReadBuf<'_>,
        ) -> Poll<std::io::Result<()>> {
            Pin::new(&mut self.read).poll_read(cx, buf)
        }
    }

    impl CmdTunnelRead<()> for TestRead {
        fn get_local_peer_id(&self) -> PeerId {
            PeerId::from(vec![9; 32])
        }

        fn get_remote_peer_id(&self) -> PeerId {
            PeerId::from(vec![1; 32])
        }
    }

    struct TestWrite {
        write: tokio::io::WriteHalf<DuplexStream>,
    }

    impl AsyncWrite for TestWrite {
        fn poll_write(
            mut self: Pin<&mut Self>,
            cx: &mut Context<'_>,
            buf: &[u8],
        ) -> Poll<std::io::Result<usize>> {
            Pin::new(&mut self.write).poll_write(cx, buf)
        }

        fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
            Pin::new(&mut self.write).poll_flush(cx)
        }

        fn poll_shutdown(
            mut self: Pin<&mut Self>,
            cx: &mut Context<'_>,
        ) -> Poll<std::io::Result<()>> {
            Pin::new(&mut self.write).poll_shutdown(cx)
        }
    }

    impl CmdTunnelWrite<()> for TestWrite {
        fn get_local_peer_id(&self) -> PeerId {
            PeerId::from(vec![9; 32])
        }

        fn get_remote_peer_id(&self) -> PeerId {
            PeerId::from(vec![2; 32])
        }
    }

    #[tokio::test]
    async fn cmd_body_bytes_round_trip() {
        let body = CmdBody::from_bytes(b"hello-body".to_vec());
        let data = body.into_bytes().await.unwrap();
        assert_eq!(data, b"hello-body");
    }

    #[tokio::test]
    async fn cmd_body_string_round_trip() {
        let body = CmdBody::from_string("hello-string".to_owned());
        let s = body.into_string().await.unwrap();
        assert_eq!(s, "hello-string");
    }

    #[tokio::test]
    async fn cmd_body_chain_respects_consumed_prefix() {
        let mut first = CmdBody::from_bytes(b"abc".to_vec());
        let mut buf = [0u8; 1];
        first.read_exact(&mut buf).await.unwrap();
        assert_eq!(&buf, b"a");

        let chained = first.chain(CmdBody::from_bytes(b"XYZ".to_vec()));
        let s = chained.into_string().await.unwrap();
        assert_eq!(s, "bcXYZ");
    }

    #[test]
    fn cmd_body_empty_and_len() {
        let empty = CmdBody::empty();
        assert!(empty.is_empty());
        assert_eq!(empty.len(), 0);

        let body = CmdBody::from_bytes(vec![1, 2, 3, 4]);
        assert!(!body.is_empty());
        assert_eq!(body.len(), 4);
    }

    #[tokio::test]
    async fn cmd_body_into_reader_and_read_all() {
        let mut body = CmdBody::from_string("reader-body".to_owned());
        let all = body.read_all().await.unwrap();
        assert_eq!(all, b"reader-body");

        let body = CmdBody::from_string("reader-body2".to_owned());
        let mut reader = body.into_reader();
        let mut out = Vec::new();
        reader.read_to_end(&mut out).await.unwrap();
        assert_eq!(out, b"reader-body2");
    }

    #[test]
    fn cmd_header_set_pkg_len() {
        let mut header = CmdHeader::<u16, u8>::new(1, false, Some(7), 0x11, 3);
        assert_eq!(header.pkg_len(), 3);
        header.set_pkg_len(9);
        assert_eq!(header.pkg_len(), 9);
    }

    #[tokio::test]
    async fn cmd_body_read_all_success_and_empty_after_read() {
        let (side_a, side_b) = tokio::io::duplex(128);
        let (a_read, a_write) = split(side_a);
        let (_b_read, mut b_write) = split(side_b);
        b_write.write_all(b"abcdef").await.unwrap();
        b_write.flush().await.unwrap();

        let tunnel = CmdTunnel::new(TestRead { read: a_read }, TestWrite { write: a_write });
        let (reader, _writer) = tunnel.split();
        let mut body_read = CmdBodyRead::new(reader, 6);

        let first = body_read.read_all().await.unwrap();
        assert_eq!(first, b"abcdef");
        let second = body_read.read_all().await.unwrap();
        assert!(second.is_empty());
    }

    #[tokio::test]
    async fn cmd_body_read_all_error_when_source_short() {
        let (side_a, side_b) = tokio::io::duplex(128);
        let (a_read, a_write) = split(side_a);
        let (_b_read, mut b_write) = split(side_b);
        b_write.write_all(b"ab").await.unwrap();
        b_write.shutdown().await.unwrap();

        let tunnel = CmdTunnel::new(TestRead { read: a_read }, TestWrite { write: a_write });
        let (reader, _writer) = tunnel.split();
        let mut body_read = CmdBodyRead::new(reader, 5);
        assert!(body_read.read_all().await.is_err());
    }
}