Table of Contents

Class BaseConnection

Namespace
DuetAPIClient
Assembly
DuetAPIClient.dll

Base class for connections that access the control server via the Duet API using a UNIX socket

public abstract class BaseConnection : IDisposable
Inheritance
BaseConnection
Implements
Derived
Inherited Members

Remarks

Create a new connection instance

Constructors

BaseConnection(ConnectionMode)

Base class for connections that access the control server via the Duet API using a UNIX socket

protected BaseConnection(ConnectionMode mode)

Parameters

mode ConnectionMode

Mode of the new connection

Remarks

Create a new connection instance

Fields

_unixSocket

Socket used for inter-process communication

protected readonly Socket _unixSocket

Field Value

Socket

Properties

Id

Identifier of this connection

public int Id { get; }

Property Value

int
See Also

IsConnected

Returns true if the socket is still connected

public bool IsConnected { get; }

Property Value

bool

Methods

Close()

Closes the current connection and disposes it

public void Close()

Connect(ClientInitMessage, string?)

Establishes a connection to the given UNIX socket file

protected void Connect(ClientInitMessage initMessage, string? socketPath)

Parameters

initMessage ClientInitMessage

Init message to send to the server

socketPath string

Optional path to the DCS UNIX socket file

Exceptions

IncompatibleVersionException

API level is incompatible

IOException

Connection mode is unavailable

SocketException

Connection has been closed

ConnectAsync(ClientInitMessage, string?, CancellationToken)

Establishes a connection to the given UNIX socket file asynchronously

protected Task ConnectAsync(ClientInitMessage initMessage, string? socketPath, CancellationToken cancellationToken)

Parameters

initMessage ClientInitMessage

Init message to send to the server

socketPath string

Optional path to the DCS UNIX socket file

cancellationToken CancellationToken

Optional cancellation token

Returns

Task

Asynchronous task

Exceptions

IncompatibleVersionException

API level is incompatible

IOException

Connection mode is unavailable

OperationCanceledException

Operation has been cancelled

SocketException

Connection has been closed

Dispose()

Cleans up the current connection and all resources associated to it

public void Dispose()

Dispose(bool)

Protected dipose implementation

protected virtual void Dispose(bool disposing)

Parameters

disposing bool

True if this instance is being disposed

~BaseConnection()

Finalizer of this class

protected ~BaseConnection()

PerformCommand(BaseCommand)

Perform an arbitrary command

protected void PerformCommand(BaseCommand command)

Parameters

command BaseCommand

Command to run

Exceptions

InternalServerException

Deserialized internal error from DCS

SocketException

Connection has been closed

PerformCommandAsync(BaseCommand, CancellationToken)

Perform an arbitrary command asynchronously

protected Task PerformCommandAsync(BaseCommand command, CancellationToken cancellationToken)

Parameters

command BaseCommand

Command to run

cancellationToken CancellationToken

Cancellation token

Returns

Task

Command result

Exceptions

InternalServerException

Deserialized internal error from DCS

OperationCanceledException

Operation has been cancelled

SocketException

Connection has been closed

PerformCommandAsync<T>(BaseCommand, CancellationToken)

Perform an arbitrary command asynchronously

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

Parameters

command BaseCommand

Command to run

cancellationToken CancellationToken

Cancellation token

Returns

Task<T>

Command result

Type Parameters

T

Type of the command result

Exceptions

InternalServerException

Deserialized internal error from DCS

OperationCanceledException

Operation has been cancelled

SocketException

Connection has been closed

PerformCommand<T>(BaseCommand)

Perform an arbitrary command

protected T PerformCommand<T>(BaseCommand command)

Parameters

command BaseCommand

Command to run

Returns

T

Command result

Type Parameters

T

Type of the command result

Exceptions

InternalServerException

Deserialized internal error from DCS

SocketException

Connection has been closed

Poll()

Check if the connection is still alive

public void Poll()

Exceptions

SocketException

Connection is no longer available

ReceiveCommandAsync<T>(CancellationToken)

Receive a deserialized object from the server asynchronously

protected ValueTask<T> ReceiveCommandAsync<T>(CancellationToken cancellationToken) where T : BaseCommand

Parameters

cancellationToken CancellationToken

Cancellation token

Returns

ValueTask<T>

Received object

Type Parameters

T

Type of the received object

Exceptions

OperationCanceledException

Operation has been cancelled

SocketException

Connection has been closed

ReceiveCommand<T>()

Receive a deserialized object from the server

protected T ReceiveCommand<T>() where T : BaseCommand

Returns

T

Received object

Type Parameters

T

Type of the received object

Exceptions

SocketException

Connection has been closed

ReceiveJsonDocument()

Receive plain JSON document

protected JsonDocument ReceiveJsonDocument()

Returns

JsonDocument

Partially deserialized data

Exceptions

SocketException

Connection has been closed

ReceiveJsonDocumentAsync(CancellationToken)

Receive plain JSON document asynchronously

protected ValueTask<JsonDocument> ReceiveJsonDocumentAsync(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

Cancellation token

Returns

ValueTask<JsonDocument>

Partially deserialized data

Exceptions

OperationCanceledException

Operation has been cancelled

SocketException

Connection has been closed

SendCommand(BaseCommand)

Serialize a command and send it to the server

protected void SendCommand(BaseCommand command)

Parameters

command BaseCommand

Command to send

Exceptions

OperationCanceledException

Operation has been cancelled

SocketException

Message could not be processed

SendCommandAsync(BaseCommand, CancellationToken)

Serialize a command and send it to the server asynchronously

protected ValueTask SendCommandAsync(BaseCommand command, CancellationToken cancellationToken)

Parameters

command BaseCommand

Command to send

cancellationToken CancellationToken

Cancellation token

Returns

ValueTask

Asynchronous task

Exceptions

OperationCanceledException

Operation has been cancelled

SocketException

Message could not be processed

SendInitMessage(InitMessage)

Serialize an init message and send it to the server

protected void SendInitMessage(InitMessage message)

Parameters

message InitMessage

Message to send

Exceptions

SocketException

Message could not be processed

SendInitMessageAsync(InitMessage, CancellationToken)

Serialize an init message and send it to the server asynchronously

protected ValueTask SendInitMessageAsync(InitMessage message, CancellationToken cancellationToken)

Parameters

message InitMessage

Message to send

cancellationToken CancellationToken

Cancellation token

Returns

ValueTask

Asynchronous task

Exceptions

OperationCanceledException

Operation has been cancelled

SocketException

Message could not be processed