Skip to content

Commit 9597fc4

Browse files
committed
Enable ConnectionModule by default, fix bugs.
- Fixed issue with reading packets from socket. - Add logging event to the connection module. - Use I/O dispatcher for scheduling tasks. - Set version field correctly for Android app.
1 parent 460520f commit 9597fc4

File tree

3 files changed

+47
-34
lines changed

3 files changed

+47
-34
lines changed

android/app/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ android {
139139
minSdkVersion rootProject.ext.minSdkVersion
140140
targetSdkVersion rootProject.ext.targetSdkVersion
141141
versionCode 1
142-
versionName "1.0"
142+
versionName "1.0.0-alpha.4"
143143
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
144144

145145
if (isNewArchitectureEnabled()) {

android/app/src/main/java/com/enderchat/modules/connection/ConnectionModule.kt

+41-32
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class ConnectionModule(reactContext: ReactApplicationContext)
6565
@ReactMethod fun writePacket(
6666
connId: String, packetId: Int, data: String, promise: Promise
6767
) = runBlocking {
68-
launch(Dispatchers.Default) {
68+
launch(Dispatchers.IO) {
6969
lock.read {
7070
if (connId == connectionId.toString()) {
7171
try {
@@ -181,40 +181,43 @@ class ConnectionModule(reactContext: ReactApplicationContext)
181181
buffer.write(buf, 0, n)
182182
}
183183

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)
192194

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+
}
203205

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)
216220
}
217-
sendEvent(reactContext = reactApplicationContext, "ecm:packet", params)
218221
lock.readLock().unlock()
219222
} catch (e: Exception) {
220223
lock.readLock().unlock()
@@ -256,4 +259,10 @@ class ConnectionModule(reactContext: ReactApplicationContext)
256259
fun removeListeners(count: Int) {
257260
// Remove upstream listeners, stop unnecessary background tasks
258261
}
262+
263+
private fun println(log: Any?) {
264+
sendEvent(reactContext = reactApplicationContext, "ecm:log", Arguments.createMap().apply {
265+
putString("log", log.toString())
266+
})
267+
}
259268
}

src/minecraft/connection/native.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { readVarInt, writeVarInt, resolveHostname, protocolMap } from '../utils'
1212
const { ConnectionModule } = NativeModules
1313

1414
export const isNativeConnectionAvailable = () =>
15-
!!ConnectionModule?.openConnection && false
15+
!!ConnectionModule?.openConnection
1616

1717
interface NativeEvent {
1818
connectionId: string
@@ -53,6 +53,10 @@ export class NativeServerConnection
5353
super()
5454
this.id = id
5555
this.options = options
56+
this.eventEmitter.addListener(
57+
'ecm:log',
58+
({ log }: NativeEvent & { log: string }) => console.log(log)
59+
)
5660
this.eventEmitter.addListener('ecm:packet', (event: NativePacketEvent) => {
5761
if (event.connectionId !== this.id) return
5862
// Run after interactions to improve user experience.

0 commit comments

Comments
 (0)