1 /* 2 * Copyright (c) 2021-2022 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 #ifndef STREAM_MANAGER_H 17 #define STREAM_MANAGER_H 18 19 #include <memory> 20 #include <string> 21 #include <utility> 22 23 #include "common_inner.h" 24 #include "i_stream.h" 25 #include "i_stream_manager.h" 26 #include "i_stream_msg_manager.h" 27 #include "i_stream_socket.h" 28 #include "session.h" 29 #include "stream_common.h" 30 31 namespace Communication { 32 namespace SoftBus { 33 class StreamManager : public std::enable_shared_from_this<StreamManager>, public IStreamManager { 34 public: 35 class StreamSocketListener : public IStreamSocketListener { 36 public: StreamSocketListener(std::shared_ptr<IStreamManagerListener> streamListener)37 explicit StreamSocketListener(std::shared_ptr<IStreamManagerListener> streamListener) 38 : listener_(streamListener) {} 39 StreamSocketListener() = delete; 40 virtual ~StreamSocketListener() = default; OnStreamReceived(std::unique_ptr<IStream> stream)41 void OnStreamReceived(std::unique_ptr<IStream> stream) override 42 { 43 listener_->OnStreamReceived(std::move(stream)); 44 } OnStreamStatus(int status)45 void OnStreamStatus(int status) override 46 { 47 listener_->OnStreamStatus(status); 48 } 49 OnStreamHdrReceived(std::unique_ptr<char[]> header,int size)50 int OnStreamHdrReceived(std::unique_ptr<char[]> header, int size) override 51 { 52 static_cast<void>(header); 53 static_cast<void>(size); 54 return 0; 55 } 56 OnQosEvent(int32_t eventId,int32_t tvCount,const QosTv * tvList)57 void OnQosEvent(int32_t eventId, int32_t tvCount, const QosTv *tvList) const override 58 { 59 listener_->OnQosEvent(eventId, tvCount, tvList); 60 } 61 OnFrameStats(const StreamSendStats * data)62 void OnFrameStats(const StreamSendStats *data) override 63 { 64 listener_->OnFrameStats(data); 65 } 66 OnRippleStats(const TrafficStats * data)67 void OnRippleStats(const TrafficStats *data) override 68 { 69 listener_->OnRippleStats(data); 70 } 71 72 private: 73 std::shared_ptr<IStreamManagerListener> listener_ = nullptr; 74 }; 75 StreamManager(std::shared_ptr<IStreamManagerListener> streamListener)76 explicit StreamManager(std::shared_ptr<IStreamManagerListener> streamListener) : streamListener_(streamListener) {} 77 StreamManager() = delete; 78 virtual ~StreamManager() = default; 79 80 int CreateStreamClientChannel(IpAndPort &local, IpAndPort remote, Proto protocol, 81 int streamType, std::pair<uint8_t*, uint32_t> sessionKey) override; 82 83 int CreateStreamServerChannel(IpAndPort &local, Proto protocol, int streamType, 84 std::pair<uint8_t*, uint32_t> sessionKey) override; 85 86 bool DestroyStreamDataChannel() override; 87 88 bool Send(std::unique_ptr<IStream> data) override; 89 90 bool SetOption(int type, const StreamAttr &value) override; 91 StreamAttr GetOption(int type) const override; 92 93 void SetStreamRecvListener(std::shared_ptr<IStreamManagerListener> recvListener) override; 94 bool PrepareEnvironment(const std::string &pkgName) override; 95 void DestroyEnvironment(const std::string &pkgName) override; 96 SetStreamMsgManager(std::shared_ptr<IStreamMsgManager> manager)97 void SetStreamMsgManager(std::shared_ptr<IStreamMsgManager> manager) 98 { 99 msgManager_ = manager; 100 } GetStreamMsgManager()101 std::shared_ptr<IStreamMsgManager> GetStreamMsgManager() 102 { 103 return msgManager_; 104 } 105 106 private: 107 StreamManager(const StreamManager &) = delete; 108 StreamManager(StreamManager &&) = delete; 109 StreamManager &operator=(const StreamManager &) = delete; 110 StreamManager &operator=(StreamManager &&) = delete; 111 112 std::map<Proto, std::shared_ptr<IStreamSocket>> socketMap_; 113 Proto curProtocol_ = VTP; 114 std::shared_ptr<IStreamSocketListener> socketListener_ = nullptr; 115 std::shared_ptr<IStreamMsgManager> msgManager_ = nullptr; 116 std::shared_ptr<IStreamManagerListener> streamListener_ = nullptr; 117 }; 118 } // namespace SoftBus 119 } // namespace Communication 120 121 #endif 122