-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use binary frame instead of plain text to send packet
- Loading branch information
Showing
15 changed files
with
356 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright © 2025 RTAkland | ||
* Author: RTAkland | ||
* Date: 2025/1/3 | ||
*/ | ||
|
||
@file:Suppress("unused") | ||
|
||
package cn.rtast.kwsify | ||
|
||
import java.util.Base64 | ||
|
||
fun String.encodeToBase64(): String { | ||
return Base64.getEncoder().encodeToString(this.toByteArray(Charsets.UTF_8)) | ||
} | ||
|
||
fun ByteArray.encodeToBase64(): String { | ||
return Base64.getEncoder().encodeToString(this) | ||
} | ||
|
||
fun String.decodeToString(): String { | ||
return String(Base64.getDecoder().decode(this), Charsets.UTF_8) | ||
} | ||
|
||
fun String.decodeToByteArray(): ByteArray { | ||
return Base64.getDecoder().decode(this) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
kotlin.code.style=official | ||
appVersion=1.1.0 | ||
libVersion=1.1.1 | ||
libVersion=1.1.1 | ||
|
||
#systemProp.http.proxyHost=127.0.0.1 | ||
#systemProp.http.proxyPort=12334 | ||
#systemProp.https.proxyHost=127.0.0.1 | ||
#systemProp.https.proxyPort=12334 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright © 2025 RTAkland | ||
* Author: RTAkland | ||
* Date: 2025/1/3 | ||
*/ | ||
|
||
|
||
package cn.rtast.kwsify.entity | ||
|
||
import java.nio.ByteBuffer | ||
|
||
data class OPCodePacket( | ||
val op: Int, | ||
val channel: String, | ||
) { | ||
fun toByteArray(): ByteArray { | ||
val buffer = ByteBuffer.allocate(4 + 4 + channel.toByteArray().size) | ||
return buffer.apply { | ||
putInt(op) | ||
putInt(channel.toByteArray().size) | ||
put(channel.toByteArray()) | ||
}.array() | ||
} | ||
|
||
companion object { | ||
fun fromByteArray(buffer: ByteBuffer): OPCodePacket { | ||
val op = buffer.int | ||
val channelSize = buffer.int | ||
val channelBytes = ByteArray(channelSize).apply { | ||
buffer.get(this) | ||
} | ||
val channel = String(channelBytes) | ||
return OPCodePacket(op, channel) | ||
} | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
src/main/kotlin/cn/rtast/kwsify/entity/OutboundMessageBytesPacket.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Copyright © 2025 RTAkland | ||
* Author: RTAkland | ||
* Date: 2025/1/3 | ||
*/ | ||
|
||
|
||
package cn.rtast.kwsify.entity | ||
|
||
import java.nio.ByteBuffer | ||
import java.util.* | ||
|
||
data class OutboundMessageBytesPacket( | ||
val op: Int, | ||
val body: ByteArray, | ||
val channel: String, | ||
val sender: Sender | ||
) { | ||
data class Sender( | ||
val host: String, | ||
val port: Int, | ||
val address: String, | ||
val uuid: UUID | ||
) | ||
|
||
|
||
fun toByteArray(): ByteArray { | ||
val hostSize = sender.host.toByteArray().size | ||
val addressSize = sender.address.toByteArray().size | ||
val channelSize = channel.toByteArray().size | ||
val totalLength = body.size + channelSize + hostSize + addressSize + 8 + 8 + 4 + 4 + 4 + 4 + 8 | ||
val buffer = ByteBuffer.allocate(totalLength) | ||
buffer.putInt(op) // op: 4 bytes | ||
buffer.putInt(body.size) // body size: 4 bytes | ||
buffer.put(body) // body data | ||
buffer.putInt(channelSize) // channel size: 4 bytes | ||
buffer.put(channel.toByteArray()) // channel data | ||
buffer.putInt(hostSize) // host size: 4 bytes | ||
buffer.put(sender.host.toByteArray()) // host data | ||
buffer.putInt(sender.port) // port: 4 bytes | ||
buffer.putInt(addressSize) // address size: 4 bytes | ||
buffer.put(sender.address.toByteArray()) // address data | ||
buffer.putLong(sender.uuid.mostSignificantBits) // UUID most significant bits: 8 bytes | ||
buffer.putLong(sender.uuid.leastSignificantBits) // UUID least significant bits: 8 bytes | ||
return buffer.array() | ||
} | ||
|
||
companion object { | ||
fun fromByteArray(bytes: ByteArray): OutboundMessageBytesPacket { | ||
val buffer = ByteBuffer.wrap(bytes) | ||
val op = buffer.getInt() | ||
val bodySize = buffer.getInt() | ||
val body = ByteArray(bodySize) | ||
buffer.get(body) | ||
val channelSize = buffer.getInt() | ||
val channelBytes = ByteArray(channelSize) | ||
buffer.get(channelBytes) | ||
val channel = String(channelBytes) | ||
val hostSize = buffer.getInt() | ||
val hostBytes = ByteArray(hostSize) | ||
buffer.get(hostBytes) | ||
val host = String(hostBytes) | ||
val port = buffer.getInt() | ||
val addressSize = buffer.getInt() | ||
val addressBytes = ByteArray(addressSize) | ||
buffer.get(addressBytes) | ||
val address = String(addressBytes) | ||
val mostSignificantBits = buffer.getLong() | ||
val leastSignificantBits = buffer.getLong() | ||
val uuid = UUID(mostSignificantBits, leastSignificantBits) | ||
val sender = Sender(host, port, address, uuid) | ||
return OutboundMessageBytesPacket(op, body, channel, sender) | ||
} | ||
} | ||
|
||
|
||
override fun equals(other: Any?): Boolean { | ||
if (this === other) return true | ||
if (javaClass != other?.javaClass) return false | ||
other as OutboundMessageBytesPacket | ||
return op == other.op && | ||
body.contentEquals(other.body) && | ||
channel == other.channel && | ||
sender == other.sender | ||
} | ||
|
||
override fun hashCode(): Int { | ||
var result = op | ||
result = 31 * result + body.contentHashCode() | ||
result = 31 * result + channel.hashCode() | ||
result = 31 * result + (sender?.hashCode() ?: 0) | ||
return result | ||
} | ||
} |
24 changes: 0 additions & 24 deletions
24
src/main/kotlin/cn/rtast/kwsify/entity/OutboundMessagePacket.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.