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
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
|
use chrono::UTC;
use std::io::Write;
use std::net::Shutdown;
use std::thread;
use std::time::{Duration as StdDuration, Instant};
use std::mem;
use super::{GatewayError, prep};
use websocket::message::{CloseData, OwnedMessage};
use websocket::stream::sync::AsTcpStream;
use websocket::sync::client::{Client, ClientBuilder};
use websocket::sync::stream::{TcpStream, TlsStream};
use websocket::WebSocketError;
use ::constants::OpCode;
use ::http;
use ::internal::prelude::*;
use ::internal::ws_impl::{ReceiverExt, SenderExt};
use ::model::event::{Event, GatewayEvent, ReadyEvent};
use ::model::{Game, GuildId, OnlineStatus};
#[cfg(feature="cache")]
use ::client::CACHE;
#[cfg(feature="voice")]
use ::ext::voice::Manager as VoiceManager;
#[cfg(feature="cache")]
use ::utils;
pub type WsClient = Client<TlsStream<TcpStream>>;
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 {
pub client: WsClient,
current_presence: CurrentPresence,
/// A tuple of:
///
/// - the last instant that a heartbeat was sent
/// - the last instant that an acknowledgement was received
///
/// This can be used to calculate [`latency`].
///
/// [`latency`]: fn.latency.html
heartbeat_instants: (Instant, Option<Instant>),
heartbeat_interval: u64,
/// This is used by the heartbeater to determine whether the last
/// heartbeat was sent without an acknowledgement, and whether to reconnect.
// This _must_ be set to `true` in `Shard::handle_event`'s
// `Ok(GatewayEvent::HeartbeatAck)` arm.
last_heartbeat_acknowledged: bool,
seq: u64,
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::gateway::Shard;
/// use serenity::http;
/// 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 = http::get_gateway().expect("Valid gateway response").url;
/// let shard = Shard::new(&gateway, &token, None)
/// .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]>)
-> Result<(Shard, ReadyEvent)> {
let mut client = connect(base_url)?;
let identification = prep::identify(token, shard_info);
client.send_json(&identification)?;
let heartbeat_interval = match client.recv_json(GatewayEvent::decode)? {
GatewayEvent::Hello(interval) => interval,
other => {
debug!("Unexpected event during shard start: {:?}", other);
return Err(Error::Gateway(GatewayError::ExpectedHello));
},
};
let heartbeat_sent = Instant::now();
// Parse READY
let event = client.recv_json(GatewayEvent::decode)?;
let (ready, sequence) = prep::parse_ready(event, &mut client, &identification)?;
Ok((feature_voice! {{
Shard {
client: client,
current_presence: (None, OnlineStatus::Online, false),
heartbeat_instants: (heartbeat_sent, None),
heartbeat_interval: heartbeat_interval,
last_heartbeat_acknowledged: true,
seq: sequence,
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 {
client: client,
current_presence: (None, OnlineStatus::Online, false),
heartbeat_instants: (heartbeat_sent, None),
heartbeat_interval: heartbeat_interval,
last_heartbeat_acknowledged: true,
seq: sequence,
token: token.to_owned(),
session_id: Some(ready.ready.session_id.clone()),
shard_info: shard_info,
ws_url: base_url.to_owned(),
}
}}, ready))
}
/// 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".
///
/// # Examples
///
/// Retrieving the shard info for the second shard, out of two shards total:
///
/// ```rust,no_run
/// # use serenity::client::gateway::Shard;
/// #
/// # let (shard, _) = Shard::new("", "", Some([1, 2])).unwrap();
/// #
/// assert_eq!(shard.shard_info(), Some([1, 2]));
/// ```
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.
///
/// # Examples
///
/// Setting the current game to playing `"Heroes of the Storm"`:
///
/// ```rust,no_run
/// # use serenity::client::gateway::Shard;
/// #
/// # let (mut shard, _) = Shard::new("", "", Some([0, 1])).unwrap();
/// #
/// use serenity::model::Game;
///
/// shard.set_game(Some(Game::playing("Heroes of the Storm")));
/// ```
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 online status, so it is
/// automatically converted to [`Invisible`].
///
/// Other presence settings are maintained.
///
/// # Examples
///
/// Setting the current online status for the shard to [`DoNotDisturb`].
///
/// ```rust,no_run
/// # use serenity::client::gateway::Shard;
/// #
/// # let (mut shard, _) = Shard::new("", "", Some([0, 1])).unwrap();
/// #
/// use serenity::model::OnlineStatus;
///
/// shard.set_status(OnlineStatus::DoNotDisturb);
/// ```
///
/// [`DoNotDisturb`]: ../../model/enum.OnlineStatus.html#variant.DoNotDisturb
/// [`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,no_run
/// # use serenity::client::gateway::Shard;
/// #
/// # let (mut shard, _) = Shard::new("", "", Some([0, 1])).unwrap();
/// #
/// use serenity::model::{Game, OnlineStatus};
///
/// shard.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>) -> Result<Option<Event>> {
match event {
Ok(GatewayEvent::Dispatch(seq, event)) => {
self.seq = seq;
self.handle_dispatch(&event);
Ok(Some(event))
},
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);
let _ = self.shutdown();
return if self.session_id.is_some() {
self.resume().map(Some)
} else {
self.reconnect().map(Some)
};
}
let map = json!({
"d": Value::Null,
"op": OpCode::Heartbeat.num(),
});
self.client.send_json(&map)?;
Ok(None)
},
Ok(GatewayEvent::HeartbeatAck) => {
self.heartbeat_instants.1 = Some(Instant::now());
self.last_heartbeat_acknowledged = true;
Ok(None)
},
Ok(GatewayEvent::Hello(interval)) => {
if interval > 0 {
self.heartbeat_interval = interval;
}
let _ = self.shutdown();
if self.session_id.is_some() {
self.resume().map(Some)
} else {
self.reconnect().map(Some)
}
},
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 _ = self.client.send_json(&identification);
Ok(None)
},
Ok(GatewayEvent::Reconnect) => {
let _ = self.shutdown();
self.reconnect().map(Some)
},
Err(Error::Gateway(GatewayError::Closed(data))) => {
let num = data.as_ref().map(|d| d.status_code);
let reason = data.map(|d| d.reason);
let clean = num == Some(1000);
{
let kind = if clean { "Cleanly" } else { "Uncleanly" };
info!("{} closing with {:?}: {:?}", kind, num, reason);
}
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, reason);
},
_ => {},
}
let resume = num.map(|num| {
num != 1000 && num != 4004 && self.session_id.is_some()
}).unwrap_or(false);
if resume {
info!("Attempting to resume");
if self.session_id.is_some() {
let _ = self.shutdown();
match self.resume() {
Ok(ev) => {
info!("Resumed");
return Ok(Some(ev));
},
Err(why) => {
warn!("Error resuming: {:?}", why);
info!("Falling back to reconnecting");
},
}
}
}
info!("Reconnecting");
let _ = self.shutdown();
self.reconnect().map(Some)
},
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 self.session_id.is_some() {
info!("Attempting to resume");
let _ = self.shutdown();
match self.resume() {
Ok(ev) => {
info!("Resumed");
return Ok(Some(ev));
},
Err(why) => {
warn!("Error resuming: {:?}", why);
info!("Falling back to reconnecting");
},
}
}
info!("Reconnecting");
let _ = self.shutdown();
self.reconnect().map(Some)
},
Err(error) => Err(error),
}
}
/// Calculates the heartbeat latency between the shard and the gateway.
///
/// # Examples
///
/// When using the [`Client`], output the latency in response to a `"~ping"`
/// message handled through [`Client::on_message`].
///
/// ```rust,no_run
/// # use serenity::client::Client;
/// #
/// # let mut client = Client::new("hello source code viewer <3");
/// #
/// client.on_message(|ctx, msg| {
/// if msg.content == "~ping" {
/// if let Some(latency) = ctx.shard.lock().unwrap().latency() {
/// let s = format!("{}.{}s", latency.as_secs(), latency.subsec_nanos());
///
/// let _ = msg.channel_id.say(&s);
/// } else {
/// let _ = msg.channel_id.say("N/A");
/// }
/// }
/// });
/// ```
///
/// [`Client`]: ../struct.Client.html
/// [`Client::on_message`]: ../struct.Client.html#method.on_message
// 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)
}
/// Shuts down the receiver by attempting to cleanly close the
/// connection.
#[doc(hidden)]
pub fn shutdown_clean(client: &mut WsClient) -> Result<()> {
{
let message = OwnedMessage::Close(Some(CloseData {
status_code: 1000,
reason: String::new(),
}));
client.send_message(&message)?;
}
let mut stream = client.stream_ref().as_tcp();
stream.flush()?;
stream.shutdown(Shutdown::Both)?;
debug!("Cleanly shutdown shard");
Ok(())
}
/// Uncleanly shuts down the receiver by not sending a close code.
#[doc(hidden)]
pub fn shutdown(&mut self) -> Result<()> {
let mut stream = self.client.stream_ref().as_tcp();
stream.flush()?;
stream.shutdown(Shutdown::Both)?;
Ok(())
}
/// Requests that one or multiple [`Guild`]s be chunked.
///
/// This will ask the gateway 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.
///
/// # Examples
///
/// Chunk a single guild by Id, limiting to 2000 [`Member`]s, and not
/// specifying a query parameter:
///
/// ```rust,no_run
/// # use serenity::client::gateway::Shard;
/// #
/// # let (mut shard, _) = Shard::new("", "", Some([0, 1])).unwrap();
/// #
/// use serenity::model::GuildId;
///
/// let guild_ids = vec![GuildId(81384788765712384)];
///
/// shard.chunk_guilds(&guild_ids, Some(2000), None);
/// ```
///
/// Chunk a single guild by Id, limiting to 20 members, and specifying a
/// query parameter of `"do"`:
///
/// ```rust,no_run
/// # use serenity::client::gateway::Shard;
/// #
/// # let (mut shard, _) = Shard::new("", "", Some([0, 1])).unwrap();
/// #
/// use serenity::model::GuildId;
///
/// let guild_ids = vec![GuildId(81384788765712384)];
///
/// shard.chunk_guilds(&guild_ids, Some(20), Some("do"));
/// ```
///
/// [`Event::GuildMembersChunk`]: ../../model/event/enum.Event.html#variant.GuildMembersChunk
/// [`Guild`]: ../../model/struct.Guild.html
/// [`Member`]: ../../model/struct.Member.html
pub fn chunk_guilds(&mut self, guild_ids: &[GuildId], limit: Option<u16>, query: Option<&str>) {
let msg = json!({
"op": OpCode::GetGuildMembers.num(),
"d": {
"guild_id": guild_ids.iter().map(|x| x.0).collect::<Vec<u64>>(),
"limit": limit.unwrap_or(0),
"query": query.unwrap_or(""),
},
});
let _ = self.client.send_json(&msg);
}
/// Calculates the number of guilds that the shard is responsible for.
///
/// If sharding is not being used (i.e. 1 shard), then the total number of
/// [`Guild`] in the [`Cache`] will be used.
///
/// **Note**: Requires the `cache` feature be enabled.
///
/// # Examples
///
/// Retrieve the number of guilds a shard is responsible for:
///
/// ```rust,no_run
/// # use serenity::client::gateway::Shard;
/// #
/// # let (shard, _) = Shard::new("will anyone read this", "", Some([0, 1])).unwrap();
/// #
/// let info = shard.shard_info();
/// let guilds = shard.guilds_handled();
///
/// println!("Shard {:?} is responsible for {} guilds", info, guilds);
/// ```
///
/// [`Cache`]: ../ext/cache/struct.Cache.html
/// [`Guild`]: ../model/struct.Guild.html
#[cfg(feature="cache")]
pub fn guilds_handled(&self) -> u16 {
let cache = CACHE.read().unwrap();
if let Some((shard_id, shard_count)) = self.shard_info.map(|s| (s[0], s[1])) {
cache.guilds
.keys()
.filter(|guild_id| utils::shard_id(guild_id.0, shard_count) == shard_id)
.count() as u16
} else {
cache.guilds.len() as u16
}
}
#[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);
}
}
}
}
}
#[doc(hidden)]
pub fn heartbeat(&mut self) -> Result<()> {
let map = json!({
"d": self.seq,
"op": OpCode::Heartbeat.num(),
});
trace!("Sending heartbeat d: {}", self.seq);
match self.client.send_json(&map) {
Ok(_) => {
self.heartbeat_instants.0 = Instant::now();
self.last_heartbeat_acknowledged = false;
Ok(())
},
Err(why) => {
match why {
Error::WebSocket(WebSocketError::IoError(err)) => {
if err.raw_os_error() != Some(32) {
debug!("Err w/ keepalive: {:?}", err);
}
},
other => warn!("Other err w/ keepalive: {:?}", other),
}
Err(Error::Gateway(GatewayError::HeartbeatFailed))
},
}
}
#[doc(hidden)]
pub fn heartbeat_interval(&self) -> i64 {
self.heartbeat_interval as i64
}
#[doc(hidden)]
pub fn last_heartbeat_acknowledged(&self) -> bool {
self.last_heartbeat_acknowledged
}
fn reconnect(&mut self) -> Result<Event> {
info!("Attempting to reconnect");
// Take a few attempts at reconnecting.
for i in 1u64..11u64 {
let gateway_url = http::get_gateway()?.url;
let shard = Shard::new(&gateway_url,
&self.token,
self.shard_info);
if let Ok((shard, ready)) = shard {
let _ = self.shutdown();
mem::replace(self, shard);
self.session_id = Some(ready.ready.session_id.clone());
return Ok(Event::Ready(ready));
}
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))
}
#[doc(hidden)]
pub fn resume(&mut self) -> Result<Event> {
let session_id = match self.session_id.clone() {
Some(session_id) => session_id,
None => return Err(Error::Gateway(GatewayError::NoSessionId)),
};
self.client = connect(&self.ws_url)?;
self.client.send_json(&json!({
"op": OpCode::Resume.num(),
"d": {
"session_id": session_id,
"seq": self.seq,
"token": self.token,
},
}))?;
// Note to self when this gets accepted in a decade:
// https://github.com/rust-lang/rfcs/issues/961
let ev;
loop {
match self.client.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) => {
self.heartbeat_interval = i;
}
GatewayEvent::InvalidateSession => {
self.client.send_json(&prep::identify(&self.token, self.shard_info))?;
},
other => {
debug!("Unexpected event: {:?}", other);
return Err(Error::Gateway(GatewayError::InvalidHandshake));
},
}
}
Ok(ev)
}
fn update_presence(&mut self) {
let (ref game, status, afk) = self.current_presence;
let now = UTC::now().timestamp() as u64;
let msg = json!({
"op": OpCode::StatusUpdate.num(),
"d": {
"afk": afk,
"since": now,
"status": status.name(),
"game": game.as_ref().map(|x| json!({
"name": x.name,
})),
},
});
let _ = self.client.send_json(&msg);
#[cfg(feature="cache")]
{
let mut cache = CACHE.write().unwrap();
let current_user_id = cache.user.id;
cache.presences.get_mut(¤t_user_id).map(|presence| {
presence.game = game.clone();
presence.last_modified = Some(now);
});
}
}
}
fn connect(base_url: &str) -> Result<WsClient> {
let url = prep::build_gateway_url(base_url)?;
let client = ClientBuilder::from_url(&url).connect_secure(None)?;
let timeout = StdDuration::from_secs(1);
{
let stream = client.stream_ref().as_tcp();
stream.set_read_timeout(Some(timeout))?;
stream.set_read_timeout(Some(timeout))?;
}
Ok(client)
}
|