Esempio n. 1
0
    public static T FromStream <T>(Stream inStream)
        where T : IRocketMessage <T>
    {
        using var bytesPipe = new BytesPipe();

        const int bufferSize = 4096;

        while (inStream.Position < inStream.Length)
        {
            var readLength = inStream.Read(bytesPipe.Writer.GetSpan(bufferSize));
            if (readLength < 0)
            {
                break;
            }

            bytesPipe.Writer.Advance(readLength);
        }

        return(IRocketMessage <T> .Import(bytesPipe.Reader.GetSequence(), BytesPool.Shared));
    }
Esempio n. 2
0
    public static async IAsyncEnumerable <TValue> GetValuesAsync <TValue>(this IKeyValueStorage <string> storage, [EnumeratorCancellation] CancellationToken cancellationToken = default)
        where TValue : IRocketMessage <TValue>
    {
        var bytesPool = BytesPool.Shared;

        using var bytesPipe = new BytesPipe(bytesPool);

        await foreach (var key in storage.GetKeysAsync(cancellationToken))
        {
            if (!await storage.TryReadAsync(key, bytesPipe.Writer, cancellationToken))
            {
                continue;
            }

            var value = IRocketMessage <TValue> .Import(bytesPipe.Reader.GetSequence(), bytesPool);

            yield return(value);

            bytesPipe.Reset();
        }
    }
Esempio n. 3
0
    public static bool TryParse <TErrorMessage>(ref ReadOnlySequence <byte> sequence, out ParsedPacketMessage <TErrorMessage> result, IBytesPool bytesPool)
        where TErrorMessage : IRocketMessage <TErrorMessage>
    {
        result = default;

        if (sequence.Length == 0)
        {
            return(false);
        }
        if (!Varint.TryGetUInt8(ref sequence, out var type))
        {
            return(false);
        }

        result = ((PacketType)type) switch
        {
            PacketType.Completed when(sequence.Length == 0) => ParsedPacketMessage <TErrorMessage> .CreateCompleted(),
            PacketType.Continue when(sequence.Length == 0) => ParsedPacketMessage <TErrorMessage> .CreateContinue(),
            PacketType.Error => ParsedPacketMessage <TErrorMessage> .CreateError(IRocketMessage <TErrorMessage> .Import(sequence, bytesPool)),
            _ => ParsedPacketMessage <TErrorMessage> .CreateUnknown(),
        };

        return(!result.IsUnknown);
    }
Esempio n. 4
0
 public static T FromBytes <T>(ReadOnlyMemory <byte> memory)
     where T : IRocketMessage <T>
 {
     return(IRocketMessage <T> .Import(new ReadOnlySequence <byte>(memory), BytesPool.Shared));
 }