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 16import socket from '@ohos.net.socket'; 17 18const TAG = 'SOCKET_SERVER' 19 20class SocketInfo { 21 message: ArrayBuffer = new ArrayBuffer(1); 22 remoteInfo: socket.SocketRemoteInfo = {} as socket.SocketRemoteInfo; 23} 24 25let tcpServer: socket.TCPSocketServer = socket.constructTCPSocketServerInstance(); 26let listenAddr: socket.NetAddress = { 27 address: '127.0.0.1', 28 port: 8080, 29 family: 1 30} 31 32let clientInfo: socket.TCPSocketConnection; 33let messageView: string = '' 34let contentLength: number = 0; 35let contentInfo: string = ''; 36let validationCode: string = ''; 37 38let on_connect_callback = (client: socket.TCPSocketConnection) => { 39 clientInfo = client; 40 client.on('message', on_message_callback); 41 console.info(TAG + '====>on_message success!') 42} 43 44let on_message_callback = (value: SocketInfo) => { 45 try { 46 const dataView = new DataView(value.message); 47 let subInfo: string = ''; 48 for (let i=0; i<dataView.byteLength; i++){ 49 let info = String.fromCharCode(dataView.getUint8(i)); 50 subInfo += info; 51 } 52 subInfo = subInfo.replace(/[\n\f\b\f\n\r\t]/g, ' '); 53 subInfo = subInfo.replace(/[\']/g, "'"); 54 subInfo = subInfo.replace(/[\"]/g, "'"); 55 subInfo = subInfo.replace(/[\\]/g, '\\'); 56 if(subInfo.indexOf('content-length')>0){ 57 let nextSpaceIndex = subInfo.substring(subInfo.indexOf('content-length'), subInfo.length-1).indexOf(' '); 58 contentLength = parseInt(subInfo.slice(subInfo.indexOf('content-length') + 'content-length:'.length, subInfo.indexOf('content-length') + nextSpaceIndex)) 59 console.info(TAG + '====>contentLength:' + contentLength) 60 } 61 if(subInfo.indexOf('boundary=') >0 ){ 62 validationCode = subInfo.substr(subInfo.indexOf('boundary=') + 9, 67); 63 console.info(TAG + '====>validationCode:' + validationCode); 64 } 65 if(subInfo.indexOf(`--${validationCode}`) != subInfo.indexOf(`--${validationCode}--`)){ 66 if(subInfo.indexOf(`--${validationCode}`) != -1){ 67 contentInfo += subInfo.substring(subInfo.indexOf(`--${validationCode}`), subInfo.length); 68 console.info(TAG + '====>contentInfo one') 69 }else{ 70 contentInfo += subInfo; 71 console.info(TAG + '====>contentInfo two') 72 } 73 }else{ 74 if(subInfo.indexOf('content-length') === -1){ 75 contentInfo += subInfo; 76 console.info(TAG + '====>contentInfo three') 77 } 78 } 79 if(subInfo.indexOf(`--${validationCode}--`) != -1){ 80 console.info(TAG + `====>contentLength: ${contentLength}`) 81 console.info(TAG + `====>contentInfo_Length: ${contentInfo.length}`) 82 console.info(TAG + `====>contentInfo: ${contentInfo}`) 83 if(contentLength === contentInfo.length){ 84 messageView = 'HTTP/1.1 200 OK\r\ncontent-length:18\r\n\r\nupload successful!' 85 }else{ 86 messageView = 'HTTP/1.1 200 OK\r\ncontent-length:14\r\n\r\nupload failed!' 87 } 88 contentInfo = ''; 89 let tcpSendOption: socket.TCPSendOptions ={ 90 data: messageView 91 } 92 clientInfo.send(tcpSendOption, ()=>{ 93 console.info(TAG + '====>send success!'); 94 }) 95 }else{ 96 console.info(TAG + '====>subInfo.indexOf:' + subInfo.indexOf(`--${validationCode}--`)) 97 } 98 }catch (err) { 99 console.info(TAG + '====>catch err: ' + JSON.stringify(err)); 100 } 101} 102 103 104let tcpExtraOptions: socket.TCPExtraOptions = { 105 keepAlive: true, 106 OOBInline: true, 107 TCPNoDelay: true, 108 socketLinger: { on: true, linger: 10 }, 109 receiveBufferSize: 1 * 1024, 110 sendBufferSize: 1 * 1024 * 1024, 111 reuseAddress: false, 112 socketTimeout: 3000 113} 114 115export default class Server { 116 async startServer(){ 117 await tcpServer.listen(listenAddr) 118 let socketStateBase: socket.SocketStateBase = await tcpServer.getState(); 119 console.info(TAG + '====>socketStateBase:' + JSON.stringify(socketStateBase)); 120 tcpServer.on('connect', on_connect_callback); 121 console.info(TAG + '====>on_connect success!'); 122 await tcpServer.setExtraOptions(tcpExtraOptions); 123 console.info(TAG + '====>setExtraOptions success!') 124 } 125}