Пример #1
0
        public async ValueTask <OmniRpcStreamReceiveResult <TMessage> > ReceiveAsync <TMessage>(CancellationToken token = default)
            where TMessage : RocketPackMessageBase <TMessage>
        {
            OmniRpcStreamReceiveResult <TMessage> receiveResult = default;

            await _connection.DequeueAsync((sequence) =>
            {
                Span <byte> type = stackalloc byte[1];
                sequence.CopyTo(type);

                switch ((PacketType)type[0])
                {
                case PacketType.Message:
                    var message   = RocketPackMessageBase <TMessage> .Formatter.Deserialize(new RocketPackReader(sequence, _bufferPool), 0);
                    receiveResult = new OmniRpcStreamReceiveResult <TMessage>(message, null, false, false);
                    break;

                case PacketType.ErrorMessage:
                    var errorMessage = OmniRpcErrorMessage.Import(sequence, _bufferPool);
                    receiveResult    = new OmniRpcStreamReceiveResult <TMessage>(null, errorMessage, false, false);
                    break;

                case PacketType.Canceled:
                    receiveResult = new OmniRpcStreamReceiveResult <TMessage>(null, null, true, false);
                    break;

                case PacketType.Completed:
                    receiveResult = new OmniRpcStreamReceiveResult <TMessage>(null, null, false, true);
                    break;
                }
            }, token);

            return(receiveResult);
        }
Пример #2
0
 public async ValueTask SendErrorMessageAsync(OmniRpcErrorMessage errorMessage, CancellationToken token = default)
 {
     await _connection.EnqueueAsync((bufferWriter) =>
     {
         bufferWriter.GetSpan(1)[0] = (byte)PacketType.ErrorMessage;
         bufferWriter.Advance(1);
         errorMessage.Export(bufferWriter, _bufferPool);
     }, token);
 }