mirror of
https://github.com/meshtastic/Meshtastic-Android.git
synced 2026-04-20 22:23:37 +00:00
feat: support custom ports in TCP interface (#1534)
This commit is contained in:
parent
f3ba084d5b
commit
6720764ed4
4 changed files with 22 additions and 12 deletions
|
|
@ -48,6 +48,15 @@ class NetworkRepository @Inject constructor(
|
|||
companion object {
|
||||
// To find all available services use SERVICE_TYPE = "_services._dns-sd._udp"
|
||||
internal const val SERVICE_NAME = "Meshtastic"
|
||||
internal val SERVICE_TYPES = listOf("_http._tcp.", "_meshtastic._tcp.")
|
||||
internal const val SERVICE_PORT = 4403
|
||||
private const val SERVICE_TYPE = "_meshtastic._tcp"
|
||||
internal val SERVICE_TYPES = setOf("_http._tcp", SERVICE_TYPE)
|
||||
|
||||
fun NsdServiceInfo.toAddressString() = buildString {
|
||||
append(@Suppress("DEPRECATION") host.toString().substring(1))
|
||||
if (serviceType.trim('.') == SERVICE_TYPE && port != SERVICE_PORT) {
|
||||
append(":$port")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,11 +31,11 @@ import java.util.concurrent.CopyOnWriteArrayList
|
|||
import kotlin.coroutines.resume
|
||||
|
||||
internal fun NsdManager.serviceList(
|
||||
serviceTypes: List<String>,
|
||||
serviceTypes: Set<String>,
|
||||
serviceName: String,
|
||||
): Flow<List<NsdServiceInfo>> {
|
||||
val flows = serviceTypes.map { serviceType -> serviceList(serviceType, serviceName) }
|
||||
return combine(flows) { lists -> lists.flatMap { it }.distinctBy { it.serviceName } }
|
||||
return combine(flows) { lists -> lists.flatMap { it } }
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ package com.geeksville.mesh.repository.radio
|
|||
|
||||
import com.geeksville.mesh.android.Logging
|
||||
import com.geeksville.mesh.concurrent.handledLaunch
|
||||
import com.geeksville.mesh.repository.network.NetworkRepository
|
||||
import com.geeksville.mesh.util.Exceptions
|
||||
import dagger.assisted.Assisted
|
||||
import dagger.assisted.AssistedInject
|
||||
|
|
@ -42,6 +43,7 @@ class TCPInterface @AssistedInject constructor(
|
|||
const val MAX_RETRIES_ALLOWED = Int.MAX_VALUE
|
||||
const val MIN_BACKOFF_MILLIS = 1 * 1000L // 1 second
|
||||
const val MAX_BACKOFF_MILLIS = 5 * 60 * 1000L // 5 minutes
|
||||
const val SERVICE_PORT = NetworkRepository.SERVICE_PORT
|
||||
}
|
||||
|
||||
private var retryCount = 1
|
||||
|
|
@ -100,7 +102,11 @@ class TCPInterface @AssistedInject constructor(
|
|||
// Create a socket to make the connection with the server
|
||||
private suspend fun startConnect() = withContext(Dispatchers.IO) {
|
||||
debug("TCP connecting to $address")
|
||||
Socket(InetAddress.getByName(address), 4403).use { socket ->
|
||||
|
||||
val (host, port) = address.split(":", limit = 2)
|
||||
.let { it[0] to (it.getOrNull(1)?.toIntOrNull() ?: SERVICE_PORT) }
|
||||
|
||||
Socket(InetAddress.getByName(host), port).use { socket ->
|
||||
socket.tcpNoDelay = true
|
||||
socket.soTimeout = 500
|
||||
this@TCPInterface.socket = socket
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue