1/* 2 * Copyright (c) 2023 Hunan OpenValley Digital Industry Development 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 */ 15import socket from '@ohos.net.socket'; 16import Logger from '../utils/Logger'; 17 18const TAG = 'SocketImpl'; 19 20/** 21 * socket 封装类 22 */ 23export default class SocketImpl { 24 private tcpSocket: socket.TCPSocket | null = null; 25 26 async createSocket(localIp: string, port: number): Promise<boolean> { 27 Logger.info(TAG,`tcp bind localIp: ${localIp}`); 28 try { 29 if (this.tcpSocket) { 30 this.tcpSocket.close(); 31 this.tcpSocket = null; 32 } 33 this.tcpSocket = socket.constructTCPSocketInstance(); 34 await this.tcpSocket.bind({ 35 address: localIp, port: port, family: 1 36 }); 37 Logger.info(TAG,`tcp bind sucess`); 38 return true; 39 } catch (e) { 40 Logger.error(TAG,`tcp bind error ${JSON.stringify(e)}`); 41 } 42 return false; 43 } 44 45 async connectSocket(address: string, port: number): Promise<boolean> { 46 Logger.info(TAG,`tcp connectSocket address: ${address}`); 47 try { 48 if (!this.tcpSocket) { 49 return false; 50 } 51 if (await this.isConnected()) { 52 Logger.info(TAG,`tcp connectSocket sucess`); 53 return true; 54 } 55 await this.tcpSocket.connect({ 56 address: { 57 address: address, 58 port: port, 59 family: 1 60 }, 61 timeout: 6000 62 }); 63 await this.tcpSocket.setExtraOptions({}); 64 Logger.info(TAG,`tcp connectSocket sucess`); 65 return true; 66 } catch (e) { 67 Logger.error(TAG,`tcp connectSocket error ${JSON.stringify(e)}`); 68 } 69 return false; 70 } 71 72 async closeSocket(): Promise<void> { 73 if (!this.tcpSocket) { 74 return; 75 } 76 await this.tcpSocket.close(); 77 this.tcpSocket.off('connect'); 78 this.tcpSocket.off('message'); 79 this.tcpSocket = null; 80 } 81 82 83 async sendData(data: ArrayBuffer): Promise<void> { 84 85 if (!this.tcpSocket) { 86 return; 87 } 88 Logger.info(TAG,`tcp sendData data `); 89 try { 90 await this.tcpSocket.send({ 91 data: data 92 }); 93 } catch (e) { 94 Logger.error(TAG,`tcp sendData error ${JSON.stringify(e)}`); 95 } 96 } 97 98 async isConnected(): Promise<boolean> { 99 if (!this.tcpSocket) { 100 return false; 101 } 102 103 try { 104 let state = await this.tcpSocket.getState(); 105 if (state.isConnected) { 106 return true; 107 } 108 } catch (e) { 109 Logger.error(TAG,`tcp getState error ${JSON.stringify(e)}`); 110 } 111 return false; 112 } 113 114 setOnMessageReceivedListener(callback: (data: ArrayBuffer) => void): void { 115 if (!this.tcpSocket) { 116 return; 117 } 118 119 this.tcpSocket.on('message', (data) => { 120 Logger.log(TAG,`TCP data: ` + JSON.stringify(data)); 121 let buffer: ArrayBuffer = data.message; 122 callback(buffer); 123 }); 124 } 125 126 setOnErrorListener(callback: () => void): void { 127 if (!this.tcpSocket) { 128 return; 129 } 130 131 this.tcpSocket.on('error', (err) => { 132 Logger.log(TAG,`TCP error: ` + JSON.stringify(err)); 133 callback(); 134 }); 135 } 136 137 setOnCloseListener(callback: () => void): void { 138 if (!this.tcpSocket) { 139 return; 140 } 141 142 this.tcpSocket.on('close', () => { 143 Logger.log(TAG,`TCP close: `); 144 callback(); 145 }); 146 } 147}