• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 "client_session.h"
17 #include "telephony_log_wrapper.h"
18 
19 namespace OHOS {
20 namespace Telephony {
~ClientSession()21 ClientSession::~ClientSession()
22 {
23     Disconnect();
24 }
25 
Connect(const std::string & peerDevId,const std::string & localName,const std::string & peerName)26 void ClientSession::Connect(const std::string &peerDevId, const std::string &localName, const std::string &peerName)
27 {
28     {
29         std::lock_guard<ffrt::mutex> lock(mutex_);
30         if (clientSocket_ > INVALID_SOCKET_ID) {
31             TELEPHONY_LOGI("client socket %{public}d already connect", clientSocket_);
32             return;
33         }
34     }
35     int32_t socket = CreateSocket(peerDevId, localName, peerName);
36     if (socket <= INVALID_SOCKET_ID) {
37         return;
38     }
39     QosTV qos[] = {
40         { .qos = QOS_TYPE_MIN_BW, .value = QOS_MIN_BW },
41         { .qos = QOS_TYPE_MAX_LATENCY, .value = QOS_MAX_LATENCY }
42     };
43     int32_t ret = BindAsync(socket, qos, sizeof(qos) / sizeof(qos[0]), &listener_);
44     TELEPHONY_LOGI("async bind socket %{public}d result %{public}d", socket, ret);
45 
46     std::lock_guard<ffrt::mutex> lock(mutex_);
47     clientSocket_ = socket;
48 }
49 
Disconnect()50 void ClientSession::Disconnect()
51 {
52     int32_t socket = INVALID_SOCKET_ID;
53     {
54         std::lock_guard<ffrt::mutex> lock(mutex_);
55         if (clientSocket_ > INVALID_SOCKET_ID) {
56             socket = clientSocket_;
57         }
58         clientSocket_ = INVALID_SOCKET_ID;
59         socket_ = INVALID_SOCKET_ID;
60         TELEPHONY_LOGI("disconnect client session");
61     }
62     if (socket > INVALID_SOCKET_ID) {
63         Shutdown(socket);
64         TELEPHONY_LOGI("close client socket %{public}d success", socket);
65     }
66 }
67 
OnSessionBind(int32_t socket)68 void ClientSession::OnSessionBind(int32_t socket)
69 {
70     {
71         std::lock_guard<ffrt::mutex> lock(mutex_);
72         socket_ = socket;
73     }
74     TELEPHONY_LOGI("session %{public}d bind success", socket);
75     if (callback_ != nullptr) {
76         callback_->OnConnected();
77     }
78 }
79 
OnSessionShutdown(int32_t socket)80 void ClientSession::OnSessionShutdown(int32_t socket)
81 {
82     std::lock_guard<ffrt::mutex> lock(mutex_);
83     if (socket == socket_) {
84         socket_ = INVALID_SOCKET_ID;
85     }
86     if (socket == clientSocket_) {
87         clientSocket_ = INVALID_SOCKET_ID;
88         socket_ = INVALID_SOCKET_ID;
89     }
90 }
91 
CreateSocket(const std::string & peerDevId,const std::string & localName,const std::string & peerName)92 int32_t ClientSession::CreateSocket(const std::string &peerDevId, const std::string &localName,
93                                     const std::string &peerName)
94 {
95     if (peerDevId.empty()) {
96         TELEPHONY_LOGE("create socket fail, empty peer dev");
97         return INVALID_SOCKET_ID;
98     }
99     SocketInfo socketInfo = {
100         .name = const_cast<char*>((localName.c_str())),
101         .peerName = const_cast<char*>(peerName.c_str()),
102         .peerNetworkId = const_cast<char*>(peerDevId.c_str()),
103         .pkgName = const_cast<char*>(PACKET_NAME),
104         .dataType = DATA_TYPE_BYTES
105     };
106     int32_t socket = Socket(socketInfo);
107     if (socket <= INVALID_SOCKET_ID) {
108         TELEPHONY_LOGE("create client socket fail %{public}d", socket);
109         return socket;
110     }
111     return socket;
112 }
113 
114 } // namespace Telephony
115 } // namespace OHOS
116