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.farsunset.cim.sdk.server.filter.ServerMessageDecoder.java

License:Apache License

private boolean tryWebsocketHandleHandshake(ChannelHandlerContext arg0, ByteBuf iobuffer, List<Object> queue) {

    iobuffer.markReaderIndex();//from  w  w  w  .  j ava 2s  .c  o  m

    byte[] data = new byte[iobuffer.readableBytes()];
    iobuffer.readBytes(data);

    String request = new String(data);
    String secKey = getSecWebSocketKey(request);
    boolean handShake = secKey != null && Objects.equals(getUpgradeProtocol(request), CIMSession.WEBSOCKET);
    if (handShake) {
        /**
         * ?????HANDSHAKE_FRAME,?session??websocket
         */
        arg0.channel().attr(AttributeKey.valueOf(CIMSession.PROTOCOL)).set(CIMSession.WEBSOCKET);

        SentBody body = new SentBody();
        body.setKey(CIMNioSocketAcceptor.WEBSOCKET_HANDLER_KEY);
        body.setTimestamp(System.currentTimeMillis());
        body.put("key", secKey);
        queue.add(body);

    } else {
        iobuffer.resetReaderIndex();
    }

    return handShake;
}

From source file:com.farsunset.cim.sdk.server.filter.ServerMessageEncoder.java

License:Apache License

@Override
protected void encode(final ChannelHandlerContext ctx, final Object object, ByteBuf out) throws Exception {
    Object protocol = ctx.channel().attr(AttributeKey.valueOf(CIMSession.PROTOCOL)).get();

    /**/*from w  ww.  j av a  2 s . c  o  m*/
     * websocket??
     */
    if (Objects.equals(CIMSession.WEBSOCKET, protocol) && object instanceof HandshakerResponse) {
        out.writeBytes(object.toString().getBytes());
        return;

    }

    /**
     * websocket? 
     */
    if (Objects.equals(CIMSession.WEBSOCKET, protocol) && object instanceof EncodeFormatable) {
        EncodeFormatable data = (EncodeFormatable) object;
        byte[] body = data.getProtobufBody();
        byte[] header = createHeader(data.getDataType(), body.length);

        byte[] protobuf = new byte[body.length + CIMConstant.DATA_HEADER_LENGTH];
        System.arraycopy(header, 0, protobuf, 0, header.length);
        System.arraycopy(body, 0, protobuf, header.length, body.length);

        byte[] binaryFrame = encodeDataFrame(protobuf);
        out.writeBytes(binaryFrame);

        return;
    }

    /**
     * ?websocket?Protobuf???
     */
    if (Objects.equals(CIMSession.NATIVEAPP, protocol) && object instanceof EncodeFormatable) {

        EncodeFormatable data = (EncodeFormatable) object;
        byte[] body = data.getProtobufBody();
        byte[] header = createHeader(data.getDataType(), body.length);

        out.writeBytes(header);
        out.writeBytes(body);

    }

}

From source file:com.farsunset.cim.sdk.server.handler.CIMNioSocketAcceptor.java

License:Apache License

@Override
protected void channelRead0(ChannelHandlerContext ctx, SentBody body) throws Exception {

    CIMSession session = new CIMSession(ctx.channel());

    CIMRequestHandler handler = innerHandlerMap.get(body.getKey());
    /**//from   ww w .  j av  a 2  s .  c  o  m
     * handler??
     */
    if (handler != null) {
        handler.process(session, body);
        return;
    }

    /**
     * ?sentbody
     */
    outerRequestHandler.process(session, body);
}

From source file:com.farsunset.cim.sdk.server.handler.CIMNioSocketAcceptor.java

License:Apache License

@Override
public void channelInactive(ChannelHandlerContext ctx) {
    channelGroup.remove(ctx.channel().id().asShortText());

    CIMSession session = new CIMSession(ctx.channel());
    SentBody body = new SentBody();
    body.setKey(CIMSESSION_CLOSED_HANDLER_KEY);
    outerRequestHandler.process(session, body);

}

From source file:com.farsunset.cim.sdk.server.handler.CIMNioSocketAcceptor.java

License:Apache License

@Override
public void channelActive(ChannelHandlerContext ctx) {
    channelGroup.put(ctx.channel().id().asShortText(), ctx.channel());
}

From source file:com.farsunset.cim.sdk.server.handler.CIMNioSocketAcceptor.java

License:Apache License

@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof IdleStateEvent && ((IdleStateEvent) evt).state().equals(IdleState.WRITER_IDLE)) {
        ctx.channel().attr(AttributeKey.valueOf(CIMConstant.HEARTBEAT_KEY)).set(System.currentTimeMillis());
        ctx.channel().writeAndFlush(HeartbeatRequest.getInstance());
    }// w w w .j a  va 2 s. c  o  m

    // ?30?
    if (evt instanceof IdleStateEvent && ((IdleStateEvent) evt).state().equals(IdleState.READER_IDLE)) {

        Long lastTime = (Long) ctx.channel().attr(AttributeKey.valueOf(CIMConstant.HEARTBEAT_KEY)).get();
        if (lastTime != null && System.currentTimeMillis() - lastTime >= PING_TIME_OUT) {
            ctx.channel().close();
        }

        ctx.channel().attr(AttributeKey.valueOf(CIMConstant.HEARTBEAT_KEY)).set(null);
    }
}

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

License:Apache License

@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {

    ///*from   ww  w.ja  v  a  2 s  . c om*/
    //ctx.attr(START_TIME).set(System.currentTimeMillis());
    mContext = ctx;
    Loger.print("channelRegistered! " + String.valueOf(ctx.channel().isOpen()));
}

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

License:Apache License

@Override
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
    Loger.print("channelUnregistered! " + String.valueOf(ctx.channel().isOpen()));
    ZXConfig.setLastRecvHBTime(0);//from   w w w  . j  a va 2s  .c om
}

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

License:Apache License

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    final Channel channel = ctx.channel();
    //if(channel.isOpen()){
    ZXConfig.setconnStatus(1);//from  w  ww . j  a  v a  2  s .  com

    //    
    Intent intent = new Intent();
    /*  Intentaction  */
    intent.setAction(SERVER_CONNECTION);
    /* Intent */
    intent.putExtra("status", "1");
    /*  */
    Activity currentActivity = ActivityManager.instance().getCurrentActivity();
    currentActivity.sendBroadcast(intent);

    Loger.print("channelActive! " + String.valueOf(ctx.channel().isOpen()));
    //}

    super.channelActive(ctx);
}

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

License:Apache License

@Override
public void channelInactive(final ChannelHandlerContext ctx) throws Exception {
    Channel channel = ctx.channel();
    //if(!channel.isOpen()) {
    ZXConfig.setconnStatus(0);//from   w w w  . j  av a 2 s. c  o m

    //      
    Intent intent = new Intent();
    /*  Intentaction  */
    intent.setAction(SERVER_CONNECTION);
    /* Intent */
    intent.putExtra("status", "0");
    /*  */
    Activity currentActivity = ActivityManager.instance().getCurrentActivity();
    currentActivity.sendBroadcast(intent);

    Loger.print("channelInactive! " + String.valueOf(ctx.channel().isOpen()));

    ctx.channel().close();

    /*       
    final EventLoop loop = ctx.channel().eventLoop();
    loop.schedule(new Runnable() {
        @Override
        public void run() {
           System.out.println("Reconnecting to: " + ctx.channel().remoteAddress());
            try {
          client.connect();
       } 
       catch (Exception e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
       }
        }
     }, 5, TimeUnit.SECONDS);
    }
    */

    super.channelInactive(ctx);
}