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
use std::{
sync::{
Arc,
atomic::{AtomicBool, Ordering},
},
time::Duration,
};
use tokio::{sync::mpsc, task::JoinHandle, time::sleep};
use anyhow::{Result, anyhow, bail};
use crate::{
PresenceClient,
socket::DiscordSock,
types::{Activity, DynamicRPCFrame, IPCCommand, RPCFrame, ReadyData},
utils::get_current_timestamp,
};
const MULTIPLE_RUN_CALL_ERR: &str = "PresenceRunner::run() called more than once";
/// 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<Result<()>>>,
on_ready: Option<Box<dyn Fn(ReadyData) + Send + Sync + 'static>>,
do_verbose_errors: bool,
}
impl PresenceRunner {
#[must_use]
pub fn new(client_id: &str) -> Self {
let (tx, rx) = mpsc::channel(32);
let client = PresenceClient {
tx,
client_id: client_id.to_string(),
running: Arc::new(AtomicBool::new(false)),
};
Self {
rx: Some(rx),
client,
join_handle: None,
on_ready: None,
do_verbose_errors: false,
}
}
/// Run a particular closure after receiving the READY event from the Discord RPC server.
///
/// This event can fire multiple times depending on how many times the client needs to reconnect with Discord RPC.
pub fn on_ready<F: Fn(ReadyData) + Send + Sync + 'static>(mut self, f: F) -> Self {
self.on_ready = Some(Box::new(f));
self
}
/// Enable verbose error logging for RPC and code events.
pub fn show_errors(mut self) -> Self {
self.do_verbose_errors = true;
self
}
/// Run the runner.
/// Must be called before any client handle operations.
pub async fn run(&mut self, wait_for_ready: bool) -> Result<&PresenceClient> {
if self.client.running.swap(true, Ordering::SeqCst) {
bail!(MULTIPLE_RUN_CALL_ERR)
}
let client_id = self.client.client_id.clone();
let running = self.client.running.clone();
let do_verbose_errors = self.do_verbose_errors.clone();
let mut rx = self
.rx
.take()
.ok_or_else(|| anyhow!(MULTIPLE_RUN_CALL_ERR))?;
// oneshot channel to signal when READY is received the first time
let (ready_tx, ready_rx) = tokio::sync::oneshot::channel::<()>();
let on_ready = self.on_ready.take();
let join_handle = tokio::spawn(async move {
let mut backoff = 1;
let mut last_activity: Option<Activity> = None;
let mut ready_tx = Some(ready_tx);
let mut session_start: Option<u64> = None;
'outer: while running.load(Ordering::SeqCst) {
// initial connect
let mut socket = match DiscordSock::new().await {
Ok(s) => s,
Err(_) => {
sleep(Duration::from_secs(backoff)).await;
continue;
}
};
// initial handshake
if socket.do_handshake(&client_id).await.is_err() {
sleep(Duration::from_secs(backoff)).await;
continue;
}
loop {
let frame = match socket.read_frame().await {
Ok(f) => f,
Err(_) => {
break;
}
};
if frame.opcode != 1 {
continue;
}
if let Ok(json) = serde_json::from_slice::<RPCFrame>(&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 {
f(data);
}
}
break;
}
if json.evt.as_deref() == Some("ERROR") && do_verbose_errors {
eprintln!("Discord RPC ready event receiver error: {:?}", json.data);
}
}
}
if let Some(activity) = &last_activity {
if let Some(t) = session_start {
if let Err(e) = socket.send_activity(activity.clone(), t).await {
eprintln!("Discord RPC last activity restore error: {e}")
}
}
}
backoff = 1;
loop {
tokio::select! {
biased;
cmd = rx.recv() => {
match cmd {
Some(cmd) => {
match cmd {
IPCCommand::SetActivity { activity } => {
let session_start_unpacked = if let Some(s) = session_start {
s
} else {
get_current_timestamp()?
};
let activity = *activity;
last_activity = Some(activity.clone());
if let Err(e) = socket.send_activity(activity, session_start_unpacked).await {
if do_verbose_errors {
eprintln!("Discord RPC send_activity error: {e}");
}
break;
}
},
IPCCommand::ClearActivity => {
last_activity = None;
session_start = None;
if let Err(e) = socket.clear_activity().await {
if do_verbose_errors {
eprintln!("Discord RPC clear_activity error: {e}");
}
break;
}
},
IPCCommand::Close => {
let _ = socket.close().await;
running.store(false, Ordering::SeqCst);
break 'outer;
}
}
},
None => break,
}
}
frame = socket.read_frame() => {
match frame {
Ok(frame) => {
match frame.opcode {
1 => {
if let Ok(json) = serde_json::from_slice::<DynamicRPCFrame>(&frame.body) {
if json.evt.as_deref() == Some("ERROR") && do_verbose_errors {
eprintln!("Discord RPC DynamicRPCFrame error: {:?}", json.data);
}
}
}
2 => break,
3
if let Err(e) = socket.send_frame(3, frame.body).await => {
if do_verbose_errors {
eprintln!("Discord RPC send_frame error: {e}");
}
break;
}
_ => {}
}
},
Err(e) => {
if do_verbose_errors {
eprintln!("Discord RPC generic frame read error: {e}")
}
break;
},
}
}
}
}
sleep(Duration::from_secs(backoff)).await;
backoff = (backoff * 2).min(4);
}
Ok(())
});
self.join_handle = Some(join_handle);
if wait_for_ready {
match ready_rx.await {
Ok(()) => (),
Err(_) => bail!("Background task exited before READY."),
}
}
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.
pub async fn wait(&mut self) -> Result<()> {
if let Some(handle) = self.join_handle.take() {
handle.await??;
}
Ok(())
}
}