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