List of usage examples for io.netty.channel ChannelHandlerContext channel
Channel channel();
From source file:com.farsunset.cim.sdk.android.filter.CIMLoggingHandler.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { if (debug) {/*from w w w.j av a 2 s .c o m*/ Log.i(TAG, String.format("RECEIVED" + getSessionInfo(ctx.channel()) + "\n%s", msg.toString())); } ctx.fireChannelRead(msg); }
From source file:com.farsunset.cim.sdk.android.filter.CIMLoggingHandler.java
License:Apache License
@Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { if (debug) {//from w ww.ja v a2s. c om Log.i(TAG, String.format("SENT" + getSessionInfo(ctx.channel()) + "\n%s", msg.toString())); } ctx.write(msg, promise); }
From source file:com.farsunset.cim.sdk.client.CIMConnectorManager.java
License:Apache License
@Override public void channelActive(ChannelHandlerContext ctx) throws Exception { logger.info("****************CIM??:" + ctx.channel().localAddress() + " NID:" + ctx.channel().id().asShortText()); setLastHeartbeatTime(ctx.channel()); Intent intent = new Intent(); intent.setAction(CIMConstant.IntentAction.ACTION_CONNECTION_SUCCESSED); sendBroadcast(intent);/*w w w . j a v a 2 s . co m*/ }
From source file:com.farsunset.cim.sdk.client.CIMConnectorManager.java
License:Apache License
@Override public void channelInactive(ChannelHandlerContext ctx) { logger.error("****************CIM?:" + ctx.channel().localAddress() + " NID:" + ctx.channel().id().asShortText()); Intent intent = new Intent(); intent.setAction(CIMConstant.IntentAction.ACTION_CONNECTION_CLOSED); sendBroadcast(intent);/*from w ww . ja va 2 s. c om*/ }
From source file:com.farsunset.cim.sdk.client.CIMConnectorManager.java
License:Apache License
@Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) { /**// w ww. j av a 2 s . c o m * wifi??? ??? * */ if (evt instanceof IdleStateEvent && ((IdleStateEvent) evt).state().equals(IdleState.READER_IDLE)) { logger.debug("****************CIM " + IdleState.READER_IDLE + ":" + ctx.channel().localAddress() + " NID:" + ctx.channel().id().asShortText()); Long lastTime = getLastHeartbeatTime(ctx.channel()); if (lastTime != null && System.currentTimeMillis() - lastTime > HEARBEAT_TIME_OUT) { channel.close(); logger.error("****************CIM ,??......" + " NID:" + ctx.channel().id().asShortText()); } } }
From source file:com.farsunset.cim.sdk.client.CIMConnectorManager.java
License:Apache License
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { logger.error("****************CIM:" + ctx.channel().localAddress() + " NID:" + ctx.channel().id().asShortText()); if (cause != null && cause.getMessage() != null) { logger.error(cause.getMessage()); }//from w w w . ja v a2 s. c o m Intent intent = new Intent(); intent.setAction(CIMConstant.IntentAction.ACTION_UNCAUGHT_EXCEPTION); intent.putExtra(Exception.class.getName(), cause); sendBroadcast(intent); }
From source file:com.farsunset.cim.sdk.client.CIMConnectorManager.java
License:Apache License
@Override public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception { if (msg instanceof Message) { Intent intent = new Intent(); intent.setAction(CIMConstant.IntentAction.ACTION_MESSAGE_RECEIVED); intent.putExtra(Message.class.getName(), (Message) msg); sendBroadcast(intent);/*from w w w . j av a 2 s . co m*/ } if (msg instanceof ReplyBody) { Intent intent = new Intent(); intent.setAction(CIMConstant.IntentAction.ACTION_REPLY_RECEIVED); intent.putExtra(ReplyBody.class.getName(), (ReplyBody) msg); sendBroadcast(intent); } // ???? if (msg instanceof HeartbeatRequest) { ctx.writeAndFlush(HeartbeatResponse.getInstance()); setLastHeartbeatTime(ctx.channel()); } }
From source file:com.farsunset.cim.sdk.server.filter.decoder.AppMessageDecoder.java
License:Apache License
@Override public void decode(ChannelHandlerContext arg0, ByteBuf buffer, List<Object> queue) throws Exception { /**//from w w w . j a va 2 s . c om * ?3? */ if (buffer.readableBytes() < CIMConstant.DATA_HEADER_LENGTH) { return; } buffer.markReaderIndex(); byte conetnType = buffer.readByte(); byte lv = buffer.readByte();// int ? byte hv = buffer.readByte();// int ? int conetnLength = getContentLength(lv, hv); // ????? if (conetnLength <= buffer.readableBytes()) { byte[] dataBytes = new byte[conetnLength]; buffer.readBytes(dataBytes); Object message = mappingMessageObject(dataBytes, conetnType); if (message != null) { arg0.channel().attr(AttributeKey.valueOf(CIMSession.PROTOCOL)).set(CIMSession.NATIVEAPP); queue.add(message); return; } } buffer.resetReaderIndex(); }
From source file:com.farsunset.cim.sdk.server.filter.decoder.WebMessageDecoder.java
License:Apache License
private void handleClose(ChannelHandlerContext arg0) { arg0.channel().close(); }
From source file:com.farsunset.cim.sdk.server.filter.ServerMessageDecoder.java
License:Apache License
@Override protected void decode(ChannelHandlerContext arg0, ByteBuf buffer, List<Object> queue) throws Exception { Object protocol = arg0.channel().attr(AttributeKey.valueOf(CIMSession.PROTOCOL)).get(); if (Objects.equals(CIMSession.WEBSOCKET, protocol)) { webMessageDecoder.decode(arg0, buffer, queue); return;//from w w w . j av a 2s . c om } if (Objects.equals(CIMSession.NATIVEAPP, protocol)) { appMessageDecoder.decode(arg0, buffer, queue); return; } boolean handshake = tryWebsocketHandleHandshake(arg0, buffer, queue); if (!handshake) { appMessageDecoder.decode(arg0, buffer, queue); } }