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:com.mastfrog.scamper.InboundBytesDecoder.java

License:Open Source License

@Override
protected void messageReceived(ChannelHandlerContext ctx, ByteBuf sctpMsg) throws Exception {
    assoc.ensureRegistered(ctx);/*from   www.  j  ava2  s. co m*/
    int sctpChannel = ctx.attr(InboundSctpMessageToByteBufDecoder.SCTP_CHANNEL_KEY).get();
    MessageTypeAndBuffer decoded = codec.decode(sctpMsg, ctx, sctpChannel);
    ctx.fireChannelRead(decoded);
}

From source file:com.netty.protocol.netty.client.LoginAuthReqHandler.java

License:Apache License

/**
 * Calls {@link ChannelHandlerContext#fireChannelRead(Object)} to forward to
 * the next {@link ChannelHandler} in the {@link ChannelPipeline}.
 * //  w w w.  j  ava  2  s.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 = (Byte) message.getBody();
        if (loginResult != (byte) 0) {
            // ?
            ctx.close();
        } else {
            System.out.println("Login is ok : " + message);
            ctx.fireChannelRead(msg);
        }
    } else
        ctx.fireChannelRead(msg);
}

From source file:com.ogarproject.ogar.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 -----
        // !!!!! WARNING !!!!!
        // THE BELOW SECTION OF CODE CHECKS TO ENSURE THAT CONNECTIONS ARE COMING
        // FROM THE OFFICIAL AGAR.IO CLIENT. IF YOU REMOVE OR MODIFY THE BELOW
        // SECTION OF CODE TO ALLOW CONNECTIONS FROM A CLIENT ON A DIFFERENT DOMAIN,
        // YOU MAY BE COMMITTING COPYRIGHT INFRINGEMENT AND LEGAL ACTION MAY BE TAKEN
        // AGAINST YOU. THIS SECTION OF CODE WAS ADDED ON JULY 9, 2015 AT THE REQUEST
        // OF THE AGAR.IO DEVELOPERS.
        String origin = request.headers().get(HttpHeaders.ORIGIN);
        if (origin != null) {
            switch (origin) {
            case "http://agar.io":
            case "https://agar.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  www  .  jav  a  2s.c  o  m
                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:com.phei.netty.protocol.netty.client.HeartBeatReqHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    NettyMessage message = (NettyMessage) msg;
    // ?????/*from w w  w.j  a  v a  2s. co m*/
    if (message.getHeader() != null && message.getHeader().getType() == MessageType.LOGIN_RESP.value()) {
        //5???
        heartBeat = ctx.executor().scheduleAtFixedRate(new HeartBeatReqHandler.HeartBeatTask(ctx), 0, 5000,
                TimeUnit.MILLISECONDS);
    } else if (message.getHeader() != null
            && message.getHeader().getType() == MessageType.HEARTBEAT_RESP.value()) {
        System.out.println("Client receive server heart beat message : ---> " + message);
    } else {
        ctx.fireChannelRead(msg);
    }
}

From source file:com.phei.netty.protocol.netty.client.LoginAuthReqHandler.java

License:Apache License

/**
 * Calls {@link ChannelHandlerContext#fireChannelRead(Object)} to forward to
 * the next {@link ChannelHandler} in the {@link ChannelPipeline}.
 * /*from   ww  w . j  a  v a  2  s  .c o  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 = (byte) message.getBody();
        if (loginResult != (byte) 0) {
            // ?
            ctx.close();
        } else {
            System.out.println("Login is ok : " + message);
            ctx.fireChannelRead(msg);
        }
    } else {
        ctx.fireChannelRead(msg);
    }
}

From source file:com.phei.netty.protocol.netty.server.HeartBeatRespHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    NettyMessage message = (NettyMessage) msg;
    // ?//  w  ww. j a  v a  2 s  .  co  m
    if (message.getHeader() != null && message.getHeader().getType() == MessageType.HEARTBEAT_REQ.value()) {
        System.out.println("Receive client heart beat message : ---> " + message);
        NettyMessage heartBeat = buildHeatBeat();
        System.out.println("Send heart beat response message to client : ---> " + heartBeat);
        ctx.writeAndFlush(heartBeat);
    } else {
        ctx.fireChannelRead(msg);
    }
}

From source file:com.shun.liu.quickserver.socksproxy.DirectClientHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    System.out.println(msg.getClass());
    ctx.fireChannelRead(msg);

}

From source file:com.shun.liu.quickserver.socksproxy.SocksServerHandler.java

License:Apache License

@Override
public void channelRead0(ChannelHandlerContext ctx, SocksMessage socksRequest) throws Exception {
    switch (socksRequest.version()) {
    case SOCKS4a:
        Socks4CommandRequest socksV4CmdRequest = (Socks4CommandRequest) socksRequest;
        if (socksV4CmdRequest.type() == Socks4CommandType.CONNECT) {
            ctx.pipeline().addLast(new SocksServerConnectHandler());
            ctx.pipeline().remove(this);
            ctx.fireChannelRead(socksRequest);
        } else {// w ww  .ja  va2s.  c o  m
            ctx.close();
        }
        break;
    case SOCKS5:
        if (socksRequest instanceof Socks5InitialRequest) {
            //                     auth support example
            //                    ctx.pipeline().addFirst(new Socks5PasswordAuthRequestDecoder());
            //                    ctx.write(new DefaultSocks5AuthMethodResponse(Socks5AuthMethod.PASSWORD));
            ctx.pipeline().addFirst(new Socks5CommandRequestDecoder());
            ctx.write(new DefaultSocks5InitialResponse(Socks5AuthMethod.NO_AUTH));
        } else if (socksRequest instanceof Socks5PasswordAuthRequest) {
            ctx.pipeline().addFirst(new Socks5CommandRequestDecoder());
            ctx.write(new DefaultSocks5PasswordAuthResponse(Socks5PasswordAuthStatus.SUCCESS));
        } else if (socksRequest instanceof Socks5CommandRequest) {
            Socks5CommandRequest socks5CmdRequest = (Socks5CommandRequest) socksRequest;
            if (socks5CmdRequest.type() == Socks5CommandType.CONNECT) {
                ctx.pipeline().addLast(new SocksServerConnectHandler());
                ctx.pipeline().remove(this);
                ctx.fireChannelRead(socksRequest);
            } else {
                ctx.close();
            }
        } else {
            ctx.close();
        }
        break;
    case UNKNOWN:
        ctx.close();
        break;
    }
}

From source file:com.slyak.services.proxy.handler.Socks5CommandRequestHandler.java

License:Apache License

@Override
protected void channelRead0(final ChannelHandlerContext requestChannelContext,
        final DefaultSocks5CommandRequest msg) throws Exception {
    if (Socks5CommandType.CONNECT.equals(msg.type())) {
        log.debug("Start to connect remote server : {}:{}", msg.dstAddr(), msg.dstPort());
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(remoteEventLoopGroup).channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override/*from   www.j  a v  a2s  .  com*/
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline pipeline = ch.pipeline();
                        pipeline.addLast(new IdleStateHandler(0, 0, 30));
                        pipeline.addLast(new IdleEventHandler());
                        pipeline.addLast(new Remote2RequestHandler(requestChannelContext.channel()));
                        pipeline.addLast(ExceptionHandler.INSTANCE);
                    }
                });
        final ChannelFuture future = bootstrap.connect(msg.dstAddr(), msg.dstPort());
        this.remoteChannel = future.channel();
        future.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(final ChannelFuture connectFuture) throws Exception {
                if (connectFuture.isSuccess()) {
                    log.debug("Connected to remote server");
                    requestChannelContext.pipeline().addLast(new Request2RemoteHandler(remoteChannel));
                    Socks5CommandResponse response = new DefaultSocks5CommandResponse(
                            Socks5CommandStatus.SUCCESS, Socks5AddressType.IPv4);
                    //add client to dest handler to receive response
                    requestChannelContext.writeAndFlush(response);
                } else {
                    log.debug("Failed to connect to remote server");
                    Socks5CommandResponse commandResponse = new DefaultSocks5CommandResponse(
                            Socks5CommandStatus.FAILURE, Socks5AddressType.IPv4);
                    requestChannelContext.writeAndFlush(commandResponse);
                }
            }
        });
    } else {
        log.debug("Fire channel read");
        requestChannelContext.fireChannelRead(msg);
    }
}

From source file:com.slyak.services.proxy.handler.Socks5InitialRequestHandler.java

License:Apache License

@Override
protected void channelRead0(ChannelHandlerContext ctx, DefaultSocks5InitialRequest msg) throws Exception {
    if (msg.decoderResult().isFailure()) {
        ctx.fireChannelRead(msg);
    } else {//  w w w  .j a  va2  s . c om
        if (SocksVersion.SOCKS5.equals(msg.version())) {
            if (isNeedAuth()) {
                Socks5InitialResponse initialResponse = new DefaultSocks5InitialResponse(
                        Socks5AuthMethod.PASSWORD);
                ctx.writeAndFlush(initialResponse);
            } else {
                Socks5InitialResponse initialResponse = new DefaultSocks5InitialResponse(
                        Socks5AuthMethod.NO_AUTH);
                ctx.writeAndFlush(initialResponse);
            }
        }
    }
}