Example #1
0
    public static ArrayPoolBufferWriter <byte> Write <T>(this IByteSerializer <T> serializer, T value)
    {
        var bufferWriter = new ArrayPoolBufferWriter <byte>();

        serializer.Write(bufferWriter, value);
        return(bufferWriter);
    }
Example #2
0
    public static ArrayPoolBufferWriter <byte> Write(this IByteSerializer serializer, object?value, Type type)
    {
        var bufferWriter = new ArrayPoolBufferWriter <byte>();

        serializer.Write(bufferWriter, value);
        return(bufferWriter);
    }
Example #3
0
    public static Channel <T> WithByteSerializer <T>(
        this Channel <ReadOnlyMemory <byte> > downstreamChannel,
        IByteSerializer <T> serializer,
        BoundedChannelOptions?channelOptions = null,
        CancellationToken cancellationToken  = default)
    {
        channelOptions ??= new BoundedChannelOptions(16)
        {
            FullMode     = BoundedChannelFullMode.Wait,
            SingleReader = true,
            SingleWriter = true,
            AllowSynchronousContinuations = true,
        };
        var pair = ChannelPair.CreateTwisted(
            Channel.CreateBounded <T>(channelOptions),
            Channel.CreateBounded <T>(channelOptions));

        downstreamChannel.Connect(pair.Channel1,
                                  serializer.Read,
                                  Write,
                                  ChannelCompletionMode.Full,
                                  cancellationToken);
        return(pair.Channel2);

        ReadOnlyMemory <byte> Write(T value)
        {
            using var bufferWriter = serializer.Write(value);
            return(bufferWriter.WrittenMemory.ToArray());
        }
    }
Example #4
0
 public static ArrayPoolBufferWriter <byte> Write <T>(this IByteSerializer serializer, T value)
 // ReSharper disable once HeapView.PossibleBoxingAllocation
 => serializer.Write(value, typeof(T));