Class BaseConnection
Base class for connections that access the control server via the Duet API using a UNIX socket
Implements
Inherited Members
Namespace: DuetAPIClient
Assembly: DuetAPIClient.dll
Syntax
public abstract class BaseConnection : IDisposable
Constructors
BaseConnection(ConnectionMode)
Create a new connection instance
Declaration
protected BaseConnection(ConnectionMode mode)
Parameters
| Type | Name | Description |
|---|---|---|
| ConnectionMode | mode | Mode of the new connection |
Fields
_unixSocket
Socket used for inter-process communication
Declaration
protected readonly Socket _unixSocket
Field Value
| Type | Description |
|---|---|
| Socket |
Properties
Id
Identifier of this connection
Declaration
public int Id { get; }
Property Value
| Type | Description |
|---|---|
| int |
See Also
IsConnected
Returns true if the socket is still connected
Declaration
public bool IsConnected { get; }
Property Value
| Type | Description |
|---|---|
| bool |
Methods
Close()
Closes the current connection and disposes it
Declaration
public void Close()
Connect(ClientInitMessage, string, CancellationToken)
Establishes a connection to the given UNIX socket file
Declaration
protected Task Connect(ClientInitMessage initMessage, string socketPath, CancellationToken cancellationToken)
Parameters
| Type | Name | Description |
|---|---|---|
| ClientInitMessage | initMessage | Init message to send to the server |
| string | socketPath | Path to the UNIX socket file |
| CancellationToken | cancellationToken | Optional cancellation token |
Returns
| Type | Description |
|---|---|
| Task | Asynchronous task |
Exceptions
| Type | Condition |
|---|---|
| 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
Declaration
public void Dispose()
Dispose(bool)
Protected dipose implementation
Declaration
protected virtual void Dispose(bool disposing)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | disposing | True if this instance is being disposed |
~BaseConnection()
Finalizer of this class
Declaration
protected ~BaseConnection()
PerformCommand(BaseCommand, CancellationToken)
Perform an arbitrary command
Declaration
protected Task PerformCommand(BaseCommand command, CancellationToken cancellationToken)
Parameters
| Type | Name | Description |
|---|---|---|
| BaseCommand | command | Command to run |
| CancellationToken | cancellationToken | Cancellation token |
Returns
| Type | Description |
|---|---|
| Task | Command result |
Exceptions
| Type | Condition |
|---|---|
| InternalServerException | Deserialized internal error from DCS |
| OperationCanceledException | Operation has been cancelled |
| SocketException | Connection has been closed |
PerformCommand<T>(BaseCommand, CancellationToken)
Perform an arbitrary command
Declaration
protected Task<T> PerformCommand<T>(BaseCommand command, CancellationToken cancellationToken)
Parameters
| Type | Name | Description |
|---|---|---|
| BaseCommand | command | Command to run |
| CancellationToken | cancellationToken | Cancellation token |
Returns
| Type | Description |
|---|---|
| Task<T> | Command result |
Type Parameters
| Name | Description |
|---|---|
| T | Type of the command result |
Exceptions
| Type | Condition |
|---|---|
| InternalServerException | Deserialized internal error from DCS |
| OperationCanceledException | Operation has been cancelled |
| SocketException | Connection has been closed |
Poll()
Check if the connection is still alive
Declaration
public void Poll()
Exceptions
| Type | Condition |
|---|---|
| SocketException | Connection is no longer available |
ReceiveJson(CancellationToken)
Receive partially deserialized object from the server
Declaration
protected ValueTask<JsonDocument> ReceiveJson(CancellationToken cancellationToken)
Parameters
| Type | Name | Description |
|---|---|---|
| CancellationToken | cancellationToken | Cancellation token |
Returns
| Type | Description |
|---|---|
| ValueTask<JsonDocument> | Partially deserialized data |
Exceptions
| Type | Condition |
|---|---|
| OperationCanceledException | Operation has been cancelled |
| SocketException | Connection has been closed |
Receive<T>(CancellationToken)
Receive a deserialized object from the server
Declaration
protected ValueTask<T> Receive<T>(CancellationToken cancellationToken)
Parameters
| Type | Name | Description |
|---|---|---|
| CancellationToken | cancellationToken | Cancellation token |
Returns
| Type | Description |
|---|---|
| ValueTask<T> | Received object |
Type Parameters
| Name | Description |
|---|---|
| T | Type of the received object |
Exceptions
| Type | Condition |
|---|---|
| OperationCanceledException | Operation has been cancelled |
| SocketException | Connection has been closed |
Send(object, CancellationToken)
Serialize an arbitrary object into JSON and send it to the server plus NL
Declaration
protected ValueTask Send(object obj, CancellationToken cancellationToken)
Parameters
| Type | Name | Description |
|---|---|---|
| object | obj | Object to send |
| CancellationToken | cancellationToken | Cancellation token |
Returns
| Type | Description |
|---|---|
| ValueTask | Asynchronous task |
Exceptions
| Type | Condition |
|---|---|
| OperationCanceledException | Operation has been cancelled |
| SocketException | Message could not be processed |