@@ -65,7 +65,7 @@ class ConnectionModule(reactContext: ReactApplicationContext)
65
65
@ReactMethod fun writePacket (
66
66
connId : String , packetId : Int , data : String , promise : Promise
67
67
) = runBlocking {
68
- launch(Dispatchers .Default ) {
68
+ launch(Dispatchers .IO ) {
69
69
lock.read {
70
70
if (connId == connectionId.toString()) {
71
71
try {
@@ -181,40 +181,43 @@ class ConnectionModule(reactContext: ReactApplicationContext)
181
181
buffer.write(buf, 0 , n)
182
182
}
183
183
184
- // Read packet.
185
- val bytes = buffer.toByteArray()
186
- val packet =
187
- if (compressionEnabled) Packet .readCompressed(bytes) ? : continue
188
- else Packet .read(bytes) ? : continue
189
- // Reset the buffer, we've been reading byte-by-byte so this is fine to do.
190
- buffer.reset() // We know packet.totalLength exists for read/readCompressed.
191
- buffer.write(bytes, packet.totalLength!! , bytes.size - packet.totalLength)
184
+ // TODO: This could be coroutined. Maybe some actor-style threading?
185
+ while (true ) {
186
+ // Read packets from the buffer.
187
+ val bytes = buffer.toByteArray()
188
+ val packet =
189
+ if (compressionEnabled) Packet .readCompressed(bytes) ? : break
190
+ else Packet .read(bytes) ? : break
191
+ // Reset the buffer, we've been reading byte-by-byte so this is fine to do.
192
+ buffer.reset() // We know packet.totalLength exists for read/readCompressed.
193
+ buffer.write(bytes, packet.totalLength!! , bytes.size - packet.totalLength)
192
194
193
- // We can handle Keep Alive, Login Success and Set Compression.
194
- if (packet.id.value == keepAliveClientBoundId) {
195
- directlyWritePacket(keepAliveServerBoundId, packet.data)
196
- } else if (packet.id.value == setCompressionId && ! loggedIn) {
197
- val threshold = VarInt .read(packet.data)?.value ? : 0
198
- compressionThreshold = threshold
199
- compressionEnabled = threshold >= 0
200
- } else if (packet.id.value == loginSuccessId && ! loggedIn) {
201
- loggedIn = true // Login Success
202
- }
195
+ // We can handle Keep Alive, Login Success and Set Compression.
196
+ if (packet.id.value == keepAliveClientBoundId) {
197
+ directlyWritePacket(keepAliveServerBoundId, packet.data)
198
+ } else if (packet.id.value == setCompressionId && ! loggedIn) {
199
+ val threshold = VarInt .read(packet.data)?.value ? : 0
200
+ compressionThreshold = threshold
201
+ compressionEnabled = threshold >= 0
202
+ } else if (packet.id.value == loginSuccessId && ! loggedIn) {
203
+ loggedIn = true // Login Success
204
+ }
203
205
204
- // Forward the packet to JavaScript.
205
- val packetLengthLength =
206
- packet.totalLength - (packet.data.size + packet.id.data.size)
207
- val params = Arguments .createMap().apply {
208
- putString(" connectionId" , connectionId.toString())
209
- putDouble(" id" , packet.id.value.toDouble())
210
- putString(" data" , Base64 .encodeToString(packet.data, Base64 .DEFAULT ))
211
- putBoolean(" compressed" , compressionEnabled)
212
- putDouble(" idLength" , packet.id.data.size.toDouble())
213
- putDouble(" dataLength" , packet.data.size.toDouble())
214
- putDouble(" packetLength" , packet.totalLength.toDouble())
215
- putDouble(" lengthLength" , packetLengthLength.toDouble())
206
+ // Forward the packet to JavaScript.
207
+ val packetLengthLength =
208
+ packet.totalLength - (packet.data.size + packet.id.data.size)
209
+ val params = Arguments .createMap().apply {
210
+ putString(" connectionId" , connectionId.toString())
211
+ putDouble(" id" , packet.id.value.toDouble())
212
+ putString(" data" , Base64 .encodeToString(packet.data, Base64 .DEFAULT ))
213
+ putBoolean(" compressed" , compressionEnabled)
214
+ putDouble(" idLength" , packet.id.data.size.toDouble())
215
+ putDouble(" dataLength" , packet.data.size.toDouble())
216
+ putDouble(" packetLength" , packet.totalLength.toDouble())
217
+ putDouble(" lengthLength" , packetLengthLength.toDouble())
218
+ }
219
+ sendEvent(reactContext = reactApplicationContext, " ecm:packet" , params)
216
220
}
217
- sendEvent(reactContext = reactApplicationContext, " ecm:packet" , params)
218
221
lock.readLock().unlock()
219
222
} catch (e: Exception ) {
220
223
lock.readLock().unlock()
@@ -256,4 +259,10 @@ class ConnectionModule(reactContext: ReactApplicationContext)
256
259
fun removeListeners (count : Int ) {
257
260
// Remove upstream listeners, stop unnecessary background tasks
258
261
}
262
+
263
+ private fun println (log : Any? ) {
264
+ sendEvent(reactContext = reactApplicationContext, " ecm:log" , Arguments .createMap().apply {
265
+ putString(" log" , log.toString())
266
+ })
267
+ }
259
268
}
0 commit comments