@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);
    }
  }
Exemplo n.º 2
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);
            }
          });
    }
  }
Exemplo n.º 3
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);
 }
Exemplo n.º 4
0
 @Override
 public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
   emit("data", ReferenceCountUtil.retain(msg));
   super.channelRead(ctx, msg);
 }