Class Connection
Wrapper around UNIX socket connections
Implements
Inherited Members
Namespace: DuetControlServer.IPC
Assembly: DuetControlServer.dll
Syntax
public sealed class Connection : IDisposable
Constructors
Connection(Socket)
Constructor for new connections
Declaration
public Connection(Socket socket)
Parameters
Type | Name | Description |
---|---|---|
Socket | socket | New UNIX socket |
Properties
ApiVersion
API version of the client
Declaration
public int ApiVersion { get; set; }
Property Value
Type | Description |
---|---|
int |
See Also
Id
Identifier of this connection
Declaration
public int Id { get; }
Property Value
Type | Description |
---|---|
int |
IsConnected
Indicates if the connection is still available
Declaration
public bool IsConnected { get; }
Property Value
Type | Description |
---|---|
bool |
IsRoot
Whether the connection is from the root user
Declaration
public bool IsRoot { get; }
Property Value
Type | Description |
---|---|
bool |
Permissions
Permissions of this connection
Declaration
public SbcPermissions Permissions { get; }
Property Value
Type | Description |
---|---|
SbcPermissions |
PluginId
Name of the connected plugin
Declaration
public string? PluginId { get; }
Property Value
Type | Description |
---|---|
string |
UnixSocket
Socket holding the connection of the UNIX socket
Declaration
public Socket UnixSocket { get; }
Property Value
Type | Description |
---|---|
Socket |
Methods
AssignPermissions()
Get the peer credentials and assign the available permissions
Declaration
public Task<bool> AssignPermissions()
Returns
Type | Description |
---|---|
Task<bool> | True if permissions could be assigned |
CheckCommandPermissions(Type[])
Check if any of the given commands may be executed by this connection
Declaration
public bool CheckCommandPermissions(Type[] supportedCommands)
Parameters
Type | Name | Description |
---|---|---|
Type[] | supportedCommands | List of supported commands |
Returns
Type | Description |
---|---|
bool | True if any command may be executed |
CheckPermissions(Type)
Check if the current permissions are sufficient to execute this command
Declaration
public void CheckPermissions(Type commandType)
Parameters
Type | Name | Description |
---|---|---|
Type | commandType | Command type to check |
Exceptions
Type | Condition |
---|---|
UnauthorizedAccessException | Permissions are insufficient |
Close()
Close the socket before shutting down
Declaration
public void Close()
Dispose()
Dispose this connection
Declaration
public void Dispose()
Poll()
Check if the connection is still alive
Declaration
public void Poll()
Exceptions
Type | Condition |
---|---|
SocketException | Connection is no longer available |
ReceiveCommand()
Receive a fully-populated instance of a BaseCommand from the client
Declaration
public ValueTask<BaseCommand> ReceiveCommand()
Returns
Type | Description |
---|---|
ValueTask<BaseCommand> | Received command or null if nothing could be read |
Exceptions
Type | Condition |
---|---|
ArgumentException | Received bad command |
SocketException | Connection has been closed |
ReceiveJson()
Read a generic JSON object from the socket
Declaration
public ValueTask<JsonDocument> ReceiveJson()
Returns
Type | Description |
---|---|
ValueTask<JsonDocument> | JsonDocument for deserialization |
Exceptions
Type | Condition |
---|---|
OperationCanceledException | Operation has been cancelled |
SocketException | Connection has been closed |
ReceivePlainJson()
Read a plain JSON object as a string from the socket
Declaration
public ValueTask<string> ReceivePlainJson()
Returns
Type | Description |
---|---|
ValueTask<string> | JsonDocument for deserialization |
Exceptions
Type | Condition |
---|---|
OperationCanceledException | Operation has been cancelled |
SocketException | Connection has been closed |
ReceiveResponse()
Read a generic response from the socket
Declaration
public ValueTask<BaseResponse> ReceiveResponse()
Returns
Type | Description |
---|---|
ValueTask<BaseResponse> | Deserialized base response |
Exceptions
Type | Condition |
---|---|
OperationCanceledException | Operation has been cancelled |
SocketException | Connection has been closed |
Send(object)
Send a JSON object to the client
Declaration
public Task Send(object obj)
Parameters
Type | Name | Description |
---|---|---|
object | obj | Object to send |
Returns
Type | Description |
---|---|
Task | Asynchronous task |
Exceptions
Type | Condition |
---|---|
SocketException | Message could not be sent |
SendResponse(object?)
Send a response to the client. The given object is send either in an empty, error, or standard response body
Declaration
public Task SendResponse(object? obj = null)
Parameters
Type | Name | Description |
---|---|---|
object | obj | Object to send |
Returns
Type | Description |
---|---|
Task | Asynchronous task |
Exceptions
Type | Condition |
---|---|
SocketException | Message could not be sent |
Send<T>(T)
Send a JSON object to the client
Declaration
public Task Send<T>(T obj)
Parameters
Type | Name | Description |
---|---|---|
T | obj | Object to send |
Returns
Type | Description |
---|---|
Task | Asynchronous task |
Type Parameters
Name | Description |
---|---|
T | Object type |
Exceptions
Type | Condition |
---|---|
SocketException | Message could not be sent |