Path: redb-route/redb.Route.Tcp/TcpCodec.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/TcpCodec.cs ---- using System.Buffers.Binary; using System.IO; using System.Text; namespace redb.Route.Tcp; /// /// Codec for reading and writing framed TCP messages. /// Supports Raw, TextLine, and LengthPrefixed framing modes. /// Thread-safe: stateless, all state is in the stream. /// internal static class TcpCodec { /// /// Reads one framed message from the stream. /// Returns null if the connection is closed (0 bytes read). /// public static async Task ReadMessageAsync( Stream stream, TcpFraming framing, string delimiter, int bufferSize, CancellationToken ct) { return framing switch { TcpFraming.TextLine => await ReadTextLineAsync(stream, delimiter, bufferSize, ct).ConfigureAwait(false), TcpFraming.LengthPrefixed => await ReadLengthPrefixedAsync(stream, ct).ConfigureAwait(false), _ => await ReadRawAsync(stream, bufferSize, ct).ConfigureAwait(false) }; } /// /// Writes one framed message to the stream. /// public static async Task WriteMessageAsync( Stream stream, byte[] data, TcpFraming framing, string delimiter, Encoding encoding, CancellationToken ct) { switch (framing) { case TcpFraming.TextLine: await stream.WriteAsync(data, ct).ConfigureAwait(false); var delimBytes = encoding.GetBytes(delimiter); await stream.WriteAsync(delimBytes, ct).ConfigureAwait(false); break; case TcpFraming.LengthPrefixed: var header = new byte[4]; BinaryPrimitives.WriteInt32BigEndian(header, data.Length); await stream.WriteAsync(header, ct).ConfigureAwait(false); await stream.WriteAsync(data, ct).ConfigureAwait(false); break; default: // Raw await stream.WriteAsync(data, ct).ConfigureAwait(false); break; } await stream.FlushAsync(ct).ConfigureAwait(false); } /// Reads bytes until the delimiter is encountered. private static async Task ReadTextLineAsync( Stream stream, string delimiter, int bufferSize, CancellationToken ct) { // We read byte-by-byte into a buffer, scanning for the delimiter. // For high-throughput, a PipeReader would be better, but this is simple and correct. var buffer = new MemoryStream(256); var delimBytes = Encoding.UTF8.GetBytes(delimiter); var matchPos = 0; var singleByte = new byte[1]; while (true) { var read = await stream.ReadAsync(singleByte, ct).ConfigureAwait(false); if (read == 0) return buffer.Position > 0 ? buffer.ToArray() : null; buffer.WriteByte(singleByte[0]); if (singleByte[0] == delimBytes[matchPos]) { matchPos++; if (matchPos == delimBytes.Length) { // Remove delimiter from the result var result = buffer.ToArray(); return result.AsSpan(0, result.Length - delimBytes.Length).ToArray(); } } else { matchPos = singleByte[0] == delimBytes[0] ? 1 : 0; } } } /// Reads a 4-byte big-endian length header, then exactly that many bytes. private static async Task ReadLengthPrefixedAsync(Stream stream, CancellationToken ct) { var header = new byte[4]; var headerRead = await ReadExactAsync(stream, header, ct).ConfigureAwait(false); if (!headerRead) return null; var length = BinaryPrimitives.ReadInt32BigEndian(header); if (length <= 0) return []; var payload = new byte[length]; var payloadRead = await ReadExactAsync(stream, payload, ct).ConfigureAwait(false); if (!payloadRead) return null; return payload; } /// Reads a single chunk of available data (raw mode). private static async Task ReadRawAsync(Stream stream, int bufferSize, CancellationToken ct) { var buffer = new byte[bufferSize]; var read = await stream.ReadAsync(buffer, ct).ConfigureAwait(false); if (read == 0) return null; return buffer.AsSpan(0, read).ToArray(); } /// Reads exactly buffer.Length bytes or returns false on EOF. private static async Task ReadExactAsync(Stream stream, byte[] buffer, CancellationToken ct) { var offset = 0; while (offset < buffer.Length) { var read = await stream.ReadAsync(buffer.AsMemory(offset), ct).ConfigureAwait(false); if (read == 0) return false; offset += read; } return true; } }