Skip to content

Adapters

An adapter is a middleware component that can be executed at any point in the pipeline and it has a single conceptual purpose: to adapt data.

graph LR;
    in[/Data In/] --> Adapter --> out[/Data Out/]

An adapter is expected to forward data to next component in the pipeline, although that might not always be the case. If an adapter doesn't forward any data, the pipeline is interrupted and no other components will be executed.

Implementing an Adapter

Unless there are very specific needs, you should inherit your adapter from the ChannelAdapter<T> abstract class instead of implementing the IChannelAdapter interface directly.

You also need to indicate whether the adapter is meant for the input or/and the output pipeline. We do that by adding the interfaces IInputChannelAdapter or/and IOutputChannelAdapter respectively.

Here's an example of how to implement an adapter that adapts from a IByteBuffer. This adapter can only be added to the input pipeline.

public class MyChannelAdapter : ChannelAdapter<IByteBuffer>, IInputChannelAdapter
{
    public override Task ExecuteAsync( IAdapterContext context, IByteBuffer data )
    {
        // adapt/transform data
        var adaptedData = ...

        // forward adapted data
        context.Forward( adaptedData );
    }
}

Adapters and Buffers

Although raw data handling in the adapters can be done with Byte[], it is recommended to use a IByteBuffer instance instead, particularly for reading data.

When using a IByteBuffer, data received in the adapters that is not read will remain in the channel's internal input buffer. When more data is received, it is delivered again along with the newly received data - think of partial packets. If an adapter use a Byte[] instead, the data in the input buffer is copied to a new Byte[] instance, and the input buffer automatically marked as read and discarded.

Learn more about buffers.

Ready-made Adapters

In addition to the abstract ChannelAdapter<T> adapter, you have a few ready-made adapters that you can use.

Adapter Target Description
AnonymousChannelAdapter Input/Output A quick way to implement an anonymous adapter
BufferLengthAdapter Input Ensures the input buffer doesn't exceed in length