Esempio n. 1
0
        private static async Task Receive(IInteract context, IServerCache cache, BufferBlock <ResponseMessageModel> messages, CancellationToken token)
        {
            var it = context.Receive();
            EnumeratorResult <ChunkedStream> messageStream;

            while ((messageStream = await it.MoveNextAsync(token)).Success)
            {
                using (messageStream.Value)
                {
                    var message = ProtoBuf.Serializer.Deserialize <RequestMessageModel>(messageStream.Value);
                    var items   = cache.TryGetItems(message.Keys);
                    if (items.Count > 0)
                    {
                        var mes = ResponseMessageModel.Create(items);
                        if (!messages.Post(mes) && messages.TryReceive(out var last))
                        {
                            messages.Post(mes);
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        private async Task Connect(CancellationToken token)
        {
            var reconnectCount = _reconnectCount;

loop:
            var client = new ClientWebSocket();

            try
            {
                client.Options.KeepAliveInterval = _keepAliveInterval;
                await Task.WhenAny(Task.Delay(_connectTimeout, token), client.ConnectAsync(_connectionString, token));

                if (client.State != WebSocketState.Open)
                {
                    throw new TimeoutException();
                }
                if (_interact != null)
                {
                    try { _interact.Dispose(); } catch { }
                }

                _interact = new Interact(client);
                _receive  = _interact.Receive();
            }
            catch (TaskCanceledException) { throw; }
            catch
            {
                try { client.Dispose(); } catch { }
                await Task.Delay(_reconnectInterval, token);

                if (--reconnectCount == 0)
                {
                    throw;
                }
                goto loop;
            }
        }