Table of Contents

Class CodeProcessor

Namespace
DuetControlServer.Codes
Assembly
DuetControlServer.dll

Main class delegating parallel G/M/T-code execution

[DiagnosticsPriority(0)]
public sealed class CodeProcessor : IDiagnostics
Inheritance
CodeProcessor
Implements
Inherited Members

Constructors

CodeProcessor(Expressions, ObjectModel, IHostApplicationLifetime, IServiceProvider)

Main class delegating parallel G/M/T-code execution

public CodeProcessor(Expressions expressions, ObjectModel model, IHostApplicationLifetime lifetime, IServiceProvider serviceProvider)

Parameters

expressions Expressions

Expression parser

model ObjectModel

Object model

lifetime IHostApplicationLifetime

Application lifetime

serviceProvider IServiceProvider

Service provider

Fields

CancellationTokenSources

List of cancellation tokens to cancel pending codes while they are waiting for their execution

public readonly CancellationTokenSource[] CancellationTokenSources

Field Value

CancellationTokenSource[]

Remarks

While it may appear nicer to move the cancellation functionality to the code pipeline itself, this coule lead to performance issues or unexpected behaviour due to intercepted codes. So leave it here for now

FileLocks

Lock around the files being written

public readonly AsyncLock[] FileLocks

Field Value

AsyncLock[]

FilesBeingWritten

Current stream writer of the files being written to (M28/M29)

public readonly StreamWriter?[] FilesBeingWritten

Field Value

StreamWriter[]

Processors

Processors per code channel

public readonly Lazy<ChannelProcessor[]> Processors

Field Value

Lazy<ChannelProcessor[]>

Methods

CancelCode(Code, Exception?)

Cancel a given code

public void CancelCode(Code code, Exception? e = null)

Parameters

code Code

Code to cancel

e Exception

Optional exception causing the cancellation

CancelPending(CodeChannel)

Cancel pending codes of the given channel

public void CancelPending(CodeChannel channel)

Parameters

channel CodeChannel

Channel to cancel codes from

CodeCompleted(Code)

Execute a given code on a given pipeline stage

public void CodeCompleted(Code code)

Parameters

code Code

Code to enqueue

DoSyncAsync(Code, CancellationToken)

Synchronize the File and File2 code streams, may only be called when a job is live

public Task<bool> DoSyncAsync(Code code, CancellationToken cancellationToken = default)

Parameters

code Code

Code to synchronize at

cancellationToken CancellationToken

Optional cancellation token

Returns

Task<bool>

True if the sync request was successful, false otherwise

Remarks

This must be called while the Job class is NOT locked and it must be called from the same code on File AND File2, else the sync request is never resolved (or at least not before the file is cancelled)

FlushAsync(CodeChannel, bool, CancellationToken)

Wait for all pending codes to finish

public Task<bool> FlushAsync(CodeChannel channel, bool flushAll = false, CancellationToken cancellationToken = default)

Parameters

channel CodeChannel

Code channel to wait for

flushAll bool

Flush all codes on all stack levels

cancellationToken CancellationToken

Optional cancellation token

Returns

Task<bool>

Whether the codes have been flushed successfully

FlushAsync(Code, bool, bool, bool, bool, CancellationToken)

Wait for all pending codes on the same stack level as the given code to finish. By default this replaces all expressions as well for convenient parsing by the code processors.

public Task<bool> FlushAsync(Code code, bool evaluateExpressions = true, bool evaluateAll = true, bool syncFileStreams = false, bool ifExecuting = true, CancellationToken cancellationToken = default)

Parameters

code Code

Code waiting for the flush

evaluateExpressions bool

Evaluate all expressions when pending codes have been flushed

evaluateAll bool

Evaluate the expressions or only SBC fields if evaluateExpressions is set to true

syncFileStreams bool

Whether the file streams are supposed to be synchronized (if applicable)

ifExecuting bool

Return true only if the corresponding code input is actually active (ignored if syncFileStreams is true)

cancellationToken CancellationToken

Optional cancellation token

Returns

Task<bool>

Whether the codes have been flushed successfully

FlushAsync(CodeFile, CancellationToken)

Wait for all pending codes of the given file to finish

public Task<bool> FlushAsync(CodeFile file, CancellationToken cancellationToken = default)

Parameters

file CodeFile

Code file

cancellationToken CancellationToken

Optional cancellation token

Returns

Task<bool>

Whether the codes have been flushed successfully

GetFirmwareState(CodeChannel)

Get the pipeline state of the firmware stage from a given channel

public PipelineStackItem GetFirmwareState(CodeChannel channel)

Parameters

channel CodeChannel

Returns

PipelineStackItem

Pop(CodeChannel)

Push a new state on the stack of a given pipeline. Only to be used by the SPI channel processor!

public void Pop(CodeChannel channel)

Parameters

channel CodeChannel

Code channel

PrintDiagnostics(StringBuilder)

Get diagnostics from every channel processor

public void PrintDiagnostics(StringBuilder builder)

Parameters

builder StringBuilder

String builder to write to

PurgeSyncRequestsFor(CodeFile)

Resolve all sync requests for a given file

public void PurgeSyncRequestsFor(CodeFile file)

Parameters

file CodeFile

File

Push(CodeChannel, CodeFile?)

Push a new state on the stack of a given channel procesor. Only to be used by the SPI channel processor!

public PipelineStackItem Push(CodeChannel channel, CodeFile? file = null)

Parameters

channel CodeChannel

Code channel

file CodeFile

Optional file

Returns

PipelineStackItem

Pipeline state

ResolveSyncRequestsAfter(long)

Resolve all sync requests that are after the given file position

public void ResolveSyncRequestsAfter(long filePosition)

Parameters

filePosition long

File position

SetJobFile(CodeChannel, CodeFile?)

Assign the job file to the given channel. Only used by the job tasks!

public void SetJobFile(CodeChannel channel, CodeFile? file)

Parameters

channel CodeChannel

Code channel

file CodeFile

Job file

StartCodeAsync(Code)

Start the execution of a given code

public ValueTask StartCodeAsync(Code code)

Parameters

code Code

Code to enqueue

Returns

ValueTask

Asynchronous task