Ejemplo n.º 1
0
  @Test
  public void testWriteUtf8() {
    String usAscii = "Some UTF-8 like äÄ∏ŒŒ";
    ByteBuf buf = ReferenceCountUtil.releaseLater(Unpooled.buffer(16));
    buf.writeBytes(usAscii.getBytes(CharsetUtil.UTF_8));
    ByteBuf buf2 = ReferenceCountUtil.releaseLater(Unpooled.buffer(16));
    ByteBufUtil.writeUtf8(buf2, usAscii);

    Assert.assertEquals(buf, buf2);
  }
Ejemplo n.º 2
0
  @Test
  public void testWriteUsAsciiString() {
    AsciiString usAscii = new AsciiString("NettyRocks");
    ByteBuf buf = ReferenceCountUtil.releaseLater(Unpooled.buffer(16));
    buf.writeBytes(usAscii.toString().getBytes(CharsetUtil.US_ASCII));
    ByteBuf buf2 = ReferenceCountUtil.releaseLater(Unpooled.buffer(16));
    ByteBufUtil.writeAscii(buf2, usAscii);

    Assert.assertEquals(buf, buf2);
  }
Ejemplo n.º 3
0
  @Test
  public void testWriteUsAsciiWrapped() {
    String usAscii = "NettyRocks";
    ByteBuf buf = Unpooled.unreleasableBuffer(ReferenceCountUtil.releaseLater(Unpooled.buffer(16)));
    assertWrapped(buf);
    buf.writeBytes(usAscii.getBytes(CharsetUtil.US_ASCII));
    ByteBuf buf2 =
        Unpooled.unreleasableBuffer(ReferenceCountUtil.releaseLater(Unpooled.buffer(16)));
    assertWrapped(buf2);
    ByteBufUtil.writeAscii(buf2, usAscii);

    Assert.assertEquals(buf, buf2);
  }
Ejemplo n.º 4
0
 @Override
 public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
   AbstractEvent event = (AbstractEvent) msg;
   EventOperator.instance().onEvent(event, false);
   logger.debug("Event Fired: " + event.toString());
   ReferenceCountUtil.release(msg);
 }
Ejemplo n.º 5
0
    @Override
    public void next(Object value) {
      if (value == null) {
        fail(new NullPointerException("value is null"));
        return;
      }
      if (done) {
        Exceptions.onNextDropped(value);
        return;
      }
      if (caughtUp && actual != null) {
        try {
          actual.onNext(value);
        } finally {
          ch.read();
          ReferenceCountUtil.release(value);
        }

      } else {
        Queue<Object> q = queue;
        if (q == null) {
          q = QueueSupplier.unbounded().get();
          queue = q;
        }
        q.offer(value);
        if (drain()) {
          caughtUp = true;
        }
      }
    }
 @Override
 public ChannelFuture write(Object msg, ChannelPromise promise) {
   AbstractChannelHandlerContext next = findContextOutbound();
   ReferenceCountUtil.touch(msg, next);
   next.invoker().invokeWrite(next, msg, promise);
   return promise;
 }
 @Override
 public void updateResponse(HttpServerResponse<O> response, Throwable error) {
   response.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
   response.getHeaders().set(HttpHeaders.Names.CONTENT_TYPE, "text/html");
   ByteBuf buffer =
       response.getChannelHandlerContext().alloc().buffer(1024); // 1KB initial length.
   PrintStream printStream = null;
   try {
     printStream = new PrintStream(new ByteBufOutputStream(buffer));
     error.printStackTrace(printStream);
     String errorPage =
         ERROR_HTML_TEMPLATE.replace(
             STACKTRACE_TEMPLATE_VARIABLE, buffer.toString(Charset.defaultCharset()));
     response.writeString(errorPage);
   } finally {
     ReferenceCountUtil.release(buffer);
     if (null != printStream) {
       try {
         printStream.flush();
         printStream.close();
       } catch (Exception e) {
         logger.error(
             "Error closing stream for generating error response stacktrace. This is harmless.",
             e);
       }
     }
   }
 }
  @Override
  protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {

    FullHttpRequest req = (FullHttpRequest) msg;
    String upgrade = req.headers().get(HttpHeaders.Names.UPGRADE);
    if (HttpHeaders.Values.WEBSOCKET.equalsIgnoreCase(upgrade)) {
      WebSocketServerHandshakerFactory wsFactory =
          new WebSocketServerHandshakerFactory(req.getUri(), null, false);
      WebSocketServerHandshaker handshaker = wsFactory.newHandshaker(req);
      if (handshaker == null) {
        WebSocketServerHandshakerFactory.sendUnsupportedWebSocketVersionResponse(ctx.channel());
      } else {
        ChannelFuture future = handshaker.handshake(ctx.channel(), req);
        future.addListener(
            f -> {
              this.configurator.switchToWebSockets(ctx.pipeline());
            });
      }
    } else {
      ReferenceCountUtil.retain(msg);
      this.configurator.switchToPlainHttp(ctx.pipeline());
      ChannelHandlerContext agg = ctx.pipeline().context(HttpObjectAggregator.class);
      agg.fireChannelRead(msg);
    }
  }
 @Override
 public ChannelHandlerContext fireChannelRead(Object msg) {
   AbstractChannelHandlerContext next = findContextInbound();
   ReferenceCountUtil.touch(msg, next);
   invokedNextChannelRead = true;
   next.invoker().invokeChannelRead(next, msg);
   return this;
 }
Ejemplo n.º 10
0
  @Override
  public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof Http2Settings) {
      // Expected
    } else if (msg instanceof FullHttpResponse) {
      FullHttpResponse response = (FullHttpResponse) msg;

      final Invocation invocation = waitsHolder.poll(response);

      if (invocation != null) {
        final ServiceInvocationContext iCtx = invocation.invocationContext();
        final SerializationFormat serializationFormat = iCtx.scheme().serializationFormat();
        try {
          final Promise<FullHttpResponse> resultPromise = invocation.resultPromise();
          if (HttpStatusClass.SUCCESS == response.status().codeClass()
              // No serialization indicates a raw HTTP protocol which should
              // have error responses returned.
              || serializationFormat == SerializationFormat.NONE) {
            iCtx.resolvePromise(resultPromise, response.retain());
          } else {
            iCtx.rejectPromise(
                resultPromise,
                new InvalidResponseException("HTTP Response code: " + response.status()));
          }
        } finally {
          ReferenceCountUtil.release(msg);
        }
      } else {
        // if invocation not found, we just bypass message to next
        ctx.fireChannelRead(msg);
      }

      if (!isMultiplex
          && HttpHeaderValues.CLOSE.contentEqualsIgnoreCase(
              response.headers().get(HttpHeaderNames.CONNECTION))) {
        ctx.close();
      }
    } else {
      try {
        throw new IllegalStateException("unexpected message type: " + msg);
      } finally {
        ReferenceCountUtil.release(msg);
      }
    }
  }
Ejemplo n.º 11
0
  @Override
  protected void doWrite(ChannelOutboundBuffer in) throws Exception {
    switch (state) {
      case OPEN:
      case BOUND:
        throw new NotYetConnectedException();
      case CLOSED:
        throw new ClosedChannelException();
    }

    final LocalChannel peer = this.peer;
    final ChannelPipeline peerPipeline = peer.pipeline();
    final EventLoop peerLoop = peer.eventLoop();

    if (peerLoop == eventLoop()) {
      for (; ; ) {
        Object msg = in.current();
        if (msg == null) {
          break;
        }
        peer.inboundBuffer.add(msg);
        ReferenceCountUtil.retain(msg);
        in.remove();
      }
      finishPeerRead(peer, peerPipeline);
    } else {
      // Use a copy because the original msgs will be recycled by AbstractChannel.
      final Object[] msgsCopy = new Object[in.size()];
      for (int i = 0; i < msgsCopy.length; i++) {
        msgsCopy[i] = ReferenceCountUtil.retain(in.current());
        in.remove();
      }

      peerLoop.execute(
          new Runnable() {
            @Override
            public void run() {
              Collections.addAll(peer.inboundBuffer, msgsCopy);
              finishPeerRead(peer, peerPipeline);
            }
          });
    }
  }
Ejemplo n.º 12
0
 @Override
 public void channelRead(ChannelHandlerContext ctx, Object msg) { // (2)
   ByteBuf in = (ByteBuf) msg;
   try {
     while (in.isReadable()) { // (1)
       System.out.print((char) in.readByte());
       System.out.flush();
     }
   } finally {
     ReferenceCountUtil.release(msg); // (2)
   }
 }
Ejemplo n.º 13
0
 @Override
 public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
   if (isRemote(ctx)) {
     ByteBuf payload = (ByteBuf) msg;
     byte[] data = getPayloadFromByteBuf(payload);
     writeBuffer(data);
     return;
   }
   ReferenceCountUtil.retain(msg);
   // propagate the data to rest of handlers in pipeline
   ctx.fireChannelRead(msg);
 }
Ejemplo n.º 14
0
 /**
  * Verifies that no content has been sent as part of the response or readable bytes is equivalent
  * to 0
  *
  * @param contents the content of the response.
  */
 private void assertNoContent(Queue<HttpObject> contents) {
   boolean endMarkerFound = false;
   for (HttpObject object : contents) {
     assertFalse(
         "There should have been no more data after the end marker was found", endMarkerFound);
     HttpContent content = (HttpContent) object;
     assertEquals("No content expected ", 0, content.content().readableBytes());
     endMarkerFound = object instanceof LastHttpContent;
     ReferenceCountUtil.release(content);
   }
   assertTrue("There should have been an end marker", endMarkerFound);
 }
Ejemplo n.º 15
0
 /**
  * Discards all the content in {@code contents}.
  *
  * @param contents the content to discard.
  * @param expectedDiscardCount the number of {@link HttpObject}s that are expected to discarded.
  */
 private void discardContent(Queue<HttpObject> contents, int expectedDiscardCount) {
   assertEquals(
       "Objects that will be discarded differ from expected",
       expectedDiscardCount,
       contents.size());
   boolean endMarkerFound = false;
   for (HttpObject object : contents) {
     assertFalse(
         "There should have been no more data after the end marker was found", endMarkerFound);
     endMarkerFound = object instanceof LastHttpContent;
     ReferenceCountUtil.release(object);
   }
   assertTrue("There should have been an end marker", endMarkerFound);
 }
 @Override
 public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
   ByteBuf in = (ByteBuf) msg;
   try {
     // System.out.println(in.toString());
     while (in.isReadable()) {
       System.out.print((char) in.readByte());
       System.out.flush();
     }
   } finally {
     // same as in.release();
     ReferenceCountUtil.release(msg);
   }
 }
  @Override
  public ByteBuf serializeResponseAsBinary(
      final ResponseMessage responseMessage, final ByteBufAllocator allocator)
      throws SerializationException {
    ByteBuf encodedMessage = null;
    try {
      final Kryo kryo = kryoThreadLocal.get();
      try (final OutputStream baos = new ByteArrayOutputStream()) {
        final Output output = new Output(baos);

        // request id - if present
        kryo.writeObjectOrNull(
            output,
            responseMessage.getRequestId() != null ? responseMessage.getRequestId() : null,
            UUID.class);

        // status
        output.writeShort(responseMessage.getStatus().getCode().getValue());
        output.writeString(responseMessage.getStatus().getMessage());
        kryo.writeClassAndObject(output, responseMessage.getStatus().getAttributes());

        // result
        kryo.writeClassAndObject(
            output,
            serializeToString
                ? serializeResultToString(responseMessage)
                : responseMessage.getResult().getData());
        kryo.writeClassAndObject(output, responseMessage.getResult().getMeta());

        final long size = output.total();
        if (size > Integer.MAX_VALUE)
          throw new SerializationException(
              String.format("Message size of %s exceeds allocatable space", size));

        encodedMessage = allocator.buffer((int) output.total());
        encodedMessage.writeBytes(output.toBytes());
      }

      return encodedMessage;
    } catch (Exception ex) {
      if (encodedMessage != null) ReferenceCountUtil.release(encodedMessage);

      logger.warn(
          "Response [{}] could not be serialized by {}.",
          responseMessage.toString(),
          GryoMessageSerializerV1d0.class.getName());
      throw new SerializationException(ex);
    }
  }
Ejemplo n.º 18
0
  @Override
  public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    try {
      ByteBuf recvBuf = Unpooled.copiedBuffer((ByteBuf) msg);
      byte[] buf = new byte[recvBuf.capacity()];
      recvBuf.readBytes(buf);

      String request = new String(buf, CharsetUtil.UTF_8);
      System.out.println("받는 Data : " + request);

      broadCast(ctx, request);
    } finally {
      ReferenceCountUtil.release(msg);
    }
  }
Ejemplo n.º 19
0
  private void testCompress0(ZlibWrapper encoderWrapper, ZlibWrapper decoderWrapper, ByteBuf data)
      throws Exception {
    EmbeddedChannel chEncoder = new EmbeddedChannel(createEncoder(encoderWrapper));

    chEncoder.writeOutbound(data.copy());
    chEncoder.flush();

    EmbeddedChannel chDecoderZlib = new EmbeddedChannel(createDecoder(decoderWrapper));
    for (; ; ) {
      ByteBuf deflatedData = chEncoder.readOutbound();
      if (deflatedData == null) {
        break;
      }
      chDecoderZlib.writeInbound(deflatedData);
    }

    byte[] decompressed = new byte[data.readableBytes()];
    int offset = 0;
    for (; ; ) {
      ByteBuf buf = chDecoderZlib.readInbound();
      if (buf == null) {
        break;
      }
      int length = buf.readableBytes();
      buf.readBytes(decompressed, offset, length);
      offset += length;
      buf.release();
      if (offset == decompressed.length) {
        break;
      }
    }
    assertEquals(data, Unpooled.wrappedBuffer(decompressed));
    assertNull(chDecoderZlib.readInbound());

    // Closing an encoder channel will generate a footer.
    assertTrue(chEncoder.finish());
    for (; ; ) {
      Object msg = chEncoder.readOutbound();
      if (msg == null) {
        break;
      }
      ReferenceCountUtil.release(msg);
    }
    // But, the footer will be decoded into nothing. It's only for validation.
    assertFalse(chDecoderZlib.finish());

    data.release();
  }
Ejemplo n.º 20
0
 /**
  * Combines all the parts in {@code contents} into one {@link ByteBuffer}.
  *
  * @param contents the content of the response.
  * @param expectedContentLength the length of the contents in bytes.
  * @return a {@link ByteBuffer} that contains all the data in {@code contents}.
  */
 private ByteBuffer getContent(Queue<HttpObject> contents, long expectedContentLength) {
   ByteBuffer buffer = ByteBuffer.allocate((int) expectedContentLength);
   boolean endMarkerFound = false;
   for (HttpObject object : contents) {
     assertFalse(
         "There should have been no more data after the end marker was found", endMarkerFound);
     HttpContent content = (HttpContent) object;
     buffer.put(content.content().nioBuffer());
     endMarkerFound = object instanceof LastHttpContent;
     ReferenceCountUtil.release(content);
   }
   assertEquals("Content length did not match expected", expectedContentLength, buffer.position());
   assertTrue("End marker was not found", endMarkerFound);
   buffer.flip();
   return buffer;
 }
  private static void testPerformOpeningHandshake0(boolean subProtocol) {
    EmbeddedChannel ch =
        new EmbeddedChannel(
            new HttpObjectAggregator(42), new HttpRequestDecoder(), new HttpResponseEncoder());

    FullHttpRequest req =
        ReferenceCountUtil.releaseLater(
            new DefaultFullHttpRequest(
                HTTP_1_1,
                HttpMethod.GET,
                "/chat",
                Unpooled.copiedBuffer("^n:ds[4U", CharsetUtil.US_ASCII)));

    req.headers().set(HttpHeaderNames.HOST, "server.example.com");
    req.headers().set(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET);
    req.headers().set(HttpHeaderNames.CONNECTION, "Upgrade");
    req.headers().set(HttpHeaderNames.ORIGIN, "http://example.com");
    req.headers().set(HttpHeaderNames.SEC_WEBSOCKET_KEY1, "4 @1  46546xW%0l 1 5");
    req.headers().set(HttpHeaderNames.SEC_WEBSOCKET_KEY2, "12998 5 Y3 1  .P00");
    req.headers().set(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL, "chat, superchat");

    if (subProtocol) {
      new WebSocketServerHandshaker00("ws://example.com/chat", "chat", Integer.MAX_VALUE)
          .handshake(ch, req);
    } else {
      new WebSocketServerHandshaker00("ws://example.com/chat", null, Integer.MAX_VALUE)
          .handshake(ch, req);
    }

    EmbeddedChannel ch2 = new EmbeddedChannel(new HttpResponseDecoder());
    ch2.writeInbound(ch.readOutbound());
    HttpResponse res = ch2.readInbound();

    Assert.assertEquals(
        "ws://example.com/chat", res.headers().get(HttpHeaderNames.SEC_WEBSOCKET_LOCATION));

    if (subProtocol) {
      Assert.assertEquals("chat", res.headers().get(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL));
    } else {
      Assert.assertNull(res.headers().get(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL));
    }
    LastHttpContent content = ch2.readInbound();

    Assert.assertEquals("8jKS'y:G*Co,Wxa-", content.content().toString(CharsetUtil.US_ASCII));
    content.release();
  }
Ejemplo n.º 22
0
  @Override
  public void channelRead(ChannelHandlerContext ctx, Object msg) { // (2)
    ByteBuf in = (ByteBuf) msg;
    try {
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      while (in.isReadable()) { // (1)
        byte readByte = in.readByte();
        baos.write(readByte);
        // System.out.flush();

        ctx.flush();
      }
      System.out.println("Msg Read: " + baos.toString());
    } finally {
      // System.out.println("finish!");
      ReferenceCountUtil.release(msg); // (2)
    }
  }
 @Override
 protected void decode(
     final ChannelHandlerContext channelHandlerContext,
     final WebSocketFrame webSocketFrame,
     final List<Object> objects)
     throws Exception {
   try {
     if (webSocketFrame instanceof BinaryWebSocketFrame) {
       final BinaryWebSocketFrame tf = (BinaryWebSocketFrame) webSocketFrame;
       objects.add(serializer.deserializeResponse(tf.content()));
     } else {
       final TextWebSocketFrame tf = (TextWebSocketFrame) webSocketFrame;
       final MessageTextSerializer textSerializer = (MessageTextSerializer) serializer;
       objects.add(textSerializer.deserializeResponse(tf.text()));
     }
   } finally {
     ReferenceCountUtil.release(webSocketFrame);
   }
 }
 @Override
 @SuppressWarnings("unchecked")
 public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
   if (matcher.match(msg)) {
     final I typedMsg = (I) msg;
     try {
       if (quotaMeter.getOneMinuteRate() > USER_QUOTA_LIMIT) {
         sendErrorResponseIfTicked(typedMsg.id);
         return;
       }
       quotaMeter.mark();
       messageReceived(ctx, handlerState, typedMsg);
     } catch (Exception e) {
       handleGeneralException(ctx, e);
     } finally {
       ThreadContext.clearMap();
       ReferenceCountUtil.release(msg);
     }
   }
 }
    @Override
    public void channelRead(@NotNull ChannelHandlerContext ctx, @NotNull Object msg) {
      try {
        bytesReceived += ((ByteBuf) msg).readableBytes();

        if (i++ % 10000 == 0) System.out.print(".");
        if (TimeUnit.NANOSECONDS.toSeconds(System.nanoTime() - startTime) >= 10) {
          long time = System.nanoTime() - startTime;
          System.out.printf("\nThroughput was %.1f MB/s%n", 1e3 * bytesReceived / time);
          return;
        }

      } finally {
        ReferenceCountUtil.release(msg); // (2)
      }

      final ByteBuf outMsg = ctx.alloc().buffer(bufferSize); // (2)
      outMsg.writeBytes(payload);

      ctx.writeAndFlush(outMsg); // (3)
    }
  @Override
  public ByteBuf serializeRequestAsBinary(
      final RequestMessage requestMessage, final ByteBufAllocator allocator)
      throws SerializationException {
    ByteBuf encodedMessage = null;
    try {
      final Kryo kryo = kryoThreadLocal.get();
      try (final OutputStream baos = new ByteArrayOutputStream()) {
        final Output output = new Output(baos);
        final String mimeType = serializeToString ? MIME_TYPE_STRINGD : MIME_TYPE;
        output.writeByte(mimeType.length());
        output.write(mimeType.getBytes(UTF8));

        kryo.writeObject(output, requestMessage.getRequestId());
        output.writeString(requestMessage.getProcessor());
        output.writeString(requestMessage.getOp());
        kryo.writeObject(output, requestMessage.getArgs());

        final long size = output.total();
        if (size > Integer.MAX_VALUE)
          throw new SerializationException(
              String.format("Message size of %s exceeds allocatable space", size));

        encodedMessage = allocator.buffer((int) size);
        encodedMessage.writeBytes(output.toBytes());
      }

      return encodedMessage;
    } catch (Exception ex) {
      if (encodedMessage != null) ReferenceCountUtil.release(encodedMessage);

      logger.warn(
          "Request [{}] could not be serialized by {}.",
          requestMessage.toString(),
          GryoMessageSerializerV1d0.class.getName());
      throw new SerializationException(ex);
    }
  }
Ejemplo n.º 27
0
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

      messageReceiveCount++;
      if (msg instanceof HttpResponse) {
        try {
          HttpResponse response = (HttpResponse) msg;

          StringBuilder sb = new StringBuilder();
          if (LOG.isDebugEnabled()) {
            sb.append("STATUS: ")
                .append(response.getStatus())
                .append(", VERSION: ")
                .append(response.getProtocolVersion())
                .append(", HEADER: ");
          }
          if (!response.headers().names().isEmpty()) {
            for (String name : response.headers().names()) {
              for (String value : response.headers().getAll(name)) {
                if (LOG.isDebugEnabled()) {
                  sb.append(name).append(" = ").append(value);
                }
                if (this.length == -1 && name.equals("Content-Length")) {
                  this.length = Long.parseLong(value);
                }
              }
            }
          }
          if (LOG.isDebugEnabled()) {
            LOG.debug(sb.toString());
          }

          if (response.getStatus().code() == HttpResponseStatus.NO_CONTENT.code()) {
            LOG.warn("There are no data corresponding to the request");
            length = 0;
            return;
          } else if (response.getStatus().code() != HttpResponseStatus.OK.code()) {
            LOG.error(response.getStatus().reasonPhrase());
            state = TajoProtos.FetcherState.FETCH_FAILED;
            return;
          }
        } catch (Exception e) {
          LOG.error(e.getMessage(), e);
        } finally {
          ReferenceCountUtil.release(msg);
        }
      }

      if (msg instanceof HttpContent) {
        try {
          HttpContent httpContent = (HttpContent) msg;
          ByteBuf content = httpContent.content();
          if (content.isReadable()) {
            content.readBytes(fc, content.readableBytes());
          }

          if (msg instanceof LastHttpContent) {
            if (raf != null) {
              fileLen = file.length();
            }

            finishTime = System.currentTimeMillis();
            if (state != TajoProtos.FetcherState.FETCH_FAILED) {
              state = TajoProtos.FetcherState.FETCH_FINISHED;
            }

            IOUtils.cleanup(LOG, fc, raf);
          }
        } catch (Exception e) {
          LOG.error(e.getMessage(), e);
        } finally {
          ReferenceCountUtil.release(msg);
        }
      }
    }
Ejemplo n.º 28
0
    boolean drain() {
      if (wip++ != 0) {
        return false;
      }

      int missed = 1;

      for (; ; ) {
        final Queue<Object> q = queue;
        final Subscriber<? super Object> a = actual;

        if (a == null) {
          return false;
        }

        long r = requested;
        long e = 0L;

        while (e != r) {
          if (isCancelled()) {
            return false;
          }

          boolean d = done;
          Object v = q != null ? q.poll() : null;
          boolean empty = v == null;

          if (d && empty) {
            cancelResource();
            if (q != null) {
              q.clear();
            }
            Throwable ex = error;
            if (ex != null) {
              a.onError(ex);
            } else {
              a.onComplete();
            }
            return false;
          }

          if (empty) {
            ch.read();
            break;
          }

          try {
            a.onNext(v);
          } finally {
            ReferenceCountUtil.release(v);
            ch.read();
          }

          e++;
        }

        if (e == r) {
          if (isCancelled()) {
            return false;
          }

          if (done && (q == null || q.isEmpty())) {
            cancelResource();
            if (q != null) {
              q.clear();
            }
            Throwable ex = error;
            if (ex != null) {
              a.onError(ex);
            } else {
              a.onComplete();
            }
            return false;
          }
        }

        if (e != 0L) {
          if (r != Long.MAX_VALUE) {
            if ((requested -= e) > 0L) {
              ch.read();
            }
          }
        }

        missed = (wip = wip - missed);
        if (missed == 0) {
          if (r == Long.MAX_VALUE) {
            ch.config().setAutoRead(true);
            ch.read();
            return true;
          }
          return false;
        }
      }
    }
 @Override
 public void channelRead(final ChannelHandlerContext ctx, final Object inputObj) throws Exception {
   try {
     final ByteBuf input = (ByteBuf) inputObj;
     if (!input.isReadable()) {
       return;
     }
     final Channel channel = ctx.channel();
     receivedData.writeBytes(input);
     ProtocolVersion handshakeversion = ProtocolVersion.NOT_SET;
     receivedData.readerIndex(0);
     int firstbyte = receivedData.readUnsignedByte();
     switch (firstbyte) {
       case 0xFE:
         { // old ping
           try {
             if (receivedData.readableBytes() == 0) { // really old protocol probably
               scheduleTask(ctx, new Ping11ResponseTask(channel), 1000, TimeUnit.MILLISECONDS);
             } else if (receivedData.readUnsignedByte() == 1) {
               if (receivedData.readableBytes() == 0) {
                 // 1.5.2 probably
                 scheduleTask(
                     ctx, new Ping152ResponseTask(this, channel), 500, TimeUnit.MILLISECONDS);
               } else if ((receivedData.readUnsignedByte() == 0xFA)
                   && "MC|PingHost"
                       .equals(
                           new String(
                               Utils.toArray(
                                   receivedData.readBytes(receivedData.readUnsignedShort() * 2)),
                               StandardCharsets.UTF_16BE))) { // 1.6.*
                 receivedData.readUnsignedShort();
                 handshakeversion = ProtocolVersion.fromId(receivedData.readUnsignedByte());
               }
             }
           } catch (IndexOutOfBoundsException ex) {
           }
           break;
         }
       case 0x02:
         { // 1.6 or 1.5.2 handshake
           try {
             handshakeversion = ProtocolVersion.fromId(receivedData.readUnsignedByte());
           } catch (IndexOutOfBoundsException ex) {
           }
           break;
         }
       default:
         { // 1.7 or 1.8 handshake
           receivedData.readerIndex(0);
           ByteBuf data = getVarIntPrefixedData(receivedData);
           if (data != null) {
             handshakeversion = readNPHandshake(data);
           }
           break;
         }
     }
     // if we detected the protocol than we save it and process data
     if (handshakeversion != ProtocolVersion.NOT_SET) {
       setProtocol(channel, receivedData, handshakeversion);
     }
   } catch (Throwable t) {
     ctx.channel().close();
   } finally {
     ReferenceCountUtil.release(inputObj);
   }
 }
Ejemplo n.º 30
0
 @Override
 public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
   emit("data", ReferenceCountUtil.retain(msg));
   super.channelRead(ctx, msg);
 }