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