mirror of
https://github.com/tursom/TursomServer.git
synced 2025-02-08 18:50:11 +08:00
添加 UDP 广播功能
This commit is contained in:
parent
dfe91aaa15
commit
3e3d2dcf70
@ -0,0 +1,49 @@
|
|||||||
|
package cn.tursom.datagram.broadcast
|
||||||
|
|
||||||
|
import cn.tursom.core.toUTF8String
|
||||||
|
import cn.tursom.datagram.UdpPackageSize.LANNetLen
|
||||||
|
import java.lang.Thread.sleep
|
||||||
|
import java.net.DatagramPacket
|
||||||
|
import java.net.DatagramSocket
|
||||||
|
import java.net.InetAddress
|
||||||
|
import java.util.*
|
||||||
|
import kotlin.concurrent.thread
|
||||||
|
|
||||||
|
class BroadcastServer(val port: Int, val bufSize: Int = LANNetLen) {
|
||||||
|
private val socket = DatagramSocket()
|
||||||
|
private val server by lazy { DatagramSocket(port) }
|
||||||
|
private val buffer = DatagramPacket(ByteArray(bufSize), bufSize)
|
||||||
|
|
||||||
|
constructor(port: Int) : this(port, LANNetLen)
|
||||||
|
|
||||||
|
private fun send(packet: DatagramPacket) {
|
||||||
|
socket.send(packet)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun send(packet: ByteArray) {
|
||||||
|
send(packet, 0, packet.size)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun send(packet: ByteArray, offset: Int = 0, size: Int = packet.size) {
|
||||||
|
send(DatagramPacket(packet, offset, size, broadcastInetAddr, port))
|
||||||
|
}
|
||||||
|
|
||||||
|
fun recv(): ByteArray {
|
||||||
|
server.receive(buffer)
|
||||||
|
return Arrays.copyOfRange(buffer.data, 0, buffer.length)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun recvBuffer(): DatagramPacket {
|
||||||
|
server.receive(buffer)
|
||||||
|
return buffer
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
const val BROADCAST_IP = "255.255.255.255"
|
||||||
|
@JvmStatic
|
||||||
|
val broadcastInetAddr = InetAddress.getByName(BROADCAST_IP)
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
fun takeOut(packet: DatagramPacket) = Arrays.copyOfRange(packet.data, 0, packet.length)
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
package cn.tursom.datagram.client
|
package cn.tursom.datagram.client
|
||||||
|
|
||||||
|
import cn.tursom.datagram.UdpPackageSize.defaultLen
|
||||||
import java.io.Closeable
|
import java.io.Closeable
|
||||||
import java.net.DatagramPacket
|
import java.net.DatagramPacket
|
||||||
import java.net.DatagramSocket
|
import java.net.DatagramSocket
|
||||||
@ -39,12 +40,4 @@ class UdpClient(
|
|||||||
override fun close() {
|
override fun close() {
|
||||||
socket.close()
|
socket.close()
|
||||||
}
|
}
|
||||||
|
|
||||||
@Suppress("MemberVisibilityCanBePrivate")
|
|
||||||
companion object {
|
|
||||||
//定义不同环境下数据报的最大大小
|
|
||||||
const val LANNetLen = 1472
|
|
||||||
const val internetLen = 548
|
|
||||||
const val defaultLen = internetLen
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -38,7 +38,7 @@ open class TokenUtil {
|
|||||||
if (decode.tim + decode.exp < System.currentTimeMillis()) {
|
if (decode.tim + decode.exp < System.currentTimeMillis()) {
|
||||||
throw TokenTimeoutException()
|
throw TokenTimeoutException()
|
||||||
}
|
}
|
||||||
return fromJson(toJson(decode.dat), dataClazz)
|
return fromJson(toJson(decode.dat!!), dataClazz)
|
||||||
}
|
}
|
||||||
|
|
||||||
open fun encrypt(secretKey: String, encryptSource: ByteArray, type: String): String {
|
open fun encrypt(secretKey: String, encryptSource: ByteArray, type: String): String {
|
||||||
@ -58,7 +58,7 @@ open class TokenUtil {
|
|||||||
|
|
||||||
private inline fun <reified T : Any> fromJson(json: String): T = fromJson(json, T::class.java)
|
private inline fun <reified T : Any> fromJson(json: String): T = fromJson(json, T::class.java)
|
||||||
|
|
||||||
data class TokenBody<T>(val tim: Long, val exp: Long, val dat: T)
|
data class TokenBody<T>(val tim: Long = System.currentTimeMillis(), val exp: Long = 0L, val dat: T? = null)
|
||||||
|
|
||||||
open class TokenException : Exception()
|
open class TokenException : Exception()
|
||||||
class WrongTokenSyntaxException : TokenException()
|
class WrongTokenSyntaxException : TokenException()
|
||||||
|
Loading…
Reference in New Issue
Block a user