concept payload in category .net

appears as: payload
Concurrency in .NET

This is an excerpt from Manning's book Concurrency in .NET.

One of the main reasons to introduce concurrency in Rx programming is to facilitate and manage offloading the payload for an event stream. This allows a set of concurrent tasks to be performed, such as maintaining a responsive UI, to free the current thread.

Listing 12.8 Stateless agent in C# using TDF
class StatefulReplyDataFlowAgent<TState, TMessage, TReply> :   #1  
                                         IReplyAgent<TMessage, TReply>
{
     private TState state;
 private readonly ActionBlock<(TMessage,   #2  
                Option<TaskCompletionSource<TReply>>)> actionBlock;

     public StatefulReplyDataFlowAgent(TState initialState,
            Func<TState, TMessage, Task<TState>> projection,
            Func<TState, TMessage, Task<(TState, TReply)>> ask,
 CancellationTokenSource cts = null)   #3  
    {
         state = initialState;
         var options = new ExecutionDataFlowBlockOptions { 
         CancellationToken = cts?.Token ?? CancellationToken.None };

            actionBlock = new ActionBlock<(TMessage, 
                          Option<TaskCompletionSource<TReply>>)>(  
              async message => {
   (TMessage msg, Option<TaskCompletionSource<TReply>> replyOpt) = message;
 await replyOpt.Match(   #4  
 None: async () => state = await projection(state, msg),   #5  
 Some: async reply => {   #6  
             (TState newState, TReply replyresult) = await ask(state, msg);
                 state = newState;
             reply.SetResult(replyresult);
             });
          }, options);
        }

        public Task<TReply> Ask(TMessage message)
        {
 var tcs = new TaskCompletionSource<TReply>();   #7  
            actionBlock.Post((message, Option.Some(tcs)));
 return tcs.Task;   #7  
        }

        public Task Send(TMessage message) =>    
            actionBlock.SendAsync((message, Option.None));
}

#1   The IReplyAgent interface defines the Ask method to ensure that the agent enables two-way communication.
#2   The ActionBlock message type is a tuple, where a TaskCompletionSource option is passed into the payload to supply a channel for communicating back to the caller asynchronously.
#3   The agent construct takes two functions to respectively define the fire-and-forget and two-way communications.
#4   The Match extension method of the Option type is used to branch behavior over the TaskCompletionSource option.
#5   If the TaskCompletionSource is None, then the projection function is applied.
#6   If the TaskCompletionSource is Some, then the Ask function is applied to reply to the caller.
#7   The Ask member creates a TaskCompletionSource used as channel to communicate back to the caller when the operation run by the agent is completed.

Then, the underlying stream is read as an Observable while loop until data is available or the operation is canceled. The Observable Defer operator waits until an observer subscribes to it, and then starts pushing the data as a stream. Next, during each iteration, a chunk of data is read from the stream. This data is then pushed into a buffer that takes the form of an ArraySegment<byte>, which slices the payload in the right length. ReadObservable returns an IObservable of ArraySegment<byte>, which is an efficient way to manage the byte arrays in a pool. The buffer size may be larger than the payload of bytes received, for example, so the use of ArraySegment<byte> holds the byte array and payload length.