public FileReaderItem Next() { TryAgain: m_nextPacket = m_stream.Read(); if ((object)m_nextPacket == null) { return(FileReaderItem.EndOfStream); } if (m_nextPacket.CommandName == "DataStreamNormal") { if (m_decoderNormal == null) { m_decoderNormal = new NormalDecoder(); } m_decoderNormal.Load(((CommandDataStreamNormal)m_nextPacket)); m_currentDecoder = m_decoderNormal; return(FileReaderItem.DataPoint); } else if (m_nextPacket.CommandName == "ProducerMetadata") { m_metadata = (SttpProducerMetadata)m_nextPacket; foreach (var item in m_metadata.DataPoints) { m_metadataLookup[item.DataPointID] = item; } return(FileReaderItem.ProducerMetadata); } goto TryAgain; }
public void Test2() { var cmd = new CommandMetadataRequestFailed("Failed", "True"); CtpCommand doc2 = (CtpCommand)cmd; Console.WriteLine(doc2.ToYAML()); }
/// <summary> /// Must occur within a lock. This will attempt to read a Command and queue a read from the stream if it fails. /// </summary> /// <returns></returns> private CtpCommand TryReadInternal() { if ((object)m_pendingCommand != null) { var command = m_pendingCommand; m_pendingCommand = null; return(command); } while (true) { if (m_readDecoder.ReadFromBuffer(out var command)) { return(command); } var task = m_stream.ReadAsync(m_readBuffer, 0, m_readBuffer.Length); if (!task.IsCompleted) { m_isReading = true; task.ContinueWith(m_continueRead.AsyncCallback); return(null); } if (task.IsFaulted) { throw task.Exception ?? new Exception("Task failed with no exception specified"); } if (task.IsCanceled) { throw new Exception("Receive was canceled"); } m_readDecoder.AppendToBuffer(m_readBuffer, task.Result); } }
public int ReadFromBuffer(out CtpCommand packet, byte[] buffer, int current, int length) { //Note: The code in these two modules are identical: packet = null; if (m_disposed) { return(0); } if (length < 2) { return(0); } if (!PacketMethods.TryReadPacket(buffer, current, length, MaximumPacketSize, out PacketContents payloadType, out int payloadFlags, out byte[] payloadBuffer, out int consumedLength)) { return(0); } switch (payloadType) { case PacketContents.CommandSchema: var scheme = new CtpCommandSchema(payloadBuffer); m_inboundSchemes[payloadFlags] = scheme; if (m_inboundSchemes.Count > MaximumSchemeCount) { throw new Exception("Too many schemes have been defined."); } break; case PacketContents.CommandSchemaWithData: packet = new CtpCommand(payloadBuffer); break; case PacketContents.CommandData: if (m_inboundSchemes.TryGetValue(payloadFlags, out var commandSchema)) { packet = new CtpCommand(commandSchema, payloadBuffer); } break; case PacketContents.CompressedDeflate: case PacketContents.CompressedZlib: packet = Inflate(payloadType, payloadFlags, payloadBuffer); break; default: throw new ArgumentOutOfRangeException(); } return(consumedLength); }
/// <summary> /// Gets the next data packet. This method should be in a while loop, decoding all /// messages before the next block of data is added to the decoder via <see cref="AppendToBuffer"/> /// /// Reads the inbound raw buffer for the next full command. /// Automatically decompresses and combines fragments and waits for the entire packet before /// responding as True. /// </summary> public bool ReadFromBuffer(out CtpCommand packet) { TryAgain: int length = ReadFromBuffer(out packet, m_inboundBuffer, m_inboundBufferCurrentPosition, m_inboundBufferLength); if (length == 0) { return(false); } m_inboundBufferLength -= length; m_inboundBufferCurrentPosition += length; if ((object)packet == null) //I read a packet that was a schema and has no data to return to the user. { goto TryAgain; } return(true); }
/// <summary> /// Attempts to read a command. If this fails, a wait object is returned to indicate when a receive is successful. /// A callback is also queued to indicate when more data has been received. /// </summary> /// <param name="command"></param> /// <param name="waiting"></param> /// <returns></returns> /// <exception cref="ObjectDisposedException"></exception> public bool TryRead(out CtpCommand command, out ManualResetEventSlim waiting) { waiting = null; command = null; try { lock (m_syncRoot) { if (m_disposed) { throw new ObjectDisposedException(GetType().FullName); } if (m_isReading) { waiting = m_waiting; return(false); } command = TryReadInternal(); if ((object)command != null) { return(true); } m_waiting = new ManualResetEventSlim(false, 1); waiting = m_waiting; return(false); } } catch (Exception e) { OnException(e); waiting = null; return(false); } }
private void ContinueRead(Task <int> task) { try { if (m_disposed) { return; } if (!task.IsCompleted) { throw new Exception("Only completed tasks should be here."); } if (task.IsFaulted) { throw task.Exception ?? new Exception("Task failed with no exception specified"); } if (task.IsCanceled) { throw new Exception("Receive was canceled"); } ManualResetEventSlim waiting; lock (m_syncRoot) { if (m_disposed) { return; } if (!m_isReading) { throw new Exception("IsReading should have been set to true."); } m_isReading = false; m_readDecoder.AppendToBuffer(m_readBuffer, task.Result); m_pendingCommand = TryReadInternal(); if ((object)m_pendingCommand == null) { return; //If null, this means another read asyc operation started. } waiting = m_waiting; m_waiting = null; } waiting?.Set(); try { if (!m_disposed) { m_notify(); } } catch (Exception) { } } catch (Exception e) { OnException(e); } }