Path: redb-route/redb.Route.Tcp/Fluent/TcpDsl.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/Fluent/TcpDsl.cs ---- using System.Text; namespace redb.Route.Tcp; /// /// Fluent entry point for TCP endpoints. /// /// // Consumer — listen for connections: /// .From(TcpDsl.Listen("0.0.0.0:9000").TextLine().InOut()) /// /// // Producer — connect to remote host: /// .To(TcpDsl.Connect("192.0.2.10:9000").TextLine().Reconnect()) /// /// public static class TcpDsl { /// Listen for incoming TCP connections (consumer). public static TcpBuilder Listen(string hostPort) => new(hostPort); /// Connect to a remote TCP endpoint (producer). public static TcpBuilder Connect(string hostPort) => new(hostPort); } /// Fluent builder for TCP endpoint URIs. Scheme: tcp. public sealed class TcpBuilder { private readonly string _hostPort; // Framing private bool _textLine; private bool _lengthPrefixed; private string? _delimiter; private string? _encoding; // Socket private bool? _keepAlive; private bool? _noDelay; private int? _receiveBufferSize; private int? _sendBufferSize; // Producer private int? _connectTimeout; private bool _reconnect; private int? _reconnectInterval; private int? _maxReconnectAttempts; // Consumer private int? _backlog; private int? _maxConnections; private bool? _inOut; // TLS private bool _ssl; private bool _trustAllCertificates; private string? _clientCertPath; private string? _clientCertPassword; private string? _sslCertPath; private string? _sslCertPassword; private string? _connectionFactory; private string? _sslTargetHost; internal TcpBuilder(string hostPort) { ArgumentException.ThrowIfNullOrWhiteSpace(hostPort); _hostPort = hostPort; } // ── Framing ───────────────────────────────────────────────────── /// Use newline-delimited text framing. public TcpBuilder TextLine() { _textLine = true; return this; } /// Use length-prefixed binary framing. public TcpBuilder LengthPrefixed() { _lengthPrefixed = true; return this; } /// Custom delimiter for text-line framing. public TcpBuilder Delimiter(string delimiter) { _delimiter = delimiter; return this; } /// Character encoding. Default utf-8. public TcpBuilder Encoding(string encoding) { _encoding = encoding; return this; } // ── Socket ────────────────────────────────────────────────────── /// Enable TCP keep-alive. Default true. public TcpBuilder KeepAlive(bool value = true) { _keepAlive = value; return this; } /// Set TCP_NODELAY. Default true. public TcpBuilder NoDelay(bool value = true) { _noDelay = value; return this; } /// Receive buffer size in bytes. public TcpBuilder ReceiveBufferSize(int size) { _receiveBufferSize = size; return this; } /// Send buffer size in bytes. public TcpBuilder SendBufferSize(int size) { _sendBufferSize = size; return this; } // ── Producer ──────────────────────────────────────────────────── /// Connection timeout in ms. public TcpBuilder ConnectTimeout(int ms) { _connectTimeout = ms; return this; } /// Enable auto-reconnect on disconnect. public TcpBuilder Reconnect(int intervalMs = 5000, int maxAttempts = 0) { _reconnect = true; _reconnectInterval = intervalMs; _maxReconnectAttempts = maxAttempts; return this; } // ── Consumer ──────────────────────────────────────────────────── /// TCP listen backlog. Default 128. public TcpBuilder Backlog(int size) { _backlog = size; return this; } /// Max concurrent connections. 0 = unlimited. public TcpBuilder MaxConnections(int max) { _maxConnections = max; return this; } /// Enable request-response pattern. public TcpBuilder InOut(bool value = true) { _inOut = value; return this; } // ── TLS ───────────────────────────────────────────────────────── /// Enable SSL/TLS. public TcpBuilder Ssl() { _ssl = true; return this; } /// Path to SSL certificate. public TcpBuilder SslCertPath(string path) { _sslCertPath = path; return this; } /// SSL certificate password. public TcpBuilder SslCertPassword(string password) { _sslCertPassword = password; return this; } /// References a named from the route registry instead of putting the certificate password in the URI. public TcpBuilder ConnectionFactory(string name) { _connectionFactory = name; return this; } /// Target host name for SSL validation. public TcpBuilder SslTargetHost(string host) { _sslTargetHost = host; return this; } /// /// Producer: accept any server certificate, including a self-signed one. Explicit on purpose -- /// turning certificate validation off should never be a side effect of another option. /// public TcpBuilder TrustAllCertificates() { _trustAllCertificates = true; return this; } /// Producer: PFX client certificate presented to a server that requires mTLS. public TcpBuilder ClientCert(string path, string? password = null) { _clientCertPath = path; _clientCertPassword = password; return this; } // ── Build ─────────────────────────────────────────────────────── public string Build() { var sb = new StringBuilder(); sb.Append("tcp:"); sb.Append(_hostPort); var sep = '?'; void Append(string key, string v) { sb.Append(sep); sb.Append(key); sb.Append('='); sb.Append(Uri.EscapeDataString(v)); sep = '&'; } void AppendInt(string key, int? v) { if (v.HasValue) Append(key, v.Value.ToString()); } void AppendBool(string key, bool v) { if (v) Append(key, "true"); } void AppendBoolN(string key, bool? v) { if (v.HasValue) Append(key, v.Value.ToString().ToLowerInvariant()); } void AppendStr(string key, string? v) { if (v != null) Append(key, v); } AppendBool("textLine", _textLine); AppendBool("lengthPrefixed", _lengthPrefixed); AppendStr("delimiter", _delimiter); AppendStr("encoding", _encoding); AppendBoolN("keepAlive", _keepAlive); AppendBoolN("noDelay", _noDelay); AppendInt("receiveBufferSize", _receiveBufferSize); AppendInt("sendBufferSize", _sendBufferSize); AppendInt("connectTimeout", _connectTimeout); AppendBool("reconnect", _reconnect); AppendInt("reconnectInterval", _reconnectInterval); AppendInt("maxReconnectAttempts", _maxReconnectAttempts); AppendInt("backlog", _backlog); AppendInt("maxConnections", _maxConnections); AppendBoolN("inOut", _inOut); AppendBool("ssl", _ssl); AppendStr("sslCertPath", _sslCertPath); AppendStr("sslCertPassword", _sslCertPassword); AppendStr("connectionFactory", _connectionFactory); AppendStr("sslTargetHost", _sslTargetHost); AppendBool("trustAllCertificates", _trustAllCertificates); AppendStr("clientCertPath", _clientCertPath); AppendStr("clientCertPassword", _clientCertPassword); return sb.ToString(); } public static implicit operator string(TcpBuilder b) => b.Build(); public override string ToString() => Build(); }