mirror of
https://github.com/kalarina-bit/Matewave.git
synced 2026-11-22 16:16:10 +00:00
Smaller fixes from the review
- BLE: at most four messages are reassembled at once; presence advertises in the balanced mode, a hosted game still in low latency. - LAN: the socket and its stream are volatile. - The spectator port and the APK share server listen on the Wi-Fi address only; the share server serves four clients at a time. - Version catalog, per-app language setting (localeConfig) and the Gradle distribution checksum. - NetworkLog writes to the debug log instead of an unread in-memory list; Log.d/Log.v are stripped from release builds. - Remove four unused strings in all languages.
This commit is contained in:
@@ -73,6 +73,9 @@ class BleTransport(
|
||||
|
||||
private const val INCOMPLETE_TTL_MS = 30_000L
|
||||
|
||||
/** Messages being reassembled at once; each may grow to [MAX_MESSAGE_BYTES]. */
|
||||
private const val MAX_PARTIAL_MESSAGES = 4
|
||||
|
||||
private const val CTRL_TOTAL_MARKER = 0
|
||||
private const val CTRL_ACK = 1
|
||||
private const val CTRL_NACK = 2
|
||||
@@ -232,6 +235,8 @@ class BleTransport(
|
||||
role = Role.PERIPHERAL
|
||||
hosting = true
|
||||
|
||||
// Presence may already be advertising in the low-power mode; restart it for hosting.
|
||||
if (advertisingActive || advertiseCallback != null) startAdvertising()
|
||||
startPresence()
|
||||
_connectionState.value = ConnectionState.CONNECTING
|
||||
}
|
||||
@@ -300,10 +305,14 @@ class BleTransport(
|
||||
}
|
||||
// One advertisement at a time: a second start would leak the first one.
|
||||
if (advertiseCallback != null) stopAdvertising()
|
||||
// A hosted game should be found at once; plain presence can save the battery.
|
||||
val settings = AdvertiseSettings.Builder()
|
||||
|
||||
.setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY)
|
||||
.setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH)
|
||||
.setAdvertiseMode(
|
||||
if (hosting) AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY else AdvertiseSettings.ADVERTISE_MODE_BALANCED
|
||||
)
|
||||
.setTxPowerLevel(
|
||||
if (hosting) AdvertiseSettings.ADVERTISE_TX_POWER_HIGH else AdvertiseSettings.ADVERTISE_TX_POWER_MEDIUM
|
||||
)
|
||||
.setConnectable(true)
|
||||
.build()
|
||||
|
||||
@@ -790,6 +799,11 @@ class BleTransport(
|
||||
val now = System.currentTimeMillis()
|
||||
incoming.entries.removeAll { now - it.value.startedAt > INCOMPLETE_TTL_MS }
|
||||
|
||||
if (messageId !in incoming && incoming.size >= MAX_PARTIAL_MESSAGES) {
|
||||
// A peer that starts many messages and never finishes them would fill the memory.
|
||||
incoming.minByOrNull { it.value.startedAt }?.let { incoming.remove(it.key) }
|
||||
NetworkLog.log("BLE", "Too many unfinished messages, oldest dropped")
|
||||
}
|
||||
val entry = incoming.getOrPut(messageId) { PartialMessage(total, now) }
|
||||
if (entry.total != total) {
|
||||
incoming[messageId] = PartialMessage(total, now).also { it.parts[index] = payload }
|
||||
|
||||
@@ -72,7 +72,8 @@ class LanTransport(
|
||||
context.getSystemService(Context.NSD_SERVICE) as? NsdManager
|
||||
|
||||
private var serverSocket: ServerSocket? = null
|
||||
private var socket: Socket? = null
|
||||
// Written by the connect and accept paths, read by the writer coroutine.
|
||||
@Volatile private var socket: Socket? = null
|
||||
|
||||
private val socketLock = Any()
|
||||
|
||||
@@ -82,7 +83,7 @@ class LanTransport(
|
||||
socket = candidate
|
||||
true
|
||||
}
|
||||
private var output: OutputStream? = null
|
||||
@Volatile private var output: OutputStream? = null
|
||||
private val dispatcher = MessageDispatcher()
|
||||
|
||||
@Volatile private var hosting = false
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package cc.skysparkle.matewave.network
|
||||
|
||||
import android.util.Log
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.update
|
||||
|
||||
enum class DisconnectReason(val userText: String, val textRes: Int) {
|
||||
NONE("—", cc.skysparkle.matewave.R.string.reason_unknown),
|
||||
@@ -18,23 +18,13 @@ enum class DisconnectReason(val userText: String, val textRes: Int) {
|
||||
PORT_BUSY("Could not open port", cc.skysparkle.matewave.R.string.reason_port_busy)
|
||||
}
|
||||
|
||||
data class NetworkEvent(
|
||||
val at: Long,
|
||||
val transport: String,
|
||||
val text: String
|
||||
)
|
||||
|
||||
object NetworkLog {
|
||||
private const val CAPACITY = 60
|
||||
|
||||
private val _events = MutableStateFlow<List<NetworkEvent>>(emptyList())
|
||||
|
||||
private val _lastReason = MutableStateFlow(DisconnectReason.NONE)
|
||||
val lastReason: StateFlow<DisconnectReason> = _lastReason
|
||||
|
||||
/** Debug output only: release builds strip Log.d (see proguard-rules.pro). */
|
||||
fun log(transport: String, text: String) {
|
||||
val event = NetworkEvent(System.currentTimeMillis(), transport, text)
|
||||
_events.update { (it + event).takeLast(CAPACITY) }
|
||||
Log.d("Matewave/$transport", text)
|
||||
}
|
||||
|
||||
fun reason(transport: String, reason: DisconnectReason, detail: String? = null) {
|
||||
|
||||
@@ -5,10 +5,12 @@ import java.io.File
|
||||
import java.io.IOException
|
||||
import java.io.OutputStream
|
||||
import java.net.Inet4Address
|
||||
import java.net.InetSocketAddress
|
||||
import java.net.NetworkInterface
|
||||
import java.net.ServerSocket
|
||||
import java.net.Socket
|
||||
import java.util.Locale
|
||||
import java.util.concurrent.atomic.AtomicInteger
|
||||
import kotlin.concurrent.thread
|
||||
|
||||
class ApkShareServer(private val apk: File, private val appName: String) {
|
||||
@@ -17,8 +19,13 @@ class ApkShareServer(private val apk: File, private val appName: String) {
|
||||
@Volatile
|
||||
private var server: ServerSocket? = null
|
||||
|
||||
private val activeClients = AtomicInteger()
|
||||
|
||||
/** Listens on the Wi-Fi address only (all interfaces if none is found), so the APK is not served elsewhere. */
|
||||
fun start(): Int {
|
||||
val socket = ServerSocket(0)
|
||||
val socket = ServerSocket()
|
||||
socket.reuseAddress = true
|
||||
socket.bind(InetSocketAddress(LocalNetwork.wifiInet4(), 0))
|
||||
server = socket
|
||||
thread(name = "apk-share-accept", isDaemon = true) {
|
||||
while (!socket.isClosed) {
|
||||
@@ -27,7 +34,19 @@ class ApkShareServer(private val apk: File, private val appName: String) {
|
||||
} catch (e: IOException) {
|
||||
break
|
||||
}
|
||||
thread(name = "apk-share-client", isDaemon = true) { serve(client) }
|
||||
// A handful of downloads at a time is plenty; more connections are refused.
|
||||
if (activeClients.incrementAndGet() > MAX_CLIENTS) {
|
||||
activeClients.decrementAndGet()
|
||||
runCatching { client.close() }
|
||||
continue
|
||||
}
|
||||
thread(name = "apk-share-client", isDaemon = true) {
|
||||
try {
|
||||
serve(client)
|
||||
} finally {
|
||||
activeClients.decrementAndGet()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return socket.localPort
|
||||
@@ -78,6 +97,10 @@ class ApkShareServer(private val apk: File, private val appName: String) {
|
||||
}
|
||||
}
|
||||
|
||||
private companion object {
|
||||
const val MAX_CLIENTS = 4
|
||||
}
|
||||
|
||||
private fun sendText(out: OutputStream, code: Int, reason: String, text: String, withBody: Boolean) {
|
||||
val body = text.toByteArray(Charsets.UTF_8)
|
||||
writeHeader(out, code, reason, "text/plain; charset=utf-8", body.size.toLong())
|
||||
@@ -117,12 +140,13 @@ class ApkShareServer(private val apk: File, private val appName: String) {
|
||||
object LocalNetwork {
|
||||
private val WIFI_PREFIXES = listOf("wlan", "swlan", "ap", "softap", "eth")
|
||||
|
||||
fun wifiIpv4(): String? = runCatching {
|
||||
fun wifiInet4(): Inet4Address? = runCatching {
|
||||
NetworkInterface.getNetworkInterfaces()?.toList().orEmpty()
|
||||
.filter { nif -> nif.isUp && !nif.isLoopback && WIFI_PREFIXES.any { nif.name.startsWith(it) } }
|
||||
.flatMap { nif -> nif.inetAddresses.toList() }
|
||||
.filterIsInstance<Inet4Address>()
|
||||
.firstOrNull { it.isSiteLocalAddress }
|
||||
?.hostAddress
|
||||
}.getOrNull()
|
||||
|
||||
fun wifiIpv4(): String? = wifiInet4()?.hostAddress
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user