From 620e9a6b26650d825392cf0fbfd097a7ed1662aa Mon Sep 17 00:00:00 2001 From: tenrys Date: Mon, 2 Apr 2018 21:42:57 +0000 Subject: [PATCH] Added Windows connection support --- .gitlab-ci.yml | 4 +-- Cargo.toml | 3 ++ src/connection/mod.rs | 4 +++ src/connection/windows.rs | 61 +++++++++++++++++++++++++++++++++++++++ src/lib.rs | 2 ++ 5 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 src/connection/windows.rs diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 3261539..d4ba0fa 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -13,8 +13,8 @@ variables: <<: *cached environment: test -test:rust_1_23_0: +test:rust_1_25_0: <<: *test_job - image: "rust:1.23.0" + image: "rust:1.25.0" script: - cargo test --all diff --git a/Cargo.toml b/Cargo.toml index b94b6e2..10c82bc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,9 @@ byteorder = "^1.0" log = "~0.4" libc = "0.2.39" # until std::process::id is stable +[target.'cfg(windows)'.dependencies] +named_pipe = "0.3.0" + [dependencies.uuid] version = "^0.6.2" features = ["v4"] diff --git a/src/connection/mod.rs b/src/connection/mod.rs index 3f1434c..891a2db 100644 --- a/src/connection/mod.rs +++ b/src/connection/mod.rs @@ -1,7 +1,11 @@ mod base; #[cfg(unix)] mod unix; +#[cfg(windows)] +mod windows; pub use self::base::Connection; #[cfg(unix)] pub use self::unix::UnixConnection; +#[cfg(windows)] +pub use self::windows::WindowsConnection; diff --git a/src/connection/windows.rs b/src/connection/windows.rs new file mode 100644 index 0000000..c5f18f8 --- /dev/null +++ b/src/connection/windows.rs @@ -0,0 +1,61 @@ +extern crate named_pipe; + +use std::{ + io::{Write, Read}, + time, + path::PathBuf, + fmt::Debug +}; + +use super::base::Connection; +use models::{Payload, Message, OpCode}; +use error::Result; + +use self::named_pipe::PipeClient; + +pub struct WindowsConnection { + socket: PipeClient, +} + +impl WindowsConnection { + fn ipc_path() -> PathBuf { + PathBuf::from(r"\\.\pipe\") + } + + fn socket_path(n: u8) -> PathBuf { + Self::ipc_path().join(format!("discord-ipc-{}", n)) + } +} + +impl Connection for WindowsConnection { + fn connect() -> Result { + let connection_name = Self::socket_path(0); + let mut socket = PipeClient::connect(connection_name)?; + socket.set_write_timeout(Some(time::Duration::from_secs(30))); + socket.set_read_timeout(Some(time::Duration::from_secs(30))); + Ok(Self { socket }) + } + + fn send(&mut self, opcode: OpCode, payload: T) -> Result<()> + where T: Payload + Debug + { + debug!("payload: {:#?}", payload); + match Message::new(opcode, payload).encode() { + Err(why) => error!("{:?}", why), + Ok(bytes) => { + self.socket.write_all(bytes.as_ref())?; + debug!("sent opcode: {:?}", opcode); + self.recv()?; + } + }; + Ok(()) + } + + fn recv(&mut self) -> Result> { + let mut buf: Vec = vec![0; 1024]; + let n = self.socket.read(buf.as_mut_slice())?; + buf.resize(n, 0); + debug!("{:?}", Message::decode(&buf)); + Ok(buf) + } +} diff --git a/src/lib.rs b/src/lib.rs index ed330dc..c5e78ff 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,3 +23,5 @@ pub use client::Client; pub use rich_presence::*; #[cfg(unix)] pub use connection::UnixConnection; +#[cfg(windows)] +pub use connection::WindowsConnection;