Added Windows connection support

This commit is contained in:
tenrys 2018-04-02 21:42:57 +00:00 committed by Patrick Auernig
parent 1e3e9485df
commit 620e9a6b26
5 changed files with 72 additions and 2 deletions

View File

@ -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

View File

@ -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"]

View File

@ -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;

61
src/connection/windows.rs Normal file
View File

@ -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<Self> {
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<T>(&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<Vec<u8>> {
let mut buf: Vec<u8> = vec![0; 1024];
let n = self.socket.read(buf.as_mut_slice())?;
buf.resize(n, 0);
debug!("{:?}", Message::decode(&buf));
Ok(buf)
}
}

View File

@ -23,3 +23,5 @@ pub use client::Client;
pub use rich_presence::*;
#[cfg(unix)]
pub use connection::UnixConnection;
#[cfg(windows)]
pub use connection::WindowsConnection;