1 /*
2  * Copyright (c) 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 #include "netconnection.h"
17 #include "netmanager_base_log.h"
18 
19 #include <mutex>
20 
21 namespace OHOS::NetManagerStandard {
22 std::map<NetConnCallbackObserver *, NetConnection *> NET_CONNECTIONS;
23 std::mutex g_netConnectionsMutex;
24 
NetConnection(EventManager * eventManager)25 NetConnection::NetConnection(EventManager *eventManager)
26     : hasNetSpecifier_(false),
27       hasTimeout_(false),
28       timeout_(0),
29       observer_(new NetConnCallbackObserver),
30       manager_(eventManager)
31 {
32 }
33 
MakeNetConnection(EventManager * eventManager)34 NetConnection *NetConnection::MakeNetConnection(EventManager *eventManager)
35 {
36     std::lock_guard<std::mutex> lock(g_netConnectionsMutex);
37     auto netConnection = new NetConnection(eventManager);
38     NET_CONNECTIONS[netConnection->observer_.GetRefPtr()] = netConnection;
39     return netConnection;
40 }
41 
DeleteNetConnection(NetConnection * netConnection)42 void NetConnection::DeleteNetConnection(NetConnection *netConnection)
43 {
44     std::lock_guard<std::mutex> lock(g_netConnectionsMutex);
45     NET_CONNECTIONS.erase(netConnection->observer_.GetRefPtr());
46     delete netConnection;
47 }
48 
GetObserver() const49 sptr<NetConnCallbackObserver> NetConnection::GetObserver() const
50 {
51     return observer_;
52 }
53 
GetEventManager() const54 EventManager *NetConnection::GetEventManager() const
55 {
56     return manager_;
57 }
NetConnection()58 NetConnection::NetConnection() {}
59 
60 } // namespace OHOS::NetManagerStandard
61