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 "net_conn_client.h"
18
19 #include <mutex>
20
21 namespace OHOS::NetManagerStandard {
22 std::map<sptr<NetConnCallbackObserver>, NetConnection *> NET_CONNECTIONS;
23 std::mutex NET_CONNECTIONS_MUTEX;
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(NET_CONNECTIONS_MUTEX);
37 NETMANAGER_BASE_LOGE("MakeNetConnection eventManager addr=%{public}p", eventManager);
38 auto netConnection = new NetConnection(eventManager);
39 NET_CONNECTIONS[netConnection->observer_] = netConnection;
40 return netConnection;
41 }
42
DeleteNetConnection(NetConnection * netConnection)43 void NetConnection::DeleteNetConnection(NetConnection *netConnection)
44 {
45 std::lock_guard<std::mutex> lock(NET_CONNECTIONS_MUTEX);
46 NETMANAGER_BASE_LOGE("DeleteNetConnection netConnection addr=%{public}p", netConnection);
47 DelayedSingleton<NetConnClient>::GetInstance()->UnregisterNetConnCallback(netConnection->observer_);
48 NET_CONNECTIONS.erase(netConnection->observer_);
49 delete netConnection;
50 }
51
GetObserver() const52 sptr<NetConnCallbackObserver> NetConnection::GetObserver() const
53 {
54 return observer_;
55 }
56
GetEventManager() const57 EventManager *NetConnection::GetEventManager() const
58 {
59 return manager_;
60 }
61 } // namespace OHOS::NetManagerStandard
62