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 "server_session.h"
17 #include "telephony_log_wrapper.h"
18 
19 namespace OHOS {
20 namespace Telephony {
~ServerSession()21 ServerSession::~ServerSession()
22 {
23     Destroy();
24 }
25 
Create(const std::string &localName)26 void ServerSession::Create(const std::string &localName)
27 {
28     if (localName.empty()) {
29         TELEPHONY_LOGE("create server socket fail, empty local name");
30         return;
31     }
32     {
33         std::lock_guard<ffrt::mutex> lock(mutex_);
34         if (serverSocket_ > INVALID_SOCKET_ID) {
35             TELEPHONY_LOGI("server socket %{public}d already exist", serverSocket_);
36             return;
37         }
38     }
39 
40     SocketInfo socketInfo = {
41         .name = const_cast<char*>((localName.c_str())),
42         .peerName = nullptr,
43         .peerNetworkId = nullptr,
44         .pkgName = const_cast<char*>(PACKET_NAME),
45         .dataType = DATA_TYPE_BYTES
46     };
47     int32_t socket = Socket(socketInfo);
48     if (socket <= INVALID_SOCKET_ID) {
49         TELEPHONY_LOGE("create server socket fail %{public}d", socket);
50         return;
51     }
52 
53     QosTV qos[] = {
54         { .qos = QOS_TYPE_MIN_BW, .value = QOS_MIN_BW },
55         { .qos = QOS_TYPE_MAX_LATENCY, .value = QOS_MAX_LATENCY }
56     };
57     int32_t ret = Listen(socket, qos, sizeof(qos) / sizeof(qos[0]), &listener_);
58     if (ret != 0) {
59         TELEPHONY_LOGE("start listen socket %{public}d fail, result %{public}d", socket, ret);
60         return;
61     }
62     TELEPHONY_LOGI("create server socket %{public}d success", socket);
63     std::lock_guard<ffrt::mutex> lockAdd(mutex_);
64     serverSocket_ = socket;
65 }
66 
Destroy()67 void ServerSession::Destroy()
68 {
69     std::lock_guard<ffrt::mutex> lock(mutex_);
70     if (serverSocket_ > INVALID_SOCKET_ID) {
71         Shutdown(serverSocket_);
72         TELEPHONY_LOGI("close server socket %{public}d success", serverSocket_);
73     }
74     serverSocket_ = INVALID_SOCKET_ID;
75     socket_ = INVALID_SOCKET_ID;
76 }
77 
OnSessionBind(int32_t socket)78 void ServerSession::OnSessionBind(int32_t socket)
79 {
80     {
81         std::lock_guard<ffrt::mutex> lock(mutex_);
82         if (socket_ > INVALID_SOCKET_ID) {
83             TELEPHONY_LOGW("replace old socket %{public}d", socket_);
84         }
85         socket_ = socket;
86     }
87     TELEPHONY_LOGI("session %{public}d bind success", socket);
88     if (callback_ != nullptr) {
89         callback_->OnConnected();
90     }
91 }
92 
OnSessionShutdown(int32_t socket)93 void ServerSession::OnSessionShutdown(int32_t socket)
94 {
95     std::lock_guard<ffrt::mutex> lock(mutex_);
96     if (socket == serverSocket_) {
97         serverSocket_ = INVALID_SOCKET_ID;
98         socket_ = INVALID_SOCKET_ID;
99     }
100     if (socket == socket_) {
101         socket_ = INVALID_SOCKET_ID;
102     }
103 }
104 
105 } // namespace Telephony
106 } // namespace OHOS
107