Faactory.Channels.Flex 1.0.0

dotnet add package Faactory.Channels.Flex --version 1.0.0
                    
NuGet\Install-Package Faactory.Channels.Flex -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Faactory.Channels.Flex" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Faactory.Channels.Flex" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Faactory.Channels.Flex" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Faactory.Channels.Flex --version 1.0.0
                    
#r "nuget: Faactory.Channels.Flex, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Faactory.Channels.Flex@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Faactory.Channels.Flex&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Faactory.Channels.Flex&version=1.0.0
                    
Install as a Cake Tool

Channels - Flex Protocol

This project contains middleware to decode and encode messages using the Flex protocol. Only version 3 of the Flex protocol specification is supported. If you need support for an older specification, you must use an older version of this library.

Learn more about Channels

Getting started

Install the package from NuGet

dotnet add package Faactory.Channels.Flex

To enable the decoding of messages on the input pipeline, register the middleware with the channel builder.

IChannelBuilder channel = ...;

channel.AddFlexMiddleware();

Since Flex is a framing protocol, you'll still need to decode the modules payload. The existing pipeline will forward a Message object for you to handle. Each message contains a collection of modules representing the application payload. You can extend the pipeline with your own adapters to decode these modules, or you can just use the Message object as is in a handler.

Acknowledge messages

The library automatically writes to the output pipeline an acknowledge message for each message received. If required, you can disable this behavior by setting the AutoAcknowledge property to false in the options when registering the middleware. If you disable the automatic acknowledge, you'll need to manually write the acknowledge message to the output pipeline.

channel.AddFlexMiddleware( options =>
{
    // default is true
    options.AutoAcknowledge = false;
} );

Heartbeat messages are not acknowledged by the library, as defined by the protocol.

Acknowledge messages sent by the library are not encrypted. This behavior follows the Flex protocol recommendation to keep control packets lightweight. If you need to send encrypted acknowledgments, you'll need to write the message yourself.

Heartbeat messages

The Flex protocol describes heartbeat messages but does not enforce their use nor define any rules for them, leaving it up to the implementer to decide if and how to use them. If you'd like to set up automatic heartbeat messages, you can do so by setting an interval when registering the middleware.

channel.AddFlexMiddleware( options =>
{
    // default is null (disabled)
    options.HeartbeatInterval = TimeSpan.FromSeconds( 30 );
} );

Heartbeat messages received on the input pipeline will be forwarded as Heartbeat objects. You can write a handler to deal with them in accordance to your needs.

Heartbeat messages sent by the library are not encrypted. This behavior follows the Flex protocol recommendation to keep control packets lightweight. If you need to send encrypted heartbeats, you'll need to write the message yourself.

Output Pipeline

The library does not build on the output pipeline. Instead, it provides a message builder to help you create outgoing messages. Messages built this way will already be encoded and ready to be written to the output pipeline as a IReadableByteBuffer object. You can get the builder accessor from a IChannel or a IChannelContext instance. You can also inject the IMessageBuilderAccessor interface into your code.

IChannel channel = ...;

// create a message with a passthrough module
var passthrough = channel.GetMessageBuilderAccessor()
    .CreateBuilder()
    .AddModule(
        0xffff,
        Encoding.UTF8.GetBytes( "$ enable 0x0001" )
    )
    .Build();

await channel.WriteAsync( passthrough );

Message Flow

Flex decoding is implemented as middleware in the input pipeline.

Incoming byte buffers are first decoded into Packet objects. Packets are then interpreted as either Heartbeat or Message instances and forwarded to the rest of the pipeline.

flowchart LR
    IReadableByteBuffer --> Packet
    Packet --> Heartbeat
    Packet --> Message
    Message -.-> p["Your Pipeline"]
    Heartbeat -.-> p

Encrypted messages

The Flex protocol does not define a specific encryption algorithm, but it does define a mechanism to encapsulate encrypted payloads. To provide support for message encryption, the library includes middleware to decrypt encrypted messages by making use of a cryptographic service. Since the protocol does not define the encryption mechanism, you'll need to write your own cryptographic service by implementing the IMessageEncryptor interface and registering it with the channel builder.

public class MyMessageEncryptor : IMessageEncryptor
{
    public void Encrypt( ReadOnlySpan<byte> plainData, IWritableByteBuffer buffer )
    {
        // The plainData parameter contains the message header and payload before encryption.
    }

    public void Decrypt( ReadOnlySpan<byte> encryptedData, IWritableByteBuffer buffer )
    {
        // The encryptedData parameter contains the encrypted payload of the message.
        // The decrypted result must contain the original message header and payload.
    }
}

After implementing the IMessageEncryptor interface, register the service with the channel builder. When the middleware encounters an encrypted message, it will decrypt it using the registered service before forwarding it.

IServiceCollection services = ...;

services.AddSingleton<IMessageEncryptor, MyMessageEncryptor>();

Messages built with the message builder will also be encrypted if a cryptographic service is registered, with the exception of acknowledge and heartbeat messages.

IMessageBuilderAccessor builder = ...;

var passthrough = builder.CreateBuilder()
    .AddModule(
        0xffff,
        Encoding.UTF8.GetBytes( "$ enable 0x0001" )
    )
    .Build();
Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 116 3/5/2026
0.3.4 242 4/12/2024
0.3.3 201 4/11/2024
0.3.2 186 4/10/2024
0.3.1 192 4/10/2024
0.3.0 204 4/9/2024
0.2.0 231 1/30/2024
0.1.0 307 10/20/2023