-
Notifications
You must be signed in to change notification settings - Fork 563
/
shard.rs
694 lines (595 loc) · 25 KB
/
shard.rs
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
use serde_json::builder::ObjectBuilder;
use std::io::Write;
use std::net::Shutdown;
use std::sync::mpsc::{self, Sender as MpscSender};
use std::sync::{Arc, Mutex};
use std::thread::{self, Builder as ThreadBuilder};
use std::time::{Duration as StdDuration, Instant};
use std::mem;
use super::super::login_type::LoginType;
use super::super::rest;
use super::{GatewayError, GatewayStatus, prep};
use time;
use websocket::client::{Client as WsClient, Sender, Receiver};
use websocket::message::Message as WsMessage;
use websocket::result::WebSocketError;
use websocket::stream::WebSocketStream;
use websocket::ws::sender::Sender as WsSender;
use ::constants::OpCode;
use ::internal::prelude::*;
use ::internal::ws_impl::{ReceiverExt, SenderExt};
use ::model::event::{Event, GatewayEvent, ReadyEvent};
use ::model::{ChannelId, Game, GuildId, OnlineStatus};
#[cfg(feature="cache")]
use ::client::CACHE;
#[cfg(feature="voice")]
use ::ext::voice::Manager as VoiceManager;
type CurrentPresence = (Option<Game>, OnlineStatus, bool);
/// A Shard is a higher-level handler for a websocket connection to Discord's
/// gateway. The shard allows for sending and receiving messages over the
/// websocket, such as setting the active game, reconnecting, syncing guilds,
/// and more.
///
/// Refer to the [module-level documentation][module docs] for information on
/// effectively using multiple shards, if you need to.
///
/// Note that there are additional methods available if you are manually
/// managing a shard yourself, although they are hidden from the documentation
/// since there are few use cases for doing such.
///
/// # Stand-alone shards
///
/// You may instantiate a shard yourself - decoupled from the [`Client`] - if
/// you need to. For most use cases, you will not need to do this, and you can
/// leave the client to do it.
///
/// This can be done by passing in the required parameters to [`new`]. You can
/// then manually handle the shard yourself and receive events via
/// [`receive`].
///
/// **Note**: You _really_ do not need to do this. Just call one of the
/// appropriate methods on the [`Client`].
///
/// # Examples
///
/// See the documentation for [`new`] on how to use this.
///
/// [`Client`]: ../struct.Client.html
/// [`new`]: #method.new
/// [`receive`]: #method.receive
/// [docs]: https://discordapp.com/developers/docs/topics/gateway#sharding
/// [module docs]: index.html#sharding
pub struct Shard {
current_presence: CurrentPresence,
/// A tuple of the last instant that a heartbeat was sent, and the last that
/// an acknowledgement was received.
///
/// This can be used to calculate [`latency`].
///
/// [`latency`]: fn.latency.html
heartbeat_instants: (Arc<Mutex<Instant>>, Option<Instant>),
keepalive_channel: MpscSender<GatewayStatus>,
seq: u64,
login_type: LoginType,
session_id: Option<String>,
shard_info: Option<[u64; 2]>,
token: String,
ws_url: String,
/// The voice connections that this Shard is responsible for. The Shard will
/// update the voice connections' states.
#[cfg(feature="voice")]
pub manager: VoiceManager,
}
impl Shard {
/// Instantiates a new instance of a Shard, bypassing the client.
///
/// **Note**: You should likely never need to do this yourself.
///
/// # Examples
///
/// Instantiating a new Shard manually for a bot with no shards, and
/// then listening for events:
///
/// ```rust,ignore
/// use serenity::client::gateway::Shard;
/// use serenity::client::{LoginType, rest};
/// use std::env;
///
/// let token = env::var("DISCORD_BOT_TOKEN").expect("Token in environment");
/// // retrieve the gateway response, which contains the URL to connect to
/// let gateway = rest::get_gateway().expect("Valid gateway response").url;
/// let shard = Shard::new(&gateway, &token, None, LoginType::Bot)
/// .expect("Working shard");
///
/// // at this point, you can create a `loop`, and receive events and match
/// // their variants
/// ```
pub fn new(base_url: &str,
token: &str,
shard_info: Option<[u64; 2]>,
login_type: LoginType)
-> Result<(Shard, ReadyEvent, Receiver<WebSocketStream>)> {
let url = prep::build_gateway_url(base_url)?;
let response = WsClient::connect(url)?.send()?;
response.validate()?;
let (mut sender, mut receiver) = response.begin().split();
let identification = prep::identify(token, shard_info);
sender.send_json(&identification)?;
let heartbeat_interval = match receiver.recv_json(GatewayEvent::decode)? {
GatewayEvent::Hello(interval) => interval,
other => {
debug!("Unexpected event during shard start: {:?}", other);
return Err(Error::Gateway(GatewayError::ExpectedHello));
},
};
let (tx, rx) = mpsc::channel();
let thread_name = match shard_info {
Some(info) => format!("serenity keepalive [shard {}/{}]",
info[0],
info[1] - 1),
None => "serenity keepalive [unsharded]".to_owned(),
};
let heartbeat_sent = Arc::new(Mutex::new(Instant::now()));
let heartbeat_clone = heartbeat_sent.clone();
ThreadBuilder::new()
.name(thread_name)
.spawn(move || {
prep::keepalive(heartbeat_interval, heartbeat_clone, sender, rx)
})?;
// Parse READY
let event = receiver.recv_json(GatewayEvent::decode)?;
let (ready, sequence) = prep::parse_ready(event,
&tx,
&mut receiver,
identification)?;
Ok((feature_voice! {{
Shard {
current_presence: (None, OnlineStatus::Online, false),
heartbeat_instants: (heartbeat_sent, None),
keepalive_channel: tx.clone(),
seq: sequence,
login_type: login_type,
token: token.to_owned(),
session_id: Some(ready.ready.session_id.clone()),
shard_info: shard_info,
ws_url: base_url.to_owned(),
manager: VoiceManager::new(tx, ready.ready.user.id),
}
} else {
Shard {
current_presence: (None, OnlineStatus::Online, false),
heartbeat_instants: (heartbeat_sent, None),
keepalive_channel: tx.clone(),
seq: sequence,
login_type: login_type,
token: token.to_owned(),
session_id: Some(ready.ready.session_id.clone()),
shard_info: shard_info,
ws_url: base_url.to_owned(),
}
}}, ready, receiver))
}
/// Retrieves a copy of the current shard information.
///
/// The first element is the _current_ shard - 0-indexed - while the second
/// element is the _total number_ of shards -- 1-indexed.
///
/// For example, if using 3 shards in total, and if this is shard 1, then it
/// can be read as "the second of three shards".
pub fn shard_info(&self) -> Option<[u64; 2]> {
self.shard_info
}
/// Sets whether the current user is afk. This helps Discord determine where
/// to send notifications.
///
/// Other presence settings are maintained.
pub fn set_afk(&mut self, afk: bool) {
self.current_presence.2 = afk;
self.update_presence();
}
/// Sets the user's current game, if any.
///
/// Other presence settings are maintained.
pub fn set_game(&mut self, game: Option<Game>) {
self.current_presence.0 = game;
self.update_presence();
}
/// Sets the user's current online status.
///
/// Note that [`Offline`] is not a valid presence, so it is automatically
/// converted to [`Invisible`].
///
/// Other presence settings are maintained.
///
/// [`Invisible`]: ../../model/enum.OnlineStatus.html#variant.Invisible
/// [`Offline`]: ../../model/enum.OnlineStatus.html#variant.Offline
pub fn set_status(&mut self, online_status: OnlineStatus) {
self.current_presence.1 = match online_status {
OnlineStatus::Offline => OnlineStatus::Invisible,
other => other,
};
self.update_presence();
}
/// Sets the user's full presence information.
///
/// Consider using the individual setters if you only need to modify one of
/// these.
///
/// # Examples
///
/// Set the current user as playing `"Heroes of the Storm"`, being online,
/// and not being afk:
///
/// ```rust,ignore
/// use serenity::model::{Game, OnlineStatus};
///
/// // assuming you are in a context
///
/// context.shard.lock()
/// .unwrap()
/// .set_presence(Some(Game::playing("Heroes of the Storm")),
/// OnlineStatus::Online,
/// false);
/// ```
pub fn set_presence(&mut self,
game: Option<Game>,
mut status: OnlineStatus,
afk: bool) {
if status == OnlineStatus::Offline {
status = OnlineStatus::Invisible;
}
self.current_presence = (game, status, afk);
self.update_presence();
}
/// Handles an event from the gateway over the receiver, requiring the
/// receiver to be passed if a reconnect needs to occur.
///
/// The best case scenario is that one of two values is returned:
///
/// - `Ok(None)`: a heartbeat, late hello, or session invalidation was
/// received;
/// - `Ok(Some((event, None)))`: an op0 dispatch was received, and the
/// shard's voice state will be updated, _if_ the `voice` feature is
/// enabled.
#[allow(cyclomatic_complexity)]
#[doc(hidden)]
pub fn handle_event(&mut self,
event: Result<GatewayEvent>,
mut receiver: &mut Receiver<WebSocketStream>)
-> Result<Option<(Event, Option<Receiver<WebSocketStream>>)>> {
match event {
Ok(GatewayEvent::Dispatch(seq, event)) => {
let status = GatewayStatus::Sequence(seq);
let _ = self.keepalive_channel.send(status);
self.seq = seq;
self.handle_dispatch(&event);
Ok(Some((event, None)))
},
Ok(GatewayEvent::Heartbeat(s)) => {
info!("Received shard heartbeat");
// Received seq is off -- attempt to resume.
if s > self.seq + 1 {
info!("Received off sequence (them: {}; us: {}); resuming",
s,
self.seq);
return if let Some(session_id) = self.session_id.clone() {
self.resume(session_id, receiver)
.map(|(ev, rec)| Some((ev, Some(rec))))
} else {
self.reconnect(receiver)
.map(|(ev, rec)| Some((ev, Some(rec))))
};
}
let map = ObjectBuilder::new()
.insert("d", Value::Null)
.insert("op", OpCode::Heartbeat.num())
.build();
let status = GatewayStatus::SendMessage(map);
let _ = self.keepalive_channel.send(status);
Ok(None)
},
Ok(GatewayEvent::HeartbeatAck) => {
self.heartbeat_instants.1 = Some(Instant::now());
Ok(None)
},
Ok(GatewayEvent::Hello(interval)) => {
if interval > 0 {
let status = GatewayStatus::Interval(interval);
let _ = self.keepalive_channel.send(status);
}
if let Some(session_id) = self.session_id.clone() {
self.resume(session_id, receiver)
.map(|(ev, rec)| Some((ev, Some(rec))))
} else {
self.reconnect(receiver)
.map(|(ev, rec)| Some((ev, Some(rec))))
}
},
Ok(GatewayEvent::InvalidateSession) => {
info!("Received session invalidation; re-identifying");
self.seq = 0;
self.session_id = None;
let identification = prep::identify(&self.token, self.shard_info);
let status = GatewayStatus::SendMessage(identification);
let _ = self.keepalive_channel.send(status);
Ok(None)
},
Ok(GatewayEvent::Reconnect) => {
self.reconnect(receiver).map(|(ev, rec)| Some((ev, Some(rec))))
},
Err(Error::Gateway(GatewayError::Closed(num, message))) => {
let clean = num == Some(1000);
{
let kind = if clean { "Cleanly" } else { "Uncleanly" };
info!("{} closing with {:?}: {}", kind, num, message);
}
match num {
Some(4001) => warn!("Sent invalid opcode"),
Some(4002) => warn!("Sent invalid message"),
Some(4003) => warn!("Sent no authentication"),
Some(4004) => warn!("Sent invalid authentication"),
Some(4005) => warn!("Already authenticated"),
Some(4007) => {
warn!("Sent invalid seq: {}", self.seq);
self.seq = 0;
},
Some(4008) => warn!("Gateway ratelimited"),
Some(4010) => warn!("Sent invalid shard"),
Some(4011) => error!("Bot requires more shards"),
Some(4006) | Some(4009) => {
info!("Invalid session");
self.session_id = None;
},
Some(other) if !clean => {
warn!("Unknown unclean close {}: {:?}", other, message);
},
_ => {},
}
let resume = num.map(|x| x != 1000 && x != 4004 && self.session_id.is_some())
.unwrap_or(false);
if resume {
info!("Attempting to resume");
if let Some(session_id) = self.session_id.clone() {
match self.resume(session_id, receiver) {
Ok((ev, rec)) => {
info!("Resumed");
return Ok(Some((ev, Some(rec))));
},
Err(why) => {
warn!("Error resuming: {:?}", why);
info!("Falling back to reconnecting");
},
}
}
}
info!("Reconnecting");
self.reconnect(receiver).map(|(ev, rec)| Some((ev, Some(rec))))
},
Err(Error::WebSocket(why)) => {
if let WebSocketError::NoDataAvailable = why {
if self.heartbeat_instants.1.is_none() {
return Ok(None);
}
}
warn!("Websocket error: {:?}", why);
info!("Will attempt to reconnect or resume");
// Attempt to resume if the following was not received:
//
// - InvalidateSession.
//
// Otherwise, fallback to reconnecting.
if let Some(session_id) = self.session_id.clone() {
info!("Attempting to resume");
match self.resume(session_id, &mut receiver) {
Ok((ev, rec)) => {
info!("Resumed");
return Ok(Some((ev, Some(rec))));
},
Err(why) => {
warn!("Error resuming: {:?}", why);
info!("Falling back to reconnecting");
},
}
}
info!("Reconnecting");
self.reconnect(receiver).map(|(ev, rec)| Some((ev, Some(rec))))
},
Err(error) => Err(error),
}
}
/// Calculates the heartbeat latency (in nanoseconds) between the shard and
/// Discord.
// Shamelessly stolen from brayzure's commit in eris:
// <https://github.com/abalabahaha/eris/commit/0ce296ae9a542bcec0edf1c999ee2d9986bed5a6>
pub fn latency(&self) -> Option<StdDuration> {
self.heartbeat_instants.1.map(|send| send - *self.heartbeat_instants.0.lock().unwrap())
}
/// Shuts down the receiver by attempting to cleanly close the
/// connection.
#[doc(hidden)]
pub fn shutdown_clean(receiver: &mut Receiver<WebSocketStream>)
-> Result<()> {
let r = receiver.get_mut().get_mut();
{
let mut sender = Sender::new(r.by_ref(), true);
let message = WsMessage::close_because(1000, "");
sender.send_message(&message)?;
}
r.flush()?;
r.shutdown(Shutdown::Both)?;
debug!("Cleanly shutdown shard");
Ok(())
}
pub fn shutdown(receiver: &mut Receiver<WebSocketStream>) -> Result<()> {
let r = receiver.get_mut().get_mut();
r.flush()?;
r.shutdown(Shutdown::Both)?;
Ok(())
}
/// Syncs a number of [`Call`]s, given by their associated channel Ids. This
/// will allow the current user to know what calls are currently occurring,
/// as otherwise events will not be received.
pub fn sync_calls(&self, channels: &[ChannelId]) {
for &channel in channels {
let msg = ObjectBuilder::new()
.insert("op", OpCode::SyncCall.num())
.insert_object("d", |obj| obj
.insert("channel_id", channel.0)
)
.build();
let _ = self.keepalive_channel.send(GatewayStatus::SendMessage(msg));
}
}
/// Requests that one or multiple [`Guild`]s be synced.
///
/// This will ask Discord to start sending member chunks for large guilds
/// (250 members+). If a guild is over 250 members, then a full member list
/// will not be downloaded, and must instead be requested to be sent in
/// "chunks" containing members.
///
/// Member chunks are sent as the [`Event::GuildMembersChunk`] event. Each
/// chunk only contains a partial amount of the total members.
///
/// If the `cache` feature is enabled, the cache will automatically be
/// updated with member chunks.
pub fn chunk_guilds(&self, guild_ids: &[GuildId], limit: Option<u16>, query: Option<&str>) {
let msg = ObjectBuilder::new()
.insert("op", OpCode::GetGuildMembers.num())
.insert_object("d", |obj| obj
.insert_array("guild_id", |a| guild_ids.iter().fold(a, |a, s| a.push(s.0)))
.insert("limit", limit.unwrap_or(0))
.insert("query", query.unwrap_or("")))
.build();
let _ = self.keepalive_channel.send(GatewayStatus::SendMessage(msg));
}
/// Syncs the user's guilds.
pub fn sync_guilds(&self, guild_ids: &[GuildId]) {
let msg = ObjectBuilder::new()
.insert("op", OpCode::SyncGuild.num())
.insert_array("d", |a| guild_ids.iter().fold(a, |a, s| a.push(s.0)))
.build();
let _ = self.keepalive_channel.send(GatewayStatus::SendMessage(msg));
}
#[allow(unused_variables)]
fn handle_dispatch(&mut self, event: &Event) {
#[cfg(feature="voice")]
{
if let Event::VoiceStateUpdate(ref update) = *event {
if let Some(guild_id) = update.guild_id {
if let Some(handler) = self.manager.get(guild_id) {
handler.update_state(&update.voice_state);
}
}
}
if let Event::VoiceServerUpdate(ref update) = *event {
if let Some(guild_id) = update.guild_id {
if let Some(handler) = self.manager.get(guild_id) {
handler.update_server(&update.endpoint, &update.token);
}
}
}
}
}
fn reconnect(&mut self, mut receiver: &mut Receiver<WebSocketStream>)
-> Result<(Event, Receiver<WebSocketStream>)> {
info!("Attempting to reconnect");
// Take a few attempts at reconnecting.
for i in 1u64..11u64 {
let gateway_url = rest::get_gateway()?.url;
let shard = Shard::new(&gateway_url,
&self.token,
self.shard_info,
self.login_type);
if let Ok((shard, ready, receiver_new)) = shard {
let _ = Shard::shutdown(&mut receiver);
mem::replace(self, shard);
self.session_id = Some(ready.ready.session_id.clone());
return Ok((Event::Ready(ready), receiver_new));
}
let seconds = i.pow(2);
debug!("Exponentially backing off for {} seconds", seconds);
// Exponentially back off.
thread::sleep(StdDuration::from_secs(seconds));
}
// Reconnecting failed; just return an error instead.
Err(Error::Gateway(GatewayError::ReconnectFailure))
}
fn resume(&mut self, session_id: String, receiver: &mut Receiver<WebSocketStream>)
-> Result<(Event, Receiver<WebSocketStream>)> {
receiver.get_mut().get_mut().shutdown(Shutdown::Both)?;
let url = prep::build_gateway_url(&self.ws_url)?;
let response = WsClient::connect(url)?.send()?;
response.validate()?;
let (mut sender, mut receiver) = response.begin().split();
sender.send_json(&ObjectBuilder::new()
.insert_object("d", |o| o
.insert("session_id", session_id)
.insert("seq", self.seq)
.insert("token", &self.token))
.insert("op", OpCode::Resume.num())
.build())?;
// Note to self when this gets accepted in a decade:
// https://github.com/rust-lang/rfcs/issues/961
let ev;
loop {
match receiver.recv_json(GatewayEvent::decode)? {
GatewayEvent::Dispatch(seq, event) => {
match event {
Event::Ready(ref ready) => {
self.session_id = Some(ready.ready.session_id.clone());
},
Event::Resumed(_) => info!("Resumed"),
ref other => warn!("Unknown resume event: {:?}", other),
}
self.seq = seq;
ev = event;
break;
},
GatewayEvent::Hello(i) => {
let _ = self.keepalive_channel.send(GatewayStatus::Interval(i));
}
GatewayEvent::InvalidateSession => {
sender.send_json(&prep::identify(&self.token, self.shard_info))?;
},
other => {
debug!("Unexpected event: {:?}", other);
return Err(Error::Gateway(GatewayError::InvalidHandshake));
},
}
}
let _ = self.keepalive_channel.send(GatewayStatus::Sender(sender));
Ok((ev, receiver))
}
fn update_presence(&self) {
let (ref game, status, afk) = self.current_presence;
let now = time::get_time().sec as u64;
let msg = ObjectBuilder::new()
.insert("op", OpCode::StatusUpdate.num())
.insert_object("d", move |mut object| {
object = object.insert("afk", afk)
.insert("since", now)
.insert("status", status.name());
match game.as_ref() {
Some(game) => {
object.insert_object("game", move |o| o
.insert("name", &game.name))
},
None => object.insert("game", Value::Null),
}
})
.build();
let _ = self.keepalive_channel.send(GatewayStatus::SendMessage(msg));
#[cfg(feature="cache")]
{
let mut cache = CACHE.write().unwrap();
let current_user_id = cache.user.id;
for (user_id, presence) in &mut cache.presences {
if *user_id != current_user_id {
continue;
}
presence.game = game.clone();
presence.last_modified = Some(now);
}
}
}
}