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 #ifndef CLIENT_TRANS_UDP_STREAM_ADAPTOR_LISTENER_H_ 17 #define CLIENT_TRANS_UDP_STREAM_ADAPTOR_LISTENER_H_ 18 19 #include "i_stream.h" 20 #include "i_stream_manager.h" 21 #include "softbus_log.h" 22 23 using Communication::SoftBus::IStreamManagerListener; 24 using Communication::SoftBus::IStream; 25 26 namespace OHOS { 27 class StreamAdaptorListener : public IStreamManagerListener { 28 public: 29 StreamAdaptorListener() = default; StreamAdaptorListener(std::shared_ptr<StreamAdaptor> adaptor)30 explicit StreamAdaptorListener(std::shared_ptr<StreamAdaptor> adaptor) : adaptor_(adaptor) {} 31 virtual ~StreamAdaptorListener() = default; OnStreamReceived(std::unique_ptr<IStream> stream)32 void OnStreamReceived(std::unique_ptr<IStream> stream) 33 { 34 if (adaptor_->GetListenerCallback() != nullptr && 35 adaptor_->GetListenerCallback()->OnStreamReceived != nullptr) { 36 auto uniptr = stream->GetBuffer(); 37 char *retbuf = uniptr.get(); 38 int buflen = stream->GetBufferLen(); 39 char *extRetBuf = stream->GetExtBuffer().get(); 40 int extRetBuflen = stream->GetExtBufferLen(); 41 42 int plainDataLength = buflen - adaptor_->GetEncryptOverhead(); 43 if (plainDataLength < 0) { 44 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, 45 "StreamAdaptorListener:OnStreamReceived:buflen:%d < GetEncryptOverhead:%d", 46 buflen, adaptor_->GetEncryptOverhead()); 47 return; 48 } 49 std::unique_ptr<char[]> plainData = std::make_unique<char[]>(plainDataLength); 50 ssize_t decLen = adaptor_->Decrypt(retbuf, buflen, plainData.get(), 51 plainDataLength, adaptor_->GetSessionKey()); 52 if (decLen != plainDataLength) { 53 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, 54 "Decrypt failed, dataLength = %d, decryptedLen = %zd", plainDataLength, decLen); 55 return; 56 } 57 58 StreamData retStreamData = { 59 plainData.get(), 60 plainDataLength, 61 }; 62 63 StreamData extStreamData = { 64 extRetBuf, 65 extRetBuflen, 66 }; 67 68 StreamFrameInfo tmpf = {}; 69 adaptor_->GetListenerCallback()->OnStreamReceived(adaptor_->GetChannelId(), 70 &retStreamData, &extStreamData, &tmpf); 71 } 72 } 73 OnStreamStatus(int status)74 void OnStreamStatus(int status) 75 { 76 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "StreamAdaptorListener: OnStreamStatus(%d) in.", status); 77 78 if (adaptor_->GetListenerCallback() != nullptr && adaptor_->GetListenerCallback()->OnStatusChange != nullptr) { 79 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_DBG, "OnStreamStatus OnStatusChange :%d", status); 80 adaptor_->GetListenerCallback()->OnStatusChange(adaptor_->GetChannelId(), status); 81 } 82 } 83 OnQosEvent(int32_t eventId,int32_t tvCount,const QosTv * tvList)84 void OnQosEvent(int32_t eventId, int32_t tvCount, const QosTv *tvList) 85 { 86 if (adaptor_->GetListenerCallback() != nullptr && adaptor_->GetListenerCallback()->OnQosEvent != nullptr) { 87 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "StreamAdaptorListener: OnQosEvent for channelId = %ld", 88 adaptor_->GetChannelId()); 89 adaptor_->GetListenerCallback()->OnQosEvent(adaptor_->GetChannelId(), eventId, tvCount, tvList); 90 } else { 91 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, 92 "Get ListenerCallback by StreamAdaptor is failed, channelId = %ld", adaptor_->GetChannelId()); 93 } 94 } 95 96 private: 97 std::shared_ptr<StreamAdaptor> adaptor_ = nullptr; 98 }; 99 } 100 101 #endif // !defined(CLIENT_TRANS_UDP_STREAM_ADAPTOR_LISTENER_H_) 102