@react-native-ohos/react-native-tcp-socket 6.2.1-rc.1 → 6.2.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.
- package/LICENSE +21 -21
- package/README.OpenSource +10 -10
- package/README.md +12 -12
- package/harmony/tcp_socket/Index.ets +8 -8
- package/harmony/tcp_socket/build-profile.json5 +31 -31
- package/harmony/tcp_socket/hvigorfile.ts +6 -6
- package/harmony/tcp_socket/obfuscation-rules.txt +22 -22
- package/harmony/tcp_socket/oh-package.json5 +11 -11
- package/harmony/tcp_socket/src/main/cpp/CMakeLists.txt +10 -10
- package/harmony/tcp_socket/src/main/cpp/TcpSocketPackage.h +26 -26
- package/harmony/tcp_socket/src/main/cpp/generated/RNOH/generated/BaseReactNativeTcpSocketPackage.h +65 -65
- package/harmony/tcp_socket/src/main/cpp/generated/RNOH/generated/turbo_modules/TcpSocketModule.cpp +28 -28
- package/harmony/tcp_socket/src/main/cpp/generated/RNOH/generated/turbo_modules/TcpSocketModule.h +16 -16
- package/harmony/tcp_socket/src/main/cpp/generated/react/renderer/components/react_native_tcp_socket/ComponentDescriptors.h +22 -22
- package/harmony/tcp_socket/src/main/cpp/generated/react/renderer/components/react_native_tcp_socket/EventEmitters.cpp +18 -18
- package/harmony/tcp_socket/src/main/cpp/generated/react/renderer/components/react_native_tcp_socket/EventEmitters.h +19 -19
- package/harmony/tcp_socket/src/main/cpp/generated/react/renderer/components/react_native_tcp_socket/Props.cpp +21 -21
- package/harmony/tcp_socket/src/main/cpp/generated/react/renderer/components/react_native_tcp_socket/Props.h +20 -20
- package/harmony/tcp_socket/src/main/cpp/generated/react/renderer/components/react_native_tcp_socket/ShadowNodes.cpp +19 -19
- package/harmony/tcp_socket/src/main/cpp/generated/react/renderer/components/react_native_tcp_socket/ShadowNodes.h +25 -25
- package/harmony/tcp_socket/src/main/cpp/generated/react/renderer/components/react_native_tcp_socket/States.cpp +18 -18
- package/harmony/tcp_socket/src/main/cpp/generated/react/renderer/components/react_native_tcp_socket/States.h +22 -22
- package/harmony/tcp_socket/src/main/ets/Logger.ts +45 -45
- package/harmony/tcp_socket/src/main/ets/TcpEventListener.ts +120 -120
- package/harmony/tcp_socket/src/main/ets/TcpSocket.ts +16 -16
- package/harmony/tcp_socket/src/main/ets/TcpSocketClient.ts +443 -443
- package/harmony/tcp_socket/src/main/ets/TcpSocketPackage.ets +27 -27
- package/harmony/tcp_socket/src/main/ets/TcpSocketServer.ts +150 -150
- package/harmony/tcp_socket/src/main/ets/TcpSocketTurboModule.ts +224 -224
- package/harmony/tcp_socket/src/main/ets/generated/components/ts.ts +5 -5
- package/harmony/tcp_socket/src/main/ets/generated/index.ets +5 -5
- package/harmony/tcp_socket/src/main/ets/generated/ts.ts +6 -6
- package/harmony/tcp_socket/src/main/ets/generated/turboModules/TcpSocketModule.ts +38 -38
- package/harmony/tcp_socket/src/main/ets/generated/turboModules/ts.ts +5 -5
- package/harmony/tcp_socket/src/main/module.json5 +11 -11
- package/harmony/tcp_socket/src/main/resources/base/element/string.json +8 -8
- package/harmony/tcp_socket/src/main/resources/en_US/element/string.json +8 -8
- package/harmony/tcp_socket/src/main/resources/zh_CN/element/string.json +8 -8
- package/harmony/tcp_socket/src/ohosTest/ets/test/Ability.test.ets +34 -34
- package/harmony/tcp_socket/src/ohosTest/ets/test/List.test.ets +4 -4
- package/harmony/tcp_socket/src/ohosTest/module.json5 +13 -13
- package/harmony/tcp_socket/src/test/List.test.ets +4 -4
- package/harmony/tcp_socket/src/test/LocalUnit.test.ets +32 -32
- package/harmony/tcp_socket/ts.ets +6 -6
- package/harmony/tcp_socket.har +0 -0
- package/lib/types/Globals.d.ts +1 -1
- package/lib/types/Server.d.ts +3 -3
- package/lib/types/Socket.d.ts +15 -11
- package/lib/types/TcpSocketModule.d.ts +18 -0
- package/package.json +90 -90
- package/src/Globals.js +12 -12
- package/src/Server.js +185 -185
- package/src/Socket.js +513 -513
- package/src/TLSServer.js +70 -70
- package/src/TLSSocket.js +93 -93
- package/src/TcpSocketModule.ts +39 -39
- package/src/index.js +131 -131
package/src/Server.js
CHANGED
|
@@ -1,185 +1,185 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
import { TurboModuleRegistry, NativeModules } from 'react-native';
|
|
4
|
-
import EventEmitter from 'eventemitter3';
|
|
5
|
-
import Socket from './Socket';
|
|
6
|
-
import { nativeEventEmitter, getNextId } from './Globals';
|
|
7
|
-
|
|
8
|
-
const Sockets = TurboModuleRegistry ? TurboModuleRegistry.get('TcpSocketModule') : NativeModules.TcpSockets;
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* @typedef {import('./TLSSocket').default} TLSSocket
|
|
12
|
-
*
|
|
13
|
-
* @typedef {object} ServerEvents
|
|
14
|
-
* @property {() => void} close
|
|
15
|
-
* @property {(socket: Socket) => void} connection
|
|
16
|
-
* @property {() => void} listening
|
|
17
|
-
* @property {(err: Error) => void} error
|
|
18
|
-
* @property {(tlsSocket: TLSSocket) => void} secureConnection
|
|
19
|
-
*
|
|
20
|
-
* @extends {EventEmitter<ServerEvents, any>}
|
|
21
|
-
*/
|
|
22
|
-
export default class Server extends EventEmitter {
|
|
23
|
-
/**
|
|
24
|
-
* @param {(socket: Socket) => void} [connectionCallback] Automatically set as a listener for the `'connection'` event.
|
|
25
|
-
*/
|
|
26
|
-
constructor(connectionCallback) {
|
|
27
|
-
super();
|
|
28
|
-
/** @protected @readonly */
|
|
29
|
-
this._id = getNextId();
|
|
30
|
-
/** @protected @readonly */
|
|
31
|
-
this._eventEmitter = nativeEventEmitter;
|
|
32
|
-
/** @private @type {Set<Socket>} */
|
|
33
|
-
this._connections = new Set();
|
|
34
|
-
/** @private */
|
|
35
|
-
this._localAddress = undefined;
|
|
36
|
-
/** @private */
|
|
37
|
-
this._localPort = undefined;
|
|
38
|
-
/** @private */
|
|
39
|
-
this._localFamily = undefined;
|
|
40
|
-
this.listening = false;
|
|
41
|
-
this._registerEvents();
|
|
42
|
-
if (connectionCallback) this.on('connection', connectionCallback);
|
|
43
|
-
this.on('close', this._setDisconnected, this);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Start a server listening for connections.
|
|
48
|
-
*
|
|
49
|
-
* This function is asynchronous. When the server starts listening, the `'listening'` event will be emitted.
|
|
50
|
-
* The last parameter `callback` will be added as a listener for the `'listening'` event.
|
|
51
|
-
*
|
|
52
|
-
* The `server.listen()` method can be called again if and only if there was an error during the first
|
|
53
|
-
* `server.listen()` call or `server.close()` has been called. Otherwise, an `ERR_SERVER_ALREADY_LISTEN`
|
|
54
|
-
* error will be thrown.
|
|
55
|
-
*
|
|
56
|
-
* @param {{ port: number; host: string; reuseAddress?: boolean}} options
|
|
57
|
-
* @param {() => void} [callback]
|
|
58
|
-
* @returns {Server}
|
|
59
|
-
*/
|
|
60
|
-
listen(options, callback) {
|
|
61
|
-
if (this._localAddress !== undefined) throw new Error('ERR_SERVER_ALREADY_LISTEN');
|
|
62
|
-
const gotOptions = { ...options };
|
|
63
|
-
gotOptions.host = gotOptions.host || '0.0.0.0';
|
|
64
|
-
this.once('listening', () => {
|
|
65
|
-
this.listening = true;
|
|
66
|
-
if (callback) callback();
|
|
67
|
-
});
|
|
68
|
-
Sockets.listen(this._id, gotOptions);
|
|
69
|
-
return this;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Asynchronously get the number of concurrent connections on the server.
|
|
74
|
-
*
|
|
75
|
-
* Callback should take two arguments `err` and `count`.
|
|
76
|
-
*
|
|
77
|
-
* @param {(err: Error | null, count: number) => void} callback
|
|
78
|
-
* @returns {Server}
|
|
79
|
-
*/
|
|
80
|
-
getConnections(callback) {
|
|
81
|
-
callback(null, this._connections.size);
|
|
82
|
-
return this;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Stops the server from accepting new connections and keeps existing connections.
|
|
87
|
-
* This function is asynchronous, the server is finally closed when all connections are ended and the server emits a `'close'` event.
|
|
88
|
-
* The optional callback will be called once the `'close'` event occurs. Unlike that event, it will be called with an `Error` as its
|
|
89
|
-
* only argument if the server was not open when it was closed.
|
|
90
|
-
*
|
|
91
|
-
* @param {(err?: Error) => void} [callback] Called when the server is closed.
|
|
92
|
-
* @returns {Server}
|
|
93
|
-
*/
|
|
94
|
-
close(callback) {
|
|
95
|
-
if (!this._localAddress) {
|
|
96
|
-
callback?.(new Error('ERR_SERVER_NOT_RUNNING'));
|
|
97
|
-
return this;
|
|
98
|
-
}
|
|
99
|
-
if (callback) this.once('close', callback);
|
|
100
|
-
this.listening = false;
|
|
101
|
-
Sockets.close(this._id);
|
|
102
|
-
if (this._connections.size === 0) this.emit('close');
|
|
103
|
-
return this;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Returns the bound `address`, the address `family` name, and `port` of the server as reported by the operating system if listening
|
|
108
|
-
* on an IP socket (useful to find which port was assigned when getting an OS-assigned address):
|
|
109
|
-
* `{ port: 12346, family: 'IPv4', address: '127.0.0.1' }`.
|
|
110
|
-
*
|
|
111
|
-
* @returns {import('./Socket').AddressInfo | null}
|
|
112
|
-
*/
|
|
113
|
-
address() {
|
|
114
|
-
if (!this._localAddress) return null;
|
|
115
|
-
return { address: this._localAddress, port: this._localPort, family: this._localFamily };
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
ref() {
|
|
119
|
-
console.warn('react-native-tcp-socket: Server.ref() method will have no effect.');
|
|
120
|
-
return this;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
unref() {
|
|
124
|
-
console.warn('react-native-tcp-socket: Server.unref() method will have no effect.');
|
|
125
|
-
return this;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* @private
|
|
130
|
-
*/
|
|
131
|
-
_registerEvents() {
|
|
132
|
-
this._listeningListener = this._eventEmitter.addListener('listening', (evt) => {
|
|
133
|
-
if (evt.id !== this._id) return;
|
|
134
|
-
this._localAddress = evt.connection.localAddress;
|
|
135
|
-
this._localPort = evt.connection.localPort;
|
|
136
|
-
this._localFamily = evt.connection.localFamily;
|
|
137
|
-
this.emit('listening');
|
|
138
|
-
});
|
|
139
|
-
this._errorListener = this._eventEmitter.addListener('error', (evt) => {
|
|
140
|
-
if (evt.id !== this._id) return;
|
|
141
|
-
this.close();
|
|
142
|
-
this.emit('error', evt.error);
|
|
143
|
-
});
|
|
144
|
-
this._connectionsListener = this._eventEmitter.addListener('connection', (evt) => {
|
|
145
|
-
if (evt.id !== this._id) return;
|
|
146
|
-
const newSocket = this._buildSocket(evt.info);
|
|
147
|
-
this._addConnection(newSocket);
|
|
148
|
-
this.emit('connection', newSocket);
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* @private
|
|
154
|
-
*/
|
|
155
|
-
_setDisconnected() {
|
|
156
|
-
this._localAddress = undefined;
|
|
157
|
-
this._localPort = undefined;
|
|
158
|
-
this._localFamily = undefined;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
* @protected
|
|
163
|
-
* @param {Socket} socket
|
|
164
|
-
*/
|
|
165
|
-
_addConnection(socket) {
|
|
166
|
-
// Emit 'close' when all connection closed
|
|
167
|
-
socket.on('close', () => {
|
|
168
|
-
this._connections.delete(socket);
|
|
169
|
-
if (!this.listening && this._connections.size === 0) this.emit('close');
|
|
170
|
-
});
|
|
171
|
-
this._connections.add(socket);
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* @protected
|
|
176
|
-
* @param {{ id: number; connection: import('./Socket').NativeConnectionInfo; }} info
|
|
177
|
-
* @returns {Socket}
|
|
178
|
-
*/
|
|
179
|
-
_buildSocket(info) {
|
|
180
|
-
const newSocket = new Socket();
|
|
181
|
-
newSocket._setId(info.id);
|
|
182
|
-
newSocket._setConnected(info.connection);
|
|
183
|
-
return newSocket;
|
|
184
|
-
}
|
|
185
|
-
}
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
import { TurboModuleRegistry, NativeModules } from 'react-native';
|
|
4
|
+
import EventEmitter from 'eventemitter3';
|
|
5
|
+
import Socket from './Socket';
|
|
6
|
+
import { nativeEventEmitter, getNextId } from './Globals';
|
|
7
|
+
|
|
8
|
+
const Sockets = TurboModuleRegistry ? TurboModuleRegistry.get('TcpSocketModule') : NativeModules.TcpSockets;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @typedef {import('./TLSSocket').default} TLSSocket
|
|
12
|
+
*
|
|
13
|
+
* @typedef {object} ServerEvents
|
|
14
|
+
* @property {() => void} close
|
|
15
|
+
* @property {(socket: Socket) => void} connection
|
|
16
|
+
* @property {() => void} listening
|
|
17
|
+
* @property {(err: Error) => void} error
|
|
18
|
+
* @property {(tlsSocket: TLSSocket) => void} secureConnection
|
|
19
|
+
*
|
|
20
|
+
* @extends {EventEmitter<ServerEvents, any>}
|
|
21
|
+
*/
|
|
22
|
+
export default class Server extends EventEmitter {
|
|
23
|
+
/**
|
|
24
|
+
* @param {(socket: Socket) => void} [connectionCallback] Automatically set as a listener for the `'connection'` event.
|
|
25
|
+
*/
|
|
26
|
+
constructor(connectionCallback) {
|
|
27
|
+
super();
|
|
28
|
+
/** @protected @readonly */
|
|
29
|
+
this._id = getNextId();
|
|
30
|
+
/** @protected @readonly */
|
|
31
|
+
this._eventEmitter = nativeEventEmitter;
|
|
32
|
+
/** @private @type {Set<Socket>} */
|
|
33
|
+
this._connections = new Set();
|
|
34
|
+
/** @private */
|
|
35
|
+
this._localAddress = undefined;
|
|
36
|
+
/** @private */
|
|
37
|
+
this._localPort = undefined;
|
|
38
|
+
/** @private */
|
|
39
|
+
this._localFamily = undefined;
|
|
40
|
+
this.listening = false;
|
|
41
|
+
this._registerEvents();
|
|
42
|
+
if (connectionCallback) this.on('connection', connectionCallback);
|
|
43
|
+
this.on('close', this._setDisconnected, this);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Start a server listening for connections.
|
|
48
|
+
*
|
|
49
|
+
* This function is asynchronous. When the server starts listening, the `'listening'` event will be emitted.
|
|
50
|
+
* The last parameter `callback` will be added as a listener for the `'listening'` event.
|
|
51
|
+
*
|
|
52
|
+
* The `server.listen()` method can be called again if and only if there was an error during the first
|
|
53
|
+
* `server.listen()` call or `server.close()` has been called. Otherwise, an `ERR_SERVER_ALREADY_LISTEN`
|
|
54
|
+
* error will be thrown.
|
|
55
|
+
*
|
|
56
|
+
* @param {{ port: number; host: string; reuseAddress?: boolean}} options
|
|
57
|
+
* @param {() => void} [callback]
|
|
58
|
+
* @returns {Server}
|
|
59
|
+
*/
|
|
60
|
+
listen(options, callback) {
|
|
61
|
+
if (this._localAddress !== undefined) throw new Error('ERR_SERVER_ALREADY_LISTEN');
|
|
62
|
+
const gotOptions = { ...options };
|
|
63
|
+
gotOptions.host = gotOptions.host || '0.0.0.0';
|
|
64
|
+
this.once('listening', () => {
|
|
65
|
+
this.listening = true;
|
|
66
|
+
if (callback) callback();
|
|
67
|
+
});
|
|
68
|
+
Sockets.listen(this._id, gotOptions);
|
|
69
|
+
return this;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Asynchronously get the number of concurrent connections on the server.
|
|
74
|
+
*
|
|
75
|
+
* Callback should take two arguments `err` and `count`.
|
|
76
|
+
*
|
|
77
|
+
* @param {(err: Error | null, count: number) => void} callback
|
|
78
|
+
* @returns {Server}
|
|
79
|
+
*/
|
|
80
|
+
getConnections(callback) {
|
|
81
|
+
callback(null, this._connections.size);
|
|
82
|
+
return this;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Stops the server from accepting new connections and keeps existing connections.
|
|
87
|
+
* This function is asynchronous, the server is finally closed when all connections are ended and the server emits a `'close'` event.
|
|
88
|
+
* The optional callback will be called once the `'close'` event occurs. Unlike that event, it will be called with an `Error` as its
|
|
89
|
+
* only argument if the server was not open when it was closed.
|
|
90
|
+
*
|
|
91
|
+
* @param {(err?: Error) => void} [callback] Called when the server is closed.
|
|
92
|
+
* @returns {Server}
|
|
93
|
+
*/
|
|
94
|
+
close(callback) {
|
|
95
|
+
if (!this._localAddress) {
|
|
96
|
+
callback?.(new Error('ERR_SERVER_NOT_RUNNING'));
|
|
97
|
+
return this;
|
|
98
|
+
}
|
|
99
|
+
if (callback) this.once('close', callback);
|
|
100
|
+
this.listening = false;
|
|
101
|
+
Sockets.close(this._id);
|
|
102
|
+
if (this._connections.size === 0) this.emit('close');
|
|
103
|
+
return this;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Returns the bound `address`, the address `family` name, and `port` of the server as reported by the operating system if listening
|
|
108
|
+
* on an IP socket (useful to find which port was assigned when getting an OS-assigned address):
|
|
109
|
+
* `{ port: 12346, family: 'IPv4', address: '127.0.0.1' }`.
|
|
110
|
+
*
|
|
111
|
+
* @returns {import('./Socket').AddressInfo | null}
|
|
112
|
+
*/
|
|
113
|
+
address() {
|
|
114
|
+
if (!this._localAddress) return null;
|
|
115
|
+
return { address: this._localAddress, port: this._localPort, family: this._localFamily };
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
ref() {
|
|
119
|
+
console.warn('react-native-tcp-socket: Server.ref() method will have no effect.');
|
|
120
|
+
return this;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
unref() {
|
|
124
|
+
console.warn('react-native-tcp-socket: Server.unref() method will have no effect.');
|
|
125
|
+
return this;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* @private
|
|
130
|
+
*/
|
|
131
|
+
_registerEvents() {
|
|
132
|
+
this._listeningListener = this._eventEmitter.addListener('listening', (evt) => {
|
|
133
|
+
if (evt.id !== this._id) return;
|
|
134
|
+
this._localAddress = evt.connection.localAddress;
|
|
135
|
+
this._localPort = evt.connection.localPort;
|
|
136
|
+
this._localFamily = evt.connection.localFamily;
|
|
137
|
+
this.emit('listening');
|
|
138
|
+
});
|
|
139
|
+
this._errorListener = this._eventEmitter.addListener('error', (evt) => {
|
|
140
|
+
if (evt.id !== this._id) return;
|
|
141
|
+
this.close();
|
|
142
|
+
this.emit('error', evt.error);
|
|
143
|
+
});
|
|
144
|
+
this._connectionsListener = this._eventEmitter.addListener('connection', (evt) => {
|
|
145
|
+
if (evt.id !== this._id) return;
|
|
146
|
+
const newSocket = this._buildSocket(evt.info);
|
|
147
|
+
this._addConnection(newSocket);
|
|
148
|
+
this.emit('connection', newSocket);
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* @private
|
|
154
|
+
*/
|
|
155
|
+
_setDisconnected() {
|
|
156
|
+
this._localAddress = undefined;
|
|
157
|
+
this._localPort = undefined;
|
|
158
|
+
this._localFamily = undefined;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* @protected
|
|
163
|
+
* @param {Socket} socket
|
|
164
|
+
*/
|
|
165
|
+
_addConnection(socket) {
|
|
166
|
+
// Emit 'close' when all connection closed
|
|
167
|
+
socket.on('close', () => {
|
|
168
|
+
this._connections.delete(socket);
|
|
169
|
+
if (!this.listening && this._connections.size === 0) this.emit('close');
|
|
170
|
+
});
|
|
171
|
+
this._connections.add(socket);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* @protected
|
|
176
|
+
* @param {{ id: number; connection: import('./Socket').NativeConnectionInfo; }} info
|
|
177
|
+
* @returns {Socket}
|
|
178
|
+
*/
|
|
179
|
+
_buildSocket(info) {
|
|
180
|
+
const newSocket = new Socket();
|
|
181
|
+
newSocket._setId(info.id);
|
|
182
|
+
newSocket._setConnected(info.connection);
|
|
183
|
+
return newSocket;
|
|
184
|
+
}
|
|
185
|
+
}
|