From efa6b5972da1b4d5105a8f8a10144415655e1963 Mon Sep 17 00:00:00 2001 From: tom Date: Sat, 16 Jul 2022 00:45:23 +0800 Subject: [PATCH 1/5] update bytes(protobuf types) mapping, from Vec to bytes:Byte issues:958 --- Cargo.toml | 1 + libs/hbb_common/build.rs | 5 +++++ libs/scrap/src/common/vpxcodec.rs | 3 ++- src/client.rs | 8 ++++---- src/clipboard_file.rs | 8 ++++---- src/common.rs | 4 ++-- src/platform/windows.rs | 2 +- src/rendezvous_mediator.rs | 14 +++++++------- src/server.rs | 6 ++++-- src/server/audio_service.rs | 2 +- src/server/connection.rs | 2 +- src/server/input_service.rs | 2 +- src/ui/cm.rs | 2 +- 13 files changed, 34 insertions(+), 25 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f270f7b30..898d81da8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -55,6 +55,7 @@ rpassword = "6.0" base64 = "0.13" sysinfo = "0.23" num_cpus = "1.13" +bytes = "1.1" [target.'cfg(not(target_os = "linux"))'.dependencies] reqwest = { version = "0.11", features = ["json", "rustls-tls"], default-features=false } diff --git a/libs/hbb_common/build.rs b/libs/hbb_common/build.rs index 5c1c6af22..3c2a3c7a4 100644 --- a/libs/hbb_common/build.rs +++ b/libs/hbb_common/build.rs @@ -5,6 +5,11 @@ fn main() { .out_dir("src/protos") .inputs(&["protos/rendezvous.proto", "protos/message.proto"]) .include("protos") + .customize( + protobuf_codegen::Customize::default() + .tokio_bytes(true) + // .tokio_bytes_for_string(true) + ) .run() .expect("Codegen failed."); } diff --git a/libs/scrap/src/common/vpxcodec.rs b/libs/scrap/src/common/vpxcodec.rs index fc54b153c..0fda53fa3 100644 --- a/libs/scrap/src/common/vpxcodec.rs +++ b/libs/scrap/src/common/vpxcodec.rs @@ -12,6 +12,7 @@ use crate::STRIDE_ALIGN; use super::vpx::{vp8e_enc_control_id::*, vpx_codec_err_t::*, *}; use std::os::raw::{c_int, c_uint}; use std::{ptr, slice}; +use hbb_common::bytes::Bytes; #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] pub enum VpxVideoCodecId { @@ -291,7 +292,7 @@ impl VpxEncoder { #[inline] fn create_frame(frame: &EncodeFrame) -> EncodedVideoFrame { EncodedVideoFrame { - data: frame.data.to_vec(), + data: Bytes::from(frame.data.to_vec()), key: frame.key, pts: frame.pts, ..Default::default() diff --git a/src/client.rs b/src/client.rs index 33d966d2a..a1c9b2b0f 100644 --- a/src/client.rs +++ b/src/client.rs @@ -201,7 +201,7 @@ impl Client { } else { peer_nat_type = ph.nat_type(); is_local = ph.is_local(); - signed_id_pk = ph.pk; + signed_id_pk = ph.pk.as_ref().to_vec(); relay_server = ph.relay_server; peer_addr = AddrMangle::decode(&ph.socket_addr); log::info!("Hole Punched {} = {}", peer, peer_addr); @@ -393,8 +393,8 @@ impl Client { let sealed_key = box_::seal(&key.0, &nonce, &their_pk_b, &out_sk_b); let mut msg_out = Message::new(); msg_out.set_public_key(PublicKey { - asymmetric_value: our_pk_b.0.into(), - symmetric_value: sealed_key, + asymmetric_value: Vec::from(our_pk_b.0).into(), + symmetric_value: sealed_key.into(), ..Default::default() }); timeout(CONNECT_TIMEOUT, conn.send(&msg_out)).await??; @@ -1122,7 +1122,7 @@ impl LoginConfigHandler { let my_id = Config::get_id(); let mut lr = LoginRequest { username: self.id.clone(), - password, + password:password.into(), my_id, my_name: crate::username(), option: self.get_option_message(true).into(), diff --git a/src/clipboard_file.rs b/src/clipboard_file.rs index 2f35065c9..d2125010c 100644 --- a/src/clipboard_file.rs +++ b/src/clipboard_file.rs @@ -67,7 +67,7 @@ pub fn clip_2_msg(clip: ClipbaordFile) -> Message { CliprdrServerFormatDataResponse { conn_id, msg_flags, - format_data, + format_data: format_data.into(), ..Default::default() }, )), @@ -117,7 +117,7 @@ pub fn clip_2_msg(clip: ClipbaordFile) -> Message { conn_id, msg_flags, stream_id, - requested_data, + requested_data: requested_data.into(), ..Default::default() }, )), @@ -156,7 +156,7 @@ pub fn msg_2_clip(msg: Cliprdr) -> Option { Some(ClipbaordFile::ServerFormatDataResponse { conn_id: data.conn_id, msg_flags: data.msg_flags, - format_data: data.format_data, + format_data: data.format_data.as_ref().to_vec(), }) } Some(cliprdr::Union::FileContentsRequest(data)) => { @@ -177,7 +177,7 @@ pub fn msg_2_clip(msg: Cliprdr) -> Option { conn_id: data.conn_id, msg_flags: data.msg_flags, stream_id: data.stream_id, - requested_data: data.requested_data, + requested_data: data.requested_data.as_ref().to_vec(), }) } _ => None, diff --git a/src/common.rs b/src/common.rs index 9917a47f2..2930a1611 100644 --- a/src/common.rs +++ b/src/common.rs @@ -49,7 +49,7 @@ pub fn create_clipboard_msg(content: String) -> Message { let mut msg = Message::new(); msg.set_clipboard(Clipboard { compress, - content, + content:content.into(), ..Default::default() }); msg @@ -80,7 +80,7 @@ pub fn update_clipboard(clipboard: Clipboard, old: Option<&Arc>>) let content = if clipboard.compress { decompress(&clipboard.content) } else { - clipboard.content + clipboard.content.as_ref().to_vec() }; if let Ok(content) = String::from_utf8(content) { if content.is_empty() { diff --git a/src/platform/windows.rs b/src/platform/windows.rs index c9f83389a..cb0fd778f 100644 --- a/src/platform/windows.rs +++ b/src/platform/windows.rs @@ -164,7 +164,7 @@ pub fn get_cursor_data(hcursor: u64) -> ResultType { Ok(CursorData { id: hcursor, - colors: cbits, + colors: cbits.into(), hotx: ii.0.xHotspot as _, hoty: ii.0.yHotspot as _, width: width as _, diff --git a/src/rendezvous_mediator.rs b/src/rendezvous_mediator.rs index 4381fbc74..a07a096ab 100644 --- a/src/rendezvous_mediator.rs +++ b/src/rendezvous_mediator.rs @@ -263,7 +263,7 @@ impl RendezvousMediator { async fn handle_request_relay(&self, rr: RequestRelay, server: ServerPtr) -> ResultType<()> { self.create_relay( - rr.socket_addr, + rr.socket_addr.as_ref().to_vec(), rr.relay_server, rr.uuid, server, @@ -300,7 +300,7 @@ impl RendezvousMediator { let mut msg_out = Message::new(); let mut rr = RelayResponse { - socket_addr, + socket_addr: socket_addr.into(), version: crate::VERSION.to_owned(), ..Default::default() }; @@ -331,8 +331,8 @@ impl RendezvousMediator { let relay_server = self.get_relay_server(fla.relay_server); msg_out.set_local_addr(LocalAddr { id: Config::get_id(), - socket_addr: AddrMangle::encode(peer_addr), - local_addr: AddrMangle::encode(local_addr), + socket_addr: AddrMangle::encode(peer_addr).into(), + local_addr: AddrMangle::encode(local_addr).into(), relay_server, version: crate::VERSION.to_owned(), ..Default::default() @@ -350,7 +350,7 @@ impl RendezvousMediator { { let uuid = Uuid::new_v4().to_string(); return self - .create_relay(ph.socket_addr, relay_server, uuid, server, true, true) + .create_relay(ph.socket_addr.as_ref().to_vec(), relay_server, uuid, server, true, true) .await; } let peer_addr = AddrMangle::decode(&ph.socket_addr); @@ -391,8 +391,8 @@ impl RendezvousMediator { self.last_id_pk_registry = id.clone(); msg_out.set_register_pk(RegisterPk { id, - uuid, - pk, + uuid: uuid.into(), + pk: pk.into(), ..Default::default() }); socket.send(&msg_out, self.addr.to_owned()).await?; diff --git a/src/server.rs b/src/server.rs index d437ce6d4..0d2e97c67 100644 --- a/src/server.rs +++ b/src/server.rs @@ -20,6 +20,8 @@ use std::{ sync::{Arc, Mutex, RwLock, Weak}, time::Duration, }; +use bytes::Bytes; + pub mod audio_service; cfg_if::cfg_if! { if #[cfg(not(any(target_os = "android", target_os = "ios")))] { @@ -126,13 +128,13 @@ pub async fn create_tcp_connection( id: sign::sign( &IdPk { id: Config::get_id(), - pk: our_pk_b.0.to_vec(), + pk: Bytes::from(our_pk_b.0.to_vec()), ..Default::default() } .write_to_bytes() .unwrap_or_default(), &sk, - ), + ).into(), ..Default::default() }); timeout(CONNECT_TIMEOUT, stream.send(&msg_out)).await??; diff --git a/src/server/audio_service.rs b/src/server/audio_service.rs index e0974a228..02db0bffd 100644 --- a/src/server/audio_service.rs +++ b/src/server/audio_service.rs @@ -367,7 +367,7 @@ fn send_f32(data: &[f32], encoder: &mut Encoder, sp: &GenericService) { Ok(data) => { let mut msg_out = Message::new(); msg_out.set_audio_frame(AudioFrame { - data, + data: data.into(), timestamp: crate::common::get_time(), ..Default::default() }); diff --git a/src/server/connection.rs b/src/server/connection.rs index e8344c4a7..64ff6cbe9 100644 --- a/src/server/connection.rs +++ b/src/server/connection.rs @@ -1063,7 +1063,7 @@ impl Connection { self.send_fs(ipc::FS::WriteBlock { id: block.id, file_num: block.file_num, - data: block.data, + data: block.data.as_ref().to_vec(), compressed: block.compressed, }); } diff --git a/src/server/input_service.rs b/src/server/input_service.rs index 3ec9c0f70..9bae8d35b 100644 --- a/src/server/input_service.rs +++ b/src/server/input_service.rs @@ -145,7 +145,7 @@ fn run_cursor(sp: MouseCursorService, state: &mut StateCursor) -> ResultType<()> msg = cached.clone(); } else { let mut data = crate::get_cursor_data(hcursor)?; - data.colors = hbb_common::compress::compress(&data.colors[..], COMPRESS_LEVEL); + data.colors = hbb_common::compress::compress(&data.colors[..], COMPRESS_LEVEL).into(); let mut tmp = Message::new(); tmp.set_cursor_data(data); msg = Arc::new(tmp); diff --git a/src/ui/cm.rs b/src/ui/cm.rs index c1acb6926..1c2b36a93 100644 --- a/src/ui/cm.rs +++ b/src/ui/cm.rs @@ -263,7 +263,7 @@ impl ConnectionManager { FileTransferBlock { id, file_num, - data, + data: data.into(), compressed, ..Default::default() }, From ae839bd5bf0641d1709ab7aaa510e7c64b67d130 Mon Sep 17 00:00:00 2001 From: tom Date: Sun, 17 Jul 2022 00:59:56 +0800 Subject: [PATCH 2/5] update linux.rs & macos.rs with bytes:Byte issues:958 --- libs/hbb_common/build.rs | 1 - src/platform/linux.rs | 12 +++++++----- src/platform/macos.rs | 2 +- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/libs/hbb_common/build.rs b/libs/hbb_common/build.rs index 3c2a3c7a4..225ec34c7 100644 --- a/libs/hbb_common/build.rs +++ b/libs/hbb_common/build.rs @@ -8,7 +8,6 @@ fn main() { .customize( protobuf_codegen::Customize::default() .tokio_bytes(true) - // .tokio_bytes_for_string(true) ) .run() .expect("Codegen failed."); diff --git a/src/platform/linux.rs b/src/platform/linux.rs index efd6476b6..f331dd6ee 100644 --- a/src/platform/linux.rs +++ b/src/platform/linux.rs @@ -109,7 +109,8 @@ pub fn get_cursor_data(hcursor: u64) -> ResultType { cd.id = (*img).cursor_serial as _; let pixels = std::slice::from_raw_parts((*img).pixels, (cd.width * cd.height) as _); - cd.colors.resize(pixels.len() * 4, 0); + // cd.colors.resize(pixels.len() * 4, 0); + let mut cd_colors = vec![0_u8; pixels.len() * 4]; for y in 0..cd.height { for x in 0..cd.width { let pos = (y * cd.width + x) as usize; @@ -122,12 +123,13 @@ pub fn get_cursor_data(hcursor: u64) -> ResultType { continue; } let pos = pos * 4; - cd.colors[pos] = r as _; - cd.colors[pos + 1] = g as _; - cd.colors[pos + 2] = b as _; - cd.colors[pos + 3] = a as _; + cd_colors[pos] = r as _; + cd_colors[pos + 1] = g as _; + cd_colors[pos + 2] = b as _; + cd_colors[pos + 3] = a as _; } } + cd.colors = cd_colors.into(); res = Some(cd); } if !img.is_null() { diff --git a/src/platform/macos.rs b/src/platform/macos.rs index dabe11e49..1a8096587 100644 --- a/src/platform/macos.rs +++ b/src/platform/macos.rs @@ -342,7 +342,7 @@ pub fn get_cursor_data(hcursor: u64) -> ResultType { } Ok(CursorData { id: hcursor, - colors, + colors: colors.into(), hotx: hotspot.x as _, hoty: hotspot.y as _, width: size.width as _, From 16b7c7c7163e7b386e29352b29828012e11ec542 Mon Sep 17 00:00:00 2001 From: tom Date: Thu, 21 Jul 2022 00:39:20 +0800 Subject: [PATCH 3/5] upgrade bytes to 1.2 --- Cargo.toml | 2 +- libs/hbb_common/Cargo.toml | 2 +- src/client.rs | 2 +- src/clipboard_file.rs | 4 ++-- src/common.rs | 2 +- src/rendezvous_mediator.rs | 4 ++-- src/server/connection.rs | 2 +- 7 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3656e8221..6237afe4e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -55,7 +55,7 @@ rpassword = "7.0" base64 = "0.13" sysinfo = "0.24" num_cpus = "1.13" -bytes = "1.1" +bytes = "1.2" default-net = "0.11.0" wol-rs = "0.9.1" diff --git a/libs/hbb_common/Cargo.toml b/libs/hbb_common/Cargo.toml index 6fec67193..8d34709fe 100644 --- a/libs/hbb_common/Cargo.toml +++ b/libs/hbb_common/Cargo.toml @@ -11,7 +11,7 @@ protobuf = { version = "3.1", features = ["with-bytes"] } tokio = { version = "1.20", features = ["full"] } tokio-util = { version = "0.7", features = ["full"] } futures = "0.3" -bytes = "1.1" +bytes = "1.2" log = "0.4" env_logger = "0.9" socket2 = { version = "0.3", features = ["reuseport"] } diff --git a/src/client.rs b/src/client.rs index c8a561c8d..9b4bb1814 100644 --- a/src/client.rs +++ b/src/client.rs @@ -215,7 +215,7 @@ impl Client { } else { peer_nat_type = ph.nat_type(); is_local = ph.is_local(); - signed_id_pk = ph.pk.as_ref().to_vec(); + signed_id_pk = ph.pk.into(); relay_server = ph.relay_server; peer_addr = AddrMangle::decode(&ph.socket_addr); log::info!("Hole Punched {} = {}", peer, peer_addr); diff --git a/src/clipboard_file.rs b/src/clipboard_file.rs index d2125010c..b6c0513e8 100644 --- a/src/clipboard_file.rs +++ b/src/clipboard_file.rs @@ -156,7 +156,7 @@ pub fn msg_2_clip(msg: Cliprdr) -> Option { Some(ClipbaordFile::ServerFormatDataResponse { conn_id: data.conn_id, msg_flags: data.msg_flags, - format_data: data.format_data.as_ref().to_vec(), + format_data: data.format_data.into(), }) } Some(cliprdr::Union::FileContentsRequest(data)) => { @@ -177,7 +177,7 @@ pub fn msg_2_clip(msg: Cliprdr) -> Option { conn_id: data.conn_id, msg_flags: data.msg_flags, stream_id: data.stream_id, - requested_data: data.requested_data.as_ref().to_vec(), + requested_data: data.requested_data.into(), }) } _ => None, diff --git a/src/common.rs b/src/common.rs index b2026e5ba..e4ec7cb9c 100644 --- a/src/common.rs +++ b/src/common.rs @@ -79,7 +79,7 @@ pub fn update_clipboard(clipboard: Clipboard, old: Option<&Arc>>) let content = if clipboard.compress { decompress(&clipboard.content) } else { - clipboard.content.as_ref().to_vec() + clipboard.content.into() }; if let Ok(content) = String::from_utf8(content) { if content.is_empty() { diff --git a/src/rendezvous_mediator.rs b/src/rendezvous_mediator.rs index 24e50f086..7cf059843 100644 --- a/src/rendezvous_mediator.rs +++ b/src/rendezvous_mediator.rs @@ -266,7 +266,7 @@ impl RendezvousMediator { async fn handle_request_relay(&self, rr: RequestRelay, server: ServerPtr) -> ResultType<()> { self.create_relay( - rr.socket_addr.as_ref().to_vec(), + rr.socket_addr.into(), rr.relay_server, rr.uuid, server, @@ -353,7 +353,7 @@ impl RendezvousMediator { { let uuid = Uuid::new_v4().to_string(); return self - .create_relay(ph.socket_addr.as_ref().to_vec(), relay_server, uuid, server, true, true) + .create_relay(ph.socket_addr.into(), relay_server, uuid, server, true, true) .await; } let peer_addr = AddrMangle::decode(&ph.socket_addr); diff --git a/src/server/connection.rs b/src/server/connection.rs index 677d8f238..10a1786fd 100644 --- a/src/server/connection.rs +++ b/src/server/connection.rs @@ -1063,7 +1063,7 @@ impl Connection { self.send_fs(ipc::FS::WriteBlock { id: block.id, file_num: block.file_num, - data: block.data.as_ref().to_vec(), + data: block.data.into(), compressed: block.compressed, }); } From 9ab955bb8eaac10f01ad023113f43c9fa8a1570e Mon Sep 17 00:00:00 2001 From: tom Date: Thu, 21 Jul 2022 16:07:19 +0800 Subject: [PATCH 4/5] enable serde feature of bytes, modify struct WriteBlock using Bytes --- Cargo.toml | 2 +- libs/hbb_common/Cargo.toml | 2 +- src/ipc.rs | 3 ++- src/server/connection.rs | 6 +++--- src/ui/cm.rs | 2 +- 5 files changed, 8 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6237afe4e..e84875462 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -55,7 +55,7 @@ rpassword = "7.0" base64 = "0.13" sysinfo = "0.24" num_cpus = "1.13" -bytes = "1.2" +bytes = { version = "1.2", features = ["serde"] } default-net = "0.11.0" wol-rs = "0.9.1" diff --git a/libs/hbb_common/Cargo.toml b/libs/hbb_common/Cargo.toml index 8d34709fe..027aefe79 100644 --- a/libs/hbb_common/Cargo.toml +++ b/libs/hbb_common/Cargo.toml @@ -11,7 +11,7 @@ protobuf = { version = "3.1", features = ["with-bytes"] } tokio = { version = "1.20", features = ["full"] } tokio-util = { version = "0.7", features = ["full"] } futures = "0.3" -bytes = "1.2" +bytes = { version = "1.2", features = ["serde"] } log = "0.4" env_logger = "0.9" socket2 = { version = "0.3", features = ["reuseport"] } diff --git a/src/ipc.rs b/src/ipc.rs index 7df06cd22..532722e38 100644 --- a/src/ipc.rs +++ b/src/ipc.rs @@ -17,6 +17,7 @@ use parity_tokio_ipc::{ }; use serde_derive::{Deserialize, Serialize}; use std::{collections::HashMap, sync::atomic::Ordering}; +use bytes::Bytes; #[cfg(not(windows))] use std::{fs::File, io::prelude::*}; @@ -63,7 +64,7 @@ pub enum FS { WriteBlock { id: i32, file_num: i32, - data: Vec, + data: Bytes, compressed: bool, }, WriteDone { diff --git a/src/server/connection.rs b/src/server/connection.rs index 10a1786fd..f4ed9da2a 100644 --- a/src/server/connection.rs +++ b/src/server/connection.rs @@ -1063,7 +1063,7 @@ impl Connection { self.send_fs(ipc::FS::WriteBlock { id: block.id, file_num: block.file_num, - data: block.data.into(), + data: block.data, compressed: block.compressed, }); } @@ -1363,8 +1363,8 @@ async fn start_ipc( file_num, data, compressed}) = data { - stream.send(&Data::FS(ipc::FS::WriteBlock{id, file_num, data: Vec::new(), compressed})).await?; - stream.send_raw(data).await?; + stream.send(&Data::FS(ipc::FS::WriteBlock{id, file_num, data: Bytes::new(), compressed})).await?; + stream.send_raw(data.into()).await?; } else { stream.send(&data).await?; } diff --git a/src/ui/cm.rs b/src/ui/cm.rs index 1c2b36a93..c1acb6926 100644 --- a/src/ui/cm.rs +++ b/src/ui/cm.rs @@ -263,7 +263,7 @@ impl ConnectionManager { FileTransferBlock { id, file_num, - data: data.into(), + data, compressed, ..Default::default() }, From e811d5f18b7df0963b272f2831561e261634362a Mon Sep 17 00:00:00 2001 From: tom Date: Thu, 21 Jul 2022 17:54:07 +0800 Subject: [PATCH 5/5] modify argument of ConnectionTmpl::send_raw() --- src/ipc.rs | 4 ++-- src/server/connection.rs | 2 +- src/ui/cm.rs | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ipc.rs b/src/ipc.rs index 532722e38..f2765ebd1 100644 --- a/src/ipc.rs +++ b/src/ipc.rs @@ -456,8 +456,8 @@ where } } - pub async fn send_raw(&mut self, data: Vec) -> ResultType<()> { - self.inner.send(bytes::Bytes::from(data)).await?; + pub async fn send_raw(&mut self, data: Bytes) -> ResultType<()> { + self.inner.send(data).await?; Ok(()) } diff --git a/src/server/connection.rs b/src/server/connection.rs index f4ed9da2a..9c326575e 100644 --- a/src/server/connection.rs +++ b/src/server/connection.rs @@ -1364,7 +1364,7 @@ async fn start_ipc( data, compressed}) = data { stream.send(&Data::FS(ipc::FS::WriteBlock{id, file_num, data: Bytes::new(), compressed})).await?; - stream.send_raw(data.into()).await?; + stream.send_raw(data).await?; } else { stream.send(&data).await?; } diff --git a/src/ui/cm.rs b/src/ui/cm.rs index c1acb6926..fe5738c31 100644 --- a/src/ui/cm.rs +++ b/src/ui/cm.rs @@ -590,7 +590,7 @@ async fn start_pa() { } else { buf.clone() }; - if let Err(err) = stream.send_raw(out).await { + if let Err(err) = stream.send_raw(out.into()).await { log::error!("Failed to send audio data:{}", err); break; }