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
use std::{sync::Arc, time::Duration};
use tokio::{sync::mpsc, task::JoinHandle, time::sleep};
use crate::{
PresenceClient,
errors::{DisconnectReason, DiscordSockError, PresenceRunnerError},
socket::{DiscordSock, Opcode},
types::{
ActivitySpec, DynamicRPCFrame, IPCCommand, ReadyRPCFrame,
data::{ActivityResponseData, ReadyData},
},
utils::get_current_timestamp,
};
type Callback<T> = Option<Arc<dyn Fn(T) + Send + Sync + 'static>>;
macro_rules! impl_callback {
($name:ident, $arg:ty, $doc:expr) => {
#[doc = $doc]
pub fn $name<F: Fn($arg) + Send + Sync + 'static>(mut self, f: F) -> Self {
self.$name = Some(Arc::new(f));
self
}
};
}
/// A runner that manages the Discord RPC background task.
/// Create a runner, configure it, run it to get a client handle, then clone the handle for sharing.
pub struct PresenceRunner {
rx: Option<tokio::sync::mpsc::Receiver<IPCCommand>>,
client: PresenceClient,
join_handle: Option<JoinHandle<()>>,
on_ready: Callback<ReadyData>,
on_activity_send: Callback<ActivityResponseData>,
on_disconnect: Callback<DisconnectReason>,
on_retry: Callback<usize>,
show_errors: bool,
max_retries: usize,
}
impl PresenceRunner {
#[must_use]
/// Create a new [`PresenceRunner`] instance. Requires the client ID of your chosen app from the
/// [Discord Developer Portal](https://discord.com/developers/applications).
pub fn new(client_id: impl Into<String>) -> Self {
let (tx, rx) = mpsc::channel(32);
let client = PresenceClient {
tx,
client_id: client_id.into(),
};
Self {
rx: Some(rx),
client,
join_handle: None,
on_ready: None,
on_activity_send: None,
on_disconnect: None,
on_retry: None,
show_errors: false,
max_retries: 0,
}
}
impl_callback!(
on_ready,
ReadyData,
"Runs a particular closure after receiving a READY event.
This can fire multiple times depending on how many times the client
needs to disconnect and reconnect."
);
impl_callback!(
on_activity_send,
ActivityResponseData,
"Run a particular closure after ensuring that a [`PresenceClient::set_activity`] has successfully managed to pass its data through the IPC channel.
This can fire multiple times."
);
impl_callback!(
on_disconnect,
DisconnectReason,
"Runs a particular closure after the RPC connection is lost.
Unlike `on_retry`, this fires only when a previously initialized connection drops, which means that you should prefer
`on_retry` instead if you want more accurate responses to the initial connect or handshake failures, and `on_disconnect`
for reacting to post-connection failures.
This can fire multiple times depending on how many times the client disconnects and reconnects again."
);
impl_callback!(
on_retry,
usize,
"Runs a particular closure when retrying for socket creation or handshake.
This can fire multiple times, or for a limited amount depending on the amount of maximum
retries that has been set (through [`PresenceRunner::set_max_retries`]).
The closure parameter is the count of retries done at the time of its execution."
);
/// Enable verbose error logging over [`std::io::stderr`] writes for RPC and code events.
#[must_use]
pub fn show_errors(mut self) -> Self {
self.show_errors = true;
self
}
/// Sets the amount of maximum retries to do on socket creation and handshakes before the runner should give up.
///
/// By default this is set to `0` internally, which means the inner loop would retry indefinitely.
#[must_use]
pub fn set_max_retries(mut self, count: usize) -> Self {
self.max_retries = count;
self
}
/// Run the runner.
/// Must be called before any client handle operations.
pub async fn run(
&mut self,
wait_for_ready: bool,
) -> Result<&PresenceClient, PresenceRunnerError> {
if self.join_handle.is_some() {
return Err(PresenceRunnerError::MultipleRun);
}
let client_id = self.client.client_id.clone();
let show_errors = self.show_errors;
let max_retries = self.max_retries;
let mut rx = self
.rx
.take()
.ok_or_else(|| PresenceRunnerError::ReceiverError)?;
// oneshot channel to signal when READY is received the first time
let (ready_tx, ready_rx) = tokio::sync::oneshot::channel::<()>();
// executable closers (executed within the loop)
let on_ready = self.on_ready.take();
let on_activity_send = self.on_activity_send.take();
let on_disconnect = self.on_disconnect.take();
let on_retry = self.on_retry.take();
let join_handle = tokio::spawn(async move {
const RETRY_DELAY: Duration = Duration::from_secs(1);
let mut last_activity: Option<ActivitySpec> = None;
let mut ready_tx = Some(ready_tx);
let mut retries = 0;
let mut session_start: Option<u64> = None;
'outer: loop {
if max_retries != 0 && retries >= max_retries {
break;
}
// initial connect
let mut socket = match DiscordSock::new().await {
Ok(s) => s,
Err(_) => {
sleep(RETRY_DELAY).await;
retries += 1;
if let Some(f) = &on_retry {
let f = f.clone();
tokio::spawn(async move { f(retries) });
}
continue;
}
};
// initial handshake
if socket.do_handshake(&client_id).await.is_err() {
sleep(RETRY_DELAY).await;
retries += 1;
if let Some(f) = &on_retry {
let f = f.clone();
tokio::spawn(async move { f(retries) });
}
continue;
}
// ready loop
loop {
let frame = match socket.read_frame().await {
Ok(f) => f,
Err(_) => {
sleep(RETRY_DELAY).await;
retries += 1;
if let Some(f) = &on_retry {
let f = f.clone();
tokio::spawn(async move { f(retries) });
}
continue 'outer;
}
};
if Opcode::Frame != frame.opcode {
continue;
}
if let Ok(json) = serde_json::from_slice::<ReadyRPCFrame>(&frame.body) {
if json.cmd.as_deref() == Some("DISPATCH")
&& json.evt.as_deref() == Some("READY")
{
if let Some(tx) = ready_tx.take() {
let _ = tx.send(());
}
if let Some(f) = &on_ready {
if let Some(data) = json.data {
let f = f.clone();
tokio::spawn(async move { f(data) });
}
}
// reset retires here
retries = 0;
break;
}
if json.evt.as_deref() == Some("ERROR") && show_errors {
eprintln!("Discord RPC ready event receiver error: {:?}", json.data);
}
}
}
// restore last activity (if any)
if let Some(activity) = &last_activity {
if let Some(t) = session_start {
if let Err(e) = socket.send_activity(activity.clone(), t).await {
if show_errors {
eprintln!("Discord RPC last activity restore error: {e}")
}
}
}
}
// generic loop for receiving commands and responding to pings from Discord itself
let disconnect_reason = loop {
tokio::select! {
cmd = rx.recv() => {
match cmd {
Some(cmd) => {
match cmd {
IPCCommand::SetActivity { activity } => {
let session_start_unpacked = if let Some(s) = session_start {
s
} else {
match get_current_timestamp() {
Ok(t) => {
session_start = Some(t);
t
},
Err(e) => {
if show_errors {
eprintln!("Discord RPC pre-send_activity time parsing error: {e}")
}
break Some(DisconnectReason::OldRelicComputer(e.to_string()));
}
}
};
let activity = *activity;
last_activity = Some(activity.clone());
if let Err(e) = socket.send_activity(activity, session_start_unpacked).await {
if show_errors {
eprintln!("Discord RPC send_activity error: {e}");
}
break Some(DisconnectReason::SendActivityError(e.to_string()));
}
},
IPCCommand::ClearActivity => {
last_activity = None;
session_start = None;
if let Err(e) = socket.clear_activity().await {
if show_errors {
eprintln!("Discord RPC clear_activity error: {e}");
}
break Some(DisconnectReason::ClearActivityError(e.to_string()));
}
},
IPCCommand::Close { done_tx }=> {
let _ = socket.close().await;
let _ = done_tx.send(());
break 'outer;
}
}
},
None => break Some(DisconnectReason::ClientChannelClosed),
}
}
frame = socket.read_frame() => {
match frame {
Ok(frame) => {
match frame.opcode {
Opcode::Frame => {
if let Ok(json) = serde_json::from_slice::<DynamicRPCFrame>(&frame.body) {
if json.evt.as_deref() == Some("ERROR") && show_errors {
eprintln!("Discord RPC DynamicRPCFrame error: {:?}", json.data);
} else if json.cmd.as_deref() == Some("SET_ACTIVITY") {
if let Some(f) = &on_activity_send {
if let Some(data) = json.data {
let data = serde_json::from_value::<ActivityResponseData>(data);
if let Ok(d) = data {
let f = f.clone();
tokio::spawn(async move { f(d)});
} else if let Err(e) = data{
println!("{e}")
}
}
}
}
}
},
Opcode::Close => break Some(DisconnectReason::ServerClosed),
Opcode::Ping => {
if let Err(e) = socket.send_frame(Opcode::Pong, frame.body).await {
if show_errors {
eprintln!("Discord RPC send_frame error: {e}");
}
break Some(DisconnectReason::SendFrameError(e.to_string()));
}
},
_ => {}
}
},
Err(e) => {
if show_errors {
eprintln!("Discord RPC generic frame read error: {e}")
}
if let DiscordSockError::IoError(error) = &e {
if error.kind() == std::io::ErrorKind::UnexpectedEof {
break Some(DisconnectReason::PeerClosed);
}
}
break Some(DisconnectReason::ReadFrameError(e.to_string()));
},
}
}
}
};
if let Some(f) = &on_disconnect {
f(disconnect_reason.unwrap_or(DisconnectReason::Unknown));
}
sleep(RETRY_DELAY).await;
}
});
self.join_handle = Some(join_handle);
if wait_for_ready {
match ready_rx.await {
Ok(()) => (),
Err(_) => return Err(PresenceRunnerError::ExitBeforeReady),
}
}
Ok(&self.client)
}
/// Returns a clone of the client handle for sharing.
#[must_use]
pub fn clone_handle(&self) -> PresenceClient {
self.client.clone()
}
/// Waits for the IPC task to finish.
///
/// NOTE: If there's no `join_handle` present, the function will do nothing and
/// just return blank.
pub async fn wait(&mut self) -> Result<(), PresenceRunnerError> {
if let Some(handle) = self.join_handle.take() {
handle.await?;
}
Ok(())
}
}