1 /*
2 * Copyright (c) 2021-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 "stream_manager.h"
17
18 #include "vtp_stream_socket.h"
19 #include "softbus_error_code.h"
20
21 #define INVALID_FD (-1)
22
23 namespace Communication {
24 namespace SoftBus {
GetInstance(std::shared_ptr<IStreamMsgManager> msgManager,std::shared_ptr<IStreamManagerListener> streamListener)25 std::shared_ptr<IStreamManager> IStreamManager::GetInstance(std::shared_ptr<IStreamMsgManager> msgManager,
26 std::shared_ptr<IStreamManagerListener> streamListener)
27 {
28 auto dataManager = std::make_shared<StreamManager>(streamListener);
29 dataManager->SetStreamMsgManager(msgManager);
30
31 return dataManager;
32 }
33
PrepareEnvironment(const std::string & pkgName)34 bool StreamManager::PrepareEnvironment(const std::string &pkgName)
35 {
36 return VtpStreamSocket::InitVtpInstance(pkgName);
37 }
38
DestroyEnvironment(const std::string & pkgName)39 void StreamManager::DestroyEnvironment(const std::string &pkgName)
40 {
41 VtpStreamSocket::DestroyVtpInstance(pkgName);
42 }
43
CreateStreamClientChannel(IpAndPort & local,IpAndPort remote,Proto protocol,int streamType,std::pair<uint8_t *,uint32_t> sessionKey)44 int StreamManager::CreateStreamClientChannel(IpAndPort &local, IpAndPort remote, Proto protocol,
45 int streamType, std::pair<uint8_t*, uint32_t> sessionKey)
46 {
47 TRANS_LOGI(TRANS_STREAM,
48 "Start to create client channel, localPort=%{public}d, remotePort=%{public}d, proto=%{public}d",
49 local.port, remote.port, protocol);
50
51 std::shared_ptr<IStreamSocket> streamSocket = nullptr;
52 if (protocol == VTP) {
53 streamSocket = std::make_shared<VtpStreamSocket>();
54 } else {
55 TRANS_LOGE(TRANS_STREAM, "do not support protocol=%{public}d", protocol);
56 return INVALID_FD;
57 }
58
59 curProtocol_ = protocol;
60 if (streamSocket->CreateClient(local, remote, streamType, sessionKey)) {
61 socketMap_.insert(std::pair<Proto, std::shared_ptr<IStreamSocket>>(curProtocol_, streamSocket));
62 SetStreamRecvListener(streamListener_);
63 int scene = SOFTBUS_SCENE;
64 if (!streamSocket->SetOption(SCENE, StreamAttr(scene))) {
65 TRANS_LOGE(TRANS_STREAM, "set stream scene failed");
66 return INVALID_FD;
67 }
68 TRANS_LOGI(TRANS_STREAM, "streamSocket CreateClient success, localPort=%{public}d", local.port);
69 return local.port;
70 }
71
72 return SOFTBUS_OK;
73 }
74
CreateStreamServerChannel(IpAndPort & local,Proto protocol,int streamType,std::pair<uint8_t *,uint32_t> sessionKey)75 int StreamManager::CreateStreamServerChannel(IpAndPort &local, Proto protocol,
76 int streamType, std::pair<uint8_t*, uint32_t> sessionKey)
77 {
78 TRANS_LOGI(TRANS_STREAM,
79 "Start to create server channel, localPort=%{public}d, protocol=%{public}d", local.port, protocol);
80
81 std::shared_ptr<IStreamSocket> streamSocket = nullptr;
82 if (protocol == VTP) {
83 streamSocket = std::make_shared<VtpStreamSocket>();
84 } else {
85 TRANS_LOGE(TRANS_STREAM, "do not support protocol=%{public}d", protocol);
86 return INVALID_FD;
87 }
88
89 curProtocol_ = protocol;
90 if (!streamSocket->CreateServer(local, streamType, sessionKey)) {
91 TRANS_LOGE(TRANS_STREAM, "create server error. protocol=%{public}d", protocol);
92 return INVALID_FD;
93 }
94
95 socketMap_.insert(std::pair<Proto, std::shared_ptr<IStreamSocket>>(curProtocol_, streamSocket));
96 SetStreamRecvListener(streamListener_);
97
98 int scene = SOFTBUS_SCENE;
99 if (!streamSocket->SetOption(SCENE, StreamAttr(scene))) {
100 TRANS_LOGE(TRANS_STREAM, "set stream scene failed");
101 return INVALID_FD;
102 }
103 return local.port;
104 }
105
DestroyStreamDataChannel()106 bool StreamManager::DestroyStreamDataChannel()
107 {
108 auto it = socketMap_.find(curProtocol_);
109 if (it != socketMap_.end()) {
110 auto streamSocket = it->second;
111 streamSocket->DestroyStreamSocket();
112 socketMap_.erase(it);
113 TRANS_LOGI(TRANS_STREAM, "curProtocol=%{public}d success", curProtocol_);
114 return true;
115 }
116 return false;
117 }
118
Send(std::unique_ptr<IStream> data)119 bool StreamManager::Send(std::unique_ptr<IStream> data)
120 {
121 auto it = socketMap_.find(curProtocol_);
122 if (it != socketMap_.end()) {
123 auto streamSocket = it->second;
124 return streamSocket->Send(std::move(data));
125 }
126 TRANS_LOGE(TRANS_STREAM, "do not found curProtocol=%{public}d", curProtocol_);
127 return false;
128 }
129
SetOption(int type,const StreamAttr & value)130 bool StreamManager::SetOption(int type, const StreamAttr &value)
131 {
132 auto it = socketMap_.find(curProtocol_);
133 if (it != socketMap_.end()) {
134 auto streamSocket = it->second;
135 streamSocket->SetOption(type, value);
136 return true;
137 }
138 return false;
139 }
140
SetMultiLayer(const void * para)141 int32_t StreamManager::SetMultiLayer(const void *para)
142 {
143 auto it = socketMap_.find(curProtocol_);
144 if (it != socketMap_.end()) {
145 auto streamSocket = it->second;
146 return streamSocket->SetMultiLayer(para);
147 }
148 TRANS_LOGE(TRANS_STREAM, "do not found curProtocol=%{public}d", curProtocol_);
149 return SOFTBUS_TRANS_SESSION_SET_CHANNEL_FAILED;
150 }
151
GetOption(int type) const152 StreamAttr StreamManager::GetOption(int type) const
153 {
154 auto it = socketMap_.find(curProtocol_);
155 if (it != socketMap_.end()) {
156 auto streamSocket = it->second;
157 return streamSocket->GetOption(type);
158 }
159 return std::move(StreamAttr());
160 }
161
SetStreamRecvListener(std::shared_ptr<IStreamManagerListener> recvListener)162 void StreamManager::SetStreamRecvListener(std::shared_ptr<IStreamManagerListener> recvListener)
163 {
164 TRANS_LOGD(TRANS_STREAM, "enter.");
165 streamListener_ = recvListener;
166 if (socketListener_ != nullptr) {
167 TRANS_LOGW(TRANS_STREAM, "Socket listener has existed");
168 return;
169 }
170
171 socketListener_ = std::make_shared<StreamSocketListener>(recvListener);
172 auto it = socketMap_.find(curProtocol_);
173 if (it != socketMap_.end()) {
174 auto streamSocket = it->second;
175 streamSocket->SetStreamListener(socketListener_);
176 TRANS_LOGD(TRANS_STREAM, "success curProtocol=%{public}d", curProtocol_);
177 }
178 }
179 } // namespace SoftBus
180 } // namespace Communication
181