@@ -6,12 +6,6 @@ import { Buffer } from 'buffer';
6
6
const Sockets = NativeModules . TcpSockets ;
7
7
import { nativeEventEmitter , getNextId } from './Globals' ;
8
8
9
- const STATE = {
10
- DISCONNECTED : 0 ,
11
- CONNECTING : 1 ,
12
- CONNECTED : 2 ,
13
- } ;
14
-
15
9
/**
16
10
* @typedef {"ascii" | "utf8" | "utf-8" | "utf16le" | "ucs2" | "ucs-2" | "base64" | "latin1" | "binary" | "hex" } BufferEncoding
17
11
*
@@ -64,8 +58,6 @@ export default class Socket extends EventEmitter {
64
58
this . _timeoutMsecs = 0 ;
65
59
/** @type {number | undefined } @private */
66
60
this . _timeout = undefined ;
67
- /** @type {number } @private */
68
- this . _state = STATE . DISCONNECTED ;
69
61
/** @private */
70
62
this . _encoding = undefined ;
71
63
/** @private */
@@ -80,12 +72,24 @@ export default class Socket extends EventEmitter {
80
72
this . _resuming = false ;
81
73
/** @private */
82
74
this . _writeBufferSize = 0 ;
75
+ /** @private */
76
+ this . _bytesRead = 0 ;
77
+ /** @private */
78
+ this . _bytesWritten = 0 ;
79
+ /** @private */
80
+ this . _connecting = false ;
81
+ /** @private */
82
+ this . _pending = true ;
83
+ /** @private */
84
+ this . _destroyed = false ;
85
+ // TODO: Add readOnly and writeOnly states
86
+ /** @type {'opening' | 'open' | 'readOnly' | 'writeOnly' } @private */
87
+ this . _readyState = 'open' ; // Incorrect, but matches NodeJS behavior
83
88
/** @type {{ id: number; data: string; }[] } @private */
84
89
this . _pausedDataEvents = [ ] ;
85
90
this . readableHighWaterMark = 16384 ;
86
91
this . writableHighWaterMark = 16384 ;
87
92
this . writableNeedDrain = false ;
88
- this . bytesSent = 0 ;
89
93
this . localAddress = undefined ;
90
94
this . localPort = undefined ;
91
95
this . remoteAddress = undefined ;
@@ -94,6 +98,30 @@ export default class Socket extends EventEmitter {
94
98
this . _registerEvents ( ) ;
95
99
}
96
100
101
+ get readyState ( ) {
102
+ return this . _readyState ;
103
+ }
104
+
105
+ get destroyed ( ) {
106
+ return this . _destroyed ;
107
+ }
108
+
109
+ get pending ( ) {
110
+ return this . _pending ;
111
+ }
112
+
113
+ get connecting ( ) {
114
+ return this . _connecting ;
115
+ }
116
+
117
+ get bytesWritten ( ) {
118
+ return this . _bytesWritten ;
119
+ }
120
+
121
+ get bytesRead ( ) {
122
+ return this . _bytesRead ;
123
+ }
124
+
97
125
get timeout ( ) {
98
126
return this . _timeout ;
99
127
}
@@ -112,7 +140,9 @@ export default class Socket extends EventEmitter {
112
140
* @param {NativeConnectionInfo } connectionInfo
113
141
*/
114
142
_setConnected ( connectionInfo ) {
115
- this . _state = STATE . CONNECTED ;
143
+ this . _connecting = false ;
144
+ this . _readyState = 'open' ;
145
+ this . _pending = false ;
116
146
this . localAddress = connectionInfo . localAddress ;
117
147
this . localPort = connectionInfo . localPort ;
118
148
this . remoteAddress = connectionInfo . remoteAddress ;
@@ -141,9 +171,8 @@ export default class Socket extends EventEmitter {
141
171
if ( customOptions . tlsCert ) {
142
172
customOptions . tlsCert = Image . resolveAssetSource ( customOptions . tlsCert ) . uri ;
143
173
}
144
- // console.log(getAndroidResourceIdentifier(customOptions.tlsCert));
145
- this . _state = STATE . CONNECTING ;
146
- this . _destroyed = false ;
174
+ this . _connecting = true ;
175
+ this . _readyState = 'opening' ;
147
176
Sockets . connect ( this . _id , customOptions . host , customOptions . port , customOptions ) ;
148
177
return this ;
149
178
}
@@ -218,7 +247,7 @@ export default class Socket extends EventEmitter {
218
247
* @param {boolean } noDelay Default: `true`
219
248
*/
220
249
setNoDelay ( noDelay = true ) {
221
- if ( this . _state != STATE . CONNECTED ) {
250
+ if ( this . _pending ) {
222
251
this . once ( 'connect' , ( ) => this . setNoDelay ( noDelay ) ) ;
223
252
return this ;
224
253
}
@@ -235,7 +264,7 @@ export default class Socket extends EventEmitter {
235
264
* @param {number } initialDelay ***IGNORED**. Default: `0`
236
265
*/
237
266
setKeepAlive ( enable = false , initialDelay = 0 ) {
238
- if ( this . _state != STATE . CONNECTED ) {
267
+ if ( this . _pending ) {
239
268
this . once ( 'connect' , ( ) => this . setKeepAlive ( enable , initialDelay ) ) ;
240
269
return this ;
241
270
}
@@ -266,17 +295,15 @@ export default class Socket extends EventEmitter {
266
295
* @param {BufferEncoding } [encoding]
267
296
*/
268
297
end ( data , encoding ) {
269
- if ( this . _destroyed ) return ;
270
298
if ( data ) {
271
299
this . write ( data , encoding , ( ) => {
272
- this . _destroyed = true ;
273
300
Sockets . end ( this . _id ) ;
274
301
} ) ;
275
302
} else {
276
- this . _destroyed = true ;
277
303
this . _clearTimeout ( ) ;
278
304
Sockets . end ( this . _id ) ;
279
305
}
306
+ return this ;
280
307
}
281
308
282
309
destroy ( ) {
@@ -285,6 +312,7 @@ export default class Socket extends EventEmitter {
285
312
this . _clearTimeout ( ) ;
286
313
Sockets . destroy ( this . _id ) ;
287
314
}
315
+ return this ;
288
316
}
289
317
290
318
/**
@@ -303,7 +331,7 @@ export default class Socket extends EventEmitter {
303
331
*/
304
332
write ( buffer , encoding , cb ) {
305
333
const self = this ;
306
- if ( this . _state === STATE . DISCONNECTED ) throw new Error ( 'Socket is not connected.' ) ;
334
+ if ( this . _pending || this . _destroyed ) throw new Error ( 'Socket is not connected.' ) ;
307
335
308
336
const generatedBuffer = this . _generateSendBuffer ( buffer , encoding ) ;
309
337
this . _writeBufferSize += generatedBuffer . byteLength ;
@@ -316,7 +344,7 @@ export default class Socket extends EventEmitter {
316
344
this . _writeBufferSize -= generatedBuffer . byteLength ;
317
345
this . _lastRcvMsgId = msgId ;
318
346
if ( self . _timeout ) self . _activateTimer ( ) ;
319
- if ( this . writableNeedDrain && this . _lastSentMsgId == msgId ) {
347
+ if ( this . writableNeedDrain && this . _lastSentMsgId === msgId ) {
320
348
this . writableNeedDrain = false ;
321
349
this . emit ( 'drain' ) ;
322
350
}
@@ -331,6 +359,7 @@ export default class Socket extends EventEmitter {
331
359
const ok = this . _writeBufferSize < this . writableHighWaterMark ;
332
360
if ( ! ok ) this . writableNeedDrain = true ;
333
361
this . _lastSentMsgId = currentMsgId ;
362
+ this . _bytesWritten += generatedBuffer . byteLength ;
334
363
Sockets . write ( this . _id , generatedBuffer . toString ( 'base64' ) , currentMsgId ) ;
335
364
return ok ;
336
365
}
@@ -409,8 +438,9 @@ export default class Socket extends EventEmitter {
409
438
_onDeviceDataEvt = ( /** @type {{ id: number; data: string; } } */ evt ) => {
410
439
if ( evt . id !== this . _id ) return ;
411
440
if ( ! this . _paused ) {
412
- const bufferTest = Buffer . from ( evt . data , 'base64' ) ;
413
- const finalData = this . _encoding ? bufferTest . toString ( this . _encoding ) : bufferTest ;
441
+ const bufferData = Buffer . from ( evt . data , 'base64' ) ;
442
+ this . _bytesRead += bufferData . byteLength ;
443
+ const finalData = this . _encoding ? bufferData . toString ( this . _encoding ) : bufferData ;
414
444
this . emit ( 'data' , finalData ) ;
415
445
} else {
416
446
// If the socket is paused, save the data events for later
@@ -479,8 +509,6 @@ export default class Socket extends EventEmitter {
479
509
* @private
480
510
*/
481
511
_setDisconnected ( ) {
482
- if ( this . _state === STATE . DISCONNECTED ) return ;
483
512
this . _unregisterEvents ( ) ;
484
- this . _state = STATE . DISCONNECTED ;
485
513
}
486
514
}
0 commit comments