All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.com.caesarealabs.rpc4k.runtime.implementation.EngineUtils.kt Maven / Gradle / Ivy

There is a newer version: 0.13.0
Show newest version
package com.caesarealabs.rpc4k.runtime.implementation

import com.caesarealabs.rpc4k.runtime.api.*
import com.caesarealabs.rpc4k.runtime.api.S2CEventMessage

public suspend fun ServerConfig.acceptEventSubscription(bytes: ByteArray, connection: EventConnection) {
//    val eventManager = engine.eventManager as EventManager
    println("Accept event subscription: ${bytes.decodeToString()}")
    try {
        when (val message = C2SEventMessage.fromByteArray(bytes)) {
            is C2SEventMessage.Subscribe -> eventManager.subscribe(message, connection)
            is C2SEventMessage.Unsubscribe -> eventManager.unsubscribe(message.event, message.listenerId)
        }
    } catch (e: InvalidRpcRequestException) {
        Rpc4kLogger.warn("Invalid client event message", e)
        // RpcServerException messages are trustworthy
        sendOrDrop(connection, S2CEventMessage.SubscriptionError("Invalid client event message: ${e.message}").toByteArray())
    } catch (e: Throwable) {
        Rpc4kLogger.error("Failed to handle request", e)
        sendOrDrop(connection, S2CEventMessage.SubscriptionError("Server failed to process subscription").toByteArray())
    }
}

public suspend fun  RpcServerEngine.SingleCall.Writing.routeRpcs(input: I, output: O, config: ServerConfig) {
    routeRpcCallImpl(input, output, config)
}

public suspend fun  RpcServerEngine.SingleCall.Returning.routeRpcs(input: I, config: ServerConfig): O {
    return routeRpcCallImpl(input, null, config)!!
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy