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