mirror of https://github.com/hykilpikonna/AquaDX
[M] Split code
parent
5290597b2b
commit
8a04bb014a
|
@ -1,6 +1,8 @@
|
||||||
package icu.samnyan.aqua.sega.maimai2
|
package icu.samnyan.aqua.sega.maimai2.worldslink
|
||||||
|
|
||||||
import ext.*
|
import ext.logger
|
||||||
|
import ext.millis
|
||||||
|
import ext.thread
|
||||||
import java.io.BufferedReader
|
import java.io.BufferedReader
|
||||||
import java.io.BufferedWriter
|
import java.io.BufferedWriter
|
||||||
import java.io.InputStreamReader
|
import java.io.InputStreamReader
|
||||||
|
@ -13,60 +15,10 @@ import java.util.concurrent.locks.ReentrantLock
|
||||||
import kotlin.collections.set
|
import kotlin.collections.set
|
||||||
import kotlin.concurrent.withLock
|
import kotlin.concurrent.withLock
|
||||||
|
|
||||||
|
|
||||||
const val PROTO_VERSION = 1
|
const val PROTO_VERSION = 1
|
||||||
const val MAX_STREAMS = 10
|
const val MAX_STREAMS = 10
|
||||||
const val SO_TIMEOUT = 10000
|
const val SO_TIMEOUT = 10000
|
||||||
|
|
||||||
private object Command {
|
|
||||||
// Control plane
|
|
||||||
const val CTL_START = 1u
|
|
||||||
const val CTL_BIND = 2u
|
|
||||||
const val CTL_HEARTBEAT = 3u
|
|
||||||
const val CTL_TCP_CONNECT = 4u // Accept a new multiplexed TCP stream
|
|
||||||
const val CTL_TCP_ACCEPT = 5u
|
|
||||||
const val CTL_TCP_ACCEPT_ACK = 6u
|
|
||||||
const val CTL_TCP_CLOSE = 7u
|
|
||||||
|
|
||||||
// Data plane
|
|
||||||
const val DATA_SEND = 21u
|
|
||||||
const val DATA_BROADCAST = 22u
|
|
||||||
}
|
|
||||||
|
|
||||||
private object Proto {
|
|
||||||
const val TCP = 6u
|
|
||||||
const val UDP = 17u
|
|
||||||
}
|
|
||||||
|
|
||||||
data class Msg(
|
|
||||||
var cmd: UInt,
|
|
||||||
var proto: UInt? = null,
|
|
||||||
var sid: UInt? = null,
|
|
||||||
var src: UInt? = null,
|
|
||||||
var sPort: UInt? = null,
|
|
||||||
var dst: UInt? = null,
|
|
||||||
var dPort: UInt? = null,
|
|
||||||
var data: String? = null
|
|
||||||
) {
|
|
||||||
override fun toString() = ls(
|
|
||||||
1, cmd, proto, sid, src, sPort, dst, dPort,
|
|
||||||
null, null, null, null, null, null, null, null, // reserved for future use
|
|
||||||
data
|
|
||||||
).joinToString(",") { it?.str ?: "" }.trimEnd(',')
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
val fields = arr(Msg::proto, Msg::sid, Msg::src, Msg::sPort, Msg::dst, Msg::dPort)
|
|
||||||
|
|
||||||
fun fromString(str: String): Msg {
|
|
||||||
val sp = str.split(',')
|
|
||||||
return Msg(0u).apply {
|
|
||||||
cmd = sp[1].toUInt()
|
|
||||||
fields.forEachIndexed { i, f -> f.set(this, sp.getOrNull(i + 2)?.some?.toUIntOrNull()) }
|
|
||||||
data = sp.drop(16).joinToString(",")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fun ctlMsg(cmd: UInt, data: String? = null) = Msg(cmd, data = data)
|
fun ctlMsg(cmd: UInt, data: String? = null) = Msg(cmd, data = data)
|
||||||
|
|
||||||
data class ActiveClient(
|
data class ActiveClient(
|
||||||
|
@ -133,7 +85,7 @@ fun ActiveClient.handle(msg: Msg) {
|
||||||
val sid = msg.sid ?: return log.warn("Accept: Stream ID not found")
|
val sid = msg.sid ?: return log.warn("Accept: Stream ID not found")
|
||||||
|
|
||||||
if (sid !in target.pendingStreams)
|
if (sid !in target.pendingStreams)
|
||||||
return log.warn("Stream ID not found in pending list: ${sid}")
|
return log.warn("Stream ID not found in pending list: $sid")
|
||||||
|
|
||||||
// Add the stream to the active list
|
// Add the stream to the active list
|
||||||
target.pendingStreams.remove(sid)
|
target.pendingStreams.remove(sid)
|
|
@ -0,0 +1,56 @@
|
||||||
|
package icu.samnyan.aqua.sega.maimai2.worldslink
|
||||||
|
|
||||||
|
import ext.arr
|
||||||
|
import ext.ls
|
||||||
|
import ext.some
|
||||||
|
import ext.str
|
||||||
|
|
||||||
|
object Command {
|
||||||
|
// Control plane
|
||||||
|
const val CTL_START = 1u
|
||||||
|
const val CTL_BIND = 2u
|
||||||
|
const val CTL_HEARTBEAT = 3u
|
||||||
|
const val CTL_TCP_CONNECT = 4u // Accept a new multiplexed TCP stream
|
||||||
|
const val CTL_TCP_ACCEPT = 5u
|
||||||
|
const val CTL_TCP_ACCEPT_ACK = 6u
|
||||||
|
const val CTL_TCP_CLOSE = 7u
|
||||||
|
|
||||||
|
// Data plane
|
||||||
|
const val DATA_SEND = 21u
|
||||||
|
const val DATA_BROADCAST = 22u
|
||||||
|
}
|
||||||
|
|
||||||
|
object Proto {
|
||||||
|
const val TCP = 6u
|
||||||
|
const val UDP = 17u
|
||||||
|
}
|
||||||
|
|
||||||
|
data class Msg(
|
||||||
|
var cmd: UInt,
|
||||||
|
var proto: UInt? = null,
|
||||||
|
var sid: UInt? = null,
|
||||||
|
var src: UInt? = null,
|
||||||
|
var sPort: UInt? = null,
|
||||||
|
var dst: UInt? = null,
|
||||||
|
var dPort: UInt? = null,
|
||||||
|
var data: String? = null
|
||||||
|
) {
|
||||||
|
override fun toString() = ls(
|
||||||
|
1, cmd, proto, sid, src, sPort, dst, dPort,
|
||||||
|
null, null, null, null, null, null, null, null, // reserved for future use
|
||||||
|
data
|
||||||
|
).joinToString(",") { it?.str ?: "" }.trimEnd(',')
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
val fields = arr(Msg::proto, Msg::sid, Msg::src, Msg::sPort, Msg::dst, Msg::dPort)
|
||||||
|
|
||||||
|
fun fromString(str: String): Msg {
|
||||||
|
val sp = str.split(',')
|
||||||
|
return Msg(0u).apply {
|
||||||
|
cmd = sp[1].toUInt()
|
||||||
|
fields.forEachIndexed { i, f -> f.set(this, sp.getOrNull(i + 2)?.some?.toUIntOrNull()) }
|
||||||
|
data = sp.drop(16).joinToString(",")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue