示例#1
0
    public async ValueTask CallActionAsync <TParam>(TParam param, CancellationToken cancellationToken = default)
        where TParam : IRocketMessage <TParam>
    {
        await _connection.Sender.SendAsync(
            bufferWriter =>
        {
            var builder = new PacketBuilder(bufferWriter, _bytesPool);
            builder.WriteCompleted(param);
        }, cancellationToken);

        var result = ParsedPacketMessage <TError> .CreateUnknown();

        await _connection.Receiver.ReceiveAsync(
            sequence =>
        {
            PacketParser.TryParse(ref sequence, out result, _bytesPool);
        }, cancellationToken);

        if (result.IsCompleted)
        {
            return;
        }
        else if (result.IsError)
        {
            throw ThrowHelper.CreateRocketRemotingApplicationException(result.ErrorMessage);
        }

        throw ThrowHelper.CreateRocketRemotingProtocolException_UnexpectedProtocol();
    }
示例#2
0
    public static bool TryParse <TMessage, TErrorMessage>(ref ReadOnlySequence <byte> sequence, out ParsedPacketMessage <TMessage, TErrorMessage> result, IBytesPool bytesPool)
        where TMessage : IRocketMessage <TMessage>
        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 <TMessage, TErrorMessage> .CreateCompleted(),
            PacketType.Completed when(sequence.Length != 0) => ParsedPacketMessage <TMessage, TErrorMessage> .CreateCompleted(IRocketMessage <TMessage> .Import(sequence, bytesPool)),
            PacketType.Continue when(sequence.Length == 0) => ParsedPacketMessage <TMessage, TErrorMessage> .CreateContinue(),
            PacketType.Continue when(sequence.Length != 0) => ParsedPacketMessage <TMessage, TErrorMessage> .CreateContinue(IRocketMessage <TMessage> .Import(sequence, bytesPool)),
            PacketType.Error => ParsedPacketMessage <TMessage, TErrorMessage> .CreateError(IRocketMessage <TErrorMessage> .Import(sequence, bytesPool)),
            _ => ParsedPacketMessage <TMessage, TErrorMessage> .CreateUnknown(),
        };

        return(!result.IsUnknown);
    }
    public async ValueTask ListenFunctionAsync <TParam, TResult>(Func <TParam, CancellationToken, ValueTask <TResult> > callback, CancellationToken cancellationToken = default)
        where TParam : IRocketMessage <TParam>
        where TResult : IRocketMessage <TResult>
    {
        using var cancellationTokenSource = this.CreateCancellationTokenSource(cancellationToken);
        var connectionCloseWaitTask = this.ConnectionCloseWaitAsync(cancellationTokenSource.Token);

        var param = ParsedPacketMessage <TParam, TError> .CreateUnknown();

        await _connection.Receiver.ReceiveAsync(
            sequence =>
        {
            PacketParser.TryParse(ref sequence, out param, _bytesPool);
        }, cancellationTokenSource.Token);

        if (param.IsCompleted)
        {
            try
            {
                var result = await callback.Invoke(param.Message, cancellationTokenSource.Token);

                await _connection.Sender.SendAsync(
                    bufferWriter =>
                {
                    var builder = new PacketBuilder(bufferWriter, _bytesPool);
                    builder.WriteCompleted(result);
                }, cancellationTokenSource.Token);

                await connectionCloseWaitTask;

                return;
            }
            catch (Exception e)
            {
                var errorMessage = _errorMessageFactory.Create(e);

                await _connection.Sender.SendAsync(
                    bufferWriter =>
                {
                    var builder = new PacketBuilder(bufferWriter, _bytesPool);
                    builder.WriteError(errorMessage);
                }, cancellationToken);

                throw;
            }
        }

        throw ThrowHelper.CreateRocketRemotingProtocolException_UnexpectedProtocol();
    }
示例#4
0
    public async ValueTask CallActionAsync(CancellationToken cancellationToken = default)
    {
        var result = ParsedPacketMessage <TError> .CreateUnknown();

        await _connection.Receiver.ReceiveAsync(
            sequence =>
        {
            PacketParser.TryParse(ref sequence, out result, _bytesPool);
        }, cancellationToken);

        if (result.IsCompleted)
        {
            return;
        }
        else if (result.IsError)
        {
            throw ThrowHelper.CreateRocketRemotingApplicationException(result.ErrorMessage);
        }

        throw ThrowHelper.CreateRocketRemotingProtocolException_UnexpectedProtocol();
    }