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 TELEPHONY_LOGI("send socket %{public}d msg len %{public}u, ret %{public}d", socket, len, ret);
54 }
55
OnReceiveMsg(int32_t socket,const char * data,uint32_t dataLen)56 void SessionAdapter::OnReceiveMsg(int32_t socket, const char* data, uint32_t dataLen)
57 {
58 {
59 std::lock_guard<ffrt::mutex> lock(mutex_);
60 if (socket != socket_) {
61 TELEPHONY_LOGW("socket %{public}d not match %{public}d", socket, socket_);
62 return;
63 }
64 }
65 if (callback_ != nullptr) {
66 callback_->OnReceiveMsg(data, dataLen);
67 }
68 }
69
OnBind(int32_t socket,PeerSocketInfo info)70 void SessionAdapter::OnBind(int32_t socket, PeerSocketInfo info)
71 {
72 TELEPHONY_LOGI("session %{public}d bind, data type %{public}d", socket, info.dataType);
73 auto transMgr = DelayedSingleton<TransmissionManager>::GetInstance();
74 if (transMgr != nullptr) {
75 transMgr->OnBind(socket);
76 }
77 }
78
OnShutdown(int32_t socket,ShutdownReason reason)79 void SessionAdapter::OnShutdown(int32_t socket, ShutdownReason reason)
80 {
81 TELEPHONY_LOGI("session %{public}d closed, reason %{public}d", socket, reason);
82 auto transMgr = DelayedSingleton<TransmissionManager>::GetInstance();
83 if (transMgr != nullptr) {
84 transMgr->OnShutdown(socket);
85 }
86 }
87
OnBytes(int32_t socket,const void * data,uint32_t dataLen)88 void SessionAdapter::OnBytes(int32_t socket, const void *data, uint32_t dataLen)
89 {
90 auto transMgr = DelayedSingleton<TransmissionManager>::GetInstance();
91 if (transMgr != nullptr) {
92 transMgr->OnReceiveMsg(socket, static_cast<const char*>(data), dataLen);
93 }
94 }
95
OnError(int32_t socket,int32_t errCode)96 void SessionAdapter::OnError(int32_t socket, int32_t errCode)
97 {
98 TELEPHONY_LOGE("async bind session %{public}d fail, reason %{public}d", socket, errCode);
99 }
100
101 } // namespace Telephony
102 } // namespace OHOS
103