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 "transmission_manager.h"
17 #include "telephony_log_wrapper.h"
18 #include "client_session.h"
19 #include "server_session.h"
20 
21 namespace OHOS {
22 namespace Telephony {
TransmissionManager()23 TransmissionManager::TransmissionManager()
24 {
25 }
26 
~TransmissionManager()27 TransmissionManager::~TransmissionManager()
28 {
29 }
30 
CreateServerSession( const std::shared_ptr<ISessionCallback> &callback)31 std::shared_ptr<SessionAdapter> TransmissionManager::CreateServerSession(
32     const std::shared_ptr<ISessionCallback> &callback)
33 {
34     auto serverSession = std::make_shared<ServerSession>(callback);
35     if (serverSession == nullptr) {
36         TELEPHONY_LOGE("create server session fail");
37     }
38 
39     std::lock_guard<ffrt::mutex> lock(mutex_);
40     session_ = serverSession;
41     return serverSession;
42 }
43 
CreateClientSession( const std::shared_ptr<ISessionCallback> &callback)44 std::shared_ptr<SessionAdapter> TransmissionManager::CreateClientSession(
45     const std::shared_ptr<ISessionCallback> &callback)
46 {
47     auto clientSession = std::make_shared<ClientSession>(callback);
48     if (clientSession == nullptr) {
49         TELEPHONY_LOGE("create client session fail");
50     }
51 
52     std::lock_guard<ffrt::mutex> lock(mutex_);
53     session_ = clientSession;
54     return clientSession;
55 }
56 
OnBind(int32_t socket)57 void TransmissionManager::OnBind(int32_t socket)
58 {
59     std::shared_ptr<SessionAdapter> session = nullptr;
60     {
61         std::lock_guard<ffrt::mutex> lock(mutex_);
62         session = session_.lock();
63     }
64     if (session != nullptr) {
65         session->OnSessionBind(socket);
66     }
67 }
68 
OnShutdown(int32_t socket)69 void TransmissionManager::OnShutdown(int32_t socket)
70 {
71     std::shared_ptr<SessionAdapter> session = nullptr;
72     {
73         std::lock_guard<ffrt::mutex> lock(mutex_);
74         session = session_.lock();
75     }
76     if (session != nullptr) {
77         session->OnSessionShutdown(socket);
78     }
79 }
80 
OnReceiveMsg(int32_t socket, const char* data, uint32_t dataLen)81 void TransmissionManager::OnReceiveMsg(int32_t socket, const char* data, uint32_t dataLen)
82 {
83     std::shared_ptr<SessionAdapter> session = nullptr;
84     {
85         std::lock_guard<ffrt::mutex> lock(mutex_);
86         session = session_.lock();
87     }
88     if (session != nullptr) {
89         session->OnReceiveMsg(socket, data, dataLen);
90     }
91 }
92 
93 } // namespace Telephony
94 } // namespace OHOS
95