Ejemplo n.º 1
0
        public async Task WriteToWebSocket <TNotification>(TNotification notification)
        {
            _context.Reset();
            _context.Renew();

            _ms.SetLength(0);

            await using (var writer = new AsyncBlittableJsonTextWriter(_context, _ms))
            {
                var notificationType = notification.GetType();

                if (notificationType == typeof(DynamicJsonValue))
                {
                    _context.Write(writer, notification as DynamicJsonValue);
                }
                else if (notificationType == typeof(BlittableJsonReaderObject))
                {
                    _context.Write(writer, notification as BlittableJsonReaderObject);
                }
                else
                {
                    ThrowNotSupportedType(notification);
                }
            }

            _ms.TryGetBuffer(out ArraySegment <byte> bytes);

            await _webSocket.SendAsync(bytes, WebSocketMessageType.Text, true, _resourceShutdown);
        }
Ejemplo n.º 2
0
        public static IEnumerable <BlittableJsonReaderObject> ReadArrayToMemory(JsonOperationContext context, PeepingTomStream peepingTomStream, UnmanagedJsonParser parser, JsonParserState state, JsonOperationContext.ManagedPinnedBuffer buffer)
        {
            if (Read(peepingTomStream, parser, state, buffer) == false)
            {
                ThrowInvalidJson(peepingTomStream);
            }

            if (state.CurrentTokenType != JsonParserToken.StartArray)
            {
                ThrowInvalidJson(peepingTomStream);
            }

            int  docsCountOnCachedRenewSession = 0;
            bool cachedItemsRenew = false;

            while (true)
            {
                if (docsCountOnCachedRenewSession <= 16 * 1024)
                {
                    if (cachedItemsRenew)
                    {
                        context.CachedProperties = new CachedProperties(context);
                        ++docsCountOnCachedRenewSession;
                    }
                }
                else
                {
                    context.Renew();
                    docsCountOnCachedRenewSession = 0;
                }

                if (Read(peepingTomStream, parser, state, buffer) == false)
                {
                    ThrowInvalidJson(peepingTomStream);
                }

                if (state.CurrentTokenType == JsonParserToken.EndArray)
                {
                    break;
                }

                using (var builder = new BlittableJsonDocumentBuilder(context, BlittableJsonDocumentBuilder.UsageMode.None, "readArray/singleResult", parser, state))
                {
                    if (cachedItemsRenew == false)
                    {
                        cachedItemsRenew = builder.NeedResetPropertiesCache();
                    }
                    ReadObject(builder, peepingTomStream, parser, buffer);

                    yield return(builder.CreateReader());
                }
            }
        }
Ejemplo n.º 3
0
            private void CheckIfContextOrCacheNeedToBeRenewed()
            {
                if (_builderContext.AllocatedMemory > 4 * 1024 * 1024 ||
                    _docsCountOnCachedRenewSession > _maxDocsCountOnCachedRenewSession)
                {
                    _builderContext.Reset();
                    _builderContext.Renew();
                    _docsCountOnCachedRenewSession = 0;
                    return;
                }

                if (_builder.NeedClearPropertiesCache())
                {
                    _builderContext.CachedProperties.ClearRenew();
                    return;
                }

                ++_docsCountOnCachedRenewSession;
            }
        public async Task StartSendingNotifications()
        {
            try
            {
                while (_cancellationTokenSource.IsCancellationRequested == false)
                {
                    _context.Reset();
                    _context.Renew();

                    var result = await _manualResetEvent.WaitAsync(TimeSpan.FromMilliseconds(5000)).ConfigureAwait(false);

                    if (IsAlive == false)
                    {
                        return;
                    }

                    if (result == false)
                    {
                        await SendMessage(WebSocketHelper.Heartbeat).ConfigureAwait(false);

                        continue;
                    }

                    _manualResetEvent.Reset();

                    while (_messages.TryDequeue(out var message))
                    {
                        _context.Reset();
                        _context.Renew();
                        if (IsAlive == false)
                        {
                            return;
                        }

                        await SendMessage(ToByteArraySegment(message)).ConfigureAwait(false);
                    }
                }
            }
            catch (Exception e)
            {
                if (Logger.IsInfoEnabled)
                {
                    Logger.Info("Error when handling web socket connection", e);
                }
            }
            finally
            {
                TrafficWatchManager.Disconnect(this);

                try
                {
                    if (_disposed == false)
                    {
                        await _webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "NORMAL_CLOSE", _cancellationTokenSource.Token);
                    }
                }
                catch
                {
                    // ignore
                }
            }
        }