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.base.research.socket.netty.ServerHandler.java

License:Apache License

@Override
public void channelRead(final ChannelHandlerContext ctx, Object msg) { // (2)
    Date date = (Date) msg;
    System.out.println("handle:" + date);

    ctx.fireChannelRead(date);

    // ?/* ww w.  jav a 2 s  .  c  o  m*/
    long timeMillis = System.currentTimeMillis();
    final ChannelFuture f = ctx.write(timeMillis);
    ctx.flush();
    System.out.println("flush");
    // final ChannelFuture f = ctx.writeAndFlush(time); // (3)
    f.addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture future) {
            assert f == future;
            ctx.close();
            System.out.println("close-----------------");
        }
    });
}

From source file:com.book.netty5.client.HeartBeatReqHandler.java

License:Apache License

public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    NettyMessage message = (NettyMessage) msg;
    // ?????//from  w w  w.  ja  va  2  s . c om
    if (message.getHeader() != null && message.getHeader().getType() == MessageType.LOGIN_RESP.value()) {
        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.book.netty5.client.LoginAuthReqHandler.java

License:Apache License

/**
 * Calls {@link ChannelHandlerContext#fireChannelRead(Object)} to forward to
 * the next {@link ChannelHandler} in the {@link ChannelPipeline}.
 * /*from www . j  a  v  a2  s.  c om*/
 * Sub-classes may override this method to change behavior.
 */
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.book.netty5.server.HeartBeatRespHandler.java

License:Apache License

public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    NettyMessage message = (NettyMessage) msg;
    // ?//from ww w . j  ava 2  s.  c  o  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.book.netty5.server.LoginAuthRespHandler.java

License:Apache License

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

    // ?????
    if (message.getHeader() != null && message.getHeader().getType() == MessageType.LOGIN_REQ.value()) {
        String nodeIndex = ctx.channel().remoteAddress().toString();
        NettyMessage loginResp = null;
        // ???
        if (nodeCheck.containsKey(nodeIndex)) {
            loginResp = buildResponse((byte) -1);
        } else {
            InetSocketAddress address = (InetSocketAddress) ctx.channel().remoteAddress();
            String ip = address.getAddress().getHostAddress();
            boolean isOK = false;
            for (String WIP : whitekList) {
                if (WIP.equals(ip)) {
                    isOK = true;
                    break;
                }
            }
            loginResp = isOK ? buildResponse((byte) 0) : buildResponse((byte) -1);
            if (isOK)
                nodeCheck.put(nodeIndex, true);
        }
        System.out.println("The login response is : " + loginResp + " body [" + loginResp.getBody() + "]");
        ctx.writeAndFlush(loginResp);
    } else {
        ctx.fireChannelRead(msg);
    }
}

From source file:com.changxx.phei.netty.protocol.netty.client.HeartBeatReqHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    NettyMessage message = (NettyMessage) msg;
    // ?????//w ww.  j  a  va  2  s.c o m
    if (message.getHeader() != null && message.getHeader().getType() == MessageType.LOGIN_RESP.value()) {
        heartBeat = ctx.executor().scheduleAtFixedRate(new 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.changxx.phei.netty.protocol.netty.client.LoginAuthReqHandler.java

License:Apache License

/**
 * Calls {@link io.netty.channel.ChannelHandlerContext#fireChannelRead(Object)} to forward to
 * the next {@link io.netty.channel.ChannelHandler} in the {@link io.netty.channel.ChannelPipeline}.
 * Sub-classes may override this method to change behavior.
 *//*from www  . j  a v a  2 s . c om*/
@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();
        System.out.println(message.getBody().toString().charAt(0));
        byte loginResult = (byte) 0;
        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.chenyang.proxy.http.HttpPreHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (preCheck(ctx, msg)) {
        ctx.fireChannelRead(msg);
    } else {/*www  .  j a v  a  2 s  .  com*/
        ReferenceCountUtil.release(msg);
    }
}

From source file:com.chenyang.proxy.http.HttpSchemaHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext uaChannelCtx, final Object msg) throws Exception {

    if (msg instanceof HttpRequest) {
        HttpRequest httpRequest = (HttpRequest) msg;

        String originalHost = HostNamePortUtil.getHostName(httpRequest);
        int originalPort = HostNamePortUtil.getPort(httpRequest);
        HttpRemote apnProxyRemote = new HttpRemote(originalHost, originalPort);

        if (!HostAuthenticationUtil.isValidAddress(apnProxyRemote.getInetSocketAddress())) {
            HttpErrorUtil.writeAndFlush(uaChannelCtx.channel(), HttpResponseStatus.FORBIDDEN);
            return;
        }/*  ww  w  .j a v a  2 s .  c  om*/

        Channel uaChannel = uaChannelCtx.channel();

        HttpConnectionAttribute apnProxyConnectionAttribute = HttpConnectionAttribute.build(
                uaChannel.remoteAddress().toString(), httpRequest.getMethod().name(), httpRequest.getUri(),
                httpRequest.getProtocolVersion().text(),
                httpRequest.headers().get(HttpHeaders.Names.USER_AGENT), apnProxyRemote);

        uaChannelCtx.attr(HttpConnectionAttribute.ATTRIBUTE_KEY).set(apnProxyConnectionAttribute);
        uaChannel.attr(HttpConnectionAttribute.ATTRIBUTE_KEY).set(apnProxyConnectionAttribute);

        if (httpRequest.getMethod().equals(HttpMethod.CONNECT)) {
            if (uaChannelCtx.pipeline().get(HttpUserAgentForwardHandler.HANDLER_NAME) != null) {
                uaChannelCtx.pipeline().remove(HttpUserAgentForwardHandler.HANDLER_NAME);
            }
            if (uaChannelCtx.pipeline().get(HttpUserAgentTunnelHandler.HANDLER_NAME) == null) {
                uaChannelCtx.pipeline().addLast(HttpUserAgentTunnelHandler.HANDLER_NAME,
                        new HttpUserAgentTunnelHandler());
            }
        } else {
            if (uaChannelCtx.pipeline().get(HttpUserAgentForwardHandler.HANDLER_NAME) == null) {
                uaChannelCtx.pipeline().addLast(HttpUserAgentForwardHandler.HANDLER_NAME,
                        new HttpUserAgentForwardHandler());
            }
        }
    }

    uaChannelCtx.fireChannelRead(msg);
}

From source file:com.cloudhopper.smpp.channel.SmppSessionLogger.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    log(Direction.UP, msg);//w w w. j a  v  a2 s  .  co m
    ctx.fireChannelRead(msg);
}