Hermes 0.7.0
Linux game streaming host
Loading...
Searching...
No Matches
session_activity_counter.h
Go to the documentation of this file.
1
5#pragma once
6
7#include <cassert>
8#include <cstddef>
9#include <mutex>
10#include <utility>
11
12namespace stream {
13
14 // Calls the platform hooks only at 0->1 and 1->0. Holding this mutex across
15 // the hook prevents a concurrent start from racing the last disconnect.
16 // Platform hooks must therefore be non-blocking.
18 public:
19 template<typename Callback>
20 void started(Callback &&on_first) {
21 std::lock_guard lock {mutex_};
22 if (++count_ == 1) {
23 std::forward<Callback>(on_first)();
24 }
25 }
26
27 template<typename Callback>
28 void stopped(Callback &&on_last) {
29 std::lock_guard lock {mutex_};
30 assert(count_ > 0 && "Unbalanced session activity stop");
31 if (count_ == 0) {
32 return;
33 }
34 if (--count_ == 0) {
35 std::forward<Callback>(on_last)();
36 }
37 }
38
39 std::size_t count() const {
40 std::lock_guard lock {mutex_};
41 return count_;
42 }
43
44 private:
45 mutable std::mutex mutex_;
46 std::size_t count_ {0};
47 };
48
49} // namespace stream