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
use std::{ffi::OsString, path::PathBuf};

use bytes::{Bytes, BytesMut};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum Request {
    /// Allocate a new terminal and start `command`.
    Start {
        command: Vec<OsString>,
        env:     Vec<(OsString, OsString)>,
        pwd:     PathBuf,
        rows:    u16,
        cols:    u16,
    },

    ListProcesses,
    Quit,

    /// Restart the stopped foreground process in the given terminal.
    /// Has no effect if the process is not stopped. If `id` is None, resumes
    /// the last stopped terminal.
    Resume {
        id:          Option<u32>,
        /// Whether the server should sent over the outputs it has accumulated
        with_output: bool,
    },

    /// Window size has changed. Should be sent by the client in response to
    /// the SIGWINCH signal.
    WindowSize {
        id:   u32,
        rows: u16,
        cols: u16,
    },
}

#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq)]
pub enum ExitStatus {
    Exited(i32),
    Signaled(i32),
}

#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq)]
pub enum ProcessState {
    Running,
    Stopped,
    Terminated(ExitStatus),
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Process {
    pub id:        u32,
    pub pid:       u32,
    pub state:     ProcessState,
    pub connected: bool,
    pub command:   OsString,
}

#[derive(Serialize, Deserialize, Debug, Clone, thiserror::Error)]
pub enum Error {
    #[error("Job {id:?} not found")]
    NotFound { id: Option<u32> },

    #[error("Job {id} is already foreground somewhere else")]
    AlreadyConnected { id: u32 },

    #[error("Client sent an invalid request")]
    InvalidRequest,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum Event {
    /// Process in the given terminal changed state, i.e. exist, stopped,
    /// started, or killed. When this is sent in response to a Start
    /// request, `id` is a newly allocated identifier.
    StateChanged {
        /// The unique identifier for the terminal.
        id:    u32,
        state: ProcessState,
    },

    /// Sent on `Resume`, report the current terminal size
    WindowSize {
        cols: u16,
        rows: u16,
    },

    Error(Error),

    /// Responses for the ListProcesses request
    Process(Process),
}

pub use bytes;

pub struct MapCodec<T, EI, DO, R, W, FD, FE> {
    inner:      T,
    map_decode: FD,
    map_encode: FE,
    #[allow(clippy::type_complexity)]
    _marker:    std::marker::PhantomData<(fn(DO) -> R, fn(W) -> EI)>,
}

impl<T, EI, DO, R, W, FD, FE> MapCodec<T, EI, DO, R, W, FD, FE> {
    pub fn new(inner: T, map_decode: FD, map_encode: FE) -> Self {
        Self {
            inner,
            map_decode,
            map_encode,
            _marker: std::marker::PhantomData,
        }
    }
}

impl<T, EI, DO, R, W, FD: FnMut(DO) -> R, FE> tokio_util::codec::Decoder
    for MapCodec<T, EI, DO, R, W, FD, FE>
where
    T: tokio_util::codec::Decoder<Item = DO>,
{
    type Error = T::Error;
    type Item = R;

    fn decode(&mut self, src: &mut bytes::BytesMut) -> Result<Option<Self::Item>, Self::Error> {
        Ok(self.inner.decode(src)?.map(|item| (self.map_decode)(item)))
    }
}

impl<T, EI, DO, R, W, FD, FE: FnMut(W) -> EI> tokio_util::codec::Encoder<W>
    for MapCodec<T, EI, DO, R, W, FD, FE>
where
    T: tokio_util::codec::Encoder<EI>,
{
    type Error = T::Error;

    fn encode(&mut self, item: W, dst: &mut bytes::BytesMut) -> Result<(), Self::Error> {
        self.inner.encode((self.map_encode)(item), dst)
    }
}
pub fn client_codec() -> impl tokio_util::codec::Encoder<Request, Error = std::io::Error>
       + tokio_util::codec::Decoder<Item = Event, Error = std::io::Error> {
    let codec = tokio_util::codec::length_delimited::LengthDelimitedCodec::new();
    MapCodec::new(
        codec,
        |bytes: BytesMut| bincode::deserialize(&bytes).unwrap(),
        |event| -> Bytes { bincode::serialize(&event).unwrap().into() },
    )
}

pub fn server_codec() -> impl tokio_util::codec::Encoder<Event, Error = std::io::Error>
       + tokio_util::codec::Decoder<Item = Request, Error = std::io::Error> {
    let codec = tokio_util::codec::length_delimited::LengthDelimitedCodec::new();
    MapCodec::new(
        codec,
        |bytes: BytesMut| bincode::deserialize(&bytes).unwrap(),
        |request| -> Bytes { bincode::serialize(&request).unwrap().into() },
    )
}

pub trait Codec<Input, Output, Error> {
    fn as_encoder(&mut self) -> &mut dyn tokio_util::codec::Encoder<Input, Error = Error>;
    fn as_decoder(&mut self) -> &mut dyn tokio_util::codec::Decoder<Item = Output, Error = Error>;
}

impl<T, I, O, E> Codec<I, O, E> for T
where
    T: tokio_util::codec::Encoder<I, Error = E> + tokio_util::codec::Decoder<Item = O, Error = E>,
{
    fn as_encoder(&mut self) -> &mut dyn tokio_util::codec::Encoder<I, Error = E> {
        self as _
    }

    fn as_decoder(&mut self) -> &mut dyn tokio_util::codec::Decoder<Item = O, Error = E> {
        self as _
    }
}

impl<I, O, E: From<std::io::Error>> tokio_util::codec::Encoder<I>
    for Box<dyn Codec<I, O, E> + Send + Sync + Unpin>
{
    type Error = E;

    fn encode(&mut self, item: I, dst: &mut bytes::BytesMut) -> Result<(), Self::Error> {
        self.as_mut().as_encoder().encode(item, dst)
    }
}

impl<I, O, E: From<std::io::Error>> tokio_util::codec::Decoder
    for Box<dyn Codec<I, O, E> + Send + Sync + Unpin>
{
    type Error = E;
    type Item = O;

    fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
        self.as_mut().as_decoder().decode(src)
    }

    fn decode_eof(&mut self, buf: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
        self.as_mut().as_decoder().decode_eof(buf)
    }
}

pub type DynServerCodec = Box<dyn Codec<Event, Request, std::io::Error> + Send + Sync + Unpin>;
pub type DynClientCodec = Box<dyn Codec<Request, Event, std::io::Error> + Send + Sync + Unpin>;