2018-03-29 17:40:56 -04:00
|
|
|
use std::{
|
2018-12-05 18:47:18 -05:00
|
|
|
io::{Write, Read, ErrorKind},
|
2018-03-29 17:40:56 -04:00
|
|
|
marker::Sized,
|
2018-04-03 06:03:08 -04:00
|
|
|
path::PathBuf,
|
2018-12-05 18:47:18 -05:00
|
|
|
thread,
|
|
|
|
time,
|
2018-03-29 17:40:56 -04:00
|
|
|
};
|
|
|
|
|
2018-12-05 18:47:18 -05:00
|
|
|
use bytes::BytesMut;
|
|
|
|
|
|
|
|
use utils;
|
|
|
|
use models::message::{Message, OpCode};
|
|
|
|
use error::{Error, Result};
|
|
|
|
|
|
|
|
|
|
|
|
/// Wait for a non-blocking connection until it's complete.
|
|
|
|
macro_rules! try_until_done {
|
|
|
|
[ $e:expr ] => {
|
|
|
|
loop {
|
|
|
|
match $e {
|
|
|
|
Ok(_) => break,
|
|
|
|
Err(Error::IoError(ref err)) if err.kind() == ErrorKind::WouldBlock => (),
|
|
|
|
Err(why) => return Err(why),
|
|
|
|
}
|
|
|
|
|
|
|
|
thread::sleep(time::Duration::from_millis(500));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-29 17:40:56 -04:00
|
|
|
|
2018-03-22 17:54:49 -04:00
|
|
|
|
2018-04-06 15:51:01 -04:00
|
|
|
pub trait Connection: Sized {
|
2018-04-03 06:03:08 -04:00
|
|
|
type Socket: Write + Read;
|
|
|
|
|
|
|
|
fn socket(&mut self) -> &mut Self::Socket;
|
|
|
|
|
|
|
|
fn ipc_path() -> PathBuf;
|
|
|
|
|
2018-03-22 17:54:49 -04:00
|
|
|
fn connect() -> Result<Self>;
|
|
|
|
|
2018-04-03 06:03:08 -04:00
|
|
|
fn socket_path(n: u8) -> PathBuf {
|
|
|
|
Self::ipc_path().join(format!("discord-ipc-{}", n))
|
|
|
|
}
|
|
|
|
|
2018-12-05 18:47:18 -05:00
|
|
|
fn handshake(&mut self, client_id: u64) -> Result<()> {
|
|
|
|
let hs = json![{
|
|
|
|
"client_id": client_id.to_string(),
|
|
|
|
"v": 1,
|
|
|
|
"nonce": utils::nonce()
|
|
|
|
}];
|
|
|
|
|
|
|
|
try_until_done!(self.send(Message::new(OpCode::Handshake, hs.clone())));
|
|
|
|
try_until_done!(self.recv());
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ping(&mut self) -> Result<OpCode> {
|
|
|
|
let message = Message::new(OpCode::Ping, json![{}]);
|
|
|
|
self.send(message)?;
|
|
|
|
let response = self.recv()?;
|
|
|
|
Ok(response.opcode)
|
|
|
|
}
|
|
|
|
|
2018-04-06 15:51:01 -04:00
|
|
|
fn send(&mut self, message: Message) -> Result<()> {
|
2018-04-05 20:12:59 -04:00
|
|
|
match message.encode() {
|
2018-04-03 06:03:08 -04:00
|
|
|
Err(why) => error!("{:?}", why),
|
|
|
|
Ok(bytes) => {
|
|
|
|
self.socket().write_all(bytes.as_ref())?;
|
|
|
|
}
|
|
|
|
};
|
2018-04-07 07:22:50 -04:00
|
|
|
debug!("-> {:?}", message);
|
2018-04-03 06:03:08 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
2018-03-22 17:54:49 -04:00
|
|
|
|
2018-04-05 20:12:59 -04:00
|
|
|
fn recv(&mut self) -> Result<Message> {
|
2018-12-05 18:47:18 -05:00
|
|
|
let mut buf = BytesMut::new();
|
|
|
|
buf.resize(1024, 0);
|
|
|
|
let n = self.socket().read(&mut buf)?;
|
|
|
|
debug!("Received {} bytes", n);
|
|
|
|
|
|
|
|
if n == 0 {
|
|
|
|
return Err(Error::ConnectionClosed);
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = buf.split_to(n);
|
2018-04-05 20:12:59 -04:00
|
|
|
let message = Message::decode(&buf)?;
|
2018-04-07 07:22:50 -04:00
|
|
|
debug!("<- {:?}", message);
|
2018-12-05 18:47:18 -05:00
|
|
|
|
2018-04-05 20:12:59 -04:00
|
|
|
Ok(message)
|
2018-04-03 06:03:08 -04:00
|
|
|
}
|
2018-03-22 17:54:49 -04:00
|
|
|
}
|