PacketWriteExtensions
static class
Provides extension methods that frame packets into an IBufferWriter<T>, a
PipeWriter, or a Stream.
Declaration
public static class PacketWriteExtensions
| Namespace | McProtoNet.Transport.Framing |
| Assembly | McProtoNet.Transport.dll |
| Source | src/McProtoNet.Transport/Framing/PacketWriteExtensions.cs#L17 ↗ |
Remarks
Without compression a frame is the packet length as a varint followed by the packet. With compression the frame carries an extra varint after the length: 0 for a packet shorter than the threshold, which is then written as it is, or the uncompressed size for a packet compressed with zlib.
Methods
WritePacket(IBufferWriter<byte>, in ReadOnlySequence<byte>, int)
public static void WritePacket(this IBufferWriter<byte> writer, in ReadOnlySequence<byte> packet, int compressionThreshold = -1)
Frames one packet that already carries its varint id into the specified buffer writer, from a segmented buffer and without joining the segments first.
A packet that is compressed is copied into one pooled buffer first.
Parameters: writer (IBufferWriter<byte>), packet (ReadOnlySequence<byte>), compressionThreshold (int)
Exceptions: ArgumentNullException - writer is null.; OverflowException - packet is longer than int.MaxValue bytes.
WritePacket(IBufferWriter<byte>, int, in ReadOnlySequence<byte>, int)
public static void WritePacket(this IBufferWriter<byte> writer, int id, in ReadOnlySequence<byte> body, int compressionThreshold = -1)
Frames one packet into the specified buffer writer from a segmented body, prefixing it with the specified packet id and without joining the segments first.
A packet that is compressed is copied into one pooled buffer first.
Parameters: writer (IBufferWriter<byte>), id (int), body (ReadOnlySequence<byte>), compressionThreshold (int)
Exceptions: ArgumentNullException - writer is null.; OverflowException - The length of body and the packet id together exceed int.MaxValue bytes.
WritePacket(IBufferWriter<byte>, int, ReadOnlySpan<byte>, int)
public static void WritePacket(this IBufferWriter<byte> writer, int id, ReadOnlySpan<byte> body, int compressionThreshold = -1)
Frames one packet into the specified buffer writer, prefixing the body with the specified packet id.
Parameters: writer (IBufferWriter<byte>), id (int), body (ReadOnlySpan<byte>), compressionThreshold (int)
Exceptions: ArgumentNullException - writer is null.
WritePacket(IBufferWriter<byte>, ReadOnlySpan<byte>, int)
public static void WritePacket(this IBufferWriter<byte> writer, ReadOnlySpan<byte> packet, int compressionThreshold = -1)
Frames one packet that already carries its varint id into the specified buffer writer.
Parameters: writer (IBufferWriter<byte>), packet (ReadOnlySpan<byte>), compressionThreshold (int)
Exceptions: ArgumentNullException - writer is null.
WritePacket(Stream, in ReadOnlySequence<byte>, int)
public static void WritePacket(this Stream stream, in ReadOnlySequence<byte> packet, int compressionThreshold = -1)
Frames one packet that already carries its varint id and writes it to the specified stream, from a segmented buffer and without joining the segments first.
The header and each segment are written separately, and the stream is not flushed. A packet that is compressed is copied into one pooled buffer first.
Parameters: stream (Stream), packet (ReadOnlySequence<byte>), compressionThreshold (int)
Exceptions: ArgumentNullException - stream is null.; OverflowException - packet is longer than int.MaxValue bytes.
WritePacket(Stream, int, in ReadOnlySequence<byte>, int)
public static void WritePacket(this Stream stream, int id, in ReadOnlySequence<byte> body, int compressionThreshold = -1)
Frames one packet from a segmented body and writes it to the specified stream, prefixing it with the specified packet id and without joining the segments first.
The header and each segment are written separately, and the stream is not flushed. A packet that is compressed is copied into one pooled buffer first.
Parameters: stream (Stream), id (int), body (ReadOnlySequence<byte>), compressionThreshold (int)
Exceptions: ArgumentNullException - stream is null.; OverflowException - The length of body and the packet id together exceed int.MaxValue bytes.
WritePacket(Stream, int, ReadOnlySpan<byte>, int)
public static void WritePacket(this Stream stream, int id, ReadOnlySpan<byte> body, int compressionThreshold = -1)
Frames one packet and writes it to the specified stream, prefixing the body with the specified packet id.
The stream is not flushed.
Parameters: stream (Stream), id (int), body (ReadOnlySpan<byte>), compressionThreshold (int)
Exceptions: ArgumentNullException - stream is null.
WritePacket(Stream, ReadOnlySpan<byte>, int)
public static void WritePacket(this Stream stream, ReadOnlySpan<byte> packet, int compressionThreshold = -1)
Frames one packet that already carries its varint id and writes it to the specified stream.
The stream is not flushed.
Parameters: stream (Stream), packet (ReadOnlySpan<byte>), compressionThreshold (int)
Exceptions: ArgumentNullException - stream is null.
WritePacketAsync(PipeWriter, ReadOnlySequence<byte>, int, CancellationToken)
public static ValueTask<FlushResult> WritePacketAsync(this PipeWriter writer, ReadOnlySequence<byte> packet, int compressionThreshold = -1, CancellationToken cancellationToken = default)
Asynchronously frames one packet that already carries its varint id into the specified pipe from a segmented buffer, and flushes it.
Parameters: writer (PipeWriter), packet (ReadOnlySequence<byte>), compressionThreshold (int), cancellationToken (CancellationToken)
Returns: ValueTask<FlushResult> - A task that represents the asynchronous flush operation. The result contains the FlushResult of the pipe.
Exceptions: ArgumentNullException - writer is null.; OverflowException - packet is longer than int.MaxValue bytes.
WritePacketAsync(PipeWriter, int, ReadOnlySequence<byte>, int, CancellationToken)
public static ValueTask<FlushResult> WritePacketAsync(this PipeWriter writer, int id, ReadOnlySequence<byte> body, int compressionThreshold = -1, CancellationToken cancellationToken = default)
Asynchronously frames one packet into the specified pipe from a segmented body, prefixing it with the specified packet id, and flushes it.
Parameters: writer (PipeWriter), id (int), body (ReadOnlySequence<byte>), compressionThreshold (int), cancellationToken (CancellationToken)
Returns: ValueTask<FlushResult> - A task that represents the asynchronous flush operation. The result contains the FlushResult of the pipe.
Exceptions: ArgumentNullException - writer is null.; OverflowException - The length of body and the packet id together exceed int.MaxValue bytes.
WritePacketAsync(PipeWriter, int, ReadOnlyMemory<byte>, int, CancellationToken)
public static ValueTask<FlushResult> WritePacketAsync(this PipeWriter writer, int id, ReadOnlyMemory<byte> body, int compressionThreshold = -1, CancellationToken cancellationToken = default)
Asynchronously frames one packet into the specified pipe, prefixing the body with the specified packet id, and flushes it.
Parameters: writer (PipeWriter), id (int), body (ReadOnlyMemory<byte>), compressionThreshold (int), cancellationToken (CancellationToken)
Returns: ValueTask<FlushResult> - A task that represents the asynchronous flush operation. The result contains the FlushResult of the pipe.
Exceptions: ArgumentNullException - writer is null.
WritePacketAsync(PipeWriter, ReadOnlyMemory<byte>, int, CancellationToken)
public static ValueTask<FlushResult> WritePacketAsync(this PipeWriter writer, ReadOnlyMemory<byte> packet, int compressionThreshold = -1, CancellationToken cancellationToken = default)
Asynchronously frames one packet that already carries its varint id into the specified pipe and flushes it.
Parameters: writer (PipeWriter), packet (ReadOnlyMemory<byte>), compressionThreshold (int), cancellationToken (CancellationToken)
Returns: ValueTask<FlushResult> - A task that represents the asynchronous flush operation. The result contains the FlushResult of the pipe.
Exceptions: ArgumentNullException - writer is null.
WritePacketAsync(Stream, ReadOnlySequence<byte>, int, CancellationToken)
public static ValueTask WritePacketAsync(this Stream stream, ReadOnlySequence<byte> packet, int compressionThreshold = -1, CancellationToken cancellationToken = default)
Asynchronously frames one packet that already carries its varint id and writes it to the specified stream, from a segmented buffer and without joining the segments first.
The header and each segment are written separately, and the stream is not flushed. A packet that is compressed is copied into one pooled buffer first.
Parameters: stream (Stream), packet (ReadOnlySequence<byte>), compressionThreshold (int), cancellationToken (CancellationToken)
Returns: ValueTask - A task that represents the asynchronous write operation.
Exceptions: ArgumentNullException - stream is null.; OperationCanceledException - The cancellation token was canceled. This exception is stored into the returned task.; OverflowException - packet is longer than int.MaxValue bytes.
WritePacketAsync(Stream, int, ReadOnlySequence<byte>, int, CancellationToken)
public static ValueTask WritePacketAsync(this Stream stream, int id, ReadOnlySequence<byte> body, int compressionThreshold = -1, CancellationToken cancellationToken = default)
Asynchronously frames one packet from a segmented body and writes it to the specified stream, prefixing it with the specified packet id and without joining the segments first.
The header and each segment are written separately, and the stream is not flushed. A packet that is compressed is copied into one pooled buffer first.
Parameters: stream (Stream), id (int), body (ReadOnlySequence<byte>), compressionThreshold (int), cancellationToken (CancellationToken)
Returns: ValueTask - A task that represents the asynchronous write operation.
Exceptions: ArgumentNullException - stream is null.; OperationCanceledException - The cancellation token was canceled. This exception is stored into the returned task.; OverflowException - The length of body and the packet id together exceed int.MaxValue bytes.
WritePacketAsync(Stream, int, ReadOnlyMemory<byte>, int, CancellationToken)
public static ValueTask WritePacketAsync(this Stream stream, int id, ReadOnlyMemory<byte> body, int compressionThreshold = -1, CancellationToken cancellationToken = default)
Asynchronously frames one packet and writes it to the specified stream, prefixing the body with the specified packet id.
The stream is not flushed.
Parameters: stream (Stream), id (int), body (ReadOnlyMemory<byte>), compressionThreshold (int), cancellationToken (CancellationToken)
Returns: ValueTask - A task that represents the asynchronous write operation.
Exceptions: ArgumentNullException - stream is null.; OperationCanceledException - The cancellation token was canceled. This exception is stored into the returned task.
WritePacketAsync(Stream, ReadOnlyMemory<byte>, int, CancellationToken)
public static ValueTask WritePacketAsync(this Stream stream, ReadOnlyMemory<byte> packet, int compressionThreshold = -1, CancellationToken cancellationToken = default)
Asynchronously frames one packet that already carries its varint id and writes it to the specified stream.
The stream is not flushed.
Parameters: stream (Stream), packet (ReadOnlyMemory<byte>), compressionThreshold (int), cancellationToken (CancellationToken)
Returns: ValueTask - A task that represents the asynchronous write operation.
Exceptions: ArgumentNullException - stream is null.; OperationCanceledException - The cancellation token was canceled. This exception is stored into the returned task.