withServer
Opens a Server, runs block, and closes it — even if block throws or is cancelled.
Source and package
- Qualified name
-
io.github .libtmux .kotlin. with Server - Package
- io.github.libtmux.kotlin
- Distribution
- io.github.libtmux:libtmux-kotlin
- Source
- libtmux-kotlin/src/main/kotlin/io/github/libtmux/kotlin/Scopes.kt
- public
Kotlin suspending functionsAllows a function to call suspending operations and pause without blocking a thread. It does not start a coroutine by itself. fun <R> withServer( config: ServerConfig , policy: ExecutionPolicy = ExecutionPolicy .default(config), block:Kotlin suspending functionsAllows a function to call suspending operations and pause without blocking a thread. It does not start a coroutine by itself. (Server) -> R, ): R -
Description
The rule this follows, stated once: the scope that acquired a Java
AutoCloseableis the only thing that closes it.closeruns withNonCancellableso a cancelledblockstill releases the transport's threads.- Type parameters
- Parameters
-
-
Default
ExecutionPolicy.default(config) -
block(Kotlin suspending functionsAllows a function to call suspending operations and pause without blocking a thread. It does not start a coroutine by itself.)
Examples
Connect to a private tmux server. Use JDK 25, Gradle 9 or newer, and tmux 3.3a or newer on Linux, with
/bin/catand/bin/shavailable. In an empty directory, save this consumer setup assettings.gradle.kts:rootProject.name = "api-example"Save this consumer setup as
build.gradle.kts. It depends on the 0.1.2 release from Maven Central:plugins {kotlin("jvm") version "2.4.10"application}repositories { mavenCentral() }dependencies {implementation("io.github.libtmux:libtmux-kotlin:0.1.2")}java { toolchain { languageVersion.set(JavaLanguageVersion.of(25)) } }kotlin { jvmToolchain(25) }application { mainClass.set(providers.gradleProperty("exampleMain")) }Save this consumer setup as
gradle.properties: View sourcegradle.properties org.gradle.jvmargs=-Xmx2g -XX:MaxMetaspaceSize=768m -Dfile.encoding=UTF-8kotlin.daemon.jvmargs=-Xmx2gorg.gradle.workers.max=2Save this complete program as
src/main/kotlin/Connect.kt. It starts a private tmux server of its own and stops it when the program closes it. View sourcesrc/main/kotlin/Connect.ktComplete program package io.github.libtmux.examples.api.kotlinimport io.github.libtmux.kotlin.*import kotlin.system.exitProcessimport kotlinx.coroutines.runBlocking/** Connect to a private tmux server. */fun main() {try {runBlocking {withPrivateServer { server ->println("connected=${server.isAlive()}")}}} catch (error: Exception) {if (error is InterruptedException) Thread.currentThread().interrupt()System.err.println("Example failed: ${error.message}")exitProcess(1)}}Build the minimal consumer with Gradle and run the saved program. An operation or cleanup error makes the command exit unsuccessfully:
Terminal window $ gradle --console=plain --quiet installDist -PexampleMain=io.github.libtmux.examples.api.kotlin.ConnectKt && \build/install/api-example/bin/api-exampleExpected output:
connected=true
Recorded calls
source callsThe Kotlin reference records declarations, not calls, so this page shows no callers or callees. That is a limit of the extracted data, not a statement that nothing calls io.github.libtmux.kotlin.withServer.
Guides
task materialPages that discuss withServer.
- Concepts
- Examples