private async Task <BlittableJsonReaderObject> Receive(RavenClientWebSocket 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(); JsonOperationContext.ManagedPinnedBuffer buffer; using (context.GetManagedBuffer(out buffer)) using (var parser = new UnmanagedJsonParser(context, state, "")) //TODO: FIXME { 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, 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; } }
private async Task <RavenJObject> Recieve(RavenClientWebSocket webSocket) { try { if (webSocket.State != WebSocketState.Open) { throw new InvalidOperationException( $"Trying to 'ReceiveAsync' WebSocket while not in Open state. State is {webSocket.State}"); } using (var ms = new MemoryStream()) { ArraySegment <byte> bytes; ms.SetLength(MaxWebSocketRecvSize); ms.TryGetBuffer(out bytes); var arraySegment = new ArraySegment <byte>(bytes.Array, 0, MaxWebSocketRecvSize); var result = await webSocket.ReceiveAsync(arraySegment, disposedToken.Token); if (result.MessageType == WebSocketMessageType.Close) { if (Logger.IsDebugEnabled) { Logger.Debug("Client got close message from server and is closing connection"); } // actual socket close from dispose return(null); } if (result.EndOfMessage == false) { var err = $"In Recieve got response longer then {MaxWebSocketRecvSize}"; var ex = new InvalidOperationException(err); Logger.DebugException(err, ex); throw ex; } using (var reader = new StreamReader(ms, Encoding.UTF8, true, MaxWebSocketRecvSize, true)) using (var jsonReader = new RavenJsonTextReader(reader) { SupportMultipleContent = true }) { if (jsonReader.Read() == false) { throw new InvalidDataException("Couldn't read recieved websocket json message"); } return(RavenJObject.Load(jsonReader)); } } } catch (WebSocketException ex) { Logger.DebugException("Failed to receive a message, client was probably disconnected", ex); throw; } }
public async Task <BlittableJsonReaderObject> TryReadFromWebSocket( JsonOperationContext context, RavenClientWebSocket webSocket, string debugTag, CancellationToken cancellationToken) { var jsonParserState = new JsonParserState(); JsonOperationContext.ManagedPinnedBuffer bytes; using (context.GetManagedBuffer(out bytes)) using (var parser = new UnmanagedJsonParser(context, jsonParserState, debugTag)) { var writer = new BlittableJsonDocumentBuilder(context, BlittableJsonDocumentBuilder.UsageMode.None, debugTag, parser, jsonParserState); writer.ReadObjectDocument(); var result = await webSocket.ReceiveAsync(bytes.Buffer, cancellationToken).ConfigureAwait(false); parser.SetBuffer(bytes, result.Count); while (writer.Read() == false) { // we got incomplete json response. // This might happen if we close the connection but still server sends something if (result.CloseStatus != null) { return(null); } result = await webSocket.ReceiveAsync(bytes.Buffer, cancellationToken).ConfigureAwait(false); parser.SetBuffer(bytes, result.Count); } writer.FinalizeDocument(); return(writer.CreateReader()); } }