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 static RavenJToken TryLoad(Stream stream) { var jsonTextReader = new RavenJsonTextReader(new StreamReader(stream)); if (jsonTextReader.Read() == false || jsonTextReader.TokenType == JsonToken.None) { return(null); } return(Load(jsonTextReader)); }
public static bool TryGetJsonReaderForStream(Stream stream, out JsonTextReader jsonTextReader, out CountingStream sizeStream) { jsonTextReader = null; sizeStream = null; try { stream.Position = 0; sizeStream = new CountingStream(new GZipStream(stream, CompressionMode.Decompress)); var streamReader = new StreamReader(sizeStream); jsonTextReader = new RavenJsonTextReader(streamReader); if (jsonTextReader.Read() == false) { return(false); } } catch (Exception e) { if (e is InvalidDataException == false) { if (sizeStream != null) { sizeStream.Dispose(); } throw; } stream.Seek(0, SeekOrigin.Begin); sizeStream = new CountingStream(stream); var streamReader = new StreamReader(sizeStream); jsonTextReader = new JsonTextReader(streamReader); if (jsonTextReader.Read() == false) { return(false); } } return(true); }
public async virtual Task ImportData(SmugglerImportOptions <RavenConnectionStringOptions> importOptions, Stream stream) { Operations.Configure(Options); Operations.Initialize(Options); await DetectServerSupportedFeatures(importOptions.To); Stream sizeStream; var sw = Stopwatch.StartNew(); // Try to read the stream compressed, otherwise continue uncompressed. JsonTextReader jsonReader; try { stream.Position = 0; sizeStream = new CountingStream(new GZipStream(stream, CompressionMode.Decompress)); var streamReader = new StreamReader(sizeStream); jsonReader = new RavenJsonTextReader(streamReader); if (jsonReader.Read() == false) { return; } } catch (Exception e) { if (e is InvalidDataException == false) { throw; } stream.Seek(0, SeekOrigin.Begin); sizeStream = new CountingStream(stream); var streamReader = new StreamReader(sizeStream); jsonReader = new JsonTextReader(streamReader); if (jsonReader.Read() == false) { return; } } if (jsonReader.TokenType != JsonToken.StartObject) { throw new InvalidDataException("StartObject was expected"); } var exportCounts = new Dictionary <string, int>(); var exportSectionRegistar = new Dictionary <string, Func <int> >(); Options.CancelToken.Token.ThrowIfCancellationRequested(); exportSectionRegistar.Add("Indexes", () => { Operations.ShowProgress("Begin reading indexes"); var indexCount = ImportIndexes(jsonReader).Result; Operations.ShowProgress(string.Format("Done with reading indexes, total: {0}", indexCount)); return(indexCount); }); exportSectionRegistar.Add("Docs", () => { Operations.ShowProgress("Begin reading documents"); var documentCount = ImportDocuments(jsonReader).Result; Operations.ShowProgress(string.Format("Done with reading documents, total: {0}", documentCount)); return(documentCount); }); exportSectionRegistar.Add("Attachments", () => { Operations.ShowProgress("Begin reading attachments"); var attachmentCount = ImportAttachments(importOptions.To, jsonReader).Result; Operations.ShowProgress(string.Format("Done with reading attachments, total: {0}", attachmentCount)); return(attachmentCount); }); exportSectionRegistar.Add("Transformers", () => { Operations.ShowProgress("Begin reading transformers"); var transformersCount = ImportTransformers(jsonReader).Result; Operations.ShowProgress(string.Format("Done with reading transformers, total: {0}", transformersCount)); return(transformersCount); }); exportSectionRegistar.Add("DocsDeletions", () => { Operations.ShowProgress("Begin reading deleted documents"); var deletedDocumentsCount = ImportDeletedDocuments(jsonReader).Result; Operations.ShowProgress(string.Format("Done with reading deleted documents, total: {0}", deletedDocumentsCount)); return(deletedDocumentsCount); }); exportSectionRegistar.Add("AttachmentsDeletions", () => { Operations.ShowProgress("Begin reading deleted attachments"); var deletedAttachmentsCount = ImportDeletedAttachments(jsonReader).Result; Operations.ShowProgress(string.Format("Done with reading deleted attachments, total: {0}", deletedAttachmentsCount)); return(deletedAttachmentsCount); }); exportSectionRegistar.Add("Identities", () => { Operations.ShowProgress("Begin reading identities"); var identitiesCount = ImportIdentities(jsonReader).Result; Operations.ShowProgress(string.Format("Done with reading identities, total: {0}", identitiesCount)); return(identitiesCount); }); exportSectionRegistar.Keys.ForEach(k => exportCounts[k] = 0); while (jsonReader.Read() && jsonReader.TokenType != JsonToken.EndObject) { Options.CancelToken.Token.ThrowIfCancellationRequested(); if (jsonReader.TokenType != JsonToken.PropertyName) { throw new InvalidDataException("PropertyName was expected"); } Func <int> currentAction; var currentSection = jsonReader.Value.ToString(); if (exportSectionRegistar.TryGetValue(currentSection, out currentAction) == false) { throw new InvalidDataException("Unexpected property found: " + jsonReader.Value); } if (jsonReader.Read() == false) { exportCounts[currentSection] = 0; continue; } if (jsonReader.TokenType != JsonToken.StartArray) { throw new InvalidDataException("StartArray was expected"); } exportCounts[currentSection] = currentAction(); } sw.Stop(); Operations.ShowProgress("Imported {0:#,#;;0} documents and {1:#,#;;0} attachments, deleted {2:#,#;;0} documents and {3:#,#;;0} attachments in {4:#,#.###;;0} s", exportCounts["Docs"], exportCounts["Attachments"], exportCounts["DocsDeletions"], exportCounts["AttachmentsDeletions"], sw.ElapsedMilliseconds / 1000f); Options.CancelToken.Token.ThrowIfCancellationRequested(); }
private async Task Receive() { try { using (var ms = new MemoryStream()) { ms.SetLength(4096); while (webSocket.State == WebSocketState.Open) { if (ms.Length > 4096 * 16) { ms.SetLength(4096); } ms.Position = 0; ArraySegment <byte> bytes; ms.TryGetBuffer(out bytes); var result = await webSocket.ReceiveAsync(new ArraySegment <byte>(bytes.Array, (int)ms.Position, (int)(ms.Length - ms.Position)), disposedToken.Token); ms.Position = result.Count; if (result.MessageType == WebSocketMessageType.Close) { await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None); return; } while (result.EndOfMessage == false) { if (ms.Length - ms.Position < 1024) { ms.SetLength(ms.Length + 4096); } ms.TryGetBuffer(out bytes); result = await webSocket.ReceiveAsync(new ArraySegment <byte>(bytes.Array, (int)ms.Position, (int)(ms.Length - ms.Position)), CancellationToken.None); ms.Position += result.Count; } ms.SetLength(ms.Position); ms.Position = 0; using (var reader = new StreamReader(ms, Encoding.UTF8, true, 1024, true)) using (var jsonReader = new RavenJsonTextReader(reader) { SupportMultipleContent = true }) while (jsonReader.Read()) { var ravenJObject = RavenJObject.Load(jsonReader); HandleRevicedNotification(ravenJObject); } } } } catch (WebSocketException ex) { logger.DebugException("Failed to receive a message, client was probably disconnected", ex); } }