From b8e46975556bb8ca5b75cd36b12f514a7539c760 Mon Sep 17 00:00:00 2001 From: Patrick Auernig Date: Tue, 3 Apr 2018 12:03:08 +0200 Subject: [PATCH] Move some common methods into Connection trait --- src/connection/base.rs | 41 +++++++++++++++++++++++++---- src/connection/unix.rs | 54 ++++++++++----------------------------- src/connection/windows.rs | 39 +++++----------------------- src/lib.rs | 2 ++ 4 files changed, 58 insertions(+), 78 deletions(-) diff --git a/src/connection/base.rs b/src/connection/base.rs index 68fa2ff..186392a 100644 --- a/src/connection/base.rs +++ b/src/connection/base.rs @@ -1,19 +1,50 @@ use std::{ + io::{Write, Read}, marker::Sized, - fmt::Debug + fmt::Debug, + path::PathBuf, }; -use models::{Payload, OpCode}; +use models::{Payload, Message, OpCode}; use error::Result; pub trait Connection where Self: Sized { + type Socket: Write + Read; + + + fn socket(&mut self) -> &mut Self::Socket; + + fn ipc_path() -> PathBuf; + fn connect() -> Result; - fn send(&mut self, opcode: OpCode, payload: T) -> Result<()> - where T: Payload + Debug; + fn socket_path(n: u8) -> PathBuf { + Self::ipc_path().join(format!("discord-ipc-{}", n)) + } - fn recv(&mut self) -> Result>; + 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/connection/unix.rs b/src/connection/unix.rs index a6664e8..a7753d2 100644 --- a/src/connection/unix.rs +++ b/src/connection/unix.rs @@ -1,14 +1,11 @@ use std::{ - os::unix::net::UnixStream, - io::{Write, Read}, time, path::PathBuf, env, - fmt::Debug + os::unix::net::UnixStream, }; use super::base::Connection; -use models::{Payload, Message, OpCode}; use error::Result; @@ -16,7 +13,17 @@ pub struct UnixConnection { socket: UnixStream, } -impl UnixConnection { +impl Connection for UnixConnection { + type Socket = UnixStream; + + fn connect() -> Result { + let connection_name = Self::socket_path(0); + let socket = UnixStream::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 ipc_path() -> PathBuf { let tmp = env::var("XDG_RUNTIME_DIR") .or_else(|_| env::var("TMPDIR")) @@ -30,40 +37,7 @@ impl UnixConnection { PathBuf::from(tmp) } - fn socket_path(n: u8) -> PathBuf { - Self::ipc_path().join(format!("discord-ipc-{}", n)) - } -} - -impl Connection for UnixConnection { - fn connect() -> Result { - let connection_name = Self::socket_path(0); - let socket = UnixStream::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) + fn socket(&mut self) -> &mut Self::Socket { + &mut self.socket } } diff --git a/src/connection/windows.rs b/src/connection/windows.rs index c5f18f8..f91e77c 100644 --- a/src/connection/windows.rs +++ b/src/connection/windows.rs @@ -1,10 +1,6 @@ -extern crate named_pipe; - use std::{ - io::{Write, Read}, time, path::PathBuf, - fmt::Debug }; use super::base::Connection; @@ -17,17 +13,9 @@ 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 { + type Socket = PipeClient; + fn connect() -> Result { let connection_name = Self::socket_path(0); let mut socket = PipeClient::connect(connection_name)?; @@ -36,26 +24,11 @@ impl Connection for WindowsConnection { 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 ipc_path() -> PathBuf { + PathBuf::from(r"\\.\pipe\") } - 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) + fn socket(&mut self) -> &mut Self::Socket { + &mut self.socket } } diff --git a/src/lib.rs b/src/lib.rs index c5e78ff..1687810 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,6 +7,8 @@ extern crate serde_json; extern crate byteorder; extern crate uuid; extern crate libc; +#[cfg(windows)] +extern crate named_pipe; #[macro_use] mod macros;