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 "session_adapter.h"
17 #include "telephony_log_wrapper.h"
18 #include "transmission_manager.h"
19
20 namespace OHOS {
21 namespace Telephony {
SessionAdapter(const std::shared_ptr<ISessionCallback> &callback)22 SessionAdapter::SessionAdapter(const std::shared_ptr<ISessionCallback> &callback) : callback_(callback)
23 {
24 listener_.OnBind = SessionAdapter::OnBind;
25 listener_.OnShutdown = SessionAdapter::OnShutdown;
26 listener_.OnBytes = SessionAdapter::OnBytes;
27 listener_.OnMessage = nullptr;
28 listener_.OnStream = nullptr;
29 listener_.OnFile = nullptr;
30 listener_.OnQos = nullptr;
31 listener_.OnError = SessionAdapter::OnError;
32 listener_.OnNegotiate = nullptr;
33 }
34
IsReady()35 bool SessionAdapter::IsReady()
36 {
37 std::lock_guard<ffrt::mutex> lock(mutex_);
38 return socket_ > INVALID_SOCKET_ID;
39 }
40
SendMsg(const void *data, uint32_t len)41 void SessionAdapter::SendMsg(const void *data, uint32_t len)
42 {
43 int32_t socket = INVALID_SOCKET_ID;
44 {
45 std::lock_guard<ffrt::mutex> lock(mutex_);
46 if (socket_ <= INVALID_SOCKET_ID) {
47 TELEPHONY_LOGE("send msg fail, invalid socket %{public}d", socket_);
48 return;
49 }
50 socket = socket_;
51 }
52 int32_t ret = SendBytes(socket, data, len);
53 if (ret != 0) {
54 TELEPHONY_LOGE("send socket %{public}d msg fail, result %{public}d", socket, ret);
55 }
56 }
57
OnReceiveMsg(int32_t socket, const char* data, uint32_t dataLen)58 void SessionAdapter::OnReceiveMsg(int32_t socket, const char* data, uint32_t dataLen)
59 {
60 {
61 std::lock_guard<ffrt::mutex> lock(mutex_);
62 if (socket != socket_) {
63 TELEPHONY_LOGW("socket %{public}d not match %{public}d", socket, socket_);
64 return;
65 }
66 }
67 if (callback_ != nullptr) {
68 callback_->OnReceiveMsg(data, dataLen);
69 }
70 }
71
OnBind(int32_t socket, PeerSocketInfo info)72 void SessionAdapter::OnBind(int32_t socket, PeerSocketInfo info)
73 {
74 TELEPHONY_LOGI("session %{public}d bind, data type %{public}d", socket, info.dataType);
75 auto transMgr = DelayedSingleton<TransmissionManager>::GetInstance();
76 if (transMgr != nullptr) {
77 transMgr->OnBind(socket);
78 }
79 }
80
OnShutdown(int32_t socket, ShutdownReason reason)81 void SessionAdapter::OnShutdown(int32_t socket, ShutdownReason reason)
82 {
83 TELEPHONY_LOGI("session %{public}d closed, reason %{public}d", socket, reason);
84 auto transMgr = DelayedSingleton<TransmissionManager>::GetInstance();
85 if (transMgr != nullptr) {
86 transMgr->OnShutdown(socket);
87 }
88 }
89
OnBytes(int32_t socket, const void *data, uint32_t dataLen)90 void SessionAdapter::OnBytes(int32_t socket, const void *data, uint32_t dataLen)
91 {
92 auto transMgr = DelayedSingleton<TransmissionManager>::GetInstance();
93 if (transMgr != nullptr) {
94 transMgr->OnReceiveMsg(socket, static_cast<const char*>(data), dataLen);
95 }
96 }
97
OnError(int32_t socket, int32_t errCode)98 void SessionAdapter::OnError(int32_t socket, int32_t errCode)
99 {
100 TELEPHONY_LOGE("async bind session %{public}d fail, reason %{public}d", socket, errCode);
101 }
102
103 } // namespace Telephony
104 } // namespace OHOS
105