1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
use std::{
    io::{Error as IoError, ErrorKind},
    str::FromStr,
    sync::Arc,
    time::{Duration, Instant}
};

use futures::{
    future::Future,
    Sink,
    stream::{SplitStream, Stream},
    sync::mpsc::{self, UnboundedSender}
};
use native_tls::TlsConnector;
use parking_lot::Mutex;
use tokio::net::TcpStream as TokioTcpStream;
use tokio::timer::Interval;
use tokio_dns::TcpStream;
use tokio_tls::TlsStream;
use tokio_tungstenite::{
    tungstenite::{
        Error as TungsteniteError,
        handshake::client::Request,
        protocol::{Message as WebsocketMessage, WebSocketConfig},
    },
    WebSocketStream
};
use tokio_tungstenite::stream::Stream as TungsteniteStream;
use url::Url;

use spectacles_model::{
    gateway::{
        GatewayEvent,
        HeartbeatPacket,
        HelloPacket,
        IdentifyPacket,
        IdentifyProperties,
        Opcodes,
        ReadyPacket,
        ReceivePacket,
        ResumeSessionPacket,
        SendablePacket,
    },
    presence::{ClientActivity, ClientPresence, Status}
};

use crate::{
    constants::{GATEWAY_URL, GATEWAY_VERSION},
    errors::{Error, Result}
};

pub type ShardSplitStream = SplitStream<WebSocketStream<TungsteniteStream<TokioTcpStream, TlsStream<TokioTcpStream>>>>;

/// A Spectacles Gateway shard.
#[derive(Clone)]
pub struct Shard {
    /// The bot token that this shard will use.
    pub token: String,
    /// The shard's info. Includes the shard's ID and the total amount of shards.
    pub info: [usize; 2],
    /// The currently active presence for this shard.
    pub presence: ClientPresence,
    /// The session ID of this shard, if applicable.
    pub session_id: Option<String>,
    /// The interval at which a heartbeat is made.
    pub interval: Option<u64>,
    /// The channel which is used to send websocket messages.
    pub sender: Arc<Mutex<UnboundedSender<WebsocketMessage>>>,
    /// The shard's message stream, which is used to receive messages.
    pub stream: Arc<Mutex<Option<ShardSplitStream>>>,
    /// Used to determine whether or not the shard is currently in a state of connecting.
    current_state: Arc<Mutex<String>>,
    /// This shard's current heartbeat.
    pub heartbeat: Arc<Mutex<Heartbeat>>,
}

/// Various actions that a shard can perform.
pub enum ShardAction {
    NoneAction,
    Autoreconnect,
    Reconnect,
    Identify,
    Resume
}
/// A shard's heartbeat information.
#[derive(Debug, Copy, Clone)]
pub struct Heartbeat {
    pub acknowledged: bool,
    pub seq: u64,
}

impl Heartbeat {
    fn new() -> Heartbeat {
        Self {
            acknowledged: false,
            seq: 0
        }
    }
}

impl Shard {
    /// Creates a new Discord Shard, with the provided token.
    pub fn new(token: String, info: [usize; 2]) -> impl Future<Item = Shard, Error = Error> {
        Shard::begin_connection(GATEWAY_URL, info[0])
            .map(move |(sender, stream)| {
                Shard {
                    token,
                    session_id: None,
                    presence: ClientPresence {
                        status: String::from("online"),
                        ..Default::default()
                    },
                    info,
                    interval: None,
                    sender: Arc::new(Mutex::new(sender)),
                    current_state: Arc::new(Mutex::new(String::from("handshake"))),
                    stream: Arc::new(Mutex::new(Some(stream))),
                    heartbeat: Arc::new(Mutex::new(Heartbeat::new()))
                }
            })
    }

    pub fn fulfill_gateway<'a>(&mut self, packet: ReceivePacket<'a>) -> Result<ShardAction> {
        let info = self.info.clone();
        let current_state = self.current_state.lock().clone();
        match packet.op {
            Opcodes::Dispatch => {
                if let Some(GatewayEvent::READY) = packet.t {
                    let ready: ReadyPacket = serde_json::from_str(packet.d.get()).unwrap();
                    *self.current_state.lock() = "connected".to_string();
                    self.session_id = Some(ready.session_id.clone());
                    trace!("[Shard {}] Received ready, set session ID as {}", &info[0], ready.session_id)
                };
                Ok(ShardAction::NoneAction)
            }
            Opcodes::Hello => {
                if self.current_state.lock().clone() == "resume".to_string() {
                    return Ok(ShardAction::NoneAction)
                };
                let hello: HelloPacket = serde_json::from_str(packet.d.get()).unwrap();
                if hello.heartbeat_interval > 0 {
                    self.interval = Some(hello.heartbeat_interval);
                }
                if current_state == "handshake".to_string() {
                    let dur = Duration::from_millis(hello.heartbeat_interval);
                    tokio::spawn(Shard::begin_interval(self.clone(), dur));
                    return Ok(ShardAction::Identify);
                }
                Ok(ShardAction::Autoreconnect)
            },
            Opcodes::HeartbeatAck => {
                let mut hb = self.heartbeat.lock().clone();
                hb.acknowledged = true;
                Ok(ShardAction::NoneAction)
            },
            Opcodes::Reconnect => Ok(ShardAction::Reconnect),
            Opcodes::InvalidSession => {
                let invalid: bool = serde_json::from_str(packet.d.get()).unwrap();
                if !invalid {
                    Ok(ShardAction::Identify)
                } else { Ok(ShardAction::Resume) }
            },
            _ => Ok(ShardAction::NoneAction)
        }
    }

    /// Identifies a shard with Discord.
    pub fn identify(&mut self) -> Result<()> {
        let token = self.token.clone();
        let shard = self.info.clone();
        let presence = self.presence.clone();
        self.send_payload(IdentifyPacket {
            large_threshold: 250,
            token,
            shard,
            compress: false,
            presence: Some(presence),
            version: GATEWAY_VERSION,
            properties: IdentifyProperties {
                os: std::env::consts::OS.to_string(),
                browser: String::from("spectacles-rs"),
                device: String::from("spectacles-rs")
            }
        })
    }

    /// Attempts to automatically reconnect the shard to Discord.
    pub fn autoreconnect(&mut self) -> Box<Future<Item = (), Error = Error> + Send>{
        if self.session_id.is_some() && self.heartbeat.lock().seq > 0 {
            Box::new(self.resume())
        } else {
            Box::new(self.reconnect())
        }
    }

    /// Makes a request to reconnect the shard.
    pub fn reconnect(&mut self) -> impl Future<Item = (), Error = Error> + Send {
        debug!("[Shard {}] Attempting to reconnect to gateway.", &self.info[0]);
        self.reset_values().expect("[Shard] Failed to reset this shard for autoreconnecting.");
        self.dial_gateway()
    }

    /// Resumes a shard's past session.
    pub fn resume(&mut self) -> impl Future<Item = (), Error = Error> + Send {
        debug!("[Shard {}] Attempting to resume gateway connection.", &self.info[0]);
        let seq = self.heartbeat.lock().seq;
        let token = self.token.clone();
        let state = self.current_state.clone();
        let session = self.session_id.clone();
        let sender = self.sender.clone();

        self.dial_gateway().then(move |result|{
            if result.is_err() { return result };
            *state.lock() = "resuming".to_string();
            let payload = ResumeSessionPacket {
                session_id: session.unwrap(),
                seq,
                token
            };

            send(&sender, WebsocketMessage::text(payload.to_json()?))
        })
    }
    /// Resolves a Websocket message into a ReceivePacket struct.
    pub fn resolve_packet<'a>(&self, mess: &'a WebsocketMessage) -> Result<ReceivePacket<'a>> {
        match mess {
            WebsocketMessage::Binary(v) => serde_json::from_slice(v),
            WebsocketMessage::Text(v) => serde_json::from_str(v),
            _ => unreachable!("Invalid type detected."),
        }.map_err(Error::from)
    }

    /// Sends a payload to the Discord Gateway.
    pub fn send_payload<T: SendablePacket>(&self, payload: T) -> Result<()> {
        let json = payload.to_json()?;
        send(&self.sender, WebsocketMessage::text(json))
    }


    /// Change the status of the current shard.
    pub fn change_status(&mut self, status: Status) -> Result<()> {
        self.presence.status = status.to_string();
        let oldpresence = self.presence.clone();
        self.change_presence(oldpresence)
    }

    /// Change the activity of the current shard.
    pub fn change_activity(&mut self, activity: ClientActivity) -> Result<()> {
        self.presence.game = Some(activity);
        let oldpresence = self.presence.clone();
        self.change_presence(oldpresence)
    }

    /// Change the presence of the current shard.
    pub fn change_presence(&mut self, presence: ClientPresence) -> Result<()> {
        debug!("[Shard {}] Sending a presence change payload. {:?}", self.info[0], presence.clone());
        self.send_payload(presence.clone())?;
        self.presence = presence;
        Ok(())
    }

    fn reset_values(&mut self) -> Result<()> {
        self.session_id = None;
        *self.current_state.lock() = "disconnected".to_string();

        let mut hb = self.heartbeat.lock();
        hb.acknowledged = true;
        hb.seq = 0;

        Ok(())
    }

    fn heartbeat(&mut self) -> Result<()> {
        debug!("[Shard {}] Sending heartbeat.", self.info[0]);
        let seq = self.heartbeat.lock().seq;

        self.send_payload(HeartbeatPacket { seq })
    }

    fn dial_gateway(&mut self) -> impl Future<Item = (), Error = Error> + Send {
        let info = self.info.clone();
        *self.current_state.lock() = String::from("connected");
        let state = self.current_state.clone();
        let orig_sender = self.sender.clone();
        let orig_stream = self.stream.clone();
        let heartbeat = self.heartbeat.clone();

        Shard::begin_connection(GATEWAY_URL, info[0])
            .map(move |(sender, stream)| {
                *orig_sender.lock() = sender;
                *heartbeat.lock() = Heartbeat::new();
                *state.lock() = String::from("handshake");
                *orig_stream.lock() = Some(stream);
            })
    }


    fn begin_interval(mut shard: Shard, duration: Duration) -> impl Future<Item = (), Error = ()> {
        let info = shard.info.clone();
        Interval::new(Instant::now(), duration)
            .map_err(move |err| {
                warn!("[Shard {}] Failed to begin heartbeat interval. {:?}", info[0], err);
            })
            .for_each(move |_| {
                if let Err(r) = shard.heartbeat() {
                    warn!("[Shard {}] Failed to perform heartbeat. {:?}", info[0], r);
                    return Err(());
                }
                Ok(())
            })
    }

    fn begin_connection(ws: &str, shard_id: usize) -> impl Future<Item = (UnboundedSender<WebsocketMessage>, ShardSplitStream), Error = Error> {
        let url = Url::from_str(ws).expect("Invalid Websocket URL has been provided.");
        let req = Request::from(url);
        let (host, port) = Shard::get_addr_info(&req);
        let tlsconn = TlsConnector::new().unwrap();
        let tlsconn = tokio_tls::TlsConnector::from(tlsconn);

        let socket = TcpStream::connect((host.as_ref(), port));
        let handshake = socket.and_then(move |socket| {
            debug!("[Shard {}] Beginning handshake with gateway.", shard_id);
            tlsconn.connect(host.as_ref(), socket)
                .map(|s| TungsteniteStream::Tls(s))
                .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))
        });
        let stream = handshake.and_then(|mut stream| {
            tokio_tungstenite::stream::NoDelay::set_nodelay(&mut stream, true)
                .map(move |()| stream)
        });
        let stream = stream.and_then(move |stream| {
            tokio_tungstenite::client_async_with_config(req, stream, Some(WebSocketConfig {
                max_message_size: Some(usize::max_value()),
                max_frame_size: Some(usize::max_value()),
                ..Default::default()
            })).map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))
        });

        stream.map(move |(wstream, _)| {
            let (tx, rx) = mpsc::unbounded();
            let (sink, stream) = wstream.split();
            tokio::spawn(rx.map_err(|err| {
                error!("Failed to select sink. {:?}", err);
                TungsteniteError::Io(IoError::new(ErrorKind::Other, "Error whilst attempting to select sink."))
            }).forward(sink).map(|_| ()).map_err(|_| ()));

            (tx, stream)
        }).from_err()
    }

    fn get_addr_info(req: &Request) -> (String, u16) {
        let host = req.url.host_str().expect("Could Not parse the Websocket Host.");
        let port = req.url.port_or_known_default().expect("Could not parse the websocket port.");

        (host.to_string(), port)
    }
}

fn send(sender: &Arc<Mutex<UnboundedSender<WebsocketMessage>>>, mess: WebsocketMessage) -> Result<()> {
    sender.lock().start_send(mess)
        .map(|_| ())
        .map_err(From::from)
}