@@ -46,7 +46,7 @@ class ConnectionModule(reactContext: ReactApplicationContext)
46
46
loggedIn = false
47
47
}
48
48
49
- private fun directlyWriteToConnection (id : Int , data : ByteArray ): Boolean {
49
+ private fun directlyWritePacket (id : Int , data : ByteArray ): Boolean {
50
50
val packet = Packet (id, data)
51
51
var asBytes =
52
52
if (compressionEnabled) packet.writeCompressedPacket(compressionThreshold)
@@ -62,15 +62,15 @@ class ConnectionModule(reactContext: ReactApplicationContext)
62
62
return socketIsOpen
63
63
}
64
64
65
- @ReactMethod fun writeToConnection (
65
+ @ReactMethod fun writePacket (
66
66
connId : String , packetId : Int , data : String , promise : Promise
67
67
) = runBlocking {
68
68
launch(Dispatchers .Default ) {
69
69
lock.read {
70
70
if (connId == connectionId.toString()) {
71
71
try {
72
72
val dataBytes = Base64 .decode(data, Base64 .DEFAULT )
73
- promise.resolve(directlyWriteToConnection (packetId, dataBytes))
73
+ promise.resolve(directlyWritePacket (packetId, dataBytes))
74
74
} catch (e: Exception ) {
75
75
promise.reject(e)
76
76
}
@@ -107,7 +107,8 @@ class ConnectionModule(reactContext: ReactApplicationContext)
107
107
108
108
// Create socket and connection ID.
109
109
socket = Socket (host, port)
110
- this .connectionId = UUID .randomUUID()
110
+ socket.soTimeout = 20 * 1000
111
+ this .connectionId = connectionId
111
112
112
113
// Create data to send in Handshake.
113
114
val portBuf = ByteBuffer .allocate(2 )
@@ -123,7 +124,8 @@ class ConnectionModule(reactContext: ReactApplicationContext)
123
124
socket.getOutputStream().write(Packet (0x00 , handshakeData.toByteArray()).writePacket())
124
125
125
126
// Send Login Start packet.
126
- socket.getOutputStream().write(Base64 .decode(loginPacket, Base64 .DEFAULT ))
127
+ val loginPacketData = Base64 .decode(loginPacket, Base64 .DEFAULT )
128
+ socket.getOutputStream().write(Packet (0x00 , loginPacketData).writePacket())
127
129
128
130
// Update the current socket and resolve/reject.
129
131
this .socket = socket
@@ -159,15 +161,18 @@ class ConnectionModule(reactContext: ReactApplicationContext)
159
161
// Re-use the current thread, start reading from the socket.
160
162
val buffer = ByteArrayOutputStream ()
161
163
val buf = ByteArray (4096 )
162
- var aesDecipher: Cipher ?
163
- while (lock.read {
164
- aesDecipher = this .aesDecipher
165
- return @read this .socket == socket
166
- }) {
164
+ while (true ) {
165
+ lock.readLock().lock()
166
+ if (this .socket != socket) {
167
+ lock.readLock().unlock()
168
+ break
169
+ }
167
170
try {
168
171
val n = socket.getInputStream().read(buf)
169
- if (n == - 1 )
172
+ if (n == - 1 ) {
173
+ lock.readLock().unlock()
170
174
break
175
+ }
171
176
172
177
// Decrypt if necessary.
173
178
if (aesDecipher != null ) {
@@ -187,8 +192,7 @@ class ConnectionModule(reactContext: ReactApplicationContext)
187
192
188
193
// We can handle Keep Alive, Login Success and Set Compression.
189
194
if (packet.id.value == keepAliveClientBoundId) {
190
- directlyWriteToConnection(keepAliveServerBoundId, packet.data)
191
- continue
195
+ directlyWritePacket(keepAliveServerBoundId, packet.data)
192
196
} else if (packet.id.value == setCompressionId && ! loggedIn) {
193
197
val threshold = VarInt .read(packet.data)?.value ? : 0
194
198
compressionThreshold = threshold
@@ -199,7 +203,7 @@ class ConnectionModule(reactContext: ReactApplicationContext)
199
203
200
204
// Forward the packet to JavaScript.
201
205
val packetLengthLength =
202
- packet.totalLength - packet.data.size - packet.id.data.size
206
+ packet.totalLength - ( packet.data.size + packet.id.data.size)
203
207
val params = Arguments .createMap().apply {
204
208
putString(" connectionId" , connectionId.toString())
205
209
putDouble(" id" , packet.id.value.toDouble())
@@ -210,15 +214,18 @@ class ConnectionModule(reactContext: ReactApplicationContext)
210
214
putDouble(" packetLength" , packet.totalLength.toDouble())
211
215
putDouble(" lengthLength" , packetLengthLength.toDouble())
212
216
}
213
- sendEvent(reactContext = reactApplicationContext, " packet" , params)
217
+ sendEvent(reactContext = reactApplicationContext, " ecm:packet" , params)
218
+ lock.readLock().unlock()
214
219
} catch (e: Exception ) {
215
- lock.write { directlyCloseConnection() }
220
+ lock.readLock().unlock()
221
+ lock.write { if (this .socket == socket) directlyCloseConnection() }
216
222
val params = Arguments .createMap().apply {
217
223
putString(" connectionId" , connectionId.toString())
218
224
putString(" stackTrace" , e.stackTraceToString())
219
225
putString(" message" , e.message)
220
226
}
221
- sendEvent(reactContext = reactApplicationContext, " error" , params)
227
+ sendEvent(reactContext = reactApplicationContext, " ecm:error" , params)
228
+ break
222
229
}
223
230
}
224
231
@@ -228,7 +235,7 @@ class ConnectionModule(reactContext: ReactApplicationContext)
228
235
val params = Arguments .createMap().apply {
229
236
putString(" connectionId" , connectionId.toString())
230
237
}
231
- sendEvent(reactContext = reactApplicationContext, " close" , params)
238
+ sendEvent(reactContext = reactApplicationContext, " ecm: close" , params)
232
239
}
233
240
}
234
241
0 commit comments