• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 = '';
37let lastValidationCode: string = '';
38let validationCodeFlag: boolean = false;
39let startContentLengthFlag: string = '';
40let endContentLengthFlag: string = '';
41
42let on_connect_callback = (client: socket.TCPSocketConnection) => {
43  clientInfo = client;
44  client.on('message', on_message_callback);
45  console.info(TAG + '====>on_message success!')
46}
47
48let on_message_callback = (value: SocketInfo) => {
49  try {
50    const dataView = new DataView(value.message);
51    let subInfo: string = '';
52    for (let i=0; i<dataView.byteLength; i++){
53      let info = String.fromCharCode(dataView.getUint8(i));
54      subInfo += info;
55    }
56    console.info(`${TAG} ====> subInfo.length: ${subInfo.length}`)
57    if(subInfo.indexOf('content-length')>0){
58      let parseInfo = subInfo;
59      parseInfo = parseInfo.replace(/[\n\f\b\f\n\r\t]/g, ' ');
60      parseInfo = parseInfo.replace(/[\']/g, "'");
61      parseInfo = parseInfo.replace(/[\"]/g, "'");
62      parseInfo = parseInfo.replace(/[\\]/g, '\\');
63      let nextSpaceIndex = parseInfo.substring(parseInfo.indexOf('content-length'), parseInfo.length-1).indexOf(' ');
64      contentLength = parseInt(parseInfo.slice(parseInfo.indexOf('content-length') + 'content-length:'.length, parseInfo.indexOf('content-length') + nextSpaceIndex))
65      console.info(TAG + '====>contentLength:' + contentLength)
66    }
67    if(subInfo.indexOf('boundary=') >0 ){
68      validationCode = subInfo.substr(subInfo.indexOf('boundary=') + 9, 67);
69      if(validationCode != lastValidationCode){
70        contentInfo = '';
71        validationCodeFlag = false;
72        console.info(`${TAG} ====> clear contentInfo`);
73      }
74      lastValidationCode = validationCode;
75      console.info(TAG + '====>validationCode:' + validationCode);
76      startContentLengthFlag = `--${validationCode}`;
77      endContentLengthFlag = `--${validationCode}--`;
78      console.info(`${TAG} ====> startContentLengthFlag.length: ${startContentLengthFlag.length}`);
79      console.info(`${TAG} ====> endContentLengthFlag.length: ${endContentLengthFlag.length}`)
80    }
81    if(subInfo.indexOf(startContentLengthFlag) != subInfo.indexOf(endContentLengthFlag)){
82      if(subInfo.indexOf(startContentLengthFlag) != -1 && validationCodeFlag === false){
83        contentInfo += subInfo.substring(subInfo.indexOf(`--${validationCode}`), subInfo.length);
84        console.info(TAG + '====>validationCode: ' + validationCode);
85        console.info(TAG + `====>contentInfo one: ${contentInfo.length}`);
86        validationCodeFlag = true;
87      }else{
88        contentInfo += subInfo;
89        console.info(TAG + `====>contentInfo_Length two: ${contentInfo.length}`);
90      }
91    }else{
92      if(subInfo.indexOf('content-length') === -1){
93        contentInfo += subInfo;
94        console.info(TAG + `====>contentInfo_Length three: ${contentInfo.length}`);
95      }
96    }
97    let contentInfoFinally:string = contentInfo.substr(contentLength-(endContentLengthFlag.length+2), endContentLengthFlag.length+2);
98    contentInfoFinally = contentInfoFinally.replace(/[\n\f\b\f\n\r\t]/g, '');
99    if(contentInfoFinally === endContentLengthFlag){
100      console.info(TAG + `====>contentLength: ${contentLength}`);
101      console.info(TAG + `====>contentInfo_Length: ${contentInfo.length}`);
102      console.info(TAG + `====>contentInfo: ${contentInfo}`);
103      if(contentLength === contentInfo.length){
104        console.info(TAG + `====>contentInfo: all`);
105        messageView = 'HTTP/1.1 200 OK\r\ncontent-length:18\r\n\r\nupload successful!'
106      }else{
107        console.info(TAG + `====>contentInfo: 400 err`);
108        messageView = 'HTTP/1.1 400 Bad Request\r\ncontent-length:14\r\n\r\nupload failed!'
109      }
110      contentInfo = '';
111      let tcpSendOption: socket.TCPSendOptions ={
112        data: messageView
113      }
114      clientInfo.send(tcpSendOption, ()=>{
115        console.info(TAG + '====>send success!');
116      })
117    }else{
118      console.info(TAG + '====>subInfo.indexOf:' + subInfo.indexOf(`--${validationCode}--`));
119    }
120  }catch (err) {
121    console.info(TAG + '====>catch err: ' + JSON.stringify(err));
122  }
123}
124
125
126let tcpExtraOptions: socket.TCPExtraOptions = {
127  keepAlive: true,
128  OOBInline: true,
129  TCPNoDelay: true,
130  socketLinger: { on: true, linger: 10 },
131  receiveBufferSize: 1 * 1024,
132  sendBufferSize: 1 * 1024 * 1024,
133  reuseAddress: false,
134  socketTimeout: 3000
135}
136
137export default class Server {
138  async startServer(){
139    await tcpServer.listen(listenAddr)
140    let socketStateBase: socket.SocketStateBase = await tcpServer.getState();
141    console.info(TAG + '====>socketStateBase:' + JSON.stringify(socketStateBase));
142    tcpServer.on('connect', on_connect_callback);
143    console.info(TAG + '====>on_connect success!');
144    await tcpServer.setExtraOptions(tcpExtraOptions);
145    console.info(TAG + '====>setExtraOptions success!');
146  }
147}