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
socketSocketNew UNIX socket
commandFactoryCommandFactoryCommand factory to create commands
loggerILoggerLogger instance
Remarks
Constructor for new connections
Properties
ApiVersion
API version of the client
public int ApiVersion { get; set; }
Property Value
- See Also
Id
Identifier of this connection
public int Id { get; }
Property Value
IsConnected
Indicates if the connection is still available
public bool IsConnected { get; }
Property Value
IsRoot
Whether the connection is from the root user
public bool IsRoot { get; }
Property Value
Permissions
Permissions of this connection
public SbcPermissions Permissions { get; }
Property Value
PluginId
Name of the connected plugin
public string? PluginId { get; }
Property Value
UnixSocket
Socket holding the connection of the UNIX socket
public Socket UnixSocket { get; }
Property Value
Methods
AssignPermissionsAsync(ObjectModel)
Get the peer credentials and assign the available permissions
public Task<bool> AssignPermissionsAsync(ObjectModel model)
Parameters
modelObjectModelObject model
Returns
CheckCommandPermissions(Type[])
Check if any of the given commands may be executed by this connection
public bool CheckCommandPermissions(Type[] supportedCommands)
Parameters
supportedCommandsType[]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
commandTypeTypeCommand 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
commandBaseCommandCommand to send
cancellationTokenCancellationTokenCancellation token
Returns
- Task<T>
Deserialized result
Type Parameters
TExpected 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
supportedCommandsType[]List of supported commands
cancellationTokenCancellationTokenCancellation 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
cancellationTokenCancellationTokenCancellation 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
cancellationTokenCancellationTokenCancellation 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
SendCommandAsync(BaseCommand)
Send a command to the client
public Task SendCommandAsync(BaseCommand command)
Parameters
commandBaseCommandCommand 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
eExceptionException 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
msgInitMessageMessage 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
databyte[]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
resultobjectObject to send
Returns
- Task
Asynchronous task
Exceptions
- SocketException
Message could not be sent