Ejemplo n.º 1
0
        private async Task <PubsubPayload> ReceiveAsync(ClientWebSocket client, CancellationToken cancelToken)
        {
            // Reset memory stream
            _memoryStream.Position = 0;
            _memoryStream.SetLength(0);

            WebSocketReceiveResult result;

            do
            {
                var buffer = _memoryStream.Buffer.RequestSegment(10 * 1024);
                result = await client.ReceiveAsync(buffer, cancelToken).ConfigureAwait(false);

                _memoryStream.Buffer.Advance(result.Count);

                if (result.CloseStatus != null)
                {
                    throw new WebSocketClosedException(result.CloseStatus.Value, result.CloseStatusDescription);
                }
            }while (!result.EndOfMessage);

            var payload = Serializer.Read <PubsubPayload>(_memoryStream.Buffer.AsReadOnlySpan());

            HandleEvent(payload);
            ReceivedPayload?.Invoke(payload, _memoryStream.Buffer.Length);
            return(payload);
        }
Ejemplo n.º 2
0
        private async Task <RpcPayload> ReceiveAsync(ClientWebSocket client, TaskCompletionSource <bool> readySignal, CancellationToken cancelToken)
        {
            // Reset stream
            _decompressed.Position = 0;
            _decompressed.SetLength(0);

            // Receive data
            WebSocketReceiveResult result;

            do
            {
                var buffer = _decompressed.Buffer.RequestSegment(10 * 1024); // 10 KB
                result = await client.ReceiveAsync(buffer, cancelToken).ConfigureAwait(false);

                _decompressed.Buffer.Advance(result.Count);

                if (result.CloseStatus != null)
                {
                    throw new WebSocketClosedException(result.CloseStatus.Value, result.CloseStatusDescription);
                }
            }while (!result.EndOfMessage);

            // Deserialize
            var payload = JsonSerializer.Read <RpcPayload>(_decompressed.Buffer.AsReadOnlySpan());

            // Handle result
            HandleEvent(payload, readySignal); // Must be before event so slow user handling can't trigger our timeouts
            ReceivedPayload?.Invoke(payload, new PayloadInfo(_decompressed.Buffer.Length, _decompressed.Buffer.Length));
            return(payload);
        }
Ejemplo n.º 3
0
        private async Task <GatewayPayload> ReceiveAsync(ClientWebSocket client, TaskCompletionSource <bool> readySignal, CancellationToken cancelToken)
        {
            // Reset compressed stream
            _compressed.Position = 0;
            _compressed.SetLength(0);

            // Receive compressed data
            WebSocketReceiveResult result;

            do
            {
                var buffer = _compressed.Buffer.RequestSegment(10 * 1024); // 10 KB
                result = await client.ReceiveAsync(buffer, cancelToken).ConfigureAwait(false);

                _compressed.Buffer.Advance(result.Count);
                _receivedData = true;

                if (result.CloseStatus != null)
                {
                    throw new WebSocketClosedException(result.CloseStatus.Value, result.CloseStatusDescription);
                }
            }while (!result.EndOfMessage);

            // Skip zlib header
            if (!_readZlibHeader)
            {
                if (_compressed.Buffer.Array[0] != 0x78)
                {
                    throw new SerializationException("First payload is missing zlib header");
                }
                _compressed.Position = 2;
                _readZlibHeader      = true;
            }

            // Reset decompressed stream
            _decompressed.Position = 0;
            _decompressed.SetLength(0);

            // Decompress
            _zlibStream.CopyTo(_decompressed);

            // Deserialize
            var payload = EtfSerializer.Read <GatewayPayload>(_decompressed.Buffer.AsReadOnlySpan());

            if (payload.Sequence.HasValue)
            {
                _lastSeq = payload.Sequence.Value;
            }

            // Handle result
            HandleEvent(payload, readySignal); // Must be before event so slow user handling can't trigger our timeouts
            ReceivedPayload?.Invoke(payload, new PayloadInfo(_decompressed.Buffer.Length, _compressed.Buffer.Length));
            return(payload);
        }