commonMain.com.caesarealabs.rpc4k.runtime.api.S2CEventMessage.kt Maven / Gradle / Ivy
package com.caesarealabs.rpc4k.runtime.api
import com.caesarealabs.rpc4k.runtime.implementation.fastConcat
internal sealed interface S2CEventMessage {
data class Emitted(val listenerId: String, val payload: ByteArray) : S2CEventMessage {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is Emitted) return false
if (listenerId != other.listenerId) return false
if (!payload.contentEquals(other.payload)) return false
return true
}
override fun hashCode(): Int {
var result = listenerId.hashCode()
result = 31 * result + payload.contentHashCode()
return result
}
}
data class SubscriptionError(val error: String) : S2CEventMessage
fun toByteArray(): ByteArray = when (this) {
is Emitted -> EmittedType.encodeToByteArray().fastConcat(Rpc.ColonCode, listenerId.encodeToByteArray(), payload)
is SubscriptionError -> ErrorType.encodeToByteArray().fastConcat(Rpc.ColonCode, error.encodeToByteArray())
}
companion object {
private const val EmittedType = "event"
private const val ErrorType = "error"
fun fromString(string: String): S2CEventMessage {
val split = string.split(":")
val type = split[0]
when (type) {
EmittedType -> {
if (split.size < 3) error("Malformed RPC event: $string")
val listenerId = split[1]
//TODO: optimize splitting and joining
val payload = split.drop(2).joinToString(":")
return Emitted(listenerId, payload.encodeToByteArray())
}
ErrorType -> {
return SubscriptionError(split.drop(1).joinToString(":"))
}
else -> {
error("Malformed RPC message, no type specified: $string")
}
}
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy