• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 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
16import worker from '@ohos.worker'; // 导入worker模块
17import net_socket from '@ohos.net.socket';
18
19const workTag = "SmartPerf::Work:: "
20let parentPort = worker.parentPort; // 获取parentPort属性
21
22export let IPv4 = 1
23
24export let IPv4BindAddr = {
25    address: "127.0.0.1",
26    family: IPv4,
27    port: 8282
28}
29
30export let UdpSendAddress = {
31    address: "127.0.0.1",
32    family: IPv4,
33    port: 8283
34}
35
36export let flagPackageNum = 0
37
38export let udp = net_socket.constructUDPSocketInstance()
39udp.bind(IPv4BindAddr, err => {
40    if (err) {
41        console.log(workTag + 'Worker socket bind fail');
42        return;
43    }
44    console.log(workTag + 'Worker socket bind success');
45    udp.getState((err, data) => {
46        if (err) {
47            console.log(workTag + 'Worker socket getState fail');
48            return;
49        }
50        console.log(workTag + 'Worker socket getState success:' + JSON.stringify(data));
51    })
52})
53
54parentPort.onmessage = function (e) {
55
56    let socketCollectItems = e.data
57    console.log(workTag + "sub worker recv:" + JSON.stringify(e.data));
58
59    let messageSetPkg = "set_pkgName::" + socketCollectItems.pkg
60    udp.getState((err, data) => {
61        if (err) {
62            parentPort.postMessage("UdpStatus$-1")
63            console.log(workTag + "Worker socket getState error", err);
64        }
65        console.log(workTag + 'Worker socket getState success:' + JSON.stringify(data));
66
67        if (socketCollectItems.testConnection) {
68            for (let i = 0;i < 10; i++) {
69                udp.send({
70                    address: UdpSendAddress,
71                    data: "set_pkgName::com.ohos.smartperf"
72                })
73                console.log(workTag + "Worker socket test connection send");
74            }
75        }
76
77        if (socketCollectItems.setDuBaiDb) {
78            udp.send({
79                address: UdpSendAddress,
80                data: "set_dubai_db"
81            })
82            console.log(workTag + "Worker socket ‘set_dubai_db’ send");
83        }
84
85        if (flagPackageNum < 2) {
86            if (socketCollectItems.pkg != undefined) {
87                udp.send({
88                    address: UdpSendAddress,
89                    data: messageSetPkg
90                })
91                flagPackageNum++
92            }
93        }
94        if (socketCollectItems.fps) {
95            let messageFps = "get_fps_and_jitters"
96            udp.send({
97                address: UdpSendAddress,
98                data: messageFps
99            })
100            console.log(workTag + "sub worker messageFps :" + messageFps);
101
102        }
103        if (socketCollectItems.ram) {
104            let messageRam = "get_ram_info::" + socketCollectItems.pkg
105            udp.send({
106                address: UdpSendAddress,
107                data: messageRam
108            })
109            console.log(workTag + "sub worker messageRam :" + messageRam);
110        }
111        if (socketCollectItems.screen_capture) {
112            udp.send({
113                address: UdpSendAddress,
114                data: "get_capture"
115            })
116            console.log(workTag + "sub worker screen_capture :" + "get_capture");
117        }
118        if (socketCollectItems.catch_trace_start) {
119            let messageTrace = "catch_trace_start"
120            udp.send({
121                address: UdpSendAddress,
122                data: messageTrace
123            })
124            console.log(workTag + "sub worker catch_trace_start :" + "catch_trace_start");
125        }
126        if (socketCollectItems.catch_trace_end) {
127            let messageTrace = "catch_trace_end"
128            udp.send({
129                address: UdpSendAddress,
130                data: messageTrace
131            })
132            console.log(workTag + "sub worker catch_trace_end :" + "catch_trace_end");
133        }
134    })
135}
136
137udp.on("message", function (data) {
138    let buffer = data.message
139    let dataView = new DataView(buffer)
140    let str = ""
141    for (let i = 0;i < dataView.byteLength; ++i) {
142        str += String.fromCharCode(dataView.getUint8(i))
143    }
144    console.log(workTag + "sub worker SocketRecv:" + str);
145    if (str.length > 0) {
146        parentPort.postMessage("UdpStatus$1")
147    }
148    try {
149        if (includes(str, "pss")) {
150            parentPort.postMessage("RAM$" + str)
151        } else if (includes(str, "fps")) {
152            if (str.indexOf("$$") != -1) {
153                let arrStr = str.split("$$")
154                if (arrStr.length > 0) {
155                    if (arrStr[0].indexOf("||") != -1 && arrStr[1].indexOf("||") != -1) {
156                        let fps = arrStr[0].split("||")
157                        let fpsJitter = arrStr[1].split("||")
158                        parentPort.postMessage("FPS$" + fps[1].toString() + "$" + fpsJitter[1].toString())
159                    }
160                }
161            }
162        }
163    } catch (e) {
164        console.log(workTag + "SockOnMessage recv callback err:" + e)
165    }
166
167})
168
169
170function includes(all, sub) {
171
172    all = all.toLowerCase();
173    sub = sub.toLowerCase();
174
175    var firstChar = sub.substring(0, 1);
176    var subLength = sub.length;
177
178    for (let i = 0; i < all.length - subLength + 1; i++) {
179
180        if (all.charAt(i) == firstChar) {
181            if (all.substring(i, i + subLength) == sub) {
182                return true;
183            }
184        }
185    }
186    return false;
187}
188
189