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
use crate::{PixelFormat, Rect, VncEncoding, VncError};
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
#[derive(Debug)]
pub(super) enum ClientMsg {
SetPixelFormat(PixelFormat),
SetEncodings(Vec<VncEncoding>),
FramebufferUpdateRequest(Rect, u8),
KeyEvent(u32, bool),
PointerEvent(u16, u16, u8),
ClientCutText(String),
}
impl ClientMsg {
pub(super) async fn write<S>(self, writer: &mut S) -> Result<(), VncError>
where
S: AsyncWrite + Unpin,
{
match self {
ClientMsg::SetPixelFormat(pf) => {
// +--------------+--------------+--------------+
// | No. of bytes | Type [Value] | Description |
// +--------------+--------------+--------------+
// | 1 | U8 [0] | message-type |
// | 3 | | padding |
// | 16 | PIXEL_FORMAT | pixel-format |
// +--------------+--------------+--------------+
let mut payload = vec![0_u8, 0, 0, 0];
payload.extend(<PixelFormat as Into<Vec<u8>>>::into(pf));
writer.write_all(&payload).await?;
Ok(())
}
ClientMsg::SetEncodings(encodings) => {
// +--------------+--------------+---------------------+
// | No. of bytes | Type [Value] | Description |
// +--------------+--------------+---------------------+
// | 1 | U8 [2] | message-type |
// | 1 | | padding |
// | 2 | U16 | number-of-encodings |
// +--------------+--------------+---------------------+
// This is followed by number-of-encodings repetitions of the following:
// +--------------+--------------+---------------+
// | No. of bytes | Type [Value] | Description |
// +--------------+--------------+---------------+
// | 4 | S32 | encoding-type |
// +--------------+--------------+---------------+
let mut payload = vec![2, 0];
payload.extend_from_slice(&(encodings.len() as u16).to_be_bytes());
for e in encodings {
payload.write_u32(e.into()).await?;
}
writer.write_all(&payload).await?;
Ok(())
}
ClientMsg::FramebufferUpdateRequest(rect, incremental) => {
// +--------------+--------------+--------------+
// | No. of bytes | Type [Value] | Description |
// +--------------+--------------+--------------+
// | 1 | U8 [3] | message-type |
// | 1 | U8 | incremental |
// | 2 | U16 | x-position |
// | 2 | U16 | y-position |
// | 2 | U16 | width |
// | 2 | U16 | height |
// +--------------+--------------+--------------+
let mut payload = vec![3, incremental];
payload.extend_from_slice(&rect.x.to_be_bytes());
payload.extend_from_slice(&rect.y.to_be_bytes());
payload.extend_from_slice(&rect.width.to_be_bytes());
payload.extend_from_slice(&rect.height.to_be_bytes());
writer.write_all(&payload).await?;
Ok(())
}
ClientMsg::KeyEvent(keycode, down) => {
// +--------------+--------------+--------------+
// | No. of bytes | Type [Value] | Description |
// +--------------+--------------+--------------+
// | 1 | U8 [4] | message-type |
// | 1 | U8 | down-flag |
// | 2 | | padding |
// | 4 | U32 | key |
// +--------------+--------------+--------------+
let mut payload = vec![4, down as u8, 0, 0];
payload.write_u32(keycode).await?;
writer.write_all(&payload).await?;
Ok(())
}
ClientMsg::PointerEvent(x, y, mask) => {
// +--------------+--------------+--------------+
// | No. of bytes | Type [Value] | Description |
// +--------------+--------------+--------------+
// | 1 | U8 [5] | message-type |
// | 1 | U8 | button-mask |
// | 2 | U16 | x-position |
// | 2 | U16 | y-position |
// +--------------+--------------+--------------+
let mut payload = vec![5, mask];
payload.write_u16(x).await?;
payload.write_u16(y).await?;
writer.write_all(&payload).await?;
Ok(())
}
ClientMsg::ClientCutText(s) => {
// +--------------+--------------+--------------+
// | No. of bytes | Type [Value] | Description |
// +--------------+--------------+--------------+
// | 1 | U8 [6] | message-type |
// | 3 | | padding |
// | 4 | U32 | length |
// | length | U8 array | text |
// +--------------+--------------+--------------+
let mut payload = vec![6_u8, 0, 0, 0];
payload.write_u32(s.len() as u32).await?;
payload.write_all(s.as_bytes()).await?;
writer.write_all(&payload).await?;
Ok(())
}
}
}
}
#[derive(Debug)]
pub(super) enum ServerMsg {
FramebufferUpdate(u16),
// SetColorMapEntries,
Bell,
ServerCutText(String),
}
impl ServerMsg {
pub(super) async fn read<S>(reader: &mut S) -> Result<Self, VncError>
where
S: AsyncRead + Unpin,
{
let server_msg = reader.read_u8().await?;
match server_msg {
0 => {
// FramebufferUpdate
// +--------------+--------------+----------------------+
// | No. of bytes | Type [Value] | Description |
// +--------------+--------------+----------------------+
// | 1 | U8 [0] | message-type |
// | 1 | | padding |
// | 2 | U16 | number-of-rectangles |
// +--------------+--------------+----------------------+
let _padding = reader.read_u8().await?;
let rects = reader.read_u16().await?;
Ok(ServerMsg::FramebufferUpdate(rects))
}
1 => {
// SetColorMapEntries
// +--------------+--------------+------------------+
// | No. of bytes | Type [Value] | Description |
// +--------------+--------------+------------------+
// | 1 | U8 [1] | message-type |
// | 1 | | padding |
// | 2 | U16 | first-color |
// | 2 | U16 | number-of-colors |
// +--------------+--------------+------------------+
unimplemented!()
}
2 => {
// Bell
// +--------------+--------------+--------------+
// | No. of bytes | Type [Value] | Description |
// +--------------+--------------+--------------+
// | 1 | U8 [2] | message-type |
// +--------------+--------------+--------------+
Ok(ServerMsg::Bell)
}
3 => {
// ServerCutText
// +--------------+--------------+--------------+
// | No. of bytes | Type [Value] | Description |
// +--------------+--------------+--------------+
// | 1 | U8 [3] | message-type |
// | 3 | | padding |
// | 4 | U32 | length |
// | length | U8 array | text |
// +--------------+--------------+--------------+
let mut padding = [0; 3];
reader.read_exact(&mut padding).await?;
let len = reader.read_u32().await?;
let mut buffer_str = vec![0; len as usize];
reader.read_exact(&mut buffer_str).await?;
Ok(Self::ServerCutText(
String::from_utf8_lossy(&buffer_str).to_string(),
))
}
_ => Err(VncError::WrongServerMessage),
}
}
}