1 /*
2  * Copyright (c) 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 RS_PROFILER_NETWORK_H
17 #define RS_PROFILER_NETWORK_H
18 
19 #include <queue>
20 #include <mutex>
21 #include <string>
22 #include <vector>
23 #include <unordered_set>
24 
25 namespace OHOS::Rosen {
26 
27 class Packet;
28 class Socket;
29 class RSCaptureData;
30 enum class PackageID;
31 
32 struct NetworkStats {
33     std::string interface;
34     uint64_t receivedBytes = 0u;
35     uint64_t transmittedBytes = 0u;
36 };
37 
38 class Network {
39 public:
40     static void Run();
41     static void Stop();
42     static void ForceShutdown();
43 
44     static std::vector<NetworkStats> GetStats(const std::string& interface);
45 
46     static void SendRdcPath(const std::string& path);
47     static void SendDclPath(const std::string& path);
48     static void SendMskpPath(const std::string& path);
49     static void SendBetaRecordPath(const std::string& path);
50 
51     static void SendSkp(const void* data, size_t size);
52     static void SendCaptureData(const RSCaptureData& data);
53     static void SendRSTreeDumpJSON(const std::string& jsonstr);
54     static void SendRSTreePerfNodeList(const std::unordered_set<uint64_t>& perfNodesList);
55     static void SendRSTreeSingleNodePerf(uint64_t id, uint64_t nanosec);
56 
57     static void SendBinary(const void* data, size_t size);
58     static void SendBinary(const std::vector<char>& data);
59     static void SendBinary(const std::string& data);
60     static void SendMessage(const std::string& message);
61 
62     static bool PopCommand(std::vector<std::string>& args);
63 
64 private:
65     static void ReportStats();
66     static void PushCommand(const std::vector<std::string>& args);
67     static void ProcessCommand(const char* data, size_t size);
68     static void ProcessBinary(const char* data, size_t size);
69     static void ProcessIncoming(Socket& socket);
70     static void ProcessOutgoing(Socket& socket);
71     static void SendPath(const std::string& path, PackageID id);
72     static void SendPacket(const Packet& packet);
73     static void Shutdown(Socket*& socket);
74 
75 private:
76     static bool isRunning_;
77     static std::atomic<bool> forceShutdown_;
78 
79     static std::mutex incomingMutex_;
80     static std::queue<std::vector<std::string>> incoming_;
81 
82     static std::mutex outgoingMutex_;
83     static std::queue<std::vector<char>> outgoing_;
84 };
85 
86 } // namespace OHOS::Rosen
87 
88 #endif // RS_PROFILER_NETWORK_H