initialize node, change the sockv4 and multicast sock and token

This commit is contained in:
asxalex 2025-07-29 11:19:21 +08:00
parent 87bee05140
commit f86069d165
3 changed files with 33 additions and 11 deletions

View File

@ -1,6 +1,7 @@
package com.jihe.punchnet.sdlan.network
// import sdlanproto.Message.*
import android.util.Log
import com.google.protobuf.kotlin.toByteString
import com.jihe.punchnet.protobuf.PunchProto
import com.jihe.punchnet.protobuf.PunchProto.SDLStunProbe
@ -127,24 +128,24 @@ class Node private constructor (
val aes: AES,
// var encryptKey: ByteArray,
val config: NodeConfig,
var config: NodeConfig,
val pendingPeers: PeerMap,
val knownPeers: PeerMap,
// 自身的公网ip
val outerIPV4: AtomicInteger,
val udpSockV4: SDLanSocket,
var udpSockV4: SDLanSocket,
val udpSockMulticast: SDLanSocket?,
val multicastSock: SDLanSock,
var udpSockMulticast: SDLanSocket?,
var multicastSock: SDLanSock,
val stats: NodeStats,
// send message to socket, if connected, and authorized
var toSocket: SendChannel<ByteArray>,
val startStopChannel: SendChannel<StartStopChanInfo>,
var startStopChannel: SendChannel<StartStopChanInfo>,
val mac: Mac = generateRandomMAC(),
var nat_type: NatType = NatType.Invalid,
@ -175,6 +176,11 @@ class Node private constructor (
toSocket,
startStopChannel,
)
} else {
instance?.config = config
instance?.udpSockV4 = v4Sock
instance?.token = token
Log.e("Initialize", "not initialize instance")
}
return instance!!
}

View File

@ -73,14 +73,19 @@ suspend fun onMessage(scope: CoroutineScope, data: SDLanTCP) {
println("reloading config: node.iface = ${node.iface}")
node.iface?.reload_config(node.deviceConfig)
println("reloading config ok: node.iface = ${node.iface}")
println("reloading config ok: node.udp = ${node.udpSockV4}")
node.networkID.set(ack.devAddr.networkId)
// println("got aes key: ${aeskey.toList()}, length is ${aeskey.size}")
node.sendStunRequest()
scope.async {
node.probeNatType()
TerminalLogger.debugf { "nat type is ${node.nat_type}"}
scope.launch {
try {
node.probeNatType()
TerminalLogger.debugf { "nat type is ${node.nat_type}"}
} catch (e: Exception) {
Log.e("NAT", "probe nat exited: $e")
}
}
/*
CoroutineScope(Dispatchers.Default).async {
@ -306,6 +311,7 @@ suspend fun loopSocketV4(scope: CoroutineScope, node: Node, sock: SDLanSocket, c
node.sendStunRequest()
}
} catch (e: Exception) {
Log.e("Socket", "job stun request exits: $e")
}
}
@ -332,6 +338,7 @@ suspend fun loopSocketV4(scope: CoroutineScope, node: Node, sock: SDLanSocket, c
}
job_stun_request.cancelAndJoin()
job_handle_packet.cancelAndJoin()
Log.e("Socket", "loop socket v4 stops")
}
suspend fun readAndParsePacket(scope: CoroutineScope, node: Node, sock: SDLanSocket) {
@ -431,6 +438,12 @@ fun initEdge(scope: CoroutineScope, iface: Iface, token: String, config: NodeCon
startStopChannel,
)
val instance = Node.getInstance()
instance.token = token
instance.udpSockV4 = sockV4
instance.udpSockMulticast = sockMulticast
instance.config = config
instance.toSocket = toSocket
instance.startStopChannel = startStopChannel
println(instance)
}

View File

@ -1,5 +1,6 @@
package com.jihe.punchnet.sdlan.network
import android.util.Log
import com.jihe.punchnet.sdlan.logs.TerminalLogger
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
@ -14,12 +15,14 @@ import java.net.DatagramSocket
import java.net.Inet4Address
import java.net.SocketAddress
class SDLanSocket(val scope: CoroutineScope, val addr: String, val port: Int, val reuseAddress: Boolean = false) {
class SDLanSocket(val scope: CoroutineScope, val addr: String, var port: Int, val reuseAddress: Boolean = false) {
private val connection: DatagramSocket = run {
val sock = DatagramSocket(port, Inet4Address.getByName(addr))
if (reuseAddress) {
sock.reuseAddress = true
}
port = sock.localPort
Log.d("SDLanSocket", "local port = $port")
sock
}
@ -42,7 +45,7 @@ class SDLanSocket(val scope: CoroutineScope, val addr: String, val port: Int, va
connection.send(packet)
}
} catch(e: Exception) {
TerminalLogger.errorf {"Failed to send to: $e, localport: ${connection.localPort}"}
TerminalLogger.errorf {"Failed to send to: $e, localport: ${port}"}
}
}
@ -56,7 +59,7 @@ class SDLanSocket(val scope: CoroutineScope, val addr: String, val port: Int, va
return packet
}
suspend fun close() {
fun close() {
connection.close()
}
}