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,const int32_t & qosMinBw)26 void ServerSession::Create(const std::string &localName, const int32_t &qosMinBw)
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 = qosMinBw },
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 int32_t socket = INVALID_SOCKET_ID;
70 {
71 std::lock_guard<ffrt::mutex> lock(mutex_);
72 if (serverSocket_ > INVALID_SOCKET_ID) {
73 socket = serverSocket_;
74 }
75 serverSocket_ = INVALID_SOCKET_ID;
76 socket_ = INVALID_SOCKET_ID;
77 TELEPHONY_LOGI("disconnect server session");
78 }
79 if (socket > INVALID_SOCKET_ID) {
80 Shutdown(socket);
81 TELEPHONY_LOGI("close server socket %{public}d success", socket);
82 }
83 }
84
OnSessionBind(int32_t socket)85 void ServerSession::OnSessionBind(int32_t socket)
86 {
87 {
88 std::lock_guard<ffrt::mutex> lock(mutex_);
89 if (socket_ > INVALID_SOCKET_ID) {
90 TELEPHONY_LOGW("replace old socket %{public}d", socket_);
91 }
92 socket_ = socket;
93 }
94 TELEPHONY_LOGI("session %{public}d bind success", socket);
95 if (callback_ != nullptr) {
96 callback_->OnConnected();
97 }
98 }
99
OnSessionShutdown(int32_t socket)100 void ServerSession::OnSessionShutdown(int32_t socket)
101 {
102 std::lock_guard<ffrt::mutex> lock(mutex_);
103 if (socket == serverSocket_) {
104 serverSocket_ = INVALID_SOCKET_ID;
105 socket_ = INVALID_SOCKET_ID;
106 }
107 if (socket == socket_) {
108 socket_ = INVALID_SOCKET_ID;
109 }
110 }
111
112 } // namespace Telephony
113 } // namespace OHOS
114