From 05c4dacbe9cff0994337364614da1abc2d2f5154 Mon Sep 17 00:00:00 2001 From: rustdesk Date: Sat, 26 Jun 2021 01:14:22 +0800 Subject: [PATCH] ipc windows work --- src/ipc.rs | 22 +++++++++++++++------- src/platform/windows.rs | 7 ++++--- src/port_forward.rs | 2 +- src/server.rs | 2 +- src/ui/cm.rs | 2 -- 5 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/ipc.rs b/src/ipc.rs index 895b8aae8..25dc36d3c 100644 --- a/src/ipc.rs +++ b/src/ipc.rs @@ -5,10 +5,13 @@ use hbb_common::{ futures::StreamExt as _, futures_util::sink::SinkExt, log, timeout, tokio, + tokio::io::{AsyncRead, AsyncWrite}, tokio_util::codec::Framed, ResultType, }; -use parity_tokio_ipc::{Connection as Conn, Endpoint, Incoming, SecurityAttributes}; +use parity_tokio_ipc::{ + Connection as Conn, ConnectionClient as ConnClient, Endpoint, Incoming, SecurityAttributes, +}; use serde_derive::{Deserialize, Serialize}; use std::{collections::HashMap, net::SocketAddr}; #[cfg(not(windows))] @@ -235,10 +238,10 @@ async fn handle(data: Data, stream: &mut Connection) { } } -pub async fn connect(ms_timeout: u64, postfix: &str) -> ResultType { +pub async fn connect(ms_timeout: u64, postfix: &str) -> ResultType> { let path = Config::ipc_path(postfix); let client = timeout(ms_timeout, Endpoint::connect(&path)).await??; - Ok(Connection::new(client)) + Ok(ConnectionTmpl::new(client)) } #[inline] @@ -284,12 +287,17 @@ fn write_pid(postfix: &str) { } } -pub struct Connection { - inner: Framed, +pub struct ConnectionTmpl { + inner: Framed, } -impl Connection { - pub fn new(conn: Conn) -> Self { +pub type Connection = ConnectionTmpl; + +impl ConnectionTmpl +where + T: AsyncRead + AsyncWrite + std::marker::Unpin, +{ + pub fn new(conn: T) -> Self { Self { inner: Framed::new(conn, BytesCodec::new()), } diff --git a/src/platform/windows.rs b/src/platform/windows.rs index 28b68048a..86583d38a 100644 --- a/src/platform/windows.rs +++ b/src/platform/windows.rs @@ -3,7 +3,6 @@ use crate::ipc; use hbb_common::{ allow_err, bail, config::{Config, APP_NAME}, - futures_util::stream::StreamExt, log, sleep, timeout, tokio, }; use std::io::prelude::*; @@ -652,7 +651,10 @@ pub fn get_active_username() -> String { return "".to_owned(); } let sl = unsafe { std::slice::from_raw_parts(buff.as_ptr(), n as _) }; - String::from_utf16(sl).unwrap_or("??".to_owned()).trim_end_matches('\0').to_owned() + String::from_utf16(sl) + .unwrap_or("??".to_owned()) + .trim_end_matches('\0') + .to_owned() } /* @@ -1038,7 +1040,6 @@ pub fn get_installed_version() -> String { "".to_owned() } - pub fn create_shortcut(id: &str) -> ResultType<()> { let exe = std::env::current_exe()?.to_str().unwrap_or("").to_owned(); let shortcut = write_cmds( diff --git a/src/port_forward.rs b/src/port_forward.rs index e1e012f0c..d74717bb6 100644 --- a/src/port_forward.rs +++ b/src/port_forward.rs @@ -25,7 +25,7 @@ pub async fn listen( interface: impl Interface, ui_receiver: mpsc::UnboundedReceiver, ) -> ResultType<()> { - let mut listener = tcp::new_listener(format!("0.0.0.0:{}", port), true).await?; + let listener = tcp::new_listener(format!("0.0.0.0:{}", port), true).await?; let addr = listener.local_addr()?; log::info!("listening on port {:?}", addr); let is_rdp = port == 0; diff --git a/src/server.rs b/src/server.rs index 359c3378b..86cb4f1e1 100644 --- a/src/server.rs +++ b/src/server.rs @@ -66,7 +66,7 @@ async fn accept_connection_(server: ServerPtr, socket: Stream, secure: bool) -> // even we drop socket, below still may fail if not use reuse_addr, // there is TIME_WAIT before socket really released, so sometimes we // see “Only one usage of each socket address is normally permitted” on windows sometimes, - let mut listener = new_listener(local_addr, true).await?; + let listener = new_listener(local_addr, true).await?; log::info!("Server listening on: {}", &listener.local_addr()?); if let Ok((stream, addr)) = timeout(CONNECT_TIMEOUT, listener.accept()).await? { create_tcp_connection_(server, Stream::from(stream), addr, secure).await?; diff --git a/src/ui/cm.rs b/src/ui/cm.rs index ceb5f0101..6bfc8e4ab 100644 --- a/src/ui/cm.rs +++ b/src/ui/cm.rs @@ -1,6 +1,4 @@ use crate::ipc::{self, new_listener, Connection, Data}; -#[cfg(windows)] -use hbb_common::futures_util::stream::StreamExt; use hbb_common::{ allow_err, config::{Config, ICON},