Пример #1
0
        private async Task <BlittableJsonReaderObject> Receive(ClientWebSocket webSocket,
                                                               JsonOperationContext context)
        {
            BlittableJsonDocumentBuilder builder = null;

            try
            {
                if (webSocket.State != WebSocketState.Open)
                {
                    throw new InvalidOperationException(
                              $"Trying to 'ReceiveAsync' WebSocket while not in Open state. State is {webSocket.State}");
                }

                var state = new JsonParserState();
                using (context.GetManagedBuffer(out var buffer))
                    using (var parser = new UnmanagedJsonParser(context, state, ""))
                    {
                        builder = new BlittableJsonDocumentBuilder(context, BlittableJsonDocumentBuilder.UsageMode.None,
                                                                   nameof(TrafficRec) + "." + nameof(Receive), parser, state);
                        builder.ReadObjectDocument();
                        while (builder.Read() == false)
                        {
                            var result = await webSocket.ReceiveAsync(buffer.Buffer, CancellationToken.None);

                            if (result.MessageType == WebSocketMessageType.Close)
                            {
                                if (_logger.IsInfoEnabled)
                                {
                                    _logger.Info("Client got close message from server and is closing connection");
                                }

                                builder.Dispose();
                                // actual socket close from dispose
                                return(null);
                            }

                            if (result.EndOfMessage == false)
                            {
                                throw new EndOfStreamException("Stream ended without reaching end of json content.");
                            }

                            parser.SetBuffer(buffer, 0, result.Count);
                        }
                        builder.FinalizeDocument();

                        return(builder.CreateReader());
                    }
            }
            catch (WebSocketException ex)
            {
                builder?.Dispose();

                if (_logger.IsInfoEnabled)
                {
                    _logger.Info("Failed to receive a message, client was probably disconnected", ex);
                }

                throw;
            }
        }
Пример #2
0
 private void DisposeInternal()
 {
     _response.Response.Dispose();
     _parser.Dispose();
     _returnBuffer.Dispose();
     _peepingTomStream.Dispose();
     _builder.Dispose();
     _returnContext.Dispose();
 }