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 VTP_STREAM_SOCKET_H 17 #define VTP_STREAM_SOCKET_H 18 19 #include <condition_variable> 20 #include <cstdint> 21 #include <map> 22 #include <memory> 23 #include <string> 24 #include <utility> 25 26 #include "fillpinc.h" 27 28 #include "common_inner.h" 29 #include "i_stream.h" 30 #include "i_stream_socket.h" 31 #include "stream_common.h" 32 #include "vtp_instance.h" 33 34 namespace Communication { 35 namespace SoftBus { 36 struct ConnectStatus { 37 enum { 38 UNCONNECTED, 39 CONNECTED, 40 CLOSED, 41 }; 42 43 int status = UNCONNECTED; 44 }; 45 46 class VtpStreamSocket : public std::enable_shared_from_this<VtpStreamSocket>, public IStreamSocket { 47 public: 48 static constexpr int FILLP_VTP_SEND_CACHE_SIZE = 500; 49 static constexpr int FILLP_VTP_RECV_CACHE_SIZE = 500; 50 static constexpr int FILLP_KEEP_ALIVE_TIME = 300000; 51 52 VtpStreamSocket(); 53 ~VtpStreamSocket() override; 54 std::shared_ptr<VtpStreamSocket> GetSelf(); 55 56 bool CreateClient(IpAndPort &local, int streamType, std::pair<uint8_t*, uint32_t> sessionKey) override; 57 bool CreateClient(IpAndPort &local, const IpAndPort &remote, int streamType, 58 std::pair<uint8_t*, uint32_t> sessionKey) override; 59 60 bool CreateServer(IpAndPort &local, int streamType, std::pair<uint8_t*, uint32_t> sessionKey) override; 61 62 void DestroyStreamSocket() override; 63 64 bool Connect(const IpAndPort &remote) override; 65 bool Send(std::unique_ptr<IStream> stream) override; 66 67 bool SetOption(int type, const StreamAttr &value) override; 68 StreamAttr GetOption(int type) const override; 69 70 bool SetStreamListener(std::shared_ptr<IStreamSocketListener> receiver) override; 71 72 static bool InitVtpInstance(const std::string &pkgName); 73 static void DestroyVtpInstance(const std::string &pkgName); 74 75 ssize_t GetEncryptOverhead() const; 76 77 ssize_t Encrypt(const void *in, ssize_t inLen, void *out, ssize_t outLen) const; 78 79 ssize_t Decrypt(const void *in, ssize_t inLen, void *out, ssize_t outLen) const; 80 81 private: 82 using MySetFunc = bool (VtpStreamSocket::*)(int, const StreamAttr &); 83 using MyGetFunc = StreamAttr (VtpStreamSocket::*)(int) const; 84 struct OptionFunc { 85 ValueType valueType; 86 MySetFunc set; 87 MyGetFunc get; 88 }; 89 90 const std::map<int, FillpConfigAppListEnum> FILLP_TYPE_MAP { 91 { SEND_CACHE, FT_CONF_SEND_CACHE }, { RECV_CACHE, FT_CONF_RECV_CACHE }, 92 { SEND_BUF_SIZE, FT_CONF_SEND_BUFFER_SIZE }, { RECV_BUF_SIZE, FT_CONF_RECV_BUFFER_SIZE }, 93 { PACKET_SIZE, FT_CONF_PACKET_SIZE }, { KEEP_ALIVE_TIMEOUT, FT_CONF_TIMER_KEEP_ALIVE }, 94 { MAX_VTP_SOCKET_NUM, FT_CONF_MAX_SOCK_NUM }, { MAX_VTP_CONNECT_NUM, FT_CONF_MAX_CONNECTION_NUM }, 95 { REDUNANCY_SWITCH, FT_CONF_USE_FEC }, { REDUNANCY_LEVEL, FT_CONF_FEC_REDUNDANCY_LEVEL }, 96 }; 97 98 const std::map<int, FillpConfigAppListEnum> INNER_FILLP_TYPE_MAP { 99 { NACK_DELAY, FT_CONF_ENABLE_NACK_DELAY }, 100 { NACK_DELAY_TIMEOUT, FT_CONF_NACK_DELAY_TIMEOUT }, 101 { PACK_INTERVAL_ENLARGE, FT_CONF_ENLARGE_PACK_INTERVAL }, 102 { PKT_STATISTICS, FT_CONF_APP_FC_STATISTICS }, 103 { PKT_LOSS, FT_CONF_APP_FC_RECV_PKT_LOSS }, 104 }; 105 106 void InsertElementToFuncMap(int type, ValueType valueType, MySetFunc set, MyGetFunc get); 107 int CreateAndBindSocket(IpAndPort &local) override; 108 bool Accept() override; 109 110 int EpollTimeout(int fd, int timeout) override; 111 int SetSocketEpollMode(int fd) override; 112 113 void InsertBufferLength(int num, int length, uint8_t *output) const; 114 std::unique_ptr<IStream> MakeStreamData(StreamData &data, const StreamFrameInfo &info) const; 115 int RecvStreamLen(); 116 void DoStreamRecv(); 117 std::unique_ptr<char[]> RecvStream(int dataLength) override; 118 119 void SetDefaultConfig(int fd); 120 bool SetIpTos(int fd, const StreamAttr &tos); 121 StreamAttr GetIpTos(int type = -1) const; 122 StreamAttr GetStreamSocketFd(int type = -1) const; 123 StreamAttr GetListenSocketFd(int type = -1) const; 124 bool SetSocketBoundInner(int fd, std::string ip = "") const; 125 bool SetSocketBindToDevices(int type, const StreamAttr &ip); 126 bool SetVtpStackConfigDelayed(int type, const StreamAttr &value); 127 bool SetVtpStackConfig(int type, const StreamAttr &value); 128 StreamAttr GetVtpStackConfig(int type) const; 129 bool SetNonBlockMode(int fd, const StreamAttr &value); 130 StreamAttr GetNonBlockMode(int fd) const; 131 StreamAttr GetIp(int type) const; 132 StreamAttr GetPort(int type) const; 133 bool SetStreamType(int type, const StreamAttr &value); 134 StreamAttr GetStreamType(int type) const; GetIpType(int type)135 StreamAttr GetIpType(int type) const 136 { 137 if (type != static_cast<int>(IP_TYPE)) { 138 return std::move(StreamAttr()); 139 } 140 return std::move(StreamAttr(std::string("V4"))); 141 } GetRemoteScopeId(int type)142 StreamAttr GetRemoteScopeId(int type) const 143 { 144 if (type != static_cast<int>(REMOTE_SCOPE_ID)) { 145 return std::move(StreamAttr()); 146 } 147 return std::move(StreamAttr(0)); 148 } 149 IsServer(int type)150 StreamAttr IsServer(int type) const 151 { 152 if (type != static_cast<int>(IS_SERVER)) { 153 return std::move(StreamAttr()); 154 } 155 return std::move(StreamAttr(listenFd_ != -1)); 156 } 157 158 bool SetStreamScene(int type, const StreamAttr &value); 159 bool SetStreamHeaderSize(int type, const StreamAttr &value); 160 161 void NotifyStreamListener(); 162 163 bool EnableBwEstimationAlgo(int streamFd, bool isServer) const; 164 165 bool EnableJitterDetectionAlgo(int streamFd) const; 166 167 bool EnableDirectlySend(int streamFd) const; 168 169 bool EnableSemiReliable(int streamFd) const; 170 171 void RegisterMetricCallback(bool isServer); /* register the metric callback function */ 172 173 static void AddStreamSocketLock(int fd, std::mutex &streamsocketlock); 174 175 static void AddStreamSocketListener(int fd, std::shared_ptr<VtpStreamSocket> streamreceiver); 176 177 static void RemoveStreamSocketLock(int fd); 178 179 static void RemoveStreamSocketListener(int fd); 180 181 static int HandleFillpFrameStats(int fd, const FtEventCbkInfo *info); 182 183 static int HandleRipplePolicy(int fd, const FtEventCbkInfo *info); 184 185 static int FillpStatistics(int fd, const FtEventCbkInfo *info); 186 187 void FillpAppStatistics(); 188 189 static void FillSupportDet(int fd, const FtEventCbkInfo *info, QosTv* metricList); 190 191 static std::map<int, std::mutex &> g_streamSocketLockMap; 192 static std::mutex g_streamSocketLockMapLock_; 193 static std::map<int, std::shared_ptr<VtpStreamSocket>> g_streamSocketMap; 194 static std::mutex g_streamSocketMapLock_; 195 196 std::map<int, OptionFunc> optFuncMap_ {}; 197 static std::shared_ptr<VtpInstance> vtpInstance_; 198 std::condition_variable configCv_; 199 std::mutex streamSocketLock_; 200 int scene_ = UNKNOWN_SCENE; 201 int streamHdrSize_ = 0; 202 bool isDestroyed_ = false; 203 }; 204 } // namespace SoftBus 205 } // namespace Communication 206 207 #endif 208