Github user markerickson-wf commented on a diff in the pull request:

    https://github.com/apache/thrift/pull/1269#discussion_r116510886
  
    --- Diff: lib/dart/lib/src/transport/t_framed_transport.dart ---
    @@ -51,33 +58,112 @@ class TFramedTransport extends TBufferedTransport {
           if (got > 0) return got;
         }
     
    -    _readFrame();
    +    // IMPORTANT: by the time you've got here,
    +    // an entire frame is available for reading
     
         return super.read(buffer, offset, length);
       }
     
       void _readFrame() {
    -    _transport.readAll(headerBytes, 0, headerByteCount);
    -    int size = headerBytes.buffer.asByteData().getUint32(0);
    +    if (_body == null) {
    +      bool gotFullHeader = _readFrameHeader();
    +      if (!gotFullHeader) {
    +        return;
    +      }
    +    }
    +
    +    _readFrameBody();
    +  }
    +
    +  bool _readFrameHeader() {
    +    var remainingHeaderBytes = headerByteCount - _receivedHeaderBytes;
     
    -    if (size < 0) {
    +    int got = _transport.read(_headerBytes, _receivedHeaderBytes, 
remainingHeaderBytes);
    +    if (got < 0) {
           throw new TTransportError(
    -          TTransportErrorType.UNKNOWN, "Read a negative frame size: 
$size");
    +          TTransportErrorType.UNKNOWN, "Socket closed during frame header 
read");
         }
     
    -    Uint8List buffer = new Uint8List(size);
    -    _transport.readAll(buffer, 0, size);
    -    _setReadBuffer(buffer);
    +    _receivedHeaderBytes += got;
    +
    +    if (_receivedHeaderBytes == headerByteCount) {
    +      int size = _headerBytes.buffer.asByteData().getUint32(0);
    +
    +      _receivedHeaderBytes = 0;
    +
    +      if (size < 0) {
    +        throw new TTransportError(
    +            TTransportErrorType.UNKNOWN, "Read a negative frame size: 
$size");
    +      }
    +
    +      _bodySize = size;
    +      _body = new Uint8List(_bodySize);
    +      _receivedBodyBytes = 0;
    +
    +      return true;
    +    } else {
    +      _registerForReadableBytes();
    --- End diff --
    
    At first I was reading it as causing a cycle, but 
`_registerForReadableBytes` will make an async call to `_transport.flush`, so I 
think this is okay on second thought.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to