Path: redb-route/redb.Route.Tcp/TcpComponent.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Tcp/TcpComponent.cs
----
using Microsoft.Extensions.Logging;
using redb.Route.Abstractions;
using redb.Route.Extensions;
using redb.Route.Core;
namespace redb.Route.Tcp;
///
/// TCP component. Scheme: "tcp".
/// Producer: TCP client — connects to remote host and sends data.
/// Consumer: TCP server — listens on a port, accepts connections, receives data.
/// URI format: tcp:host:port?textLine=true&delimiter=\n&inOut=true
///
public class TcpComponent : ComponentBase
{
///
public override string Scheme => "tcp";
///
public override IEndpoint CreateEndpoint(EndpointUri uri)
{
ArgumentNullException.ThrowIfNull(uri);
var options = new TcpEndpointOptions();
// Parse host:port from URI path
ParseHostPort(uri.Path, options);
options.BindFromUri(uri.RawParameters);
// Named ConnectionFactory keeps the TLS certificate password out of the route URI.
if (!string.IsNullOrEmpty(options.ConnectionFactory))
{
// A set-but-unknown name fails loud -- never a silent fallback to URI params.
var factory = Context.GetRequiredFromRegistry(options.ConnectionFactory);
factory.ApplyTo(options, uri);
}
options.Validate();
return new TcpEndpoint(uri, this, options);
}
///
/// Parses "host:port" from the URI path segment.
/// Path format: "/host:port" or "host:port".
///
internal static void ParseHostPort(string path, TcpEndpointOptions options)
{
var clean = path.TrimStart('/');
var colonIdx = clean.LastIndexOf(':');
if (colonIdx > 0 && int.TryParse(clean[(colonIdx + 1)..], out var port))
{
options.Host = clean[..colonIdx];
options.Port = port;
}
else
{
options.Host = clean;
}
}
}
///
/// TCP endpoint. Creates either a producer (TCP client) or consumer (TCP server).
///
public class TcpEndpoint : EndpointBase
{
/// Creates a TCP endpoint.
public TcpEndpoint(EndpointUri uri, TcpComponent component, TcpEndpointOptions options)
: base(uri, component, options)
{
}
/// The endpoint options for external access.
internal TcpEndpointOptions EndpointOptions => Options;
///
public override IProducer CreateProducer()
{
return new TcpProducer(this, Options);
}
///
public override IConsumer CreateConsumer(IProcessor processor)
{
ArgumentNullException.ThrowIfNull(processor);
return new TcpConsumer(this, processor, Options);
}
}