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

commonMain.com.caesarealabs.rpc4k.runtime.api.RpcServer.kt Maven / Gradle / Ivy

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


// LOWPRIO: try to simplify this
public sealed interface RpcServerEngine {
//    public val eventManager: EventManager<*>
    /**
     * Returns true if the message was reached.
     *
     * Return false if the target of the connection is gone, and the connection should be removed
     */
    public suspend fun sendMessage(connection: EventConnection, bytes: ByteArray): Boolean

    public sealed interface SingleCall : RpcServerEngine {
        public suspend fun read(input: I): ByteArray
        public interface Returning : SingleCall {
            public suspend fun respond(body: ByteArray): O
            public suspend fun respondError(message: String, errorType: RpcError): O
        }

        public interface Writing : SingleCall {
            public suspend fun write(body: ByteArray, output: O)
            public suspend fun writeError(message: String, errorType: RpcError, output: O)
        }
    }

    public interface MultiCall : RpcServerEngine {
        public interface Instance {
            public fun start(wait: Boolean)
            public fun stop()
        }

        public fun create(config: ServerConfig): Instance
    }
}

/**
 * If the condition is not true, the current RPC call will return and error signaling that there is some problem with the input of the client.
 *
 * **SECURITY NOTE** - the message will be sent to clients. Make sure to not leak sensitive info.
 * Use this to verify inputs in service methods.
 */
public inline fun serverRequirement(condition: Boolean, message: () -> String) {
    if (!condition) {
        throw InvalidRpcRequestException(message())
    }
}

public enum class RpcError {
    InvalidRequest,
    InternalError
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy