示例#1
0
  @Override
  protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer)
      throws Exception {
    int messageBeginIndex = buffer.readerIndex();
    ChannelBuffer messageBuffer = null;

    try {
      TNiftyTransport transport =
          new TNiftyTransport(channel, buffer, ThriftTransportType.UNFRAMED);
      TBinaryProtocol protocol = new TBinaryProtocol(transport);

      protocol.readMessageBegin();
      TProtocolUtil.skip(protocol, TType.STRUCT);
      protocol.readMessageEnd();

      messageBuffer = buffer.slice(messageBeginIndex, buffer.readerIndex());
    } catch (IndexOutOfBoundsException e) {
      buffer.readerIndex(messageBeginIndex);
      return null;
    } catch (Throwable th) {
      buffer.readerIndex(messageBeginIndex);
      return null;
    }

    return messageBuffer;
  }
  @Override
  public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {

    ResultCode success;
    ChannelBuffer buf = (ChannelBuffer) e.getMessage();
    TMemoryInputTransport trans = new TMemoryInputTransport(buf.array());
    TBinaryProtocol proto = new TBinaryProtocol(trans);
    TMessage msg = proto.readMessageBegin();
    if (msg.type == TMessageType.EXCEPTION) {
      proto.readMessageEnd();
    }
    TField field;
    proto.readStructBegin();
    while (true) {
      field = proto.readFieldBegin();
      if (field.type == TType.STOP) {
        break;
      }
      switch (field.id) {
        case 0: // SUCCESS
          if (field.type == TType.I32) {
            success = ResultCode.findByValue(proto.readI32());
            stats.accumulateOutcomeWithDelta(
                success.getValue() == 0 ? Outcome.SUCCESS : Outcome.GRACEFUL_FAILURE, 0);
          } else {
            TProtocolUtil.skip(proto, field.type);
          }
          break;
        default:
          TProtocolUtil.skip(proto, field.type);
      }
      proto.readFieldEnd();
    }
    proto.readStructEnd();
    proto.readMessageEnd();
  }