/// <summary> /// Parser method to copy all body bytes. /// </summary> /// <param name="reader"> </param> /// <returns></returns> /// <remarks>Needed since a TCP packet can contain multiple messages /// after each other, or partial messages.</remarks> private bool ParseBody(IBufferReader reader) { if (reader.RemainingLength == 0) { return(false); } if (_currentMessage.Body == null) { if (_currentMessage.ContentLength > _bufferSize) { _currentMessage.Body = new FileStream( Path.Combine(Path.GetTempPath(), "http." + Guid.NewGuid().ToString("N") + ".tmp"), FileMode.CreateNew); } else { var slice = _bufferPool.Pop(); _currentMessage.Body = new SliceStream(slice); } } var bytesLeft = (int)Math.Min(_currentMessage.ContentLength - _currentMessage.Body.Length, reader.RemainingLength); reader.CopyTo(_currentMessage.Body, bytesLeft); return(_currentMessage.Body.Length == _currentMessage.ContentLength); }
//========================================================================================= public StateScanner(ScannerSpecification specification, int tabsize) { this.Errors = new List<string>(); this.Specification = specification; this._Reader = new BufferReader(tabsize); this.TabSize = tabsize; }
public static DefaultDeserializationContext GetInitializedThreadLocal(IBufferReader bufferReader) { var instance = threadLocalInstance.Value; instance.Initialize(bufferReader); return(instance); }
public DfaLexemeReader(IStateContainer stateContainer, IBufferReader bufferReader) { _stateContainer = stateContainer ?? throw new ArgumentNullException(nameof(bufferReader)); _bufferReader = bufferReader ?? throw new ArgumentNullException(nameof(bufferReader)); _bufferReader.AdvanceBuffer(); //Preload the buffer }
//========================================================================================= public StateScanner(ScannerSpecification specification, int tabsize) { this.Errors = new List <string>(); this.Specification = specification; this._Reader = new BufferReader(tabsize); this.TabSize = tabsize; }
public AmpMessage Decode(IBufferReader reader) { if (reader.ReadableBytes == 0) { return(null); } AmpMessage msg = new AmpMessage(); if (reader.ReadableBytes < 10) { throw new Rpc.Exceptions.RpcCodecException("消息不正确,小于头长度"); } msg.Version = reader.ReadByte(); int length = reader.ReadInt(); msg.Sequence = reader.ReadInt(); byte type = reader.ReadByte(); msg.InvokeMessageType = InvokeMessageTypeParser.Parse(type); msg.ServiceId = reader.ReadUShort(); msg.MessageId = reader.ReadUShort(); int left = length - 14; if (left > 0) { if (left > reader.ReadableBytes) { throw new Rpc.Exceptions.RpcCodecException("消息长度不正确"); } msg.Data = new byte[left]; reader.ReadBytes(msg.Data); } return(msg); }
public async Task TryReadValueAsync_ForGattCommunicationStatus_SetsProtocolError( RawValueReader sut, [Freeze] IBufferReader reader, IGattReadResultWrapper result, IGattCharacteristicWrapper characteristic, byte protocolError) { WithTryReadValueResult(reader, Array.Empty <byte> ( )); result.Status .Returns(GattCommunicationStatus.Success); result.ProtocolError .Returns(protocolError); characteristic.WithCharacteristicProperties(GattCharacteristicProperties.Read) .WithReadValueAsyncResult(result); _ = await sut.TryReadValueAsync(characteristic); sut.ProtocolError .Should( ) .Be(protocolError); }
private static Utf8StreamingJsonReader From(IBufferReader <byte> reader, bool dispose, JsonReaderOptions options, out Utf8JsonReader jsonReader) { var Reader = new Utf8StreamingJsonReader(reader, dispose); jsonReader = Reader.GetInitialReader(options); return(Reader); }
/// <summary> /// Append more bytes to your message building /// </summary> /// <param name="reader">Contains bytes which was received from the other end</param> /// <returns><c>true</c> if a complete message has been built; otherwise <c>false</c>.</returns> /// <remarks>You must handle/read everything which is available in the buffer</remarks> /// <exception cref="InvalidDataException">Failed to parse the received information.</exception> public bool Append(IBufferReader reader) { while (_parserMethod(reader)) { } return(_messages.Count > 0); }
/// <summary> /// Append more bytes to your message building /// </summary> /// <param name="reader">Contains bytes which was received from the other end</param> /// <returns><c>true</c> if a complete message has been built; otherwise <c>false</c>.</returns> /// <remarks>You must handle/read everything which is available in the buffer</remarks> /// <exception cref="InvalidDataException">Failed to parse the received information.</exception> public bool Append(IBufferReader reader) { while (_parserMethod(reader)) { } return _messages.Count > 0; }
public void Reset() { this.bufferReader = null; this.payloadLength = 0; #if GRPC_CSHARP_SUPPORT_SYSTEM_MEMORY this.cachedSliceBuffer.Invalidate(); #endif }
private async Task HandleReceiveBufferReader(IBufferReader bufferReader, CancellationToken cancellationToken) { if (!XmlDeserializer.TryGetXmlElement(bufferReader, out var xmlElement)) { return; } await this.SendToReceivePipeAsync(xmlElement, cancellationToken); }
/// <summary> /// Fills in gltfTexture.unityTexture with a Texture2D. /// </summary> /// <param name="gltfTexture">The glTF texture to convert.</param> /// <param name="loader">The IUriLoader to use for loading image files.</param> /// <returns>On completion of the coroutine, gltfTexture.unityTexture will be non-null /// on success.</returns> private static IEnumerable ConvertTextureCoroutine( GltfTextureBase gltfTexture, IUriLoader loader) { if (gltfTexture.unityTexture != null) { throw new InvalidOperationException("Already converted"); } if (gltfTexture.SourcePtr == null) { Debug.LogErrorFormat("No image for texture {0}", gltfTexture.GltfId); yield break; } Texture2D tex; if (gltfTexture.SourcePtr.data != null) { // This case is hit if the client code hooks up its own threaded // texture-loading mechanism. var data = gltfTexture.SourcePtr.data; tex = new Texture2D(data.colorWidth, data.colorHeight, data.format, true); yield return(null); tex.SetPixels32(data.colorData); yield return(null); tex.Apply(); yield return(null); } else { #if UNITY_EDITOR // Prefer to load the Asset rather than create a new Texture2D; // that lets the resulting prefab reference the texture rather than // embedding it inside the prefab. tex = loader.LoadAsAsset(gltfTexture.SourcePtr.uri); #else tex = null; #endif if (tex == null) { byte[] textureBytes; using (IBufferReader r = loader.Load(gltfTexture.SourcePtr.uri)) { textureBytes = new byte[r.GetContentLength()]; r.Read(textureBytes, destinationOffset: 0, readStart: 0, readSize: textureBytes.Length); } tex = new Texture2D(1, 1); tex.LoadImage(textureBytes, markNonReadable: false); yield return(null); } } tex.name = SanitizeName(gltfTexture.SourcePtr.uri); gltfTexture.unityTexture = tex; }
protected override void Decode(IChannelHandlerContext context, IByteBuffer input, List <object> output) { IBufferReader reader = NettyBufferManager.CreateBufferReader(input); IMessage message = this._codecs.Decode(reader); if (message != null) { output.Add(message); } }
private int Read(IBufferReader reader) { if (_lookAhead != char.MinValue) { var tmp = _lookAhead; _lookAhead = char.MinValue; return(tmp); } return(reader.Read()); }
private int Read(IBufferReader reader) { if (_parseThisFirst != char.MinValue) { var tmp = _parseThisFirst; _parseThisFirst = char.MinValue; return tmp; } return reader.Read(); }
public bool CanUnFrameData(IBufferReader readerBuffer) { if (_packageLength == -1) { return(readerBuffer.TryRead(_headerSize)); } else { return(readerBuffer.TryRead(_packageLength)); } }
private int Read(IBufferReader reader) { if (_lookAhead != char.MinValue) { var tmp = _lookAhead; _lookAhead = char.MinValue; return tmp; } return reader.Read(); }
private void WithTryReadValueResult(IBufferReader reader, IEnumerable bytes) { reader.TryReadValue(Arg.Any <IBuffer> ( ), out var _) .Returns(x => { x [1] = bytes; return(true); }); }
protected override void Decode(IChannelHandlerContext context, IByteBuffer input, List <object> output) { IBufferReader reader = ByteBufferManager.CreateBufferReader(input); var message = this._protocol.Parse(reader); if (message != null) { output.Add(message); } reader = null; }
public RawValueReader( [NotNull] ILogger logger, [NotNull] IBufferReader reader) { Guard.ArgumentNotNull(logger, nameof(logger)); Guard.ArgumentNotNull(reader, nameof(reader)); _logger = logger; _reader = reader; }
/// <summary> /// Handles streaming read completion. /// </summary> protected void HandleReadFinished(bool success, IBufferReader receivedMessageReader) { // if success == false, the message reader will report null payload. It that case we will // treat this completion as the last read an rely on C core to handle the failed // read (e.g. deliver approriate statusCode on the clientside). TRead msg = default(TRead); var deserializeException = (success && receivedMessageReader.TotalLength.HasValue) ? TryDeserialize(receivedMessageReader, out msg) : null; TaskCompletionSource <TRead> origTcs = null; bool releasedResources; lock (myLock) { origTcs = streamingReadTcs; if (!receivedMessageReader.TotalLength.HasValue) { // This was the last read. readingDone = true; } if (deserializeException != null && IsClient) { readingDone = true; // TODO(jtattermusch): it might be too late to set the status CancelWithStatus(DeserializeResponseFailureStatus); } if (!readingDone) { streamingReadTcs = null; } releasedResources = ReleaseResourcesIfPossible(); } if (releasedResources) { OnAfterReleaseResourcesUnlocked(); } if (deserializeException != null && !IsClient) { origTcs.SetException(new IOException("Failed to deserialize request message.", deserializeException)); return; } origTcs.SetResult(msg); }
public void UnFrameData(IBufferReader readerBuffer) { if (_packageLength == -1) { if (readerBuffer.TryRead(_headerSize)) { ReadHeader(readerBuffer); ReadData(readerBuffer); } } else { ReadData(readerBuffer); } }
private void FillContinguousBuffer(IBufferReader reader, byte[] destination) { #if GRPC_CSHARP_SUPPORT_SYSTEM_MEMORY PayloadAsReadOnlySequence().CopyTo(new Span <byte>(destination)); #else int offset = 0; while (reader.TryGetNextSlice(out Slice slice)) { slice.CopyTo(new ArraySegment <byte>(destination, offset, (int)slice.Length)); offset += (int)slice.Length; } // check that we filled the entire destination GrpcPreconditions.CheckState(offset == payloadLength); #endif }
/// <summary> /// Will try to parse everything in the buffer /// </summary> /// <param name="reader">Reader to read from.</param> /// <remarks><para>Do note that the parser is for the header only. The <see cref="Completed"/> event will /// indicate that there might be body bytes left in the buffer. You have to handle them by yourself.</para></remarks> public void Parse(IBufferReader reader) { var theByte = 0; while ((theByte = Read(reader)) != -1) { var ch = (char) theByte; _logger.Trace(_parserMethod.Method.Name + ": " + ch); _parserMethod(ch); if (_isCompleted) break; } _isCompleted = false; }
internal ReadFormatContext(IBufferReader <TReader> bufferReader, TReader reader, FormatOptions options = null) { if (bufferReader == null) { throw new ArgumentNullException(nameof(bufferReader)); } if (reader == null) { throw new ArgumentNullException(nameof(reader)); } this.BufferReader = bufferReader; this.Reader = reader; this.Options = options ?? new FormatOptions(); }
public static byte[] TakeAsArray(this IBufferReader reader, int bytes) { var segments = reader.Take(bytes).ToList(); var len = segments.Sum(x => x.Count); var buffer = new byte[len]; int bi = 0; foreach (var arraySegment in segments) { for (int i = 0; i < arraySegment.Count; i++) { buffer[bi++] = arraySegment.Array[arraySegment.Offset + i]; } } return(buffer); }
public LocationsList(IBufferReader reader, long size) { long last = reader.Position + size; Version = reader.ReadUintBE(); EncodedDate = reader.ReadUintBE(); while (reader.Position < last) { locations.Add(new Location() { Offset = reader.ReadUintBE(), Length = reader.ReadUintBE() }); } }
/// <summary> /// Will try to parse everything in the buffer /// </summary> /// <param name="reader">Reader to read from.</param> /// <remarks><para>Do note that the parser is for the header only. The <see cref="Completed"/> event will /// indicate that there might be body bytes left in the buffer. You have to handle them by yourself.</para></remarks> public void Parse(IBufferReader reader) { var theByte = 0; while ((theByte = Read(reader)) != -1) { var ch = (char)theByte; _logger.Trace(_parserMethod.Method.Name + ": " + ch); _parserMethod(ch); if (_isCompleted) { break; } } _isCompleted = false; }
/// <summary> /// Will try to parse everything in the buffer /// </summary> /// <param name="reader">Reader to read from.</param> /// <remarks><para>Do note that the parser is for the header only. The <see cref="Completed"/> event will /// indicate that there might be body bytes left in the buffer. You have to handle them by yourself.</para></remarks> public void Parse(IBufferReader reader) { var theByte = 0; while ((theByte = this.Read(reader)) != -1) { var ch = (char)theByte; this.logger.Trace(this.parserMethod.GetMethodInfo().Name + ": " + ch); this.parserMethod(ch); if (this.isCompleted) { break; } } this.isCompleted = false; }
/// <summary> /// Reads a <see cref="IBufferReader{T}"/> to completion, writing to a <see cref="IBufferWriterAsync{T}"/> /// </summary> /// <typeparam name="T">The type of element</typeparam> /// <param name="reader">The reader to read from</param> /// <param name="writer">The writer to write to</param> /// <param name="token">A cancellation token to abort the operation</param> /// <returns>A task representing the progress fo the write operation</returns> public static async ValueTask CopyToAsync <T>(this IBufferReader <T> reader, IBufferWriterAsync <T> writer, CancellationToken token = default) { while (reader.CanRead) { var InBuffer = reader.GetMemory(0); if (InBuffer.IsEmpty) { break; } InBuffer.Span.CopyTo(writer.GetSpan(InBuffer.Length)); await writer.AdvanceAsync(InBuffer.Length, token); reader.Advance(InBuffer.Length); } }
public CommandLineMessage Parse(IBufferReader reader) { byte[] buffer = new byte[reader.ReadableBytes]; reader.ReadBytes(buffer); string content = Encoding.UTF8.GetString(buffer); var arr = content.Split(new string[] { SPLITER }, StringSplitOptions.RemoveEmptyEntries); if (arr.Length == 0) { return(new CommandLineMessage(string.Empty)); } if (arr.Length == 1) { return(new CommandLineMessage(arr[0])); } return(new CommandLineMessage(arr[0], arr.Skip(1).ToArray())); }
/// <summary> /// Creates a new Binary Text Reader /// </summary> /// <param name="reader">The buffer reader to read from</param> /// <param name="encoding">The encoding of the text in the byte array</param> /// <param name="detectEncoding">Whether to detect the encoding using the preamble</param> /// <param name="bufferSize">The buffer size to use when reading the text</param> public BinaryTextReader(IBufferReader <byte> reader, Encoding encoding, bool detectEncoding, int bufferSize) : base() { if (bufferSize < MinBufferSize) { bufferSize = MinBufferSize; } _Reader = reader; Encoding = encoding; _Decoder = encoding.GetDecoder(); _DetectEncoding = detectEncoding; _Preamble = encoding.GetPreamble(); _CheckPreamble = (_Preamble.Length > 0); _BufferSize = bufferSize; _Buffer = new char[encoding.GetMaxCharCount(bufferSize)]; }
/// <summary> /// Read all header bytes from the incoming buffer /// </summary> /// <param name="stream">The stream.</param> /// <returns><c>false</c> if we have not received all bytes yet; otherwise <c>true</c>.</returns> protected virtual bool ReadHeaderBytes(IBufferReader stream) { var bytesLeftInStream = stream.Count - stream.Position; var bytesToCopy = bytesLeftInStream < _bytesLeft ? bytesLeftInStream : _bytesLeft; stream.Read(_header, 0, bytesToCopy); _bytesLeft -= bytesToCopy; if (_bytesLeft > 0) return false; _packet = CreatePacket(_header); _bytesLeft = _packet.ContentLength; _parserMethod = ReadBodyBytes; return true; }
/// <summary> /// Reads a <see cref="IBufferReader{T}"/> to completion, writing to a <see cref="IBufferWriter{T}"/> /// </summary> /// <typeparam name="T">The type of element</typeparam> /// <param name="reader">The reader to read from</param> /// <param name="writer">The writer to write to</param> public static void CopyTo <T>(this IBufferReader <T> reader, IBufferWriter <T> writer) { while (reader.CanRead) { var InBuffer = reader.GetSpan(0); if (InBuffer.IsEmpty) { break; } var OutBuffer = writer.GetSpan(InBuffer.Length); InBuffer.CopyTo(OutBuffer); writer.Advance(InBuffer.Length); reader.Advance(InBuffer.Length); } }
private void ReadData(IBufferReader readerBuffer) { if (readerBuffer.TryRead(_packageLength)) { var data = readerBuffer.ReadResult(_packageLength); if (_receivedHandler != null) { try { _receivedHandler(data); } catch (Exception ex) { Log <LengthPrefixMessageFramer> .Error(ex, "Handle received message fail."); } } _packageLength = -1; } }
/// <summary> /// Append more bytes to your message building /// </summary> /// <param name="reader">Contains bytes which was received from the other end</param> /// <returns><c>true</c> if a complete message has been built; otherwise <c>false</c>.</returns> /// <remarks>You must handle/read everything which is available in the buffer</remarks> public bool Append(IBufferReader reader) { _headerParser.Parse(reader); if (_bodyBytestLeft > 0) { var bytesToRead = Math.Min(reader.RemainingLength, _bodyBytestLeft); reader.CopyTo(_bodyStream, bytesToRead); _bodyBytestLeft -= bytesToRead; if (_bodyBytestLeft == 0) { _bodyStream.Position = 0; _messages.Enqueue(_message); _message = null; } if (reader.RemainingLength > 0) { _headerParser.Parse(reader); } } return _messages.Count > 0; }
/// <summary> /// Read all body bytes from the incoming buffer /// </summary> /// <param name="reader">Contains received bytes</param> /// <returns><c>false</c> if we have not received all bytes yet; otherwise <c>true</c>.</returns> protected virtual bool ReadBodyBytes(IBufferReader reader) { var bytesLeftInStream = reader.Count - reader.Position; var bytesToCopy = bytesLeftInStream < _bytesLeft ? bytesLeftInStream : _bytesLeft; reader.CopyTo(_packet.Message, bytesToCopy); _bytesLeft -= bytesToCopy; if (_bytesLeft > 0) return false; _packet.Message.Position = 0; _messages.Enqueue(_packet); _packet = null; _bytesLeft = Packet.HeaderLength; _parserMethod = ReadHeaderBytes; return true; }
/// <summary> /// Parser method to copy all body bytes. /// </summary> /// <param name="reader"> </param> /// <returns></returns> /// <remarks>Needed since a TCP packet can contain multiple messages /// after each other, or partial messages.</remarks> private bool ParseBody(IBufferReader reader) { if (reader.RemainingLength == 0) return false; if (_currentMessage.Body == null) { if (_currentMessage.ContentLength > _bufferSize) _currentMessage.Body = new FileStream( Path.Combine(Path.GetTempPath(), "http." + Guid.NewGuid().ToString("N") + ".tmp"), FileMode.CreateNew); else { var slice = _bufferPool.Pop(); _currentMessage.Body = new SliceStream(slice); } } var bytesLeft = (int) Math.Min(_currentMessage.ContentLength - _currentMessage.Body.Length, reader.RemainingLength); reader.CopyTo(_currentMessage.Body, bytesLeft); return _currentMessage.Body.Length == _currentMessage.ContentLength; }
/// <summary> /// Initializes a new instance of the <see cref="Received"/> class. /// </summary> /// <param name="remoteEndPoint">The remote end point.</param> /// <param name="reader">Buffer reader.</param> public Received(EndPoint remoteEndPoint, IBufferReader reader) { RemoteEndPoint = remoteEndPoint; BufferReader = reader; }
public void SetupBufferAndReader() { buffer = new float[1]; reader = BufferReaderFactory.GetFloatReader(buffer); }
/// <summary> /// Initializes a new instance of the <see cref="ReceivedBufferEventArgs" /> class. /// </summary> /// <param name="bufferReader">The buffer reader.</param> public ReceivedBufferEventArgs(IBufferReader bufferReader) { BufferReader = bufferReader; }