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 netSocket 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 = netSocket.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 let socketCollectItems = e.data; 56 console.log(workTag + 'sub worker recv:' + JSON.stringify(e.data)); 57 58 let messageSetPkg = 'set_pkgName::' + socketCollectItems.pkg; 59 udp.getState((err, data) => { 60 if (err) { 61 parentPort.postMessage('UdpStatus$-1'); 62 console.log(workTag + 'Worker socket getState error', err); 63 } 64 console.log(workTag + 'Worker socket getState success:' + JSON.stringify(data)); 65 66 if (socketCollectItems.testConnection) { 67 for (let i = 0; i < 10; i++) { 68 udp.send({ 69 address: UdpSendAddress, 70 data: 'set_pkgName::com.ohos.smartperf', 71 }); 72 console.log(workTag + 'Worker socket test connection send'); 73 } 74 } 75 76 if (socketCollectItems.setDuBaiDb) { 77 udp.send({ 78 address: UdpSendAddress, 79 data: 'set_dubai_db', 80 }); 81 console.log(workTag + 'Worker socket ‘set_dubai_db’ send'); 82 } 83 84 if (flagPackageNum < 2) { 85 if (socketCollectItems.pkg !== undefined) { 86 udp.send({ 87 address: UdpSendAddress, 88 data: messageSetPkg, 89 }); 90 flagPackageNum++; 91 } 92 } 93 if (socketCollectItems.fps) { 94 let messageFps = 'get_fps_and_jitters'; 95 udp.send({ 96 address: UdpSendAddress, 97 data: messageFps, 98 }); 99 console.log(workTag + 'sub worker messageFps :' + messageFps); 100 } 101 if (socketCollectItems.ram) { 102 let messageRam = 'get_ram_info::' + socketCollectItems.pkg; 103 udp.send({ 104 address: UdpSendAddress, 105 data: messageRam, 106 }); 107 console.log(workTag + 'sub worker messageRam :' + messageRam); 108 } 109 if (socketCollectItems.screenCapture) { 110 udp.send({ 111 address: UdpSendAddress, 112 data: 'get_capture', 113 }); 114 console.log(workTag + 'sub worker screen_capture :' + 'get_capture'); 115 } 116 if (socketCollectItems.catch_trace_start) { 117 let messageTrace = 'catch_trace_start'; 118 udp.send({ 119 address: UdpSendAddress, 120 data: messageTrace, 121 }); 122 console.log(workTag + 'sub worker catch_trace_start :' + 'catch_trace_start'); 123 } 124 if (socketCollectItems.catch_trace_end) { 125 let messageTrace = 'catch_trace_end'; 126 udp.send({ 127 address: UdpSendAddress, 128 data: messageTrace, 129 }); 130 console.log(workTag + 'sub worker catch_trace_end :' + 'catch_trace_end'); 131 } 132 }); 133}; 134 135udp.on('message', function (data) { 136 let buffer = data.message; 137 let dataView = new DataView(buffer); 138 let str = ''; 139 for (let i = 0; i < dataView.byteLength; ++i) { 140 str += String.fromCharCode(dataView.getUint8(i)); 141 } 142 console.log(workTag + 'sub worker SocketRecv:' + str); 143 if (str.length > 0) { 144 parentPort.postMessage('UdpStatus$1'); 145 } 146 try { 147 if (includes(str, 'pss')) { 148 parentPort.postMessage('RAM$' + str); 149 } else if (includes(str, 'fps')) { 150 if (str.indexOf('$$') !== -1) { 151 let arrStr = str.split('$$'); 152 if (arrStr.length > 0) { 153 if (arrStr[0].indexOf('||') !== -1 && arrStr[1].indexOf('||') !== -1) { 154 let fps = arrStr[0].split('||'); 155 let fpsJitter = arrStr[1].split('||'); 156 parentPort.postMessage('FPS$' + fps[1].toString() + '$' + fpsJitter[1].toString()); 157 } 158 } 159 } 160 } 161 } catch (e) { 162 console.log(workTag + 'SockOnMessage recv callback err:' + e); 163 } 164}); 165 166function includes(all, sub) { 167 all = all.toLowerCase(); 168 sub = sub.toLowerCase(); 169 170 let firstChar = sub.substring(0, 1); 171 let subLength = sub.length; 172 173 for (let i = 0; i < all.length - subLength + 1; i++) { 174 if (all.charAt(i) === firstChar) { 175 if (all.substring(i, i + subLength) === sub) { 176 return true; 177 } 178 } 179 } 180 return false; 181}