Table of Contents

Class Connection

Namespace
DuetControlServer.IPC
Assembly
DuetControlServer.dll

Wrapper around UNIX socket connections

public sealed class Connection : IDisposable
Inheritance
Connection
Implements
Inherited Members

Remarks

Constructor for new connections

Constructors

Connection(Socket, CommandFactory, ILogger)

Wrapper around UNIX socket connections

public Connection(Socket socket, CommandFactory commandFactory, ILogger logger)

Parameters

socket Socket

New UNIX socket

commandFactory CommandFactory

Command factory to create commands

logger ILogger

Logger instance

Remarks

Constructor for new connections

Properties

ApiVersion

API version of the client

public int ApiVersion { get; set; }

Property Value

int
See Also

Id

Identifier of this connection

public int Id { get; }

Property Value

int

IsConnected

Indicates if the connection is still available

public bool IsConnected { get; }

Property Value

bool

IsRoot

Whether the connection is from the root user

public bool IsRoot { get; }

Property Value

bool

Permissions

Permissions of this connection

public SbcPermissions Permissions { get; }

Property Value

SbcPermissions

PluginId

Name of the connected plugin

public string? PluginId { get; }

Property Value

string

UnixSocket

Socket holding the connection of the UNIX socket

public Socket UnixSocket { get; }

Property Value

Socket

Methods

AssignPermissionsAsync(ObjectModel)

Get the peer credentials and assign the available permissions

public Task<bool> AssignPermissionsAsync(ObjectModel model)

Parameters

model ObjectModel

Object model

Returns

Task<bool>

True if permissions could be assigned

CheckCommandPermissions(Type[])

Check if any of the given commands may be executed by this connection

public bool CheckCommandPermissions(Type[] supportedCommands)

Parameters

supportedCommands Type[]

List of supported commands

Returns

bool

True if any command may be executed

CheckPermissions(Type)

Check if the current permissions are sufficient to execute this command

public void CheckPermissions(Type commandType)

Parameters

commandType Type

Command type to check

Exceptions

UnauthorizedAccessException

Permissions are insufficient

Close()

Close the socket before shutting down

public void Close()

Dispose()

Dispose this connection

public void Dispose()

PerformCommandAsync<T>(BaseCommand, CancellationToken)

Send a command and await its typed result. Mirrors BaseConnection.PerformCommandAsync<T> on the client side

public Task<T?> PerformCommandAsync<T>(BaseCommand command, CancellationToken cancellationToken)

Parameters

command BaseCommand

Command to send

cancellationToken CancellationToken

Cancellation token

Returns

Task<T>

Deserialized result

Type Parameters

T

Expected result type (must be registered in CommandContext)

Exceptions

InternalServerException

Server reported an error response

OperationCanceledException

Operation has been cancelled

SocketException

Connection has been closed

Poll()

Check if the connection is still alive

public void Poll()

Exceptions

SocketException

Connection is no longer available

ReceiveCommandAsync(Type[], CancellationToken)

Receive a fully-populated instance of a BaseCommand from the client

public ValueTask<BaseCommand> ReceiveCommandAsync(Type[] supportedCommands, CancellationToken cancellationToken)

Parameters

supportedCommands Type[]

List of supported commands

cancellationToken CancellationToken

Cancellation token

Returns

ValueTask<BaseCommand>

Received command or null if nothing could be read

Exceptions

ArgumentException

Received bad command

SocketException

Connection has been closed

ReceiveInitMessageAsync(CancellationToken)

Read a client init message from the socket

public ValueTask<ClientInitMessage> ReceiveInitMessageAsync(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

Cancellation token

Returns

ValueTask<ClientInitMessage>

Client init message

Exceptions

OperationCanceledException

Operation has been cancelled

SocketException

Connection has been closed

ReceiveResponseAsync(CancellationToken)

Read a generic response from the socket asynchronously

public ValueTask<BaseResponse> ReceiveResponseAsync(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

Cancellation token

Returns

ValueTask<BaseResponse>

Deserialized base response

Exceptions

OperationCanceledException

Operation has been cancelled

SocketException

Connection has been closed

ResolvePeerPluginIdAsync()

Resolve the peer PID to a plugin ID via the matching plugin service (root or non-root). Used by commands that need to identify the caller's plugin when PluginId was not assigned at connect time (notably root-owned plugins, which skip the PID lookup in AssignPermissionsAsync(ObjectModel)). Caches the result on PluginId so later permission checks can treat the connection as owner

public Task<string?> ResolvePeerPluginIdAsync()

Returns

Task<string>

Plugin id if matched, null otherwise

SendCommandAsync(BaseCommand)

Send a command to the client

public Task SendCommandAsync(BaseCommand command)

Parameters

command BaseCommand

Command to send

Returns

Task

Asynchronous task

Exceptions

SocketException

Message could not be sent

SendExceptionAsync(Exception)

Send an exception to the client. The given object is send either in an empty, error, or standard response body

public Task SendExceptionAsync(Exception e)

Parameters

e Exception

Exception to send

Returns

Task

Asynchronous task

Exceptions

SocketException

Message could not be sent

SendInitMessageAsync(InitMessage)

Send an init message to the client

public Task SendInitMessageAsync(InitMessage msg)

Parameters

msg InitMessage

Message to send

Returns

Task

Asynchronous task

Exceptions

SocketException

Message could not be sent

SendRawDataAsync(byte[])

Send raw data to the client

public Task SendRawDataAsync(byte[] data)

Parameters

data byte[]

Data to send

Returns

Task

Asynchronous task

Exceptions

SocketException

Message could not be sent

SendResponseAsync(object?)

Send a success response to the client

public Task SendResponseAsync(object? result = null)

Parameters

result object

Object to send

Returns

Task

Asynchronous task

Exceptions

SocketException

Message could not be sent