@react-native-ohos/react-native-tcp-socket 6.3.1-rc.3 → 6.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/LICENSE +21 -21
  2. package/README.OpenSource +10 -10
  3. package/README.md +12 -12
  4. package/harmony/tcp_socket/BuildProfile.ets +16 -16
  5. package/harmony/tcp_socket/Index.ets +5 -5
  6. package/harmony/tcp_socket/build-profile.json5 +31 -31
  7. package/harmony/tcp_socket/hvigorfile.ts +6 -6
  8. package/harmony/tcp_socket/obfuscation-rules.txt +22 -22
  9. package/harmony/tcp_socket/oh-package.json5 +11 -11
  10. package/harmony/tcp_socket/src/main/cpp/CMakeLists.txt +8 -8
  11. package/harmony/tcp_socket/src/main/cpp/TcpSocketPackage.h +18 -18
  12. package/harmony/tcp_socket/src/main/cpp/generated/RNOH/generated/BaseReactNativeTcpSocketPackage.h +72 -72
  13. package/harmony/tcp_socket/src/main/cpp/generated/RNOH/generated/turbo_modules/TcpSocketModule.cpp +28 -28
  14. package/harmony/tcp_socket/src/main/cpp/generated/RNOH/generated/turbo_modules/TcpSocketModule.h +16 -16
  15. package/harmony/tcp_socket/src/main/cpp/generated/react/renderer/components/react_native_tcp_socket/ComponentDescriptors.h +24 -24
  16. package/harmony/tcp_socket/src/main/cpp/generated/react/renderer/components/react_native_tcp_socket/EventEmitters.cpp +16 -16
  17. package/harmony/tcp_socket/src/main/cpp/generated/react/renderer/components/react_native_tcp_socket/EventEmitters.h +17 -17
  18. package/harmony/tcp_socket/src/main/cpp/generated/react/renderer/components/react_native_tcp_socket/Props.cpp +19 -19
  19. package/harmony/tcp_socket/src/main/cpp/generated/react/renderer/components/react_native_tcp_socket/Props.h +18 -18
  20. package/harmony/tcp_socket/src/main/cpp/generated/react/renderer/components/react_native_tcp_socket/ShadowNodes.cpp +17 -17
  21. package/harmony/tcp_socket/src/main/cpp/generated/react/renderer/components/react_native_tcp_socket/ShadowNodes.h +23 -23
  22. package/harmony/tcp_socket/src/main/cpp/generated/react/renderer/components/react_native_tcp_socket/States.cpp +16 -16
  23. package/harmony/tcp_socket/src/main/cpp/generated/react/renderer/components/react_native_tcp_socket/States.h +18 -18
  24. package/harmony/tcp_socket/src/main/ets/Logger.ts +45 -45
  25. package/harmony/tcp_socket/src/main/ets/TcpEventListener.ts +120 -120
  26. package/harmony/tcp_socket/src/main/ets/TcpSocket.ts +16 -16
  27. package/harmony/tcp_socket/src/main/ets/TcpSocketClient.ts +443 -443
  28. package/harmony/tcp_socket/src/main/ets/TcpSocketPackage.ts +27 -27
  29. package/harmony/tcp_socket/src/main/ets/TcpSocketServer.ts +150 -150
  30. package/harmony/tcp_socket/src/main/ets/TcpSocketTurboModule.ts +224 -224
  31. package/harmony/tcp_socket/src/main/ets/generated/components/ts.ts +5 -5
  32. package/harmony/tcp_socket/src/main/ets/generated/index.ets +5 -5
  33. package/harmony/tcp_socket/src/main/ets/generated/ts.ts +6 -6
  34. package/harmony/tcp_socket/src/main/ets/generated/turboModules/TcpSocketModule.ts +38 -38
  35. package/harmony/tcp_socket/src/main/ets/generated/turboModules/ts.ts +5 -5
  36. package/harmony/tcp_socket/src/main/module.json5 +11 -11
  37. package/harmony/tcp_socket/src/main/resources/base/element/string.json +8 -8
  38. package/harmony/tcp_socket/src/main/resources/en_US/element/string.json +8 -8
  39. package/harmony/tcp_socket/src/main/resources/zh_CN/element/string.json +8 -8
  40. package/harmony/tcp_socket/src/ohosTest/ets/test/Ability.test.ets +34 -34
  41. package/harmony/tcp_socket/src/ohosTest/ets/test/List.test.ets +4 -4
  42. package/harmony/tcp_socket/src/ohosTest/module.json5 +13 -13
  43. package/harmony/tcp_socket/src/test/List.test.ets +4 -4
  44. package/harmony/tcp_socket/src/test/LocalUnit.test.ets +32 -32
  45. package/harmony/tcp_socket/ts.ts +6 -6
  46. package/harmony/tcp_socket.har +0 -0
  47. package/lib/types/Globals.d.ts +2 -2
  48. package/lib/types/Server.d.ts +137 -137
  49. package/lib/types/Socket.d.ts +272 -272
  50. package/lib/types/TLSServer.d.ts +28 -28
  51. package/lib/types/TLSSocket.d.ts +50 -50
  52. package/lib/types/index.d.ts +80 -80
  53. package/package.json +76 -76
  54. package/src/Globals.js +12 -12
  55. package/src/Server.js +271 -271
  56. package/src/Socket.js +513 -513
  57. package/src/TLSServer.js +70 -70
  58. package/src/TLSSocket.js +93 -93
  59. package/src/TcpSocketModule.ts +39 -39
  60. package/src/index.js +144 -144
@@ -1,444 +1,444 @@
1
- /*
2
- * Copyright (c) 2024 Huawei Device Co., Ltd. All rights reserved
3
- * Use of this source code is governed by a MIT license that can be
4
- * found in the LICENSE file.
5
- */
6
- import { TcpSocket } from './TcpSocket'
7
- import { TcpEventListener } from "./TcpEventListener"
8
- import { socket } from '@kit.NetworkKit';
9
- import { BusinessError } from '@kit.BasicServicesKit';
10
- import { util } from '@kit.ArkTS';
11
- import Logger from './Logger'
12
-
13
-
14
- interface SocketLinger {
15
- on: boolean;
16
- linger: number;
17
- }
18
-
19
- class SocketInfo {
20
- message: ArrayBuffer = new ArrayBuffer(1);
21
- remoteInfo: socket.SocketRemoteInfo = {} as socket.SocketRemoteInfo;
22
- }
23
-
24
- export class TcpSocketClient extends TcpSocket {
25
- private receiverListener?: TcpEventListener;
26
- private cid?: number;
27
- private connectTimeout: number = 6000;
28
- private tcpSocket?: socket.TCPSocket;
29
- private tlsSocket?: socket.TLSSocket;
30
- private tcpSocketConnection?: socket.TCPSocketConnection;
31
- private tlsSocketConnection?: socket.TLSSocketConnection;
32
- private paused: boolean = false;
33
- private tcpExtraOptions: socket.TCPExtraOptions = {
34
- keepAlive: false, //是否保持连接。默认为false
35
- OOBInline: false, //是否为OOB内联。默认为false
36
- TCPNoDelay: false, //TCPSocket连接是否无时延。默认为false
37
- socketLinger: {
38
- on: true,
39
- linger: 10000
40
- } as SocketLinger, //socket是否继续逗留。- on:是否逗留(true:逗留;false:不逗留)。- linger:逗留时长,单位毫秒(ms),取值范围为0~65535。当入参on设置为true时,才需要设置。
41
- receiveBufferSize: 4096, //接收缓冲区大小(单位:Byte),默认为0
42
- sendBufferSize: 4096, //发送缓冲区大小(单位:Byte),默认为0。
43
- reuseAddress: false, //是否重用地址。默认为false。
44
- socketTimeout: 30000 //套接字超时时间,单位毫秒(ms),默认为0。
45
- }
46
- tcpEvtListener: any;
47
-
48
- constructor(receiverListener: TcpEventListener, cid: number,
49
- tcpSocketConnection?: socket.TCPSocketConnection, tlsSocketConnection?: socket.TLSSocketConnection) {
50
- super(cid);
51
- this.receiverListener = receiverListener;
52
- this.cid = cid;
53
- this.tcpSocketConnection = tcpSocketConnection;
54
- this.tlsSocketConnection = tlsSocketConnection;
55
- }
56
-
57
- getSocket(): socket.TCPSocket | socket.TLSSocket | socket.TCPSocketConnection | socket.TLSSocketConnection | undefined {
58
- if (this.tcpSocketConnection) {
59
- return this.tcpSocketConnection;
60
- } else if (this.tlsSocketConnection) {
61
- return this.tlsSocketConnection;
62
- } else if (this.tlsSocket) {
63
- return this.tlsSocket;
64
- } else {
65
- return this.tcpSocket;
66
- }
67
- }
68
-
69
- private waitForData() {
70
- return new Promise<void>(resolve => {
71
- if (!this.paused) {
72
- resolve();
73
- } else {
74
- const interval = setInterval(() => {
75
- if (!this.paused) {
76
- clearInterval(interval);
77
- resolve();
78
- }
79
- }, 100);
80
- }
81
- });
82
- }
83
-
84
- private tcpSocketConnect(clientAddress: socket.NetAddress, serverAddress: socket.NetAddress,
85
- options: Object): Promise<void> {
86
-
87
- return new Promise((resolve, reject) => {
88
- this.tcpSocket = socket.constructTCPSocketInstance();
89
- this.tcpSocket.bind(clientAddress, (err: BusinessError) => {
90
- if (err) {
91
- Logger.info('bind fail:' + JSON.stringify(err));
92
- return;
93
- }
94
- Logger.info('TcpSocketClient bind success');
95
- let tcpConnect: socket.TCPConnectOptions = {} as socket.TCPConnectOptions;
96
- tcpConnect.address = serverAddress;
97
- tcpConnect.timeout = this.connectTimeout;
98
- this.tcpSocket?.connect(tcpConnect, (err: BusinessError) => {
99
- if (err) {
100
- Logger.info('TcpSocketClient connect fail');
101
- return;
102
- }
103
- this.tcpEvtListener?.onConnect(this.getId(), this);
104
- Logger.info('TcpSocketClient connect success');
105
- this.tcpExtraOptions.reuseAddress = options['reuseAddress'] ? true : false;
106
- this.tcpSocket?.setExtraOptions(this.tcpExtraOptions, (err: BusinessError) => {
107
- if (err) {
108
- Logger.info('TcpSocketClient tcpSocket setExtraOptions fail:' + err.message);
109
- return;
110
- }
111
- Logger.info('TcpSocketClient setExtraOptions success');
112
- });
113
- this.tcpSocket.on("close", () => {
114
- this.receiverListener?.onClose(this.getId(), "");
115
- });
116
- this.tcpSocket.on("message", (value: SocketInfo) => {
117
- let buffer = value.message;
118
- let base64Helper = new util.Base64Helper;
119
- let str = base64Helper.encodeToStringSync(new Uint8Array(buffer));
120
- this.waitForData();
121
- this.receiverListener?.onData(this.getId(), str);
122
- });
123
-
124
- this.tcpSocket.on('error', (err: BusinessError) => {
125
- if (err?.message) {
126
- this.receiverListener?.onError(this.getId(), err.message);
127
- }
128
- });
129
- resolve();
130
- })
131
- });
132
- });
133
- }
134
-
135
- private tlsSocketConnect(clientAddress: socket.NetAddress, serverAddress: socket.NetAddress,
136
- tlsOptions: Object): Promise<void> {
137
-
138
- return new Promise((resolve, reject) => {
139
- if (!this.tcpSocket) {
140
- this.receiverListener?.onError(this.cid, "tcpSocketClient is null");
141
- return;
142
- }
143
- let ca: string[] = tlsOptions["ca"];
144
- let key: string = tlsOptions["key"];
145
- let cert: string = tlsOptions["cert"];
146
- let androidKeyStore: string = tlsOptions["androidKeyStore"];
147
- let caAlias: string = tlsOptions["caAlias"];
148
- let keyAlias: string = tlsOptions["keyAlias"];
149
- let certAlias: string = tlsOptions["certAlias"];
150
- this.tlsSocket = socket.constructTLSSocketInstance(this.tcpSocket);
151
- let secureOptions: socket.TLSSecureOptions = {
152
- password: "",
153
- protocols: socket.Protocol.TLSv12,
154
- useRemoteCipherPrefer: true,
155
- signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256",
156
- cipherSuite: "AES256-SHA256"
157
- }
158
- if (key) {
159
- secureOptions.key = key
160
- }
161
- if (cert) {
162
- secureOptions.cert = cert
163
- }
164
- if (ca) {
165
- secureOptions.ca = ca
166
- }
167
- let tlsConnectOptions: socket.TLSConnectOptions = {
168
- address: serverAddress,
169
- secureOptions: secureOptions,
170
- ALPNProtocols: ["spdy/1", "http/1.1"]
171
- }
172
- this.tlsSocket.connect(tlsConnectOptions, (err: BusinessError) => {
173
- if (err) {
174
- Logger.error("=====1TcpSocketClient connect callback error" + JSON.stringify(err));
175
- }
176
- this.tlsSocket.on("close", () => {
177
- Logger.info("TcpSocketClient on close success");
178
- this.receiverListener?.onClose(this.getId(), "");
179
- });
180
- this.tlsSocket.on("message", (value: SocketInfo) => {
181
- let buffer = value.message;
182
- let base64Helper = new util.Base64Helper;
183
- let str = base64Helper.encodeToStringSync(new Uint8Array(buffer));
184
- this.waitForData();
185
- this.receiverListener?.onData(this.getId(), str);
186
- });
187
-
188
- this.tlsSocket.on('error', (err: BusinessError) => {
189
- this.receiverListener?.onError(this.getId(), err.message);
190
- });
191
- resolve();
192
- });
193
- });
194
- }
195
-
196
-
197
- async connect(host: string, port: number, options: Object, tlsOptions: Object) {
198
- let localAddress: string = options['localAddress'] ? options['localAddress'] : "0.0.0.0";
199
- let localPort: number = options['localPort'] ? options['localPort'] : 0;
200
- if (this.tcpSocket || this.tlsSocket || this.tcpSocketConnection || this.tlsSocketConnection) {
201
- throw new Error("TcpSocketClient Already connected");
202
- }
203
- let clientAddress: socket.NetAddress = {
204
- address: localAddress,
205
- port: localPort,
206
- family: 1
207
- }
208
- let serverAddress: socket.NetAddress = {
209
- address: host,
210
- port: port,
211
- family: 1
212
- }
213
- await this.tcpSocketConnect(clientAddress, serverAddress, options);
214
- if (tlsOptions) {
215
- await this.tlsSocketConnect(clientAddress, serverAddress, tlsOptions);
216
- }
217
- }
218
-
219
- async startTLS(tlsOptions: Object) {
220
- if (this.tlsSocket) {
221
- Logger.info("TlsSocketClient Already connected");
222
- return;
223
- }
224
- let remoteAddress = await this.tcpSocket?.getRemoteAddress() as socket.NetAddress;
225
- this.tlsSocket = socket.constructTLSSocketInstance(this.tcpSocket);
226
- let ca: string[] = tlsOptions["ca"];
227
- let key: string = tlsOptions["key"];
228
- let cert: string = tlsOptions["cert"];
229
-
230
- let secureOptions: socket.TLSSecureOptions = {
231
- password: "",
232
- protocols: socket.Protocol.TLSv12,
233
- useRemoteCipherPrefer: true,
234
- signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256",
235
- cipherSuite: "AES256-SHA256"
236
- }
237
- if (key) {
238
- secureOptions.key = key
239
- }
240
- if (cert) {
241
- secureOptions.cert = cert
242
- }
243
- if (ca) {
244
- secureOptions.ca = ca;
245
- }
246
- let serverAddress: socket.NetAddress = {
247
- address: remoteAddress.address,
248
- port: remoteAddress.port,
249
- family: remoteAddress.family
250
- }
251
- let tlsConnectOptions: socket.TLSConnectOptions = {
252
- address: serverAddress,
253
- secureOptions: secureOptions,
254
- ALPNProtocols: ["spdy/1", "http/1.1"]
255
- }
256
- this.tlsSocket.connect(tlsConnectOptions, (err: BusinessError) => {
257
- if (err) {
258
- Logger.error("TcpSocketClient startTLS connect callback error" + err.message);
259
- }
260
- this.tlsSocket.on("close", () => {
261
- this.receiverListener?.onClose(this.getId(), "");
262
- });
263
- this.tlsSocket.on("message", (value: SocketInfo) => {
264
- let buffer = value.message;
265
- let base64Helper = new util.Base64Helper;
266
- let str = base64Helper.encodeToStringSync(new Uint8Array(buffer));
267
- this.waitForData();
268
- this.receiverListener?.onData(this.getId(), str);
269
- });
270
-
271
- this.tlsSocket.on('error', (err: BusinessError) => {
272
- this.receiverListener?.onError(this.getId(), err.message);
273
- });
274
- });
275
- }
276
-
277
- write(msgId: number, data: ArrayBuffer) { //待实现多线程
278
-
279
- if (this.tcpSocketConnection) {
280
- let tcpSendOption: socket.TCPSendOptions = {
281
- data: data
282
- }
283
- this.tcpSocketConnection.send(tcpSendOption, (err: BusinessError) => {
284
- if (err) {
285
- this.receiverListener?.onWritten(this.getId(), msgId, err.message);
286
- this.receiverListener?.onError(this.getId(), err.message);
287
- }
288
- this.receiverListener?.onWritten(this.getId(), msgId, '');
289
- });
290
-
291
- } else if (this.tlsSocketConnection) {
292
- this.tlsSocketConnection.send(data, (err: BusinessError) => {
293
- if (err) {
294
- this.receiverListener?.onWritten(this.getId(), msgId, err.message);
295
- this.receiverListener?.onError(this.getId(), err.message);
296
- }
297
- this.receiverListener?.onWritten(this.getId(), msgId, '');
298
- });
299
-
300
- } else if (this.tlsSocket) {
301
- this.tlsSocket.send(data, (err: BusinessError) => {
302
- if (err) {
303
- this.receiverListener?.onWritten(this.getId(), msgId, err.message);
304
- this.receiverListener?.onError(this.getId(), err.message);
305
- }
306
- this.receiverListener?.onWritten(this.getId(), msgId, '');
307
- });
308
- } else {
309
- if (this.tcpSocket) {
310
- let tcpSendOptions: socket.TCPSendOptions = {
311
- data: data
312
- }
313
- this.tcpSocket.send(tcpSendOptions, (err: BusinessError) => {
314
- if (err) {
315
- this.receiverListener?.onWritten(this.getId(), msgId, err.message);
316
- this.receiverListener?.onError(this.getId(), err.message);
317
- return;
318
- }
319
- this.receiverListener?.onWritten(this.getId(), msgId, '');
320
- })
321
- } else {
322
- this.receiverListener?.onError(this.getId(), "Attempted to write to closed socket");
323
- return;
324
- }
325
- }
326
- }
327
-
328
- startListening() {
329
- if (this.tlsSocketConnection) {
330
- this.tlsSocketConnection.on("close", () => {
331
- this.receiverListener?.onClose(this.getId(), "");
332
- });
333
- this.tlsSocketConnection.on("message", (value: SocketInfo) => {
334
- let buffer = value.message;
335
- let base64Helper = new util.Base64Helper;
336
- let str = base64Helper.encodeToStringSync(new Uint8Array(buffer));
337
- this.waitForData();
338
- this.receiverListener?.onData(this.getId(), str);
339
- });
340
-
341
- this.tlsSocketConnection.on('error', (err: BusinessError) => {
342
- this.receiverListener?.onError(this.getId(), err.message);
343
- });
344
-
345
- }
346
- if (this.tcpSocketConnection) {
347
- this.tcpSocketConnection.on("close", () => {
348
- this.receiverListener?.onClose(this.getId(), "");
349
- });
350
- this.tcpSocketConnection.on("message", (value: SocketInfo) => {
351
- let buffer = value.message;
352
- let base64Helper = new util.Base64Helper;
353
- let str = base64Helper.encodeToStringSync(new Uint8Array(buffer));
354
- this.waitForData();
355
- this.receiverListener?.onData(this.getId(), str);
356
- });
357
-
358
- this.tcpSocketConnection.on('error', (err: BusinessError) => {
359
- this.receiverListener?.onError(this.getId(), err.message);
360
- });
361
- }
362
- }
363
-
364
- async getPeerCertificate(): Promise<string> {
365
- if (!this.tlsSocket) {
366
- this.receiverListener?.onError(this.getId(), "no peer certificate");
367
- return '';
368
- }
369
- let data: socket.X509CertRawData = await this.tlsSocket.getRemoteCertificate();
370
- let base64Helper = new util.Base64Helper;
371
- let result = base64Helper.encodeToStringSync(data.data);
372
- return result;
373
- }
374
-
375
- async getCertificate(): Promise<string> {
376
- if (!this.tlsSocket) {
377
- this.receiverListener?.onError(this.getId(), "no certificate");
378
- return '';
379
- }
380
- let data: socket.X509CertRawData = await this.tlsSocket.getCertificate();
381
- let base64Helper = new util.Base64Helper;
382
- let result = base64Helper.encodeToStringSync(data.data);
383
- return result;
384
- }
385
-
386
- destroy() {
387
- if (this.tlsSocketConnection) {
388
- this.tlsSocketConnection.close((err: BusinessError) => {
389
- if (err) {
390
- this.receiverListener?.onClose(this.getId(), err.message);
391
- return;
392
- }
393
- })
394
- } else if (this.tcpSocketConnection) {
395
- this.tcpSocketConnection.close((err: BusinessError) => {
396
- if (err) {
397
- this.receiverListener?.onClose(this.getId(), err.message);
398
- return;
399
- }
400
- })
401
- } else if (this.tlsSocket) {
402
- this.tlsSocket.close((err: BusinessError) => {
403
- if (err) {
404
- this.receiverListener?.onClose(this.getId(), err.message);
405
- return;
406
- }
407
- })
408
- } else if (this.tcpSocket) {
409
- this.tcpSocket.close((err: BusinessError) => {
410
- if (err) {
411
- this.receiverListener?.onClose(this.getId(), err.message);
412
- return;
413
- }
414
- })
415
- }
416
- this.receiverListener?.onClose(this.getId(), '');
417
- }
418
-
419
- setNoDelay(noDelay: boolean) {
420
- this.tcpExtraOptions.TCPNoDelay = noDelay
421
- if (this.tlsSocket) {
422
- this.tlsSocket?.setExtraOptions(this.tcpExtraOptions)
423
- } else {
424
- this.tcpSocket?.setExtraOptions(this.tcpExtraOptions)
425
- }
426
- }
427
-
428
- setKeepAlive(enable: boolean) {
429
- this.tcpExtraOptions.keepAlive = enable;
430
- if (this.tlsSocket) {
431
- this.tlsSocket?.setExtraOptions(this.tcpExtraOptions)
432
- } else {
433
- this.tcpSocket?.setExtraOptions(this.tcpExtraOptions)
434
- }
435
- }
436
-
437
- pause(): void {
438
- this.paused = true;
439
- }
440
-
441
- resume(): void {
442
- this.paused = false;
443
- }
1
+ /*
2
+ * Copyright (c) 2024 Huawei Device Co., Ltd. All rights reserved
3
+ * Use of this source code is governed by a MIT license that can be
4
+ * found in the LICENSE file.
5
+ */
6
+ import { TcpSocket } from './TcpSocket'
7
+ import { TcpEventListener } from "./TcpEventListener"
8
+ import { socket } from '@kit.NetworkKit';
9
+ import { BusinessError } from '@kit.BasicServicesKit';
10
+ import { util } from '@kit.ArkTS';
11
+ import Logger from './Logger'
12
+
13
+
14
+ interface SocketLinger {
15
+ on: boolean;
16
+ linger: number;
17
+ }
18
+
19
+ class SocketInfo {
20
+ message: ArrayBuffer = new ArrayBuffer(1);
21
+ remoteInfo: socket.SocketRemoteInfo = {} as socket.SocketRemoteInfo;
22
+ }
23
+
24
+ export class TcpSocketClient extends TcpSocket {
25
+ private receiverListener?: TcpEventListener;
26
+ private cid?: number;
27
+ private connectTimeout: number = 6000;
28
+ private tcpSocket?: socket.TCPSocket;
29
+ private tlsSocket?: socket.TLSSocket;
30
+ private tcpSocketConnection?: socket.TCPSocketConnection;
31
+ private tlsSocketConnection?: socket.TLSSocketConnection;
32
+ private paused: boolean = false;
33
+ private tcpExtraOptions: socket.TCPExtraOptions = {
34
+ keepAlive: false, //是否保持连接。默认为false
35
+ OOBInline: false, //是否为OOB内联。默认为false
36
+ TCPNoDelay: false, //TCPSocket连接是否无时延。默认为false
37
+ socketLinger: {
38
+ on: true,
39
+ linger: 10000
40
+ } as SocketLinger, //socket是否继续逗留。- on:是否逗留(true:逗留;false:不逗留)。- linger:逗留时长,单位毫秒(ms),取值范围为0~65535。当入参on设置为true时,才需要设置。
41
+ receiveBufferSize: 4096, //接收缓冲区大小(单位:Byte),默认为0
42
+ sendBufferSize: 4096, //发送缓冲区大小(单位:Byte),默认为0。
43
+ reuseAddress: false, //是否重用地址。默认为false。
44
+ socketTimeout: 30000 //套接字超时时间,单位毫秒(ms),默认为0。
45
+ }
46
+ tcpEvtListener: any;
47
+
48
+ constructor(receiverListener: TcpEventListener, cid: number,
49
+ tcpSocketConnection?: socket.TCPSocketConnection, tlsSocketConnection?: socket.TLSSocketConnection) {
50
+ super(cid);
51
+ this.receiverListener = receiverListener;
52
+ this.cid = cid;
53
+ this.tcpSocketConnection = tcpSocketConnection;
54
+ this.tlsSocketConnection = tlsSocketConnection;
55
+ }
56
+
57
+ getSocket(): socket.TCPSocket | socket.TLSSocket | socket.TCPSocketConnection | socket.TLSSocketConnection | undefined {
58
+ if (this.tcpSocketConnection) {
59
+ return this.tcpSocketConnection;
60
+ } else if (this.tlsSocketConnection) {
61
+ return this.tlsSocketConnection;
62
+ } else if (this.tlsSocket) {
63
+ return this.tlsSocket;
64
+ } else {
65
+ return this.tcpSocket;
66
+ }
67
+ }
68
+
69
+ private waitForData() {
70
+ return new Promise<void>(resolve => {
71
+ if (!this.paused) {
72
+ resolve();
73
+ } else {
74
+ const interval = setInterval(() => {
75
+ if (!this.paused) {
76
+ clearInterval(interval);
77
+ resolve();
78
+ }
79
+ }, 100);
80
+ }
81
+ });
82
+ }
83
+
84
+ private tcpSocketConnect(clientAddress: socket.NetAddress, serverAddress: socket.NetAddress,
85
+ options: Object): Promise<void> {
86
+
87
+ return new Promise((resolve, reject) => {
88
+ this.tcpSocket = socket.constructTCPSocketInstance();
89
+ this.tcpSocket.bind(clientAddress, (err: BusinessError) => {
90
+ if (err) {
91
+ Logger.info('bind fail:' + JSON.stringify(err));
92
+ return;
93
+ }
94
+ Logger.info('TcpSocketClient bind success');
95
+ let tcpConnect: socket.TCPConnectOptions = {} as socket.TCPConnectOptions;
96
+ tcpConnect.address = serverAddress;
97
+ tcpConnect.timeout = this.connectTimeout;
98
+ this.tcpSocket?.connect(tcpConnect, (err: BusinessError) => {
99
+ if (err) {
100
+ Logger.info('TcpSocketClient connect fail');
101
+ return;
102
+ }
103
+ this.tcpEvtListener?.onConnect(this.getId(), this);
104
+ Logger.info('TcpSocketClient connect success');
105
+ this.tcpExtraOptions.reuseAddress = options['reuseAddress'] ? true : false;
106
+ this.tcpSocket?.setExtraOptions(this.tcpExtraOptions, (err: BusinessError) => {
107
+ if (err) {
108
+ Logger.info('TcpSocketClient tcpSocket setExtraOptions fail:' + err.message);
109
+ return;
110
+ }
111
+ Logger.info('TcpSocketClient setExtraOptions success');
112
+ });
113
+ this.tcpSocket.on("close", () => {
114
+ this.receiverListener?.onClose(this.getId(), "");
115
+ });
116
+ this.tcpSocket.on("message", (value: SocketInfo) => {
117
+ let buffer = value.message;
118
+ let base64Helper = new util.Base64Helper;
119
+ let str = base64Helper.encodeToStringSync(new Uint8Array(buffer));
120
+ this.waitForData();
121
+ this.receiverListener?.onData(this.getId(), str);
122
+ });
123
+
124
+ this.tcpSocket.on('error', (err: BusinessError) => {
125
+ if (err?.message) {
126
+ this.receiverListener?.onError(this.getId(), err.message);
127
+ }
128
+ });
129
+ resolve();
130
+ })
131
+ });
132
+ });
133
+ }
134
+
135
+ private tlsSocketConnect(clientAddress: socket.NetAddress, serverAddress: socket.NetAddress,
136
+ tlsOptions: Object): Promise<void> {
137
+
138
+ return new Promise((resolve, reject) => {
139
+ if (!this.tcpSocket) {
140
+ this.receiverListener?.onError(this.cid, "tcpSocketClient is null");
141
+ return;
142
+ }
143
+ let ca: string[] = tlsOptions["ca"];
144
+ let key: string = tlsOptions["key"];
145
+ let cert: string = tlsOptions["cert"];
146
+ let androidKeyStore: string = tlsOptions["androidKeyStore"];
147
+ let caAlias: string = tlsOptions["caAlias"];
148
+ let keyAlias: string = tlsOptions["keyAlias"];
149
+ let certAlias: string = tlsOptions["certAlias"];
150
+ this.tlsSocket = socket.constructTLSSocketInstance(this.tcpSocket);
151
+ let secureOptions: socket.TLSSecureOptions = {
152
+ password: "",
153
+ protocols: socket.Protocol.TLSv12,
154
+ useRemoteCipherPrefer: true,
155
+ signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256",
156
+ cipherSuite: "AES256-SHA256"
157
+ }
158
+ if (key) {
159
+ secureOptions.key = key
160
+ }
161
+ if (cert) {
162
+ secureOptions.cert = cert
163
+ }
164
+ if (ca) {
165
+ secureOptions.ca = ca
166
+ }
167
+ let tlsConnectOptions: socket.TLSConnectOptions = {
168
+ address: serverAddress,
169
+ secureOptions: secureOptions,
170
+ ALPNProtocols: ["spdy/1", "http/1.1"]
171
+ }
172
+ this.tlsSocket.connect(tlsConnectOptions, (err: BusinessError) => {
173
+ if (err) {
174
+ Logger.error("=====1TcpSocketClient connect callback error" + JSON.stringify(err));
175
+ }
176
+ this.tlsSocket.on("close", () => {
177
+ Logger.info("TcpSocketClient on close success");
178
+ this.receiverListener?.onClose(this.getId(), "");
179
+ });
180
+ this.tlsSocket.on("message", (value: SocketInfo) => {
181
+ let buffer = value.message;
182
+ let base64Helper = new util.Base64Helper;
183
+ let str = base64Helper.encodeToStringSync(new Uint8Array(buffer));
184
+ this.waitForData();
185
+ this.receiverListener?.onData(this.getId(), str);
186
+ });
187
+
188
+ this.tlsSocket.on('error', (err: BusinessError) => {
189
+ this.receiverListener?.onError(this.getId(), err.message);
190
+ });
191
+ resolve();
192
+ });
193
+ });
194
+ }
195
+
196
+
197
+ async connect(host: string, port: number, options: Object, tlsOptions: Object) {
198
+ let localAddress: string = options['localAddress'] ? options['localAddress'] : "0.0.0.0";
199
+ let localPort: number = options['localPort'] ? options['localPort'] : 0;
200
+ if (this.tcpSocket || this.tlsSocket || this.tcpSocketConnection || this.tlsSocketConnection) {
201
+ throw new Error("TcpSocketClient Already connected");
202
+ }
203
+ let clientAddress: socket.NetAddress = {
204
+ address: localAddress,
205
+ port: localPort,
206
+ family: 1
207
+ }
208
+ let serverAddress: socket.NetAddress = {
209
+ address: host,
210
+ port: port,
211
+ family: 1
212
+ }
213
+ await this.tcpSocketConnect(clientAddress, serverAddress, options);
214
+ if (tlsOptions) {
215
+ await this.tlsSocketConnect(clientAddress, serverAddress, tlsOptions);
216
+ }
217
+ }
218
+
219
+ async startTLS(tlsOptions: Object) {
220
+ if (this.tlsSocket) {
221
+ Logger.info("TlsSocketClient Already connected");
222
+ return;
223
+ }
224
+ let remoteAddress = await this.tcpSocket?.getRemoteAddress() as socket.NetAddress;
225
+ this.tlsSocket = socket.constructTLSSocketInstance(this.tcpSocket);
226
+ let ca: string[] = tlsOptions["ca"];
227
+ let key: string = tlsOptions["key"];
228
+ let cert: string = tlsOptions["cert"];
229
+
230
+ let secureOptions: socket.TLSSecureOptions = {
231
+ password: "",
232
+ protocols: socket.Protocol.TLSv12,
233
+ useRemoteCipherPrefer: true,
234
+ signatureAlgorithms: "rsa_pss_rsae_sha256:ECDSA+SHA256",
235
+ cipherSuite: "AES256-SHA256"
236
+ }
237
+ if (key) {
238
+ secureOptions.key = key
239
+ }
240
+ if (cert) {
241
+ secureOptions.cert = cert
242
+ }
243
+ if (ca) {
244
+ secureOptions.ca = ca;
245
+ }
246
+ let serverAddress: socket.NetAddress = {
247
+ address: remoteAddress.address,
248
+ port: remoteAddress.port,
249
+ family: remoteAddress.family
250
+ }
251
+ let tlsConnectOptions: socket.TLSConnectOptions = {
252
+ address: serverAddress,
253
+ secureOptions: secureOptions,
254
+ ALPNProtocols: ["spdy/1", "http/1.1"]
255
+ }
256
+ this.tlsSocket.connect(tlsConnectOptions, (err: BusinessError) => {
257
+ if (err) {
258
+ Logger.error("TcpSocketClient startTLS connect callback error" + err.message);
259
+ }
260
+ this.tlsSocket.on("close", () => {
261
+ this.receiverListener?.onClose(this.getId(), "");
262
+ });
263
+ this.tlsSocket.on("message", (value: SocketInfo) => {
264
+ let buffer = value.message;
265
+ let base64Helper = new util.Base64Helper;
266
+ let str = base64Helper.encodeToStringSync(new Uint8Array(buffer));
267
+ this.waitForData();
268
+ this.receiverListener?.onData(this.getId(), str);
269
+ });
270
+
271
+ this.tlsSocket.on('error', (err: BusinessError) => {
272
+ this.receiverListener?.onError(this.getId(), err.message);
273
+ });
274
+ });
275
+ }
276
+
277
+ write(msgId: number, data: ArrayBuffer) { //待实现多线程
278
+
279
+ if (this.tcpSocketConnection) {
280
+ let tcpSendOption: socket.TCPSendOptions = {
281
+ data: data
282
+ }
283
+ this.tcpSocketConnection.send(tcpSendOption, (err: BusinessError) => {
284
+ if (err) {
285
+ this.receiverListener?.onWritten(this.getId(), msgId, err.message);
286
+ this.receiverListener?.onError(this.getId(), err.message);
287
+ }
288
+ this.receiverListener?.onWritten(this.getId(), msgId, '');
289
+ });
290
+
291
+ } else if (this.tlsSocketConnection) {
292
+ this.tlsSocketConnection.send(data, (err: BusinessError) => {
293
+ if (err) {
294
+ this.receiverListener?.onWritten(this.getId(), msgId, err.message);
295
+ this.receiverListener?.onError(this.getId(), err.message);
296
+ }
297
+ this.receiverListener?.onWritten(this.getId(), msgId, '');
298
+ });
299
+
300
+ } else if (this.tlsSocket) {
301
+ this.tlsSocket.send(data, (err: BusinessError) => {
302
+ if (err) {
303
+ this.receiverListener?.onWritten(this.getId(), msgId, err.message);
304
+ this.receiverListener?.onError(this.getId(), err.message);
305
+ }
306
+ this.receiverListener?.onWritten(this.getId(), msgId, '');
307
+ });
308
+ } else {
309
+ if (this.tcpSocket) {
310
+ let tcpSendOptions: socket.TCPSendOptions = {
311
+ data: data
312
+ }
313
+ this.tcpSocket.send(tcpSendOptions, (err: BusinessError) => {
314
+ if (err) {
315
+ this.receiverListener?.onWritten(this.getId(), msgId, err.message);
316
+ this.receiverListener?.onError(this.getId(), err.message);
317
+ return;
318
+ }
319
+ this.receiverListener?.onWritten(this.getId(), msgId, '');
320
+ })
321
+ } else {
322
+ this.receiverListener?.onError(this.getId(), "Attempted to write to closed socket");
323
+ return;
324
+ }
325
+ }
326
+ }
327
+
328
+ startListening() {
329
+ if (this.tlsSocketConnection) {
330
+ this.tlsSocketConnection.on("close", () => {
331
+ this.receiverListener?.onClose(this.getId(), "");
332
+ });
333
+ this.tlsSocketConnection.on("message", (value: SocketInfo) => {
334
+ let buffer = value.message;
335
+ let base64Helper = new util.Base64Helper;
336
+ let str = base64Helper.encodeToStringSync(new Uint8Array(buffer));
337
+ this.waitForData();
338
+ this.receiverListener?.onData(this.getId(), str);
339
+ });
340
+
341
+ this.tlsSocketConnection.on('error', (err: BusinessError) => {
342
+ this.receiverListener?.onError(this.getId(), err.message);
343
+ });
344
+
345
+ }
346
+ if (this.tcpSocketConnection) {
347
+ this.tcpSocketConnection.on("close", () => {
348
+ this.receiverListener?.onClose(this.getId(), "");
349
+ });
350
+ this.tcpSocketConnection.on("message", (value: SocketInfo) => {
351
+ let buffer = value.message;
352
+ let base64Helper = new util.Base64Helper;
353
+ let str = base64Helper.encodeToStringSync(new Uint8Array(buffer));
354
+ this.waitForData();
355
+ this.receiverListener?.onData(this.getId(), str);
356
+ });
357
+
358
+ this.tcpSocketConnection.on('error', (err: BusinessError) => {
359
+ this.receiverListener?.onError(this.getId(), err.message);
360
+ });
361
+ }
362
+ }
363
+
364
+ async getPeerCertificate(): Promise<string> {
365
+ if (!this.tlsSocket) {
366
+ this.receiverListener?.onError(this.getId(), "no peer certificate");
367
+ return '';
368
+ }
369
+ let data: socket.X509CertRawData = await this.tlsSocket.getRemoteCertificate();
370
+ let base64Helper = new util.Base64Helper;
371
+ let result = base64Helper.encodeToStringSync(data.data);
372
+ return result;
373
+ }
374
+
375
+ async getCertificate(): Promise<string> {
376
+ if (!this.tlsSocket) {
377
+ this.receiverListener?.onError(this.getId(), "no certificate");
378
+ return '';
379
+ }
380
+ let data: socket.X509CertRawData = await this.tlsSocket.getCertificate();
381
+ let base64Helper = new util.Base64Helper;
382
+ let result = base64Helper.encodeToStringSync(data.data);
383
+ return result;
384
+ }
385
+
386
+ destroy() {
387
+ if (this.tlsSocketConnection) {
388
+ this.tlsSocketConnection.close((err: BusinessError) => {
389
+ if (err) {
390
+ this.receiverListener?.onClose(this.getId(), err.message);
391
+ return;
392
+ }
393
+ })
394
+ } else if (this.tcpSocketConnection) {
395
+ this.tcpSocketConnection.close((err: BusinessError) => {
396
+ if (err) {
397
+ this.receiverListener?.onClose(this.getId(), err.message);
398
+ return;
399
+ }
400
+ })
401
+ } else if (this.tlsSocket) {
402
+ this.tlsSocket.close((err: BusinessError) => {
403
+ if (err) {
404
+ this.receiverListener?.onClose(this.getId(), err.message);
405
+ return;
406
+ }
407
+ })
408
+ } else if (this.tcpSocket) {
409
+ this.tcpSocket.close((err: BusinessError) => {
410
+ if (err) {
411
+ this.receiverListener?.onClose(this.getId(), err.message);
412
+ return;
413
+ }
414
+ })
415
+ }
416
+ this.receiverListener?.onClose(this.getId(), '');
417
+ }
418
+
419
+ setNoDelay(noDelay: boolean) {
420
+ this.tcpExtraOptions.TCPNoDelay = noDelay
421
+ if (this.tlsSocket) {
422
+ this.tlsSocket?.setExtraOptions(this.tcpExtraOptions)
423
+ } else {
424
+ this.tcpSocket?.setExtraOptions(this.tcpExtraOptions)
425
+ }
426
+ }
427
+
428
+ setKeepAlive(enable: boolean) {
429
+ this.tcpExtraOptions.keepAlive = enable;
430
+ if (this.tlsSocket) {
431
+ this.tlsSocket?.setExtraOptions(this.tcpExtraOptions)
432
+ } else {
433
+ this.tcpSocket?.setExtraOptions(this.tcpExtraOptions)
434
+ }
435
+ }
436
+
437
+ pause(): void {
438
+ this.paused = true;
439
+ }
440
+
441
+ resume(): void {
442
+ this.paused = false;
443
+ }
444
444
  }