flatline 0.0.11

ssh-2.0 client library
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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
use crate::error::builder;

use super::ssh::common::code::*;
use bytes::Buf;
use bytes::BufMut;
use bytes::BytesMut;
use derive_new::new;
use num_derive::FromPrimitive;
use snafu::OptionExt;
use std::cmp::min;
use std::io;
use std::mem::transmute;
use std::mem::ManuallyDrop;
use std::pin::Pin;
use std::task::ready;
use std::task::Context;
use std::task::Poll;
use tokio::io::AsyncRead;
use tokio::io::AsyncWrite;
use tokio::io::ReadBuf;
use tokio::sync::mpsc::error::TryRecvError;

use super::error::Result;
use super::msg::Request;
use super::{o_channel, BoxFuture, MReceiver, MSender};

#[repr(transparent)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct ChannelOpenFailureReson(pub u32);

impl ChannelOpenFailureReson {
    pub const ADMINISTRATIVELY_PROHIBITED: Self = Self(SSH_OPEN_ADMINISTRATIVELY_PROHIBITED);
    pub const CONNECT_FAILED: Self = Self(SSH_OPEN_CONNECT_FAILED);
    pub const UNKNOWN_CHANNELTYPE: Self = Self(SSH_OPEN_UNKNOWN_CHANNELTYPE);
    pub const RESOURCE_SHORTAGE: Self = Self(SSH_OPEN_RESOURCE_SHORTAGE);
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Signal(pub String);

impl std::fmt::Display for Signal {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.0.fmt(f)
    }
}

impl PartialEq<&str> for Signal {
    fn eq(&self, other: &&str) -> bool {
        self.0 == *other
    }
}

impl<T: Into<String>> From<T> for Signal {
    fn from(value: T) -> Self {
        Self(value.into())
    }
}

impl Signal {
    pub const ABRT: &'static str = "ABRT";
    pub const FPE: &'static str = "FPE";
    pub const HUP: &'static str = "HUP";
    pub const ILL: &'static str = "ILL";
    pub const INT: &'static str = "INT";
    pub const KILL: &'static str = "KILL";
    pub const PIPE: &'static str = "PIPE";
    pub const QUIT: &'static str = "QUIT";
    pub const SEGV: &'static str = "SEGV";
    pub const TERM: &'static str = "TERM";
    pub const USR1: &'static str = "USR1";
    pub const USR2: &'static str = "USR2";
}

#[derive(Debug, Clone, new)]
pub enum ExitStatus {
    Normal(u32),
    Interrupt {
        signal: Signal,
        core_dumped: bool,
        error_msg: String,
        tag: String,
    },
}

// #[derive(Debug)]
pub enum Message {
    /// It means the channel was closed by server, it can't be read or written;
    Close,
    /// It means no more data will be sent by server;
    Eof,
    /// Obviously this is the standard output data, println!() in rust;
    Stdout(Vec<u8>),
    /// Obviously this is the standard error data, eprintln!() in rust;
    Stderr(Vec<u8>),
    /// When the channel::exec is called and the process ends, the server will send this to the client;
    /// it may be sent before the Eof
    Exit(ExitStatus),
}

impl std::fmt::Debug for Message {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Close => write!(f, "Message::Close"),
            Self::Eof => write!(f, "Message::Eof"),
            Self::Stdout(arg0) => write!(f, "Message::Stdout {{ len: {} }}", arg0.len()),
            Self::Stderr(arg0) => write!(f, "Message::Stderr {{ len: {} }}", arg0.len()),
            Self::Exit(arg0) => write!(f, "Message::Exit ( {:?} )", arg0),
        }
    }
}

pub struct Stream {
    channel: Box<Channel>, // boxed due to self referencing

    // be careful, self referencing here
    read_future: Option<BoxFuture<'static, Result<Vec<u8>>>>,

    // be careful, self referencing here
    write_future: Option<BoxFuture<'static, Result<usize>>>,

    stdout: BytesMut,

    stderr: BytesMut,

    pub rw_stdout: bool,

    closed: bool,

    eof: bool,
}

impl Stream {
    pub fn new(channel: Channel) -> Self {
        Self {
            channel: Box::new(channel),
            read_future: None,
            write_future: None,
            stdout: BytesMut::with_capacity(4096),
            stderr: BytesMut::with_capacity(4096),
            rw_stdout: true,
            closed: false,
            eof: false,
        }
    }

    pub async fn close(self) -> Result<()> {
        self.channel.close().await
    }
}

impl AsyncWrite for Stream {
    fn poll_write(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &[u8],
    ) -> Poll<io::Result<usize>> {
        if self.write_future.is_none() {
            let future = self.channel.write(buf);
            let future: BoxFuture<'_, Result<usize>> = Box::pin(future);

            self.write_future = unsafe {
                transmute::<
                    Option<BoxFuture<'_, Result<usize>>>,
                    Option<BoxFuture<'_, Result<usize>>>,
                >(Some(future))
            };
        }
        // we can't return Ok(0) here because Ok(0) means error in tokio::io::copy_bidirectional;
        loop {
            let res = ready!(self.write_future.as_mut().unwrap().as_mut().poll(cx));
            self.write_future = None;
            match res {
                Ok(0) => {
                    let future = async {
                        tokio::task::yield_now().await;
                        self.channel.write(buf).await
                    };
                    let future: BoxFuture<'_, Result<usize>> = Box::pin(future);

                    self.write_future = unsafe {
                        transmute::<
                            Option<BoxFuture<'_, Result<usize>>>,
                            Option<BoxFuture<'_, Result<usize>>>,
                        >(Some(future))
                    };
                }
                Ok(size) => break Poll::Ready(Ok(size)),
                Err(err) => {
                    break Poll::Ready(Err(io::Error::new(io::ErrorKind::Other, Box::new(err))))
                }
            }
        }

        // Poll::Ready(match res {
        //     Ok(size) => Ok(size),
        //     Err(err) => Err(io::Error::new(io::ErrorKind::Other, Box::new(err))),
        // })
    }

    fn poll_flush(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<io::Result<()>> {
        Poll::Ready(Ok(()))
    }

    fn poll_shutdown(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<io::Result<()>> {
        Poll::Ready(Ok(()))
    }
}

impl AsyncRead for Stream {
    fn poll_read(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &mut ReadBuf<'_>,
    ) -> Poll<io::Result<()>> {
        if self.read_future.is_none() {
            let f = self.read_maximum(buf.remaining());
            let f: Option<BoxFuture<'_, Result<Vec<u8>>>> = Some(Box::pin(f));

            self.read_future = unsafe {
                transmute::<
                    Option<BoxFuture<'_, Result<Vec<u8>>>>,
                    Option<BoxFuture<'_, Result<Vec<u8>>>>,
                >(f)
            };
        }

        let res = ready!(self.read_future.as_mut().unwrap().as_mut().poll(cx));
        self.read_future = None;
        Poll::Ready(match res {
            Ok(data) => {
                buf.put_slice(&data);
                Ok(())
            }
            Err(err) => Err(io::Error::new(io::ErrorKind::Other, Box::new(err))),
        })
    }
}

impl Stream {
    async fn read_maximum(&mut self, max: usize) -> Result<Vec<u8>> {
        loop {
            if self.rw_stdout {
                if self.stdout.is_empty() {
                    if self.closed {
                        // return Err(Error::ChannelClosed);
                        return builder::ChannelClosed.fail();
                    }
                    if self.eof {
                        return Ok(vec![]);
                    }
                    let msg = self.channel.recv().await?;
                    // println!("msg from channel: {:?}", msg);
                    match msg {
                        Message::Close => {
                            self.closed = true;
                            // return Err(Error::ChannelClosed);
                            return builder::ChannelClosed.fail();
                        }
                        Message::Eof => {
                            self.eof = true;
                            return Ok(vec![]);
                        }
                        Message::Stdout(mut data) => {
                            if data.len() <= max {
                                return Ok(data);
                            } else {
                                self.stdout.extend(&data[max..]);
                                data.truncate(max);
                                return Ok(data);
                            }
                        }
                        Message::Stderr(data) => {
                            self.stderr.extend(data);
                        }
                        Message::Exit(_) => continue,
                    }
                } else {
                    let min = min(max, self.stdout.len());
                    return Ok(self.stdout.split_to(min).to_vec());
                }
            } else if self.stderr.is_empty() {
                if self.closed {
                    // return Err(Error::ChannelClosed);
                    return builder::ChannelClosed.fail();
                }
                if self.eof {
                    return Ok(vec![]);
                }
                let msg = self.channel.recv().await?;
                match msg {
                    Message::Close => {
                        self.closed = true;
                        // return Err(Error::ChannelClosed);
                        return builder::ChannelClosed.fail();
                    }
                    Message::Eof => {
                        self.eof = true;
                        return Ok(vec![]);
                    }
                    Message::Stdout(data) => {
                        self.stdout.extend(data);
                    }
                    Message::Stderr(mut data) => {
                        if data.len() <= max {
                            return Ok(data);
                        } else {
                            self.stderr.extend(&data[max..]);
                            data.truncate(max);
                            return Ok(data);
                        }
                    }
                    Message::Exit(_) => continue,
                }
            } else {
                let min = min(max, self.stderr.len());
                return Ok(self.stderr.split_to(min).to_vec());
            }
        }
    }
}

#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, FromPrimitive)]
pub enum TerminalMode {
    // #[allow(non_camel_case_types)]
    // TTY_OP_END,
    VINTR = 1,
    VQUIT = 2,
    VERASE = 3,
    VKILL = 4,
    VEOF = 5,
    VEOL = 6,
    VEOL2 = 7,
    VSTART = 8,
    VSTOP = 9,
    VSUSP = 10,
    VDSUSP = 11,
    VREPRINT = 12,
    VWERASE = 13,
    VLNEXT = 14,
    VFLUSH = 15,
    VSWTCH = 16,
    VSTATUS = 17,
    VDISCARD = 18,
    IGNPAR = 30,
    PARMRK = 31,
    INPCK = 32,
    ISTRIP = 33,
    INLCR = 34,
    IGNCR = 35,
    ICRNL = 36,
    IUCLC = 37,
    IXON = 38,
    IXANY = 39,
    IXOFF = 40,
    IMAXBEL = 41,
    ISIG = 50,
    ICANON = 51,
    XCASE = 52,
    ECHO = 53,
    ECHOE = 54,
    ECHOK = 55,
    ECHONL = 56,
    NOFLSH = 57,
    TOSTOP = 58,
    IEXTEN = 59,
    ECHOCTL = 60,
    ECHOKE = 61,
    PENDIN = 62,
    OPOST = 70,
    OLCUC = 71,
    ONLCR = 72,
    OCRNL = 73,
    ONOCR = 74,
    ONLRET = 75,
    CS7 = 90,
    CS8 = 91,
    PARENB = 92,
    PARODD = 93,
    #[allow(non_camel_case_types)]
    TTY_OP_ISPEED = 128,
    #[allow(non_camel_case_types)]
    TTY_OP_OSPEED = 129,
}

pub(crate) struct BufferChannel {
    channel: Channel,
    stdout: BytesMut,
    stderr: BytesMut,
    stdin: BytesMut,

    closed: bool,

    eof: bool,
}

impl BufferChannel {
    async fn recv(&mut self) -> Result<()> {
        if self.closed {
            // return Err(Error::ChannelClosed);
            return builder::ChannelClosed.fail();
        }
        if self.eof {
            // return Err(Error::ChannelEof);
            return builder::ChannelEof.fail();
        }
        loop {
            let msg = self.channel.recv().await?;
            match msg {
                Message::Close => {
                    self.closed = true;
                    // return Err(Error::ChannelClosed);
                    return builder::ChannelClosed.fail();
                }
                Message::Eof => {
                    self.eof = true;
                    // return Err(Error::ChannelEof);
                    return builder::ChannelEof.fail();
                }
                Message::Stdout(data) => {
                    self.stdout.put(data.as_ref());
                    return Ok(());
                }
                Message::Stderr(data) => {
                    self.stderr.put(data.as_ref());
                }
                Message::Exit(_) => continue,
            }
        }
    }

    pub(crate) fn new(channel: Channel) -> Self {
        let default_size = 8 * 1024;
        Self {
            channel,
            stdout: BytesMut::with_capacity(default_size),
            stderr: BytesMut::with_capacity(default_size),
            stdin: BytesMut::with_capacity(default_size),
            closed: false,
            eof: false,
        }
    }

    pub(crate) fn into_inner(self) -> Channel {
        self.channel
    }

    pub(crate) async fn fill(&mut self, len: usize) -> Result<&[u8]> {
        while self.stdout.len() < len {
            self.recv().await?;
        }

        Ok(&self.stdout[..len])
    }

    pub(crate) fn consume(&mut self, len: usize) {
        self.stdout.advance(len);
    }

    pub(crate) async fn write(&mut self, data: impl AsRef<[u8]>) -> Result<()> {
        if self.closed {
            // return Err(Error::ChannelClosed);
            return builder::ChannelClosed.fail();
        }
        self.stdin.put(data.as_ref());

        let len = self.channel.write(self.stdin.to_vec()).await?;

        drop(self.stdin.split_to(len));
        Ok(())
    }

    pub(crate) async fn write_all(&mut self, data: impl AsRef<[u8]>) -> Result<()> {
        if self.closed {
            // return Err(Error::ChannelClosed);
            return builder::ChannelClosed.fail();
        }
        self.stdin.put(data.as_ref());

        while !self.stdin.is_empty() {
            let len = self.channel.write(self.stdin.to_vec()).await?;

            self.stdin.advance(len)
        }
        Ok(())
    }

    pub(crate) async fn flush(&mut self) -> Result<()> {
        while !self.stdin.is_empty() {
            let len = self.channel.write(self.stdin.to_vec()).await?;

            self.stdin.advance(len);
        }
        Ok(())
    }
}

pub struct Channel {
    pub(crate) id: u32,
    recver: ManuallyDrop<Option<MReceiver<Message>>>,
    session: ManuallyDrop<MSender<Request>>,
}

pub struct Receiver {
    recver: MReceiver<Message>,
}

impl Receiver {
    pub async fn recv(&mut self) -> Result<Message> {
        self.recver.recv().await.context(builder::Disconnected)
    }

    pub fn try_recv(&mut self) -> Result<Option<Message>> {
        match self.recver.try_recv() {
            Ok(msg) => Ok(Some(msg)),
            Err(TryRecvError::Empty) => Ok(None),
            Err(TryRecvError::Disconnected) => builder::Disconnected.fail(), // Err(Error::Disconnected),
        }
    }
}

impl Drop for Channel {
    fn drop(&mut self) {
        let _ = self.session.send(Request::ChannelDrop {
            id: self.id,
            sender: None,
        });
        self.manually_drop()
    }
}

impl Channel {
    pub(crate) fn new(id: u32, channel: MReceiver<Message>, session: MSender<Request>) -> Self {
        Self {
            id,
            recver: ManuallyDrop::new(Some(channel)),
            session: ManuallyDrop::new(session),
        }
    }

    fn manually_drop(&mut self) {
        unsafe {
            ManuallyDrop::drop(&mut self.recver);
            ManuallyDrop::drop(&mut self.session);
        }
    }

    async fn close_without_drop(&self) -> Result<()> {
        let (sender, recver) = o_channel();
        self.send_request(Request::ChannelDrop {
            id: self.id,
            sender: Some(sender),
        })?;
        recver.await?
    }

    pub async fn close(mut self) -> Result<()> {
        let res = self.close_without_drop().await;
        self.manually_drop();
        std::mem::forget(self);
        res
    }

    pub async fn send_signal(&self, signal: impl Into<Signal>) -> Result<()> {
        let (sender, recver) = o_channel();
        let request = Request::ChannelSendSignal {
            id: self.id,
            signal: signal.into(),
            sender,
        };

        self.send_request(request)?;

        recver.await?
    }

    pub async fn set_env(&self, name: impl Into<String>, value: impl Into<Vec<u8>>) -> Result<()> {
        let name = name.into();
        let value = value.into();
        let (sender, recver) = o_channel();
        let request = Request::ChannelSetEnv {
            id: self.id,
            name,
            value,
            sender,
        };

        self.send_request(request)?;

        recver.await?
    }

    pub async fn exec(&self, cmd: impl Into<String>) -> Result<()> {
        let (sender, recver) = o_channel();
        let request = Request::ChannelExec {
            id: self.id,
            cmd: cmd.into(),
            sender,
        };

        self.send_request(request)?;

        recver.await?
    }

    pub fn merge_receiver(&mut self, recver: Receiver) -> std::result::Result<(), Receiver> {
        if self.recver.is_some() {
            return Err(recver);
        }
        *self.recver = Some(recver.recver);
        Ok(())
    }

    pub fn take_receiver(&mut self) -> Option<Receiver> {
        self.recver.take().map(|r| Receiver { recver: r })
    }

    pub fn has_receiver(&self) -> bool {
        self.recver.is_some()
    }

    pub async fn recv(&mut self) -> Result<Message> {
        self.recver
            .as_mut()
            .context(builder::ChannelReceiverIsNone)?
            .recv()
            .await
            .context(builder::Disconnected)
        // .ok_or(Error::Disconnected)
    }

    pub fn try_recv(&mut self) -> Result<Option<Message>> {
        match self
            .recver
            .as_mut()
            .context(builder::ChannelReceiverIsNone)?
            .try_recv()
        {
            Ok(msg) => Ok(Some(msg)),
            Err(TryRecvError::Empty) => Ok(None),
            Err(TryRecvError::Disconnected) => builder::Disconnected.fail(), //Err(Error::Disconnected),
        }
    }

    pub async fn write(&self, data: impl Into<Vec<u8>>) -> Result<usize> {
        let data: Vec<u8> = data.into();
        let (sender, recver) = o_channel();
        let request = Request::ChannelWriteStdout {
            id: self.id,
            data,
            sender,
        };

        self.send_request(request)?;

        recver.await?
    }

    pub async fn eof(&self) -> Result<()> {
        let (sender, recver) = o_channel();
        let request = Request::ChannelEof {
            id: self.id,
            sender,
        };
        self.send_request(request)?;

        recver.await?
    }

    pub async fn request_shell(&self) -> Result<()> {
        let (sender, recver) = o_channel();
        let request = Request::ChannelReuqestShell {
            id: self.id,
            sender,
        };

        self.send_request(request)?;

        recver.await?
    }

    pub async fn request_pty(
        &self,
        term: impl Into<String>,
        columns: u32,
        rows: u32,
        width: u32,
        height: u32,
        terimal_modes: impl Into<Vec<(TerminalMode, u32)>>,
    ) -> Result<()> {
        let (sender, recver) = o_channel();
        let request = Request::ChannelRequestPty {
            id: self.id,
            term: term.into(),
            columns,
            rows,
            width,
            height,
            terimal_modes: terimal_modes.into(),
            sender,
        };

        self.send_request(request)?;

        recver.await?
    }

    pub async fn pty_change_size(
        &self,
        columns: u32,
        rows: u32,
        width: u32,
        height: u32,
    ) -> Result<()> {
        let (sender, recver) = o_channel();
        let request = Request::ChannelPtyChangeSize {
            id: self.id,
            columns,
            rows,
            width,
            height,
            sender,
        };

        self.send_request(request)?;
        recver.await?
    }

    pub async fn request_x11_forward(
        &self,
        single_connection: bool,
        protocol: impl Into<String>,
        cookie: impl Into<String>,
        screen_number: u32,
    ) -> Result<()> {
        let (sender, recver) = o_channel();

        let request = Request::X11Forward {
            id: self.id,
            single_connection,
            protocol: protocol.into(),
            cookie: cookie.into(),
            screen_number,
            sender,
        };

        self.send_request(request)?;

        recver.await?
    }

    pub async fn xon_xoff(&self, allow: bool) -> Result<()> {
        let (sender, recvr) = o_channel();
        let request = Request::XonXoff {
            id: self.id,
            allow,
            sender,
        };

        self.send_request(request)?;

        recvr.await?
    }

    fn send_request(&self, msg: Request) -> Result<()> {
        self.session
            .send(msg)
            .map_err(|_| builder::Disconnected.build()) //.map_err(|_| Error::Disconnected)
    }

    pub(crate) fn session(&self) -> MSender<Request> {
        (*self.session).clone()
    }
}

pub(crate) struct Endpoint {
    pub(crate) id: u32,
    pub(crate) initial: u32,
    pub(crate) size: u32,
    pub(crate) maximum: u32,
    pub(crate) eof: bool,
    pub(crate) closed: bool,
}

impl Endpoint {
    pub(crate) fn new(id: u32, initial: u32, maximum: u32) -> Self {
        Self {
            id,
            initial,
            size: initial,
            maximum,
            eof: false,
            closed: false,
        }
    }
}

#[derive(new)]
pub(crate) struct ChannelInner {
    pub(crate) client: Endpoint,
    pub(crate) server: Endpoint,
    // pub(crate) stdout: IOSender,
    // pub(crate) stderr: IOSender,
    pub(crate) sender: MSender<Message>,
    // #[new(default)]
    // pub(crate) stdout_buf: Vec<u8>,
    // pub(crate) exit_status: Option<ExitStatus>,
}

impl ChannelInner {
    pub(crate) fn server_close(&mut self) {
        self.server.closed = true;
        let _ = self.sender.send(Message::Close);
    }

    pub(crate) fn server_eof(&mut self) {
        self.server.eof = true;
        let _ = self.sender.send(Message::Eof);
    }
}