• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef NET_MONITOR_H
17 #define NET_MONITOR_H
18 
19 #include <atomic>
20 #include <condition_variable>
21 #include <memory>
22 #include <mutex>
23 
24 #include "refbase.h"
25 
26 #include "i_net_monitor_callback.h"
27 #include "net_conn_types.h"
28 #include "net_http_probe.h"
29 #include "net_link_info.h"
30 
31 namespace OHOS {
32 namespace NetManagerStandard {
33 class NetMonitor : public virtual RefBase, public std::enable_shared_from_this<NetMonitor> {
34 public:
35     /**
36      * Construct a new NetMonitor to detection a network
37      *
38      * @param netId Detection network's id
39      * @param bearType bearType network type
40      * @param netLinkInfo Network link information
41      * @param callback Network monitor callback weak reference
42      */
43     NetMonitor(uint32_t netId, NetBearType bearType, const NetLinkInfo &netLinkInfo,
44                const std::weak_ptr<INetMonitorCallback> &callback);
45 
46     /**
47      * Destroy the NetMonitor
48      *
49      */
50     virtual ~NetMonitor() = default;
51 
52     /**
53      * Start detection
54      *
55      */
56     void Start();
57 
58     /**
59      * Stop detecting
60      *
61      */
62     void Stop();
63 
64     /**
65      * Set network socket parameter
66      *
67      * @return Socket parameter setting result
68      */
69     int32_t SetSocketParameter(int32_t sockFd);
70 
71     /**
72      * Is network monitor detecting
73      *
74      * @return Status value of whether the network is detecting
75      */
76     bool IsDetecting();
77 
78     /**
79      * Network monitor detection
80      *
81      */
82     void Detection();
83 
84     /**
85      * Update global http proxy
86      *
87      */
88     void UpdateGlobalHttpProxy(const HttpProxy &httpProxy);
89 
90 private:
91     NetHttpProbeResult SendHttpProbe(ProbeType probeType);
92     void GetHttpProbeUrlFromConfig(std::string &httpUrl, std::string &httpsUrl);
93     void LoadGlobalHttpProxy();
94 
95 private:
96     uint32_t netId_ = 0;
97     std::atomic<bool> isDetecting_ = false;
98     int32_t detectionSteps_ = 0;
99     std::mutex detectionMtx_;
100     std::mutex probeMtx_;
101     std::condition_variable detectionCond_;
102     uint32_t detectionDelay_ = 0;
103     std::weak_ptr<INetMonitorCallback> netMonitorCallback_;
104     std::unique_ptr<NetHttpProbe> httpProbe_;
105 };
106 } // namespace NetManagerStandard
107 } // namespace OHOS
108 #endif // NET_MONITOR_H
109