Core Library0.xAPI reference

Choose documentation 3

latest

Current version

latest
English

Early release libtmux is released, and its APIs may still change between releases. This site's structure and URLs may change too.

MoreLess about this notice

method libtmux-dotnet

Server.CreateOwnedAsync

Starts a server and takes ownership of it.

Source and package
Qualified name
LibTmux.Server.CreateOwnedAsync
Namespace
LibTmux
Declared in
Server
Package
LibTmux
Source
src/LibTmux/Server.Lifecycle.cs

Description

The scope holds an endpoint rather than a materialized server, because a tmux server with no sessions exits at once. Value stays that endpoint after a session is created through it; its session, window, and pane listings and CaptureSnapshotAsync() discover the live server on each call, and the objects they return carry the discovered handle.

On the socket tmux picks when a caller names none, the server in TMUX or else the default socket, a server already listening is refused rather than adopted: disposing the scope stops the server, and on a developer's machine that socket holds the one they are using. A named socket or path stays the caller's to adopt deliberately. The check and the start are two tmux calls, so two callers that own the default socket at the same moment can both pass it; give each owned server a socket of its own.

A call that fails or is cancelled after starting a server stops it, since no scope reaches the caller to do so; a server it found running is left.

Parameters
Returns

A scope that stops the server when disposed.

Errors

Examples

Distinguish an endpoint handle from a discovered server. Connection discovery returns an immutable replacement without capturing child relations. Use Linux or macOS with tmux 3.3a or newer, the .NET 10 SDK or newer, and the .NET 8 and 10 runtimes. In an empty directory, save this project as Example.csproj. It references the 0.1.1 release of LibTmux on NuGet: View source

Example.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net8.0;net10.0</TargetFrameworks>
<LangVersion>12.0</LangVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>disable</ImplicitUsings>
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<AnalysisLevel>10-recommended</AnalysisLevel>
</PropertyGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<PackageReference Include="LibTmux" Version="[0.1.1]" />
</ItemGroup>
</Project>

Save this complete program as Program.cs. It creates a private server; await using disposes its owned scopes after success, cancellation or failure. The complete file contains explicit imports, a top-level async entry point and a cancellation deadline. View source

Program.csComplete program
using System;
using System.IO;
using System.Threading;
using LibTmux;
if (OperatingSystem.IsWindows())
{
throw new PlatformNotSupportedException("This example requires tmux on Linux or macOS.");
}
using CancellationTokenSource deadline = new(TimeSpan.FromSeconds(10));
CancellationToken token = deadline.Token;
string? testTmux = Environment.GetEnvironmentVariable("LIBTMUX_TEST_TMUX");
string? configured = !string.IsNullOrWhiteSpace(testTmux)
? testTmux
: Environment.GetEnvironmentVariable("LIBTMUX_TMUX");
string binary = string.IsNullOrWhiteSpace(configured) ? "tmux" : configured;
using TemporaryDirectory socketRoot = new();
ServerConnectionOptions options = new()
{
SocketPath = Path.Combine(socketRoot.FullName, "tmux.sock"),
ConfigurationFile = "/dev/null",
TmuxBinaryPath = binary,
};
// Open records the endpoint without running tmux or reading its state.
Server endpoint = Server.Open(options);
if (endpoint.IsMaterialized)
{
throw new InvalidOperationException("Open must not discover a server.");
}
await using OwnedServerScope owned = await Server.CreateOwnedAsync(options, token);
await using OwnedSessionScope session = await owned.Value.CreateOwnedSessionAsync(
new NewSessionRequest { Name = "demo", Command = "/bin/cat" }, token);
// Connect discovers the live daemon and returns a new materialized handle.
Server connected = await endpoint.ConnectAsync(token);
Server sameEndpoint = await Server.ConnectAsync(options, token);
if (!connected.IsMaterialized || !sameEndpoint.IsMaterialized || endpoint.IsMaterialized)
{
throw new InvalidOperationException("Connecting must leave the original handle unchanged.");
}
if (connected.Generation != sameEndpoint.Generation || connected.Sessions.IsCaptured)
{
throw new InvalidOperationException("Connection discovery must not capture the hierarchy.");
}
Console.WriteLine("Open records an endpoint; Connect discovers the running server.");
Console.WriteLine("The original handle stays unmaterialized; relations remain uncaptured.");
internal sealed class TemporaryDirectory : IDisposable
{
private readonly DirectoryInfo _directory = Directory.CreateTempSubdirectory("libtmux-example-");
public string FullName => _directory.FullName;
public void Dispose() => _directory.Delete(recursive: true);
}

Build the project and run both supported frameworks. Each program checks its result and exits unsuccessfully after an unhandled error:

Terminal window
$ dotnet build Example.csproj --configuration Release && \
dotnet bin/Release/net8.0/Example.dll && \
dotnet bin/Release/net10.0/Example.dll

Expected program output from each framework:

Open records an endpoint; Connect discovers the running server.
The original handle stays unmaterialized; relations remain uncaptured.

Recorded calls

source calls
No call edges in the C# model.

The C# 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 LibTmux.Server.CreateOwnedAsync.

tmux command

task material

The tmux commands behind CreateOwnedAsync.

tmux C API

task material

The C declarations in tmux that implement these commands.

Guides

task material

Pages that discuss CreateOwnedAsync.

Esc
Search in

Type to search.

Reference previewOpen reference