readFrame method

  1. @override
Future readFrame()
override

Implementation

@override
Future readFrame() async {
  transport_.readAll(i32buf, 0, 4);
  int word1 = TFramedTransport.decodeWord(i32buf);

  if ((word1 & TBinaryProtocol.VERSION_MASK) == TBinaryProtocol.VERSION_1) {
    throw TTransportError(null, "This transport does not support Unframed");
  } else if (word1 == HTTP_SERVER_MAGIC) {
    throw TTransportError(null, "This transport does not support HTTP");
  } else {
    if (word1 - 4 > MAX_FRAME_SIZE) {
      // special case for the most common question in user-group
      // this will probably saves hours of engineering effort.
      int magic1 = 0x61702048; // ASCII "ap H" in little endian
      int magic2 = 0x6C6C6F63; // ASCII "lloc" in little endian
      if (word1 == magic1 || word1 == magic2) {
        throw TTransportError(null,
            "The Thrift server received an ASCII request and safely ignored it. In all likelihood, this isn't the reason of your problem (probably a local daemon sending HTTP content to all listening ports).");
      }
      throw TTransportError(null, "Framed transport frame is too large");
    }

    // Could be framed or header format.  Check next word.
    transport_.readAll(i32buf, 0, 4);
    int version = TFramedTransport.decodeWord(i32buf);
    if ((version & TBinaryProtocol.VERSION_MASK) ==
        TBinaryProtocol.VERSION_1) {
      clientType = ClientTypes.FRAMED_DEPRECATED;
      Int8List buff = Int8List(word1);
      buff.setRange(0, 4, i32buf);

      transport_.readAll(buff, 4, word1 - 4);
      readBuffer_?.reset(buf: buff);
    } else if ((version & HEADER_MAGIC_MASK) == HEADER_MAGIC) {
      clientType = ClientTypes.HEADERS;
      if (word1 - 4 < 10) {
        throw TTransportError(null, "Header transport frame is too small");
      }
      Int8List buff = Int8List(word1);
      buff.setRange(0, 4, i32buf);

      // read packet minus version
      transport_.readAll(buff, 4, word1 - 4);

      _flags = version & HEADER_FLAGS_MASK;
      // read seqId
      _seqId = TFramedTransport.decodeWord(buff, 4);
      int headerSize = TFramedTransport.decodeShort(buff, 8);

      readHeaderFormat(headerSize, buff);
    } else {
      clientType = ClientTypes.UNKNOWN;
      throw TTransportError(null, "Unsupported client type");
    }
  }
}