change Semantics of Client AutoConnect.searchServer.
* now returns an optional * uses multiple trys.
This commit is contained in:
parent
3c2984a404
commit
4cd4481182
@ -1,21 +1,25 @@
|
|||||||
package org.hso.texturesyncclient.controller.net
|
package org.hso.texturesyncclient.controller.net
|
||||||
|
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
import java.lang.Exception
|
|
||||||
import java.net.DatagramPacket
|
import java.net.DatagramPacket
|
||||||
import java.net.DatagramSocket
|
import java.net.DatagramSocket
|
||||||
import java.net.InetAddress
|
import java.net.InetAddress
|
||||||
|
|
||||||
class AutoConnect {
|
class AutoConnect private constructor() {
|
||||||
private constructor() {}
|
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
@Throws(IOException::class)
|
@Throws(IOException::class)
|
||||||
fun search_server(mulicastAddr: String = "ff02::dd42:c0fe", port: Int = 10796, timeoutMs : Int = 4000): Connection {
|
fun searchServer(
|
||||||
|
mulicastAddr: String = "ff02::dd42:c0fe",
|
||||||
|
port: Int = 10796,
|
||||||
|
timeoutMs: Int = 400,
|
||||||
|
trys: Int = 10
|
||||||
|
): Connection? {
|
||||||
val sock = DatagramSocket()
|
val sock = DatagramSocket()
|
||||||
try {
|
try {
|
||||||
sock.soTimeout = timeoutMs
|
sock.soTimeout = timeoutMs
|
||||||
|
|
||||||
|
for (i in 0..trys) {
|
||||||
val bytes = "TextureSync".toByteArray()
|
val bytes = "TextureSync".toByteArray()
|
||||||
|
|
||||||
sock.send(
|
sock.send(
|
||||||
@ -32,17 +36,25 @@ class AutoConnect {
|
|||||||
val portData = ByteArray(2)
|
val portData = ByteArray(2)
|
||||||
val response = DatagramPacket(portData, portData.size)
|
val response = DatagramPacket(portData, portData.size)
|
||||||
|
|
||||||
|
try {
|
||||||
sock.receive(response)
|
sock.receive(response)
|
||||||
|
|
||||||
// 2-Byte BE to Port Number
|
// 2-Byte BE to Port Number
|
||||||
val port = (portData[0].toInt().shl(8)).or(portData[1].toInt())
|
val serverPort = (portData[0].toInt().shl(8)).or(portData[1].toInt())
|
||||||
|
|
||||||
return Connection(response.address, port)
|
return Connection(response.address, serverPort)
|
||||||
} catch ( e : Exception) {
|
} catch (e: IOException) {
|
||||||
|
// Timed out
|
||||||
|
// NOP
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
throw e
|
throw e
|
||||||
} finally {
|
} finally {
|
||||||
sock.close()
|
sock.close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user