List of usage examples for io.netty.util AttributeKey valueOf
@SuppressWarnings("unchecked") public static <T> AttributeKey<T> valueOf(String name)
From source file:com.dinstone.rpc.netty.client.SessionUtil.java
License:Apache License
public static Connection getConnection(Channel session) { return (Connection) session.attr(AttributeKey.valueOf(Connection.class.getName())).get(); }
From source file:com.farsunset.cim.sdk.android.CIMConnectorManager.java
License:Apache License
private void setLastHeartbeatTime(Channel channel) { channel.attr(AttributeKey.valueOf(KEY_LAST_HEART_TIME)).set(System.currentTimeMillis()); }
From source file:com.farsunset.cim.sdk.android.CIMConnectorManager.java
License:Apache License
private Long getLastHeartbeatTime(Channel channel) { return (Long) channel.attr(AttributeKey.valueOf(KEY_LAST_HEART_TIME)).get(); }
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 { /**// ww w. ja v a2s . co m * ?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.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;/*w w w. ja v a 2 s. co m*/ } 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); } }
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 www . j av a 2 s. com 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(); /**// ww w .ja v a 2 s . co 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 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 . java 2s . 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.farsunset.cim.sdk.server.session.CIMSession.java
License:Apache License
public void setAttribute(String key, Object value) { if (session != null) session.attr(AttributeKey.valueOf(key)).set(value); }
From source file:com.farsunset.cim.sdk.server.session.CIMSession.java
License:Apache License
public boolean containsAttribute(String key) { if (session != null) return session.hasAttr(AttributeKey.valueOf(key)); return false; }