• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 ARKTS_PLUGIN_H
17 #define ARKTS_PLUGIN_H
18 
19 #include "arkts_plugin_config.pb.h"
20 #include "plugin_module_api.h"
21 #include "schedule_task_manager.h"
22 
23 struct WebSocketFrame {
24     uint8_t fin;
25     uint8_t opCode;
26     uint8_t mask;
27     char maskingKey[5];
28     uint64_t payloadLen;
29     std::unique_ptr<char[]> payload;
30 };
31 
32 class ArkTSPlugin {
33 public:
34     ArkTSPlugin() = default;
35     ~ArkTSPlugin() = default;
36     int32_t Start(const uint8_t* configData, uint32_t configSize);
37     int32_t Stop();
38     void SetWriter(WriterStruct* writer);
39 
40 private:
41     void Snapshot();
42     void FlushData();
43     bool ClientConnectUnixWebSocket(const std::string& sockName, uint32_t timeoutLimit = 0);
44     bool ClientSendWSUpgradeReq();
45     bool ClientRecvWSUpgradeRsp();
46     bool ClientSendReq(const std::string& message);
47     void Close();
48     bool SetWebSocketTimeOut(int32_t fd, uint32_t timeoutLimit);
49     std::string Decode();
50     bool HandleFrame(WebSocketFrame& wsFrame);
51     bool DecodeMessage(WebSocketFrame& wsFrame);
52     uint64_t NetToHostLongLong(char* buf, uint32_t len);
53     bool Recv(int32_t client, char* buf, size_t totalLen, int32_t flags) const;
54 
55 private:
56     ArkTSConfig protoConfig_;
57     WriterStruct* resultWriter_{nullptr};
58     int32_t pid_{0};
59     std::vector<char> buffer_;
60     std::string snapshotCmd_;
61     std::string timelineCmd_;
62     ScheduleTaskManager scheduleTaskManager_;
63     int32_t client_{-1};
64     enum SocketState : uint8_t {
65         UNINITED,
66         INITED,
67         CONNECTED,
68     };
69     std::atomic<SocketState> socketState_{SocketState::UNINITED};
70 };
71 
72 #endif // ARKTS_PLUGIN_H