public async Task <ActionResult> SendMessage(PostilionMessage message)
        {
            var activeAcquiringConnection = _acquiringTracker.GetCurrent();

            if (activeAcquiringConnection == null) // no active acquiring connection
            {
                return(new BadRequestResult());    // todo: better error
            }
            await activeAcquiringConnection.SendMessageAsync(message);

            return(new OkResult());
        }
            protected sealed override ValueTask OnReceiveAsync(ReadOnlySequence <byte> payload)
            {
                //void DisposeOnCompletion(/*Value*/Task task, ref IMemoryOwner<byte> message)
                //{
                //    task/*.AsTask()*/.ContinueWith((t, s) => ((IMemoryOwner<byte>)s)?.Dispose(), message);
                //    message = null; // caller no longer owns it, logically; don't wipe on exit
                //}

                var msg = payload.Lease(); // copy to buffer pool, ProcessAsync must release it

                // https://devblogs.microsoft.com/pfxteam/task-run-vs-task-factory-startnew/
                Task.Run(() =>
                {
                    PostilionMessage pm;
                    try
                    {
                        pm = new PostilionMessage();
                        pm.ParseFromBuffer(msg.Memory.Span);
                    }
                    finally
                    {
                        msg.Dispose(); // release back to buffer pool
                    }

                    _messageHandler.ProcessMessage(pm); // todo: make async?
                });                                     // parse & process message on worker thread, let this thread read next message from pipeline

                //try
                //{
                //var pendingAction = _sink.ProcessAsync(msg);
                //if (!pendingAction.IsCompletedSuccessfully)
                //    DisposeOnCompletion(pendingAction, ref msg);
                //}
                //finally
                //{   // might have been wiped if we went async
                //    msg?.Dispose();
                //}
                return(default);
Exemplo n.º 3
0
 public void ProcessMessage(PostilionMessage message)
 {
     LastMessage = message;
     _onProcessMessageEvent.Set();
 }