thrift-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From markerickson-wf <...@git.apache.org>
Subject [GitHub] thrift pull request #1269: THRIFT-4187 Allow dart framed transport to read i...
Date Mon, 15 May 2017 13:36:02 GMT
Github user markerickson-wf commented on a diff in the pull request:

    https://github.com/apache/thrift/pull/1269#discussion_r116491597
  
    --- 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();
    +      return false;
    +    }
    +  }
    +
    +  void _readFrameBody() {
    +    var remainingBodyBytes = _bodySize - _receivedBodyBytes;
    +
    +    int got = _transport.read(_body, _receivedBodyBytes, remainingBodyBytes);
    +    if (got < 0) {
    +      throw new TTransportError(
    +          TTransportErrorType.UNKNOWN, "Socket closed during frame body read");
    +    }
    +
    +    _receivedBodyBytes += got;
    +
    +    if (_receivedBodyBytes == _bodySize) {
    +      var body = _body;
    +
    +      _bodySize = 0;
    +      _body = null;
    +      _receivedBodyBytes = 0;
    +
    +      _setReadBuffer(body);
    +
    +      var completer = _frameCompleter;
    +      _frameCompleter = null;
    +      completer.complete(new Object());
    +    } else {
    +      _registerForReadableBytes();
    +    }
       }
     
       Future flush() {
    -    Uint8List buffer = consumeWriteBuffer();
    -    int length = buffer.length;
    +    if (_frameCompleter == null) {
    +      Uint8List buffer = consumeWriteBuffer();
    +      int length = buffer.length;
    +
    +      _headerBytes.buffer.asByteData().setUint32(0, length);
    +      _transport.write(_headerBytes, 0, headerByteCount);
    +      _transport.write(buffer, 0, length);
    +
    +      _frameCompleter  = new Completer<Object>(); // FIXME: .sync?!
    --- End diff --
    
    Did you mean to address this `FIXME`?


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message