Ejemplo n.º 1
0
        public void WaitForReady()
        {
            var callOptions = new CallOptions();

            Assert.IsFalse(callOptions.IsWaitForReady);

            Assert.AreEqual(CallFlags.WaitForReady, callOptions.WithWaitForReady().Flags);
            Assert.IsTrue(callOptions.WithWaitForReady().IsWaitForReady);
            Assert.IsFalse(callOptions.WithWaitForReady(true).WithWaitForReady(false).IsWaitForReady);
        }
        private CallOptions ApplyConfigToCallOptions(CallOptions callOptions)
        {
            if (callOptions.Headers == null)
            {
                // Add empty metadata to options:
                callOptions = callOptions.WithHeaders(new Metadata());
            }

            if (_configuration.WaitForReady && callOptions.IsWaitForReady != _configuration.WaitForReady)
            {
                callOptions = callOptions.WithWaitForReady();
            }

            if (_configuration.FallbackCancellationToken != default && callOptions.CancellationToken != _configuration.FallbackCancellationToken)
            {
                callOptions = callOptions.WithCancellationToken(_configuration.FallbackCancellationToken);
            }

            return(callOptions);
        }
Ejemplo n.º 3
0
        public void Start()
        {
            if (Sourse != null && !Sourse.IsCancellationRequested)
            {
                return;
            }

            Sourse = new CancellationTokenSource();

            var callOptions = new CallOptions(new Metadata {
                new Metadata.Entry("mailbox-name", "game")
            }, cancellationToken: Sourse.Token);
            var _call = _client.Mailbox(callOptions.WithWaitForReady());

            _ = Task.Run(async() =>
            {
                var incomeMailQueue = _dbMailQueueRepository.GetIncomeMailQueue();
                await foreach (var message in _call.ResponseStream.ReadAllAsync(Sourse.Token))
                {
                    await incomeMailQueue.WriteAsync(new MailPacket {
                        Id      = message.Id, Content = message.Content.ToByteArray(),
                        Reserve = message.Reserve, UserId = message.UserId, ClientId = message.ClientId
                    });
                }
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("!!!end");
                Console.ResetColor();
            }, Sourse.Token);


            _ = Task.Run(async() =>
            {
                while (!Sourse.Token.IsCancellationRequested)
                {
                    try
                    {
                        if (CurrentMail == null)
                        {
                            if (await _mailChannel.Reader.WaitToReadAsync(Sourse.Token))
                            {
                                if (_mailChannel.Reader.TryRead(out var mail))
                                {
                                    CurrentMail = mail;
                                    await WriteDBMail(CurrentMail);
                                    CurrentMail = null;
                                }
                            }
                            else
                            {
                                break;
                            }
                        }
                        else
                        {
                            await WriteDBMail(CurrentMail);
                            CurrentMail = null;
                        }
                    }
                    catch (Exception e)
                    {
                        _logger.LogWarning(e.Message);
                        Sourse.Cancel();
                    }
                }
            }, Sourse.Token);

            async Task WriteDBMail(MailPacket mail)
            {
                var forward = new ForwardMailMessage
                {
                    Id      = mail.Id,
                    Content = mail.Content != null?Google.Protobuf.ByteString.CopyFrom(mail.Content) : Google.Protobuf.ByteString.Empty,
                                  Reserve = mail.ClientId, UserId = mail.UserId, ClientId = mail.ClientId
                };

                try
                {
                    await _call.RequestStream.WriteAsync(forward);
                }
                catch (Exception e)
                {
                    _logger.LogWarning(e.Message);
                    Sourse.Cancel();
                    EventCancelled?.Invoke(_type);
                }
            }
        }