steamkit-rs 0.1.2

Rust port of SteamKit2, a .NET library that aims to interoperate with the Steam network.
Documentation
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
//! Tracks pending requests and completes them when their response arrives.
//!
//! The awaitable job types live in [`crate::types::job_id`]; this module owns
//! the registry that completes, heartbeats, times out and cancels in-flight
//! jobs, and re-exports the job types.
//!
//! A posted callback goes both to the callback queue and to the job waiting for
//! it. A `Box` can't have two owners, so the manager gives the job a clone of
//! the concrete callback and the queue keeps the original.

use std::any::TypeId;
use std::fmt;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};

use dashmap::DashMap;
use parking_lot::Mutex;
use tokio::sync::oneshot;
use tokio::task::JoinHandle;

use crate::error::SteamError;
use crate::types::job_id::JobID;

use super::callback_mgr::callback_msg::CallbackMsg;

pub use crate::types::job_id::{
    AsyncJob, AsyncJobMultiple, JobHandle, JobReceiver, MultiJobReceiver, ResultSet,
    TypedJobReceiver,
};

/// How long a job waits for its response by default.
pub const DEFAULT_JOB_TIMEOUT: Duration = Duration::from_secs(10);

/// How much a heartbeat from Steam extends a job's timeout.
pub const JOB_HEARTBEAT_EXTENSION: Duration = Duration::from_secs(10);

/// How often the manager checks for timed out jobs.
pub const JOB_TIMEOUT_CHECK_INTERVAL: Duration = Duration::from_secs(1);

/// The awaiting side of a multi-result job.
pub type MultiJobHandle<T> = AsyncJobMultiple<T>;

/// The set of callback results of a multi-result job.
pub type JobResultSet<T> = ResultSet<T>;

/// The outcome a multi-result job delivers when it completes.
enum MultiOutcome {
    /// The job completed with the accumulated results.
    Results {
        /// The accumulated callbacks.
        results: Vec<Box<dyn CallbackMsg>>,
        /// Whether the finish condition of the job was met.
        complete: bool,
        /// Whether the job failed because Steam reported a remote failure.
        failed: bool,
    },
    /// The job failed.
    Error(SteamError),
}

/// The completion channel of a multi-result job.
type MultiCompleter = Box<dyn FnOnce(MultiOutcome) + Send + Sync>;

/// The completion channel of a tracked job.
enum Completer {
    /// A job that completes with a single callback.
    Single(Mutex<Option<oneshot::Sender<Result<Box<dyn CallbackMsg>, SteamError>>>>),
    /// A job that completes with a set of callbacks.
    Multiple(Mutex<Option<MultiCompleter>>),
}

impl Completer {
    /// Delivers the callback of a single-result job.
    fn complete_single(&self, callback: Box<dyn CallbackMsg>) {
        let Self::Single(sender) = self else {
            return;
        };

        if let Some(sender) = sender.lock().take() {
            let _ = sender.send(Ok(callback));
        }
    }

    /// Delivers the results of a multi-result job.
    fn complete_multiple(&self, results: Vec<Box<dyn CallbackMsg>>, complete: bool, failed: bool) {
        let Self::Multiple(completer) = self else {
            return;
        };

        if let Some(completer) = completer.lock().take() {
            completer(MultiOutcome::Results {
                results,
                complete,
                failed,
            });
        }
    }

    /// Fails the job with the given error.
    fn fail(&self, error: SteamError) {
        match self {
            Self::Single(sender) => {
                if let Some(sender) = sender.lock().take() {
                    let _ = sender.send(Err(error));
                }
            }
            Self::Multiple(completer) => {
                if let Some(completer) = completer.lock().take() {
                    completer(MultiOutcome::Error(error));
                }
            }
        }
    }
}

/// The state of a single tracked job.
struct JobEntry {
    type_id: TypeId,
    clone_callback: fn(&dyn CallbackMsg) -> Option<Box<dyn CallbackMsg>>,
    completer: Completer,
    timeout: Mutex<Duration>,
    started: Instant,
    finish_condition: Mutex<Option<Arc<dyn Fn(&dyn CallbackMsg) -> bool + Send + Sync>>>,
    results: Mutex<Vec<Box<dyn CallbackMsg>>>,
}

impl JobEntry {
    fn new(
        type_id: TypeId,
        clone_callback: fn(&dyn CallbackMsg) -> Option<Box<dyn CallbackMsg>>,
        completer: Completer,
    ) -> Self {
        Self {
            type_id,
            clone_callback,
            completer,
            timeout: Mutex::new(DEFAULT_JOB_TIMEOUT),
            started: Instant::now(),
            finish_condition: Mutex::new(None),
            results: Mutex::new(Vec::new()),
        }
    }

    fn heartbeat(&self) {
        let mut timeout = self.timeout.lock();
        *timeout += JOB_HEARTBEAT_EXTENSION;
    }

    fn is_timedout(&self) -> bool {
        self.started.elapsed() >= *self.timeout.lock()
    }

    fn timeout(&self) -> Duration {
        *self.timeout.lock()
    }
}

impl fmt::Debug for JobEntry {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("JobEntry")
            .field("type_id", &self.type_id)
            .field("timeout", &self.timeout())
            .field("elapsed", &self.started.elapsed())
            .finish()
    }
}

/// The state shared by an [`AsyncJobManager`] and its timeout task.
struct AsyncJobManagerInner {
    jobs: DashMap<JobID, Arc<JobEntry>>,
}

impl fmt::Debug for AsyncJobManagerInner {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("AsyncJobManagerInner")
            .field("jobs", &self.jobs.len())
            .finish()
    }
}

/// Tracks the jobs waiting for a response from Steam. Handlers use it to await
/// their requests.
pub struct AsyncJobManager {
    inner: Arc<AsyncJobManagerInner>,
    timeouts_enabled: AtomicBool,
    timeout_task: Mutex<Option<JoinHandle<()>>>,
}

impl Default for AsyncJobManager {
    fn default() -> Self {
        Self::new()
    }
}

impl fmt::Debug for AsyncJobManager {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("AsyncJobManager")
            .field("inner", &self.inner)
            .field("timeouts_enabled", &self.timeouts_enabled.load(Ordering::SeqCst))
            .finish()
    }
}

impl AsyncJobManager {
    /// Creates an empty job manager.
    pub fn new() -> Self {
        Self {
            inner: Arc::new(AsyncJobManagerInner {
                jobs: DashMap::new(),
            }),
            timeouts_enabled: AtomicBool::new(false),
            timeout_task: Mutex::new(None),
        }
    }

    /// The number of jobs that are currently tracked.
    pub fn tracked_job_count(&self) -> usize {
        self.inner.jobs.len()
    }

    /// Tracks a job that completes with a single callback.
    pub fn start_job<T>(&self, job_id: JobID) -> AsyncJob<T>
    where
        T: CallbackMsg + Clone + 'static,
    {
        let mut job = AsyncJob::<T>::new(job_id);
        let sender = job.take_sender();

        let entry = Arc::new(JobEntry::new(
            TypeId::of::<T>(),
            clone_callback_of::<T>,
            Completer::Single(Mutex::new(sender)),
        ));

        self.inner.jobs.insert(job_id, entry);

        job
    }

    /// Tracks a job that completes once `finish_condition` returns `true`.
    pub fn start_job_multiple<T>(
        &self,
        job_id: JobID,
        finish_condition: impl FnMut(&T) -> bool + Send + Sync + 'static,
    ) -> MultiJobHandle<T>
    where
        T: CallbackMsg + Clone + 'static,
    {
        let finish_condition = Arc::new(Mutex::new(finish_condition));

        let manager_condition = {
            let finish_condition = Arc::clone(&finish_condition);

            Arc::new(move |callback: &dyn CallbackMsg| {
                match callback.as_any().downcast_ref::<T>() {
                    Some(typed) => {
                        let mut guard = finish_condition.lock();
                        let condition = &mut *guard;
                        condition(typed)
                    }
                    None => false,
                }
            })
        };

        let job_condition = {
            let finish_condition = Arc::clone(&finish_condition);

            move |typed: &T| {
                let mut guard = finish_condition.lock();
                let condition = &mut *guard;
                condition(typed)
            }
        };

        let mut job = AsyncJobMultiple::<T>::new(job_id, job_condition);
        let sender = job.take_sender();

        let completer: MultiCompleter = Box::new(move |outcome| {
            let Some(sender) = sender else {
                return;
            };

            let result = match outcome {
                MultiOutcome::Results {
                    results,
                    complete,
                    failed,
                } => {
                    let typed: Vec<T> =
                        results.into_iter().filter_map(downcast_box::<T>).collect();

                    Ok(ResultSet {
                        complete,
                        failed,
                        results: Some(typed),
                    })
                }
                MultiOutcome::Error(error) => Err(error),
            };

            let _ = sender.send(result);
        });

        let entry = Arc::new(JobEntry::new(
            TypeId::of::<T>(),
            clone_callback_of::<T>,
            Completer::Multiple(Mutex::new(Some(completer))),
        ));

        *entry.finish_condition.lock() = Some(manager_condition);

        self.inner.jobs.insert(job_id, entry);

        job
    }

    /// Hands a callback to the job waiting for it. Ignored if no job with that
    /// id is tracked.
    pub fn try_complete_job(&self, job_id: JobID, callback: &dyn CallbackMsg) {
        let Some(entry) = self.inner.jobs.get(&job_id).map(|tracked| Arc::clone(tracked.value())) else {
            return;
        };

        let finish_condition = entry.finish_condition.lock().clone();

        match finish_condition {
            None => {
                if entry.type_id != callback.as_any().type_id() {
                    entry.completer.fail(SteamError::Malformed(format!(
                        "callback type {:?} does not match the expected type of job {}",
                        callback.as_any().type_id(),
                        job_id.value()
                    )));
                } else if let Some(copy) = (entry.clone_callback)(callback) {
                    entry.completer.complete_single(copy);
                }

                self.inner.jobs.remove(&job_id);
            }
            Some(condition) => {
                let finished = condition(callback);

                if let Some(copy) = (entry.clone_callback)(callback) {
                    entry.results.lock().push(copy);
                }

                if finished {
                    let results = std::mem::take(&mut *entry.results.lock());
                    entry.completer.complete_multiple(results, true, false);
                    self.inner.jobs.remove(&job_id);
                } else {
                    entry.heartbeat();
                }
            }
        }
    }

    /// Extends a job's timeout (Steam sent a heartbeat for it).
    pub fn heartbeat_job(&self, job_id: JobID) {
        if let Some(entry) = self.inner.jobs.get(&job_id) {
            entry.heartbeat();
        }
    }

    /// Fails a job because Steam reported an error for it.
    pub fn fail_job(&self, job_id: JobID) {
        let Some((_, entry)) = self.inner.jobs.remove(&job_id) else {
            return;
        };

        let results = std::mem::take(&mut *entry.results.lock());

        if results.is_empty() {
            entry.completer.fail(SteamError::JobFailed);
        } else {
            entry.completer.complete_multiple(results, false, true);
        }
    }

    /// Cancels and drops every tracked job.
    pub fn cancel_pending_jobs(&self) {
        let entries: Vec<Arc<JobEntry>> = self
            .inner
            .jobs
            .iter()
            .map(|tracked| Arc::clone(tracked.value()))
            .collect();
        self.inner.jobs.clear();

        for entry in entries {
            let results = std::mem::take(&mut *entry.results.lock());

            if results.is_empty() {
                entry.completer.fail(SteamError::Cancelled);
            } else {
                entry.completer.complete_multiple(results, false, false);
            }
        }
    }

    /// Turns the periodic timeout check on or off.
    pub fn set_timeouts_enabled(&self, enable: bool) {
        self.timeouts_enabled.store(enable, Ordering::SeqCst);

        if enable {
            self.start_timeout_task();
        } else {
            self.stop_timeout_task();
        }
    }

    /// Whether the periodic timeout check is running.
    pub fn timeouts_enabled(&self) -> bool {
        self.timeouts_enabled.load(Ordering::SeqCst)
    }

    /// Cancels and drops every job that has timed out.
    pub fn cancel_timed_out_jobs(&self) {
        cancel_timed_out_jobs(&self.inner);
    }

    fn start_timeout_task(&self) {
        let mut task = self.timeout_task.lock();
        if task.as_ref().is_some_and(|handle| !handle.is_finished()) {
            return;
        }

        let Ok(handle) = tokio::runtime::Handle::try_current() else {
            return;
        };

        let inner = Arc::downgrade(&self.inner);

        *task = Some(handle.spawn(async move {
            let mut interval = tokio::time::interval(JOB_TIMEOUT_CHECK_INTERVAL);
            interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);

            loop {
                interval.tick().await;

                let Some(inner) = inner.upgrade() else {
                    break;
                };

                cancel_timed_out_jobs(&inner);
            }
        }));
    }

    fn stop_timeout_task(&self) {
        let mut task = self.timeout_task.lock();
        if let Some(handle) = task.take() {
            handle.abort();
        }
    }
}

/// Cancels every job of the given manager that has timed out.
fn cancel_timed_out_jobs(inner: &AsyncJobManagerInner) {
    let timed_out: Vec<JobID> = inner
        .jobs
        .iter()
        .filter(|tracked| tracked.value().is_timedout())
        .map(|tracked| *tracked.key())
        .collect();

    for job_id in timed_out {
        let Some((_, entry)) = inner.jobs.remove(&job_id) else {
            continue;
        };

        let results = std::mem::take(&mut *entry.results.lock());

        if results.is_empty() {
            entry.completer.fail(SteamError::Timeout(entry.timeout()));
        } else {
            entry.completer.complete_multiple(results, false, false);
        }
    }
}

/// Clones the concrete callback so the job and the callback queue can each
/// own one.
fn clone_callback_of<T: CallbackMsg + Clone + 'static>(
    callback: &dyn CallbackMsg,
) -> Option<Box<dyn CallbackMsg>> {
    callback
        .as_any()
        .downcast_ref::<T>()
        .map(|typed| Box::new(typed.clone()) as Box<dyn CallbackMsg>)
}

/// Downcasts a callback message to the concrete callback type.
fn downcast_box<T: CallbackMsg + 'static>(callback: Box<dyn CallbackMsg>) -> Option<T> {
    // `CallbackMsg: Any`, so upcast to `dyn Any` and downcast from there.
    let any: Box<dyn std::any::Any> = callback;
    any.downcast::<T>().ok().map(|typed| *typed)
}