public static void UpdatePayload(int currentBytesRead, EnvelopeReaderBuffer readerBuffer) { // read all available Data if (readerBuffer.Payload == null) // first read, because readerBuffer.Payload is null { readerBuffer.Payload = new byte[currentBytesRead]; Buffer.BlockCopy(readerBuffer.Buffer, 0, readerBuffer.Payload, 0, currentBytesRead); } else { var existingLenght = readerBuffer.Payload.Length; var newPayload = new byte[existingLenght + currentBytesRead]; Buffer.BlockCopy(readerBuffer.Payload, 0, newPayload, 0, existingLenght); Buffer.BlockCopy(readerBuffer.Buffer, 0, newPayload, 0 + existingLenght, currentBytesRead); readerBuffer.Payload = newPayload; } }
public static async Task <List <IEnvelope> > ReceivePackets(EnvelopeReaderBuffer readerBuffer, Stream stream, CancellationToken ct) { var receivedPackets = new List <IEnvelope>(); int currentBytesRead; NOTLSEnvelope packet; do { currentBytesRead = await stream.ReadAsync(readerBuffer.Buffer, 0, readerBuffer.Buffer.Length, ct); NOTLSEnvelopeExtensions.UpdatePayload(currentBytesRead, readerBuffer); packet = NOTLSEnvelopeExtensions.TryTakeOnePacket(ref readerBuffer.Payload); if (packet != null) { receivedPackets.Add(packet); } } while (currentBytesRead > 0 && (packet == null || readerBuffer.Payload != null)); return(receivedPackets); }