Example usage for io.netty.channel ChannelHandlerContext fireChannelRead

List of usage examples for io.netty.channel ChannelHandlerContext fireChannelRead

Introduction

In this page you can find the example usage for io.netty.channel ChannelHandlerContext fireChannelRead.

Prototype

@Override
    ChannelHandlerContext fireChannelRead(Object msg);

Source Link

Usage

From source file:org.Client.LoginAuthReqHandler.java

License:Apache License

/**
 * Calls {@link ChannelHandlerContext#fireChannelRead(Object)} to forward to
 * the next {@link ChannelHandler} in the {@link ChannelPipeline}.
 * //from w w w  .  ja v a 2s . co  m
 * Sub-classes may override this method to change behavior.
 */
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    NettyMessage message = (NettyMessage) msg;

    // ??????
    if (message.getHeader() != null && message.getHeader().getType() == MessageType.LOGIN_RESP.value()) {
        byte[] loginResult = toByteArray(message.getBody());
        if (loginResult[0] != (byte) 0) {
            // ?
            ctx.close();
        } else {
            System.out.println("Login is ok : " + message);
            ctx.fireChannelRead(msg);
        }
    } else
        ctx.fireChannelRead(msg);
}

From source file:org.clitherproject.clither.server.net.Handshaker.java

License:Open Source License

@Override
protected void channelRead0(ChannelHandlerContext handlerContext, Object request) throws Exception {

    if (request instanceof FullHttpRequest) {
        FullHttpRequest fullRequest = (FullHttpRequest) request;
        WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(
                "ws://" + fullRequest.headers().get(HttpHeaders.HOST) + "/", null, true);
        serverHandshaker = wsFactory.newHandshaker(fullRequest);

        if (serverHandshaker == null) {
            WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(handlerContext.channel());
        } else {/*from w  w w .  j a  va 2  s .com*/
            serverHandshaker.handshake(handlerContext.channel(), fullRequest);
        }
    } else if (request instanceof WebSocketFrame) {

        WebSocketFrame frame = (WebSocketFrame) request;

        if (request instanceof CloseWebSocketFrame) {
            if (serverHandshaker != null) {
                serverHandshaker.close(handlerContext.channel(), ((CloseWebSocketFrame) request).retain());
            }
        } else if (request instanceof PingWebSocketFrame) {
            handlerContext.channel().write(new PongWebSocketFrame(frame.content().retain()));
        } else {
            handlerContext.fireChannelRead(frame.retain());
        }
    }
}

From source file:org.clitherproject.clither.server.net.WebSocketHandler.java

License:Open Source License

@Override
protected void channelRead0(ChannelHandlerContext ctx, Object req) throws Exception {
    if (req instanceof FullHttpRequest) {
        FullHttpRequest request = (FullHttpRequest) req;
        // ----- Client authenticity check code -----
        String origin = request.headers().get(HttpHeaders.ORIGIN);
        if (origin != null) {
            switch (origin) {
            case "http://slither.io":
            case "https://slither.io":
            case "http://localhost":
            case "https://localhost":
            case "http://127.0.0.1":
            case "https://127.0.0.1":
                break;
            default:
                ctx.channel().close();//from  w ww .j a  v  a2s  .c om
                return;
            }
        }
        // -----/Client authenticity check code -----

        WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(
                "ws://" + request.headers().get(HttpHeaders.HOST) + "/", null, true);
        handshaker = wsFactory.newHandshaker(request);
        if (handshaker == null) {
            WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
        } else {
            handshaker.handshake(ctx.channel(), request);
        }
    } else if (req instanceof WebSocketFrame) {
        WebSocketFrame frame = (WebSocketFrame) req;

        if (req instanceof CloseWebSocketFrame) {
            if (handshaker != null) {
                handshaker.close(ctx.channel(), ((CloseWebSocketFrame) req).retain());
            }
        } else if (req instanceof PingWebSocketFrame) {
            ctx.channel().write(new PongWebSocketFrame(frame.content().retain()));
        } else {
            ctx.fireChannelRead(frame.retain());
        }
    }
}

From source file:org.cloudfoundry.reactor.routing.v1.tcproutes.EventStreamDecoderChannelHandler.java

License:Apache License

private void send(ChannelHandlerContext context) {
    if (this.nameStartPosition == this.valueEndPosition) {
        if (this.event != null) {
            context.fireChannelRead(this.event.build());
            this.event = null;
        }/*  w  w  w  .  ja  v  a2 s .c  om*/
    } else {
        String name = this.characters.substring(this.nameStartPosition, this.nameEndPosition);
        String value = this.characters.substring(this.valueStartPosition, this.valueEndPosition);

        if ("id".equals(name)) {
            this.event = this.event != null ? this.event.id(value) : ServerSentEvent.builder().id(value);
        } else if ("event".equals(name)) {
            this.event = this.event != null ? this.event.eventType(value)
                    : ServerSentEvent.builder().eventType(value);
        } else if ("data".equals(name)) {
            if (this.event != null) {
                ServerSentEvent event = this.event.build();
                String data = event.getData() == null ? value : String.format("%s\n%s", event.getData(), value);

                this.event = ServerSentEvent.builder().id(event.getId()).eventType(event.getEventType())
                        .data(data).retry(event.getRetry());
            } else {
                this.event = ServerSentEvent.builder().data(value);
            }
        } else if ("retry".equals(name)) {
            this.event = this.event != null ? this.event.retry(Integer.parseInt(value))
                    : ServerSentEvent.builder().retry(Integer.parseInt(value));
        }
    }

    this.nameStartPosition = this.position;
    this.valueEndPosition = this.position;
    this.stage = Stage.NAME;
}

From source file:org.cloudfoundry.reactor.util.MultipartDecoderChannelHandler.java

License:Apache License

private void sendBody(ChannelHandlerContext context, int start, int end) {
    int length = end - start;
    if (length > 0) {
        context.fireChannelRead(this.byteBuf.slice(start, length).retain());
    }// w  w  w. java  2 s  .  c  o  m
}

From source file:org.cloudfoundry.reactor.util.MultipartDecoderChannelHandler.java

License:Apache License

private void sendCloseDelimiter(ChannelHandlerContext context) {
    context.fireChannelRead(CLOSE_DELIMITER);
    this.stage = Stage.BODY;
}

From source file:org.cloudfoundry.reactor.util.MultipartDecoderChannelHandler.java

License:Apache License

private void sendDelimiter(ChannelHandlerContext context) {
    context.fireChannelRead(DELIMITER);
    this.stage = Stage.BODY;
}

From source file:org.code_house.ebus.netty.codec.OutboundEBusHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    // receive//from w  w  w. j a  va 2s.c o m
    if (msg instanceof MasterHeader) {
        this.masterHeader = (MasterHeader) msg;
    } else if (msg instanceof MasterData) {
        this.masterData = (MasterData) msg;
    }

    if (msg instanceof ByteBuf) {
        ByteBuf buffer = (ByteBuf) msg;
        if (writeInProgress && buffer.capacity() == 1) {
            byte aByte = buffer.getByte(0);
            if (device.getMaster().getAddress() == aByte) {
                // we got access to bus, lets try to flush something
                accessGranted = true;
                flush(ctx);
                return;
            }
        }
        writeInProgress = false;

        if (buffer.isReadable()) {
            byte lastByte = buffer.getByte(buffer.readerIndex());
            if (lastByte == Constants.SYN) {
                // we received SYN symbol, try to write own address
                writeInProgress = true;
                ctx.writeAndFlush(Unpooled.wrappedBuffer(new byte[] { device.getMaster().getAddress() })); // force writing master address to bus
            }
        }
    }

    ctx.fireChannelRead(msg);
}

From source file:org.dcache.xrootd.core.XrootdAuthorizationHandler.java

License:Open Source License

@Override
protected Void doOnStat(ChannelHandlerContext ctx, StatRequest req) throws XrootdException {
    authorize(ctx, req, FilePerm.READ);//w  w w  .j a v a 2 s.  co  m
    ctx.fireChannelRead(req);
    return null;
}

From source file:org.dcache.xrootd.core.XrootdAuthorizationHandler.java

License:Open Source License

@Override
protected Void doOnStatx(ChannelHandlerContext ctx, StatxRequest req) throws XrootdException {
    if (req.getPaths().length == 0) {
        throw new XrootdException(kXR_ArgMissing, "no paths specified");
    }/*  www  . ja va 2  s  .  c  o  m*/

    String[] paths = req.getPaths();
    String[] opaques = req.getOpaques();
    for (int i = 0; i < paths.length; i++) {
        paths[i] = authorize(ctx, req, FilePerm.READ, paths[i], opaques[i]);
    }
    req.setPaths(paths);

    ctx.fireChannelRead(req);
    return null;
}