Ejemplo n.º 1
0
        public async Task<ICollection<ChatMessage>> ListenChatAsync(int accountId)
        {
            Program.Log($"Starting RPC ListenChatAsync (accountId {accountId})", true);

            // parameters can be passed to call one by one or be composed 
            // into a CallOptions using a fluent syntax.
            var callOptions = new CallOptions()
                .WithCancellationToken(CancellationToken.None)
                .WithDeadline(DateTime.UtcNow.AddMinutes(2))
                .WithHeaders(Metadata.Empty);

            using (var call = _grpcClient.ListenChat(new ChatMessageRequest { AccountId = accountId }, callOptions))
            {
                // Custom response header
                Metadata responseHeader = await call.ResponseHeadersAsync;
                Program.Log($"ListenChatAsync response header {String.Join(",", responseHeader.Select(m => m.ToString()))}");

                var chatMessages = new List<ChatMessage>();
                while (await call.ResponseStream.MoveNext())
                {
                    ChatMessage chatMessage = call.ResponseStream.Current;
                    Program.Log($"RPC ListenChatAsync received {chatMessage}");
                    chatMessages.Add(chatMessage);
                }

                // Custom response trailer
                Metadata responseTrailer = call.GetTrailers();
                Program.Log($"ListenChatAsync response trailer {String.Join(",", responseTrailer.Select(m => m.ToString()))}");

                return chatMessages;
            }
        }
        public void WithMethods()
        {
            var options = new CallOptions();
            
            var metadata = new Metadata();
            Assert.AreSame(metadata, options.WithHeaders(metadata).Headers);

            var deadline = DateTime.UtcNow;
            Assert.AreEqual(deadline, options.WithDeadline(deadline).Deadline.Value);

            var cancellationToken = new CancellationTokenSource().Token;
            Assert.AreEqual(cancellationToken, options.WithCancellationToken(cancellationToken).CancellationToken);

            var writeOptions = new WriteOptions();
            Assert.AreSame(writeOptions, options.WithWriteOptions(writeOptions).WriteOptions);

            var propagationToken = new ContextPropagationToken(CallSafeHandle.NullInstance, DateTime.UtcNow, 
                CancellationToken.None, ContextPropagationOptions.Default);
            Assert.AreSame(propagationToken, options.WithPropagationToken(propagationToken).PropagationToken);

            var credentials = new FakeCallCredentials();
            Assert.AreSame(credentials, options.WithCredentials(credentials).Credentials);

            // Check that the original instance is unchanged.
            Assert.IsNull(options.Headers);
            Assert.IsNull(options.Deadline);
            Assert.AreEqual(CancellationToken.None, options.CancellationToken);
            Assert.IsNull(options.WriteOptions);
            Assert.IsNull(options.PropagationToken);
            Assert.IsNull(options.Credentials);
        }
Ejemplo n.º 3
0
        public void WriteOptions_Unary()
        {
            helper.UnaryHandler = new UnaryServerMethod<string, string>(async (request, context) =>
            {
                context.WriteOptions = new WriteOptions(WriteFlags.NoCompress);
                return request;
            });

            var callOptions = new CallOptions(writeOptions: new WriteOptions(WriteFlags.NoCompress));
            Calls.BlockingUnaryCall(helper.CreateUnaryCall(callOptions), "abc");
        }
Ejemplo n.º 4
0
        protected async Task <TResponse> CallUnaryMethodAsync <TRequest, TResponse>(TRequest request, string serviceName, string methodName, CancellationToken ct)
            where TRequest : class
            where TResponse : class
        {
            var callOptions = new G.CallOptions(cancellationToken: ct);

            using (var call = _invoker.AsyncUnaryCall(GetMethodDefinition <TRequest, TResponse>(G.MethodType.Unary, serviceName, methodName), null, callOptions, request))
            {
                return(await call.ResponseAsync.ConfigureAwait(false));
            }
        }
Ejemplo n.º 5
0
        public async Task <TResponse> CallUnaryMethodAsync <TRequest, TResponse>(TRequest request, string serviceName, string methodName, CancellationToken token)
            where TRequest : class
            where TResponse : class
        {
            var callOptions      = new GrpcCore.CallOptions(cancellationToken: token).WithWaitForReady();
            var methodDefinition = this.GetMethodDefinition <TRequest, TResponse>(GrpcCore.MethodType.Unary, serviceName, methodName);

            using (var call = this._invoker.AsyncUnaryCall(methodDefinition, null, callOptions, request))
            {
                var result = await call.ResponseAsync.ConfigureAwait(false);

                return(result);
            }
        }
Ejemplo n.º 6
0
        protected override TResponse CallUnaryMethodCore <TRequest, TResponse>(GrpcProxyMethod methodDef, TRequest request, CancellationToken cancellationToken)
        {
            if (methodDef is null)
            {
                throw new ArgumentNullException(nameof(methodDef));
            }

            DateTime?deadline    = this.GetCallDeadline();
            var      callOptions = new GrpcCore.CallOptions(deadline: deadline, cancellationToken: cancellationToken);

            var typedMethod = this.grpcMethodsCache.GetGrpcMethod <TRequest, TResponse>(methodDef);

            var response = this.grpcInvoker.BlockingUnaryCall(typedMethod, null, callOptions, request);

            return(response);
        }
Ejemplo n.º 7
0
        protected override ValueTask <IAsyncStreamingServerCall <TResponse> > CallStreamingMethodAsync <TRequest, TResponse>(TRequest request, GrpcProxyMethod method, CancellationToken cancellationToken)
        {
            if (method is null)
            {
                throw new ArgumentNullException(nameof(method));
            }

            DateTime?deadline    = this.GetStreamingCallDeadline();
            var      callOptions = new GrpcCore.CallOptions(deadline: deadline, cancellationToken: cancellationToken);

            var typedMethod = this.grpcMethodsCache.GetGrpcMethod <TRequest, TResponse>(method);

#pragma warning disable CA2000 // Dispose objects before losing scope
            return(new ValueTask <IAsyncStreamingServerCall <TResponse> >(
                       new GrpcAsyncServerStreamingCall <TResponse>(this.grpcInvoker.AsyncServerStreamingCall(typedMethod, null, callOptions, request))));

#pragma warning restore CA2000 // Dispose objects before losing scope
        }
Ejemplo n.º 8
0
        protected override async Task <TResponse> CallUnaryMethodCoreAsync <TRequest, TResponse>(GrpcProxyMethod methodDef, TRequest request, CancellationToken cancellationToken)
        {
            if (methodDef is null)
            {
                throw new ArgumentNullException(nameof(methodDef));
            }

            DateTime?deadline    = this.GetCallDeadline();
            var      callOptions = new GrpcCore.CallOptions(deadline: deadline, cancellationToken: cancellationToken);

            var typedMethod = this.grpcMethodsCache.GetGrpcMethod <TRequest, TResponse>(methodDef);

            using (var asyncCall = this.grpcInvoker.AsyncUnaryCall(typedMethod, null, callOptions, request))
            {
                var response = await asyncCall.ResponseAsync.ContextFree();

                // TODO: Handle response.Status
                return(response);
            }
        }
Ejemplo n.º 9
0
        public void WithMethods()
        {
            var options = new CallOptions();
            
            var metadata = new Metadata();
            Assert.AreSame(metadata, options.WithHeaders(metadata).Headers);

            var deadline = DateTime.UtcNow;
            Assert.AreEqual(deadline, options.WithDeadline(deadline).Deadline.Value);

            var token = new CancellationTokenSource().Token;
            Assert.AreEqual(token, options.WithCancellationToken(token).CancellationToken);

            // Change original instance is unchanged.
            Assert.IsNull(options.Headers);
            Assert.IsNull(options.Deadline);
            Assert.AreEqual(CancellationToken.None, options.CancellationToken);
            Assert.IsNull(options.WriteOptions);
            Assert.IsNull(options.PropagationToken);
            Assert.IsNull(options.Credentials);
        }
Ejemplo n.º 10
0
        public async Task PropagateCancellation()
        {
            helper.UnaryHandler = new UnaryServerMethod<string, string>(async (request, context) =>
            {
                // check that we didn't obtain the default cancellation token.
                Assert.IsTrue(context.CancellationToken.CanBeCanceled);
                return "PASS";
            });

            helper.ClientStreamingHandler = new ClientStreamingServerMethod<string, string>(async (requestStream, context) =>
            {
                var propagationToken = context.CreatePropagationToken();
                Assert.IsNotNull(propagationToken.ParentCall);

                var callOptions = new CallOptions(propagationToken: propagationToken);
                return await Calls.AsyncUnaryCall(helper.CreateUnaryCall(callOptions), "xyz");
            });
                
            var cts = new CancellationTokenSource();
            var call = Calls.AsyncClientStreamingCall(helper.CreateClientStreamingCall(new CallOptions(cancellationToken: cts.Token)));
            await call.RequestStream.CompleteAsync();
            Assert.AreEqual("PASS", await call);
        }
Ejemplo n.º 11
0
 public AsyncUnaryCall<global::Walletrpc.CloseWalletResponse> CloseWalletAsync(global::Walletrpc.CloseWalletRequest request, CallOptions options)
 {
   var call = CreateCall(__Method_CloseWallet, options);
   return Calls.AsyncUnaryCall(call, request);
 }
Ejemplo n.º 12
0
 public AsyncUnaryCall<global::Walletrpc.WalletExistsResponse> WalletExistsAsync(global::Walletrpc.WalletExistsRequest request, CallOptions options)
 {
   var call = CreateCall(__Method_WalletExists, options);
   return Calls.AsyncUnaryCall(call, request);
 }
Ejemplo n.º 13
0
 public AsyncUnaryCall<global::Walletrpc.PublishTransactionResponse> PublishTransactionAsync(global::Walletrpc.PublishTransactionRequest request, CallOptions options)
 {
   var call = CreateCall(__Method_PublishTransaction, options);
   return Calls.AsyncUnaryCall(call, request);
 }
Ejemplo n.º 14
0
 public AsyncUnaryCall<global::Walletrpc.ImportPrivateKeyResponse> ImportPrivateKeyAsync(global::Walletrpc.ImportPrivateKeyRequest request, CallOptions options)
 {
   var call = CreateCall(__Method_ImportPrivateKey, options);
   return Calls.AsyncUnaryCall(call, request);
 }
Ejemplo n.º 15
0
 public AsyncUnaryCall<global::Walletrpc.NextAddressResponse> NextAddressAsync(global::Walletrpc.NextAddressRequest request, CallOptions options)
 {
   var call = CreateCall(__Method_NextAddress, options);
   return Calls.AsyncUnaryCall(call, request);
 }
Ejemplo n.º 16
0
 public AsyncUnaryCall<global::Walletrpc.RenameAccountResponse> RenameAccountAsync(global::Walletrpc.RenameAccountRequest request, CallOptions options)
 {
   var call = CreateCall(__Method_RenameAccount, options);
   return Calls.AsyncUnaryCall(call, request);
 }
Ejemplo n.º 17
0
        public override sealed GrpcCore.AsyncUnaryCall <TResponse> AsyncUnaryCall <TRequest, TResponse>(GrpcCore.Method <TRequest, TResponse> method, string host, GrpcCore.CallOptions options, TRequest request)
        {
            Assert.NotNull(method);
            Assert.IsNotEmpty(method.Name);

            var response = UnaryFunc <TRequest, TResponse>(method.Name, request);

            return(new GrpcCore.AsyncUnaryCall <TResponse>(Task.FromResult(response), null, null, null, () => { }));
        }
Ejemplo n.º 18
0
 public virtual grpc.AsyncDuplexStreamingCall <RequestMessage, ResponseMessage> CreateStreaming(grpc.CallOptions options)
 {
     return(CallInvoker.AsyncDuplexStreamingCall(__Method_CreateStreaming, null, options));
 }
Ejemplo n.º 19
0
 public override sealed GrpcCore.AsyncDuplexStreamingCall <TRequest, TResponse> AsyncDuplexStreamingCall <TRequest, TResponse>(GrpcCore.Method <TRequest, TResponse> method, string host, GrpcCore.CallOptions options)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 20
0
        public override sealed TResponse BlockingUnaryCall <TRequest, TResponse>(GrpcCore.Method <TRequest, TResponse> method, string host, GrpcCore.CallOptions options, TRequest request)
        {
            Assert.NotNull(method);
            Assert.IsNotEmpty(method.Name);

            var response = UnaryFunc <TRequest, TResponse>(method.Name, request);

            return(response);
        }
Ejemplo n.º 21
0
 public AsyncUnaryCall<global::Walletrpc.StartBtcdRpcResponse> StartBtcdRpcAsync(global::Walletrpc.StartBtcdRpcRequest request, CallOptions options)
 {
   var call = CreateCall(__Method_StartBtcdRpc, options);
   return Calls.AsyncUnaryCall(call, request);
 }
 public CallInvocationDetails<string, string> CreateDuplexStreamingCall(CallOptions options = default(CallOptions))
 {
     return new CallInvocationDetails<string, string>(channel, duplexStreamingMethod, options);
 }
Ejemplo n.º 23
0
 public global::Walletrpc.AccountNumberResponse AccountNumber(global::Walletrpc.AccountNumberRequest request, CallOptions options)
 {
   var call = CreateCall(__Method_AccountNumber, options);
   return Calls.BlockingUnaryCall(call, request);
 }
Ejemplo n.º 24
0
 public global::Walletrpc.RenameAccountResponse RenameAccount(global::Walletrpc.RenameAccountRequest request, CallOptions options)
 {
   var call = CreateCall(__Method_RenameAccount, options);
   return Calls.BlockingUnaryCall(call, request);
 }
Ejemplo n.º 25
0
 public AsyncUnaryCall<global::Walletrpc.AccountNumberResponse> AccountNumberAsync(global::Walletrpc.AccountNumberRequest request, CallOptions options)
 {
   var call = CreateCall(__Method_AccountNumber, options);
   return Calls.AsyncUnaryCall(call, request);
 }
Ejemplo n.º 26
0
 public global::Walletrpc.NextAddressResponse NextAddress(global::Walletrpc.NextAddressRequest request, CallOptions options)
 {
   var call = CreateCall(__Method_NextAddress, options);
   return Calls.BlockingUnaryCall(call, request);
 }
Ejemplo n.º 27
0
 public global::Walletrpc.BalanceResponse Balance(global::Walletrpc.BalanceRequest request, CallOptions options)
 {
   var call = CreateCall(__Method_Balance, options);
   return Calls.BlockingUnaryCall(call, request);
 }
Ejemplo n.º 28
0
 public global::Walletrpc.ImportPrivateKeyResponse ImportPrivateKey(global::Walletrpc.ImportPrivateKeyRequest request, CallOptions options)
 {
   var call = CreateCall(__Method_ImportPrivateKey, options);
   return Calls.BlockingUnaryCall(call, request);
 }
Ejemplo n.º 29
0
 public AsyncUnaryCall<global::Walletrpc.BalanceResponse> BalanceAsync(global::Walletrpc.BalanceRequest request, CallOptions options)
 {
   var call = CreateCall(__Method_Balance, options);
   return Calls.AsyncUnaryCall(call, request);
 }
Ejemplo n.º 30
0
 public global::Walletrpc.PublishTransactionResponse PublishTransaction(global::Walletrpc.PublishTransactionRequest request, CallOptions options)
 {
   var call = CreateCall(__Method_PublishTransaction, options);
   return Calls.BlockingUnaryCall(call, request);
 }
Ejemplo n.º 31
0
 public global::Walletrpc.GetTransactionsResponse GetTransactions(global::Walletrpc.GetTransactionsRequest request, CallOptions options)
 {
   var call = CreateCall(__Method_GetTransactions, options);
   return Calls.BlockingUnaryCall(call, request);
 }
Ejemplo n.º 32
0
 public global::Walletrpc.WalletExistsResponse WalletExists(global::Walletrpc.WalletExistsRequest request, CallOptions options)
 {
   var call = CreateCall(__Method_WalletExists, options);
   return Calls.BlockingUnaryCall(call, request);
 }
Ejemplo n.º 33
0
        /// <summary>
        /// Creates a new call to given method.
        /// </summary>
        protected CallInvocationDetails <TRequest, TResponse> CreateCall <TRequest, TResponse>(Method <TRequest, TResponse> method, CallOptions options)
            where TRequest : class
            where TResponse : class
        {
            var interceptor = HeaderInterceptor;

            if (interceptor != null)
            {
                if (options.Headers == null)
                {
                    options = options.WithHeaders(new Metadata());
                }
                var authUri = authUriBase != null ? authUriBase + method.ServiceName : null;
                interceptor(authUri, options.Headers);
            }
            return(new CallInvocationDetails <TRequest, TResponse>(channel, method, Host, options));
        }
Ejemplo n.º 34
0
 public global::Walletrpc.CloseWalletResponse CloseWallet(global::Walletrpc.CloseWalletRequest request, CallOptions options)
 {
   var call = CreateCall(__Method_CloseWallet, options);
   return Calls.BlockingUnaryCall(call, request);
 }
Ejemplo n.º 35
0
 public AsyncServerStreamingCall<global::Walletrpc.AccountNotificationsResponse> AccountNotifications(global::Walletrpc.AccountNotificationsRequest request, CallOptions options)
 {
   var call = CreateCall(__Method_AccountNotifications, options);
   return Calls.AsyncServerStreamingCall(call, request);
 }
Ejemplo n.º 36
0
 public global::Walletrpc.StartBtcdRpcResponse StartBtcdRpc(global::Walletrpc.StartBtcdRpcRequest request, CallOptions options)
 {
   var call = CreateCall(__Method_StartBtcdRpc, options);
   return Calls.BlockingUnaryCall(call, request);
 }
Ejemplo n.º 37
0
 public global::Walletrpc.ChangePassphraseResponse ChangePassphrase(global::Walletrpc.ChangePassphraseRequest request, CallOptions options)
 {
   var call = CreateCall(__Method_ChangePassphrase, options);
   return Calls.BlockingUnaryCall(call, request);
 }
 public CallInvocationDetails<string, string> CreateUnaryCall(CallOptions options = default(CallOptions))
 {
     return new CallInvocationDetails<string, string>(channel, unaryMethod, options);
 }
Ejemplo n.º 39
0
 public AsyncUnaryCall<global::Walletrpc.ChangePassphraseResponse> ChangePassphraseAsync(global::Walletrpc.ChangePassphraseRequest request, CallOptions options)
 {
   var call = CreateCall(__Method_ChangePassphrase, options);
   return Calls.AsyncUnaryCall(call, request);
 }
Ejemplo n.º 40
0
 public AsyncUnaryCall<global::Walletrpc.GetTransactionsResponse> GetTransactionsAsync(global::Walletrpc.GetTransactionsRequest request, CallOptions options)
 {
   var call = CreateCall(__Method_GetTransactions, options);
   return Calls.AsyncUnaryCall(call, request);
 }
Ejemplo n.º 41
0
 public override sealed GrpcCore.AsyncServerStreamingCall <TResponse> AsyncServerStreamingCall <TRequest, TResponse>(GrpcCore.Method <TRequest, TResponse> method, string host, GrpcCore.CallOptions options, TRequest request)
 {
     return(new GrpcCore.AsyncServerStreamingCall <TResponse>(ServerStreamingFunc <TRequest, TResponse>(method.Name, request, options.CancellationToken), Task.FromResult(new GrpcCore.Metadata()), null, null, () => { }));
 }