Example usage for io.netty.channel ChannelHandlerContext channel

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

Introduction

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

Prototype

Channel channel();

Source Link

Document

Return the Channel which is bound to the ChannelHandlerContext .

Usage

From source file:com.feihong.newzxclient.tcp.NettyClientHandler.java

License:Apache License

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
    Loger.print("exceptionCaught() : " + cause.getMessage());
    ctx.channel().close();
    ctx.close();/*from w w  w. j a v  a 2 s.c  o m*/
}

From source file:com.fire.login.http.HttpInboundHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof HttpRequest) {
        HttpRequest req = (HttpRequest) msg;
        URI uri = URI.create(req.getUri());
        ctx.channel().attr(KEY_PATH).set(uri.getPath());

        if (req.getMethod().equals(HttpMethod.GET)) {
            QueryStringDecoder decoder = new QueryStringDecoder(URI.create(req.getUri()));
            Map<String, List<String>> parameter = decoder.parameters();
            dispatch(ctx.channel(), parameter);
            return;
        }/*from  w  w w  .j ava  2s . c  om*/

        decoder = new HttpPostRequestDecoder(factory, req);
    }

    if (decoder != null) {
        if (msg instanceof HttpContent) {
            HttpContent chunk = (HttpContent) msg;
            decoder.offer(chunk);

            List<InterfaceHttpData> list = decoder.getBodyHttpDatas();
            Map<String, List<String>> parameter = new HashMap<>();
            for (InterfaceHttpData data : list) {
                if (data.getHttpDataType() == HttpDataType.Attribute) {
                    Attribute attribute = (Attribute) data;
                    addParameter(attribute.getName(), attribute.getValue(), parameter);
                }
            }

            if (chunk instanceof LastHttpContent) {
                reset();
                dispatch(ctx.channel(), parameter);
            }
        }
    }
}

From source file:com.flowpowered.network.pipeline.MessageHandler.java

License:MIT License

@Override
public void channelActive(ChannelHandlerContext ctx) {
    final Channel c = ctx.channel();
    Session s = connectionManager.newSession(c);
    if (!this.session.compareAndSet(null, s)) {
        throw new IllegalStateException("Session may not be set more than once");
    }/*w  ww  . ja  v a  2  s .c om*/
    s.onReady();
}

From source file:com.flowpowered.networking.pipeline.MessageHandler.java

License:MIT License

@Override
public void channelActive(ChannelHandlerContext ctx) {
    final Channel c = ctx.channel();
    Session s = connectionManager.newSession(c);
    setSession(s);/*from w  w  w.j  a va2  s  .  co m*/
    s.onReady();
}

From source file:com.flowpowered.networking.pipeline.MessageHandler.java

License:MIT License

@Override
public void channelInactive(ChannelHandlerContext ctx) {
    Channel c = ctx.channel();
    Session session = this.session.get();
    // TODO needed?
    session.validate(c);// w  w w .jav a  2s. c  om
    session.onDisconnect();
    connectionManager.sessionInactivated(session);
}

From source file:com.flowpowered.networking.pipeline.MessageHandler.java

License:MIT License

@Override
protected void channelRead0(ChannelHandlerContext ctx, Message i) {
    Session session = this.session.get();
    session.validate(ctx.channel());
    session.messageReceived(i);/*from w  w  w . j a  v  a 2s  . c  o m*/
}

From source file:com.flysoloing.learning.network.netty.factorial.FactorialClientHandler.java

License:Apache License

@Override
public void channelRead0(ChannelHandlerContext ctx, final BigInteger msg) {
    receivedMessages++;//from ww w.  j a  va  2  s  .  c  o m
    if (receivedMessages == FactorialClient.COUNT) {
        // Offer the answer after closing the connection.
        ctx.channel().close().addListener(new ChannelFutureListener() {
            public void operationComplete(ChannelFuture future) {
                boolean offered = answer.offer(msg);
                assert offered;
            }
        });
    }
}

From source file:com.flysoloing.learning.network.netty.file.FileServerHandler.java

License:Apache License

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
    cause.printStackTrace();//from   w  ww.ja v  a 2 s .  c o  m

    if (ctx.channel().isActive()) {
        ctx.writeAndFlush("ERR: " + cause.getClass().getSimpleName() + ": " + cause.getMessage() + '\n')
                .addListener(ChannelFutureListener.CLOSE);
    }
}

From source file:com.flysoloing.learning.network.netty.http.websocketx.server.WebSocketFrameHandler.java

License:Apache License

@Override
protected void channelRead0(ChannelHandlerContext ctx, WebSocketFrame frame) throws Exception {
    // ping and pong frames already handled

    if (frame instanceof TextWebSocketFrame) {
        // Send the uppercase string back.
        String request = ((TextWebSocketFrame) frame).text();
        logger.info("{} received {}", ctx.channel(), request);
        ctx.channel().writeAndFlush(new TextWebSocketFrame(request.toUpperCase(Locale.US)));
    } else {/* www  .  java  2s .c om*/
        String message = "unsupported frame type: " + frame.getClass().getName();
        throw new UnsupportedOperationException(message);
    }
}