Branch data Line data Source code
1 : : /*
2 : : * Copyright 2025 Jinwoo Sung
3 : : *
4 : : * Licensed under the Apache License, Version 2.0 (the "License");
5 : : * you may not use this file except in compliance with the License.
6 : : * You may obtain a copy of the License at
7 : : *
8 : : * http://www.apache.org/licenses/LICENSE-2.0
9 : : *
10 : : * Unless required by applicable law or agreed to in writing, software
11 : : * distributed under the License is distributed on an "AS IS" BASIS,
12 : : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 : : * See the License for the specific language governing permissions and
14 : : * limitations under the License.
15 : : */
16 : :
17 : : #pragma once
18 : : #include <mutex>
19 : :
20 : : #include "wirestead/base/common.hpp"
21 : :
22 : : namespace wirestead::wrapper::detail {
23 : : // Independent of the send-admission lock. A retry is not a terminal error, and a
24 : : // terminal failure before any connection must not invent a disconnect.
25 : : struct LifecycleEvents {
26 : : std::mutex mutex;
27 : : bool connected = false;
28 : : bool terminal_error = false;
29 : : struct Notification {
30 : : bool disconnect;
31 : : bool error;
32 : : };
33 : 1759 : void reset() {
34 : 1759 : std::lock_guard<std::mutex> lock(mutex);
35 : 1759 : connected = false;
36 : 1759 : terminal_error = false;
37 : 1759 : }
38 : 2074 : Notification observe(base::LinkState state, bool ready) {
39 : 2074 : std::lock_guard<std::mutex> lock(mutex);
40 [ + + + + ]: 2074 : const bool lost = connected && !ready;
41 : 2074 : connected = ready;
42 [ + + ]: 2074 : if (ready) terminal_error = false;
43 [ + + + + ]: 2074 : const bool error = state == base::LinkState::Error && !terminal_error;
44 [ + + ]: 2074 : if (error) terminal_error = true;
45 : 2074 : return {lost, error};
46 : 2074 : }
47 : : };
48 : : } // namespace wirestead::wrapper::detail
|