public override async Task getAllCustomer(Empty request, Grpc.Core.IServerStreamWriter <Customer> responseStream, Grpc.Core.ServerCallContext context) { List <Customer> responses = new List <Customer>(); try { conPG.Open(); strQuery = "SELECT * FROM handson_user"; NpgsqlCommand command = ExecutePGQuery(strQuery); NpgsqlDataReader dtr = command.ExecuteReader(); while (dtr.Read()) { Customer cus = new Customer(); cus.Id = Convert.ToString(dtr["id"]); cus.Username = Convert.ToString(dtr["username"]); cus.Password = Convert.ToString(dtr["password"]); responses.Add(cus); } } catch (Exception) { throw; } foreach (var response in responses) { await responseStream.WriteAsync(response); } }
public override async Task LoadSitesList(SettingsRequest request, Grpc.Core.IServerStreamWriter <Settings> responseStream, Grpc.Core.ServerCallContext context) { List <Settings> responses = null; string userid = request.Userid; List <String> settings = db.GetUserSettings(userid, "site"); foreach (var setting in settings) { await responseStream.WriteAsync(new Settings { Userid = userid, Field = setting }); } Console.WriteLine("\nSettings sent to client. Userid: " + userid); //responses.Add(); //лямбдой //responses = db.GetUserSettings(userid, "site").Cast<List<Settings>>(){ new Settings { }}; //settings.FindAll((feature) => feature.Exists() && request.Contains(feature.Location)); //получаем из бд настройки для конкретного пользователя //foreach (var response in responses) //{ // await responseStream.WriteAsync(response); //} }
public override async Task RequestFrameStream( Grpc.Core.IAsyncStreamReader <CameraAcquisitionRequest> requestStream, Grpc.Core.IServerStreamWriter <BufferedFramesResponse> responseStream, Grpc.Core.ServerCallContext context) { lock (_frameStreamWriterLock) { _frameStreamWriter = responseStream; } while (await requestStream.MoveNext() && !context.CancellationToken.IsCancellationRequested) { #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed var result = await RequestCameraAcquisition(requestStream.Current, context); if (result.Error != null) { lock (_frameStreamWriterLock) { responseStream.WriteAsync(new BufferedFramesResponse() { Error = result.Error }); break; } } #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed } lock (_frameStreamWriterLock) { _frameStreamWriter = null; } }
public override global::System.Threading.Tasks.Task Import(Grpc.Core.IAsyncStreamReader <ImportRequest> requestStream, Grpc.Core.IServerStreamWriter <ImportResponse> responseStream, Grpc.Core.ServerCallContext context) { var res = new ImportResponse(); res.Result = "result123xyz"; Console.WriteLine("Server got request, sending response"); return(responseStream.WriteAsync(res)); }
public override async Task SayHellos(HelloRequest request, Grpc.Core.IServerStreamWriter <HelloReply> responseStream, Grpc.Core.ServerCallContext context) { var i = 0; while (!context.CancellationToken.IsCancellationRequested) { var message = $"How are you {request.Name}? {++i}"; _logger.LogInformation($"Sending greeting {message}."); await responseStream.WriteAsync(new HelloReply { Message = message }); await Task.Delay(1000); } }
public override async Task GreetServerStream(GreetRequest request, Grpc.Core.IServerStreamWriter <GreetResponse> responseStream, Grpc.Core.ServerCallContext context) { Console.WriteLine("Server received a normal greet request. Server will response with a stream!!!"); string response = string.Format("Hello {0} {1}", request.Greet.FirstName, request.Greet.LastName); foreach (var item in Enumerable.Range(1, 10)) { await responseStream.WriteAsync(new GreetResponse() { Response = response }); } }
//public override Task<NewsReply> FindNews(NewsRequest request, ServerCallContext context) //{ // //load settings drom db // foreach(string site in db.GetUserSettings(request.Userid, "site")){ // RSSReader.ReadUserFeed(site, db.GetUserSettings(request.Userid, "tag")); // } // //streaming // return Task.FromResult(ConvertToRssItem(RSSReader.ReadFeed2(request.Url)[0])); //} public override async Task FindNews(NewsRequest request, Grpc.Core.IServerStreamWriter <NewsReply> responseStream, ServerCallContext context) { //load settings drom db foreach (string site in db.GetUserSettings(request.Userid, "site")) { //streaming foreach (SyndicationItem item in RSSReader.ReadUserFeed(site, db.GetUserSettings(request.Userid, "tag"))) { await responseStream.WriteAsync(ConvertToRssItem(item)); } } Console.WriteLine("\nNews sent to client. Userid: " + request.Userid); //return ; <NewsReply> }
public override async Task ReadySetGo(Grpc.Core.IAsyncStreamReader <RaceMessage> requestStream, Grpc.Core.IServerStreamWriter <RaceMessage> responseStream, Grpc.Core.ServerCallContext context) { var raceDuration = TimeSpan.Parse(context.RequestHeaders.Single(h => h.Key == "race-duration").Value); RaceMessage?lastMessageReceived = null; var readTask = Task.Run(async() => { await foreach (var message in requestStream.ReadAllAsync()) { lastMessageReceived = message; } }); var sw = Stopwatch.StartNew(); var sent = 0; while (sw.Elapsed < raceDuration) { await responseStream.WriteAsync(new RaceMessage { Count = ++sent }); } await readTask; }
public override async Task Mailbox(Grpc.Core.IAsyncStreamReader <ForwardMailMessage> requestStream, Grpc.Core.IServerStreamWriter <MailboxMessage> responseStream, Grpc.Core.ServerCallContext context) { var mailboxName = context.RequestHeaders.Single(e => e.Key == "mailbox-name").Value; var mailQueue = _messageQueueRepository.GetMailQueue(mailboxName); _logger.LogInformation($"Connected to {mailboxName}"); mailQueue.Changed += ReportChanges; try { while (await requestStream.MoveNext()) { if (mailQueue.TryForwardMail(out var message)) { _logger.LogInformation($"Forwarded mail:{message.Content}"); } else { _logger.LogWarning("No mail to forward."); } } } finally { mailQueue.Changed -= ReportChanges; } _logger.LogInformation($"{mailboxName} disconnected."); async Task ReportChanges((int totalCount, int forwardCount, MailboxMessage.Types.Reason reason) state) { await responseStream.WriteAsync(new MailboxMessage { Forwarded = state.forwardCount, New = state.totalCount - state.forwardCount, Reason = state.reason }); } }