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 if(contentInfo.includes('Content-Type: SUB_REQUEST_Agent_contentType_0100')){ 85 console.info(TAG + `====>contentInfo: ${1}`) 86 messageView = 'HTTP/1.1 200 OK\r\ncontent-length:18\r\nsuccess:contentTypeTest0100\r\n\r\nupload successful!' 87 } else if (contentInfo.includes('SUB_REQUEST_Agent_contentType_0200') && contentInfo.includes('Content-Type: txt')) { 88 console.info(TAG + `====>contentInfo: ${2}`) 89 messageView = 'HTTP/1.1 200 OK\r\ncontent-length:18\r\nsuccess:contentTypeTest0200\r\n\r\nupload successful!' 90 } else { 91 console.info(TAG + `====>contentInfo: ${3}`) 92 messageView = 'HTTP/1.1 200 OK\r\ncontent-length:18\r\n\r\nupload successful!' 93 } 94 }else{ 95 messageView = 'HTTP/1.1 200 OK\r\ncontent-length:14\r\n\r\nupload failed!' 96 } 97 contentInfo = ''; 98 let tcpSendOption: socket.TCPSendOptions ={ 99 data: messageView 100 } 101 clientInfo.send(tcpSendOption, ()=>{ 102 console.info(TAG + '====>send success!'); 103 }) 104 }else{ 105 console.info(TAG + '====>subInfo.indexOf:' + subInfo.indexOf(`--${validationCode}--`)) 106 } 107 }catch (err) { 108 console.info(TAG + '====>catch err: ' + JSON.stringify(err)); 109 } 110} 111 112 113let tcpExtraOptions: socket.TCPExtraOptions = { 114 keepAlive: true, 115 OOBInline: true, 116 TCPNoDelay: true, 117 socketLinger: { on: true, linger: 10 }, 118 receiveBufferSize: 1 * 1024, 119 sendBufferSize: 1 * 1024 * 1024, 120 reuseAddress: false, 121 socketTimeout: 3000 122} 123 124export default class Server { 125 async startServer(){ 126 await tcpServer.listen(listenAddr) 127 let socketStateBase: socket.SocketStateBase = await tcpServer.getState(); 128 console.info(TAG + '====>socketStateBase:' + JSON.stringify(socketStateBase)); 129 tcpServer.on('connect', on_connect_callback); 130 console.info(TAG + '====>on_connect success!'); 131 await tcpServer.setExtraOptions(tcpExtraOptions); 132 console.info(TAG + '====>setExtraOptions success!') 133 } 134}