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