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