public override BasicHeader Deserialize(RecyclableMemoryStream target) { base.Deserialize(target); var contentTypeLength = target.ReadUInt16(); ContentType = EmptyContent; if (contentTypeLength > 0) { ContentType = new byte[contentTypeLength]; target.Read(ContentType, 0, contentTypeLength); } int len = target.ReadInt16(); ContentLength = new int[len]; for (var i = 0; i < len; i++) { ContentLength[i] = target.Read7BitInt(); } return(this); }
public void PublicMethodThrowAfterDispose() { var stream = new RecyclableMemoryStream(); stream.Dispose(); byte[] buffer = new byte[1]; Assert.That( () => stream.SetLength(10), Throws.InstanceOf <ObjectDisposedException>()); Assert.That( () => stream.WriteByte(0x00), Throws.InstanceOf <ObjectDisposedException>()); Assert.That( () => stream.Write(buffer, 0, 1), Throws.InstanceOf <ObjectDisposedException>()); Assert.That( () => stream.ReadByte(), Throws.InstanceOf <ObjectDisposedException>()); Assert.That( () => stream.Read(buffer, 0, 1), Throws.InstanceOf <ObjectDisposedException>()); }
public static void Main(string[] args) { Console.WriteLine("Websocket Client"); Console.WriteLine("Enter URI of websocket server to connect to"); while (_WsChannel == null) { var input = Console.ReadLine(); bool validURI = Uri.TryCreate(input, UriKind.Absolute, out Uri uri); if (validURI == false) { Console.WriteLine("Uri you entered isn't valid"); continue; } var task = Task.Run(async() => await TryConnect(2000, uri)); task.Wait(); KeyValuePair <bool, WebSocket> connected = task.Result; if (connected.Key == false) { Console.WriteLine("Connection failed"); continue; } Console.WriteLine("Connected"); _WsChannel = new WsChannel(connected.Value, Format.Text); } Task.Run(async() => { while (true) { var stream = await _WsChannel.ReadAsync(); RecyclableMemoryStream rs = stream as RecyclableMemoryStream; if (stream == null) { break; } byte[] buffer = new byte[rs.Length]; rs.Position = 0; rs.Read(buffer, 0, (int)rs.Length); var message = Encoding.UTF8.GetString(buffer); Console.WriteLine($"Arrived Message: {message}"); } }); Console.WriteLine("Enter Messages to send to server. Enter q to Quit"); while (true) { string input = Console.ReadLine(); if (input == "q") { break; } var data = Encoding.UTF8.GetBytes(input); MemoryStream stream = RecyclableMemoryStreamManager.Default.GetStream("userInput", data.Length, true); stream.Write(data, 0, data.Length); var writeTask = Task.Run(async() => await _WsChannel.WriteAsync(stream)); writeTask.Wait(); stream.Dispose(); } }
public async ValueTask <XeusClue> Import(string path, CancellationToken token = default) { if (path == null) { throw new ArgumentNullException(nameof(path)); } return(await Task.Run(async() => { // Check lock (_lockObject) { var info = _contentMetadataStorage.GetFileContentMetadata(path); if (info != null) { return info.Clue; } } XeusClue?clue = null; var lockedHashes = new HashSet <OmniHash>(); SharedBlocksMetadata?sharedBlocksInfo = null; { const int blockLength = 1024 * 1024; const OmniHashAlgorithmType hashAlgorithmType = OmniHashAlgorithmType.Sha2_256; const CorrectionAlgorithmType correctionAlgorithmType = CorrectionAlgorithmType.ReedSolomon8; byte depth = 0; var merkleTreeSectionList = new List <MerkleTreeSection>(); // File using (var stream = new UnbufferedFileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, FileOptions.None, _bufferPool)) { if (stream.Length <= blockLength) { OmniHash hash; using (var bufferMemoryOwner = _bufferPool.Rent((int)stream.Length)) { stream.Read(bufferMemoryOwner.Memory.Span); if (hashAlgorithmType == OmniHashAlgorithmType.Sha2_256) { hash = new OmniHash(OmniHashAlgorithmType.Sha2_256, Sha2_256.ComputeHash(bufferMemoryOwner.Memory.Span)); } } sharedBlocksInfo = new SharedBlocksMetadata(path, (ulong)stream.Length, (uint)stream.Length, new OmniHash[] { hash }); clue = new XeusClue(hash, depth); } else { var sharedHashes = new List <OmniHash>(); for (; ;) { var targetHashes = new List <OmniHash>(); var targetMemoryOwners = new List <IMemoryOwner <byte> >(); ulong sumLength = 0; try { for (int i = 0; stream.Position < stream.Length; i++) { token.ThrowIfCancellationRequested(); uint length = (uint)Math.Min(stream.Length - stream.Position, blockLength); var bufferMemoryOwner = _bufferPool.Rent((int)length); try { stream.Read(bufferMemoryOwner.Memory.Span); sumLength += length; } catch (Exception e) { bufferMemoryOwner.Dispose(); throw e; } OmniHash hash; if (hashAlgorithmType == OmniHashAlgorithmType.Sha2_256) { hash = new OmniHash(OmniHashAlgorithmType.Sha2_256, Sha2_256.ComputeHash(bufferMemoryOwner.Memory.Span)); } sharedHashes.Add(hash); targetHashes.Add(hash); targetMemoryOwners.Add(bufferMemoryOwner); if (targetMemoryOwners.Count >= 128) { break; } } var parityHashes = await this.ParityEncode(targetMemoryOwners.Select(n => n.Memory), hashAlgorithmType, correctionAlgorithmType, token); lockedHashes.UnionWith(parityHashes); merkleTreeSectionList.Add(new MerkleTreeSection(correctionAlgorithmType, sumLength, CollectionHelper.Unite(targetHashes, parityHashes).ToArray())); } finally { foreach (var memoryOwner in targetMemoryOwners) { memoryOwner.Dispose(); } } if (stream.Position == stream.Length) { break; } } sharedBlocksInfo = new SharedBlocksMetadata(path, (ulong)stream.Length, blockLength, sharedHashes.ToArray()); depth++; } } while (merkleTreeSectionList.Count > 0) { // Index using (var stream = new RecyclableMemoryStream(_bufferPool)) { RocketPackHelper.MessageToStream(new MerkleTreeNode(merkleTreeSectionList.ToArray()), stream); stream.Seek(0, SeekOrigin.Begin); merkleTreeSectionList.Clear(); if (stream.Length <= blockLength) { OmniHash hash; using (var bufferMemoryOwner = _bufferPool.Rent((int)stream.Length)) { stream.Read(bufferMemoryOwner.Memory.Span); if (hashAlgorithmType == OmniHashAlgorithmType.Sha2_256) { hash = new OmniHash(OmniHashAlgorithmType.Sha2_256, Sha2_256.ComputeHash(bufferMemoryOwner.Memory.Span)); } _blockStorage.Lock(hash); bool result = _blockStorage.TrySet(hash, bufferMemoryOwner.Memory.Span); if (!result) { throw new ImportFailed("Failed to save Block."); } lockedHashes.Add(hash); } clue = new XeusClue(hash, depth); } else { for (; ;) { var targetHashes = new List <OmniHash>(); var targetMemoryOwners = new List <IMemoryOwner <byte> >(); ulong sumLength = 0; try { for (int i = 0; stream.Position < stream.Length; i++) { token.ThrowIfCancellationRequested(); uint length = (uint)Math.Min(stream.Length - stream.Position, blockLength); var bufferMemoryOwner = _bufferPool.Rent((int)length); try { stream.Read(bufferMemoryOwner.Memory.Span); sumLength += length; } catch (Exception e) { bufferMemoryOwner.Dispose(); throw e; } OmniHash hash; if (hashAlgorithmType == OmniHashAlgorithmType.Sha2_256) { hash = new OmniHash(OmniHashAlgorithmType.Sha2_256, Sha2_256.ComputeHash(bufferMemoryOwner.Memory.Span)); } _blockStorage.Lock(hash); bool result = _blockStorage.TrySet(hash, bufferMemoryOwner.Memory.Span); if (!result) { throw new ImportFailed("Failed to save Block."); } lockedHashes.Add(hash); targetHashes.Add(hash); targetMemoryOwners.Add(bufferMemoryOwner); if (targetMemoryOwners.Count >= 128) { break; } } var parityHashes = await this.ParityEncode(targetMemoryOwners.Select(n => n.Memory), hashAlgorithmType, correctionAlgorithmType, token); lockedHashes.UnionWith(parityHashes); merkleTreeSectionList.Add(new MerkleTreeSection(correctionAlgorithmType, sumLength, CollectionHelper.Unite(targetHashes, parityHashes).ToArray())); } finally { foreach (var memoryOwner in targetMemoryOwners) { memoryOwner.Dispose(); } } if (stream.Position == stream.Length) { break; } } depth++; } } } } if (clue == null) { throw new ImportFailed("clue is null"); } lock (_lockObject) { if (!_contentMetadataStorage.ContainsFileContentMetadata(path)) { _contentMetadataStorage.Add(new ContentMetadata(clue, lockedHashes.ToArray(), sharedBlocksInfo)); foreach (var hash in lockedHashes) { _blockStorage.Lock(hash); } } } return clue; }, token)); }
public override int Read(byte[] buffer, int offset, int count) { return(_decompressedStream.Read(buffer, offset, count)); }