コード例 #1
0
        public async Task <byte[]?> Execute(RpcMethod method, RpcPeerInfo callingPeer)
        {
            var  div = Div.FromMethod(method);
            byte ret = (byte)(div.dividend / div.divisor);  // Yes, may throw div/0 exception

            return(await Task.FromResult(new byte[] { ret }));
        }
コード例 #2
0
 public async Task <RpcMessage?> Receive(CancellationToken cancellationToken)
 {
     while (isRunning)
     {
         if (receiving.TryDequeue(out var msg))
         {
             if (msg.IsRpcMethod())
             {
                 ReceivedDivs.Enqueue(Div.FromMethod(msg.DecodeRpcMethod()));
             }
             return(msg);
         }
         await Task.Delay(25);
     }
     return(null);
 }
コード例 #3
0
 public async Task Send(RpcMessage message, CancellationToken cancellationToken)
 {
     if (message.IsRpcMethod())
     {
         // Peer sent us a div task. "Execute" the method (by waiting executionTimeMs) and
         // add the result to the receiving queue and log the div.
         var div = Div.FromMethod(message.DecodeRpcMethod());
         Log.Trace($"Div {div.methodID} sent to DivMock");
         SentDivs.Enqueue(div);
         Action setResult = () => {
             div.result = div.ComputeExpectedResult();
             receiving.Enqueue(RpcMessage.Encode(div.result));
             Log.Trace($"Div {div.methodID} executed on DivMock");
         };
         if (executionTimeMs > 0)
         {
             _ = Task.Delay(executionTimeMs).ContinueWith(_ => setResult());
         }
         else
         {
             setResult(); // Immediate execution on the same thread
         }
     }
     else if (message.IsRpcResult())
     {
         // Peer sent the result from a div call. Log it.
         var result = message.DecodeRpcResult();
         var div    = ReceivedDivs.AsEnumerable().FirstOrDefault(it => it.methodID == result.MethodID);
         if (div == null)
         {
             Debug.WriteLine("Unexpected method ID");
         }
         else
         {
             div.result = result;
         }
     }
 }