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
modeConnectionModeMode of the new connection
Remarks
Create a new connection instance
Fields
_unixSocket
Socket used for inter-process communication
protected readonly Socket _unixSocket
Field Value
Properties
Id
Identifier of this connection
public int Id { get; }
Property Value
- See Also
IsConnected
Returns true if the socket is still connected
public bool IsConnected { get; }
Property Value
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
initMessageClientInitMessageInit message to send to the server
socketPathstringOptional 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
initMessageClientInitMessageInit message to send to the server
socketPathstringOptional path to the DCS UNIX socket file
cancellationTokenCancellationTokenOptional 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
disposingboolTrue 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
commandBaseCommandCommand 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
commandBaseCommandCommand to run
cancellationTokenCancellationTokenCancellation 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
commandBaseCommandCommand to run
cancellationTokenCancellationTokenCancellation token
Returns
- Task<T>
Command result
Type Parameters
TType 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
commandBaseCommandCommand to run
Returns
- T
Command result
Type Parameters
TType 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
cancellationTokenCancellationTokenCancellation token
Returns
- ValueTask<T>
Received object
Type Parameters
TType 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
TType 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
cancellationTokenCancellationTokenCancellation 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
commandBaseCommandCommand 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
commandBaseCommandCommand to send
cancellationTokenCancellationTokenCancellation 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
messageInitMessageMessage 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
messageInitMessageMessage to send
cancellationTokenCancellationTokenCancellation token
Returns
- ValueTask
Asynchronous task
Exceptions
- OperationCanceledException
Operation has been cancelled
- SocketException
Message could not be processed