Added Windows connection support
This commit is contained in:
parent
1e3e9485df
commit
620e9a6b26
|
@ -13,8 +13,8 @@ variables:
|
||||||
<<: *cached
|
<<: *cached
|
||||||
environment: test
|
environment: test
|
||||||
|
|
||||||
test:rust_1_23_0:
|
test:rust_1_25_0:
|
||||||
<<: *test_job
|
<<: *test_job
|
||||||
image: "rust:1.23.0"
|
image: "rust:1.25.0"
|
||||||
script:
|
script:
|
||||||
- cargo test --all
|
- cargo test --all
|
||||||
|
|
|
@ -21,6 +21,9 @@ byteorder = "^1.0"
|
||||||
log = "~0.4"
|
log = "~0.4"
|
||||||
libc = "0.2.39" # until std::process::id is stable
|
libc = "0.2.39" # until std::process::id is stable
|
||||||
|
|
||||||
|
[target.'cfg(windows)'.dependencies]
|
||||||
|
named_pipe = "0.3.0"
|
||||||
|
|
||||||
[dependencies.uuid]
|
[dependencies.uuid]
|
||||||
version = "^0.6.2"
|
version = "^0.6.2"
|
||||||
features = ["v4"]
|
features = ["v4"]
|
||||||
|
|
|
@ -1,7 +1,11 @@
|
||||||
mod base;
|
mod base;
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
mod unix;
|
mod unix;
|
||||||
|
#[cfg(windows)]
|
||||||
|
mod windows;
|
||||||
|
|
||||||
pub use self::base::Connection;
|
pub use self::base::Connection;
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
pub use self::unix::UnixConnection;
|
pub use self::unix::UnixConnection;
|
||||||
|
#[cfg(windows)]
|
||||||
|
pub use self::windows::WindowsConnection;
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
|
@ -23,3 +23,5 @@ pub use client::Client;
|
||||||
pub use rich_presence::*;
|
pub use rich_presence::*;
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
pub use connection::UnixConnection;
|
pub use connection::UnixConnection;
|
||||||
|
#[cfg(windows)]
|
||||||
|
pub use connection::WindowsConnection;
|
||||||
|
|
Loading…
Reference in New Issue