List of usage examples for io.netty.channel ChannelHandlerContext attr
@Deprecated @Override <T> Attribute<T> attr(AttributeKey<T> key);
From source file:com.zhucode.longio.transport.netty.AbstractNettyHandler.java
License:Open Source License
@Override public void channelActive(ChannelHandlerContext ctx) throws Exception { ctx.attr(handlerKey).set(this); this.sessionId = this.getNettyConnector().registHandlerContext(ctx); }
From source file:com.zhucode.longio.transport.netty.HttpClientHandler.java
License:Open Source License
@Override public void channelRegistered(ChannelHandlerContext ctx) throws Exception { super.channelRegistered(ctx); ctx.attr(handlerKey).set(this); this.sessionId = this.getNettyConnector().registHandlerContext(ctx); }
From source file:com.zhucode.longio.transport.netty.HttpHandler.java
License:Open Source License
private void handleWebSocketFrame(ChannelHandlerContext ctx, WebSocketFrame frame) throws ProtocolException { // Check for closing frame if (frame instanceof CloseWebSocketFrame) { AttributeKey<WebSocketServerHandshaker> key = AttributeKey.valueOf("WebSocketServerHandshaker"); WebSocketServerHandshaker handshaker = ctx.attr(key).get(); handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame.retain()); return;//from w w w .ja v a2 s . c om } if (frame instanceof PingWebSocketFrame) { ctx.channel().write(new PongWebSocketFrame(frame.content().retain())); return; } if (!(frame instanceof TextWebSocketFrame)) { throw new UnsupportedOperationException( String.format("%s frame types not supported", frame.getClass().getName())); } ByteBuf buf = ((TextWebSocketFrame) frame).content(); process(ctx, buf); }
From source file:com.zhucode.longio.transport.netty.HttpHandler.java
License:Open Source License
private void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest req) throws ProtocolException { HttpHeaders headers = req.headers(); AttributeKey<Boolean> isWs = AttributeKey.valueOf("isWs"); String upgrade = headers.get("Upgrade"); String connection = headers.get("Connection"); if (upgrade != null && upgrade.equalsIgnoreCase("websocket") && connection != null && connection.equalsIgnoreCase("Upgrade")) { // Handshake WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory( getWebSocketLocation(req), null, true); WebSocketServerHandshaker handshaker = wsFactory.newHandshaker(req); if (handshaker == null) { WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel()); } else {//w ww.j a v a 2 s .c o m handshaker.handshake(ctx.channel(), req); AttributeKey<WebSocketServerHandshaker> key = AttributeKey.valueOf("WebSocketServerHandshaker"); ctx.attr(key).set(handshaker); } ctx.attr(isWs).set(true); } else { ctx.attr(isWs).set(false); if (HttpHeaders.is100ContinueExpected(req)) { ctx.write(new DefaultFullHttpResponse(HTTP_1_1, CONTINUE)); } boolean ka = HttpHeaders.isKeepAlive(req); ctx.attr(keepAlive).set(ka); ByteBuf buf = req.content(); process(ctx, buf); } }
From source file:com.zhucode.longio.transport.netty.HttpHandler.java
License:Open Source License
@Override public ChannelFuture sendMessage(ChannelHandlerContext ctx, MessageBlock mb) { byte[] bytes = null; try {//from w ww . j a v a2s. co m bytes = pp.encode(mb); } catch (ProtocolException e) { e.printStackTrace(); } AttributeKey<Boolean> isWs = AttributeKey.valueOf("isWs"); if (ctx.attr(isWs).get()) { return sendForWebSocket(ctx, bytes); } else { return sendForHttp(ctx, bytes); } }
From source file:com.zhucode.longio.transport.netty.HttpHandler.java
License:Open Source License
private ChannelFuture sendForHttp(ChannelHandlerContext ctx, byte[] bytes) { FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(bytes)); response.headers().set(CONTENT_TYPE, "text/json; charset=utf-8"); response.headers().set(CONTENT_LENGTH, response.content().readableBytes()); boolean ka = ctx.attr(keepAlive).get(); if (!ka) {/*from w w w .j ava 2 s .c om*/ return ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); } else { response.headers().set(CONNECTION, Values.KEEP_ALIVE); return ctx.writeAndFlush(response); } }
From source file:com.zhucode.longio.transport.netty.NettyConnector.java
License:Open Source License
protected ChannelFuture send(MessageBlock message) { long sid = message.getSessionId(); ChannelHandlerContext ctx = ctxs.get(sid); AttributeKey<AbstractNettyHandler> handlerKey = AttributeKey.valueOf("AbstractNettyHandler"); AbstractNettyHandler handler = ctx.attr(handlerKey).get(); ChannelFuture future = handler.sendMessage(ctx, message); return future; }
From source file:com.zhucode.longio.transport.netty.RawSocketClientHandler.java
License:Open Source License
@Override public void channelRegistered(ChannelHandlerContext ctx) throws Exception { ctx.attr(handlerKey).set(this); this.sessionId = this.getNettyConnector().registHandlerContext(ctx); }
From source file:de.ruedigermoeller.reallive.client.wsgate.WebSocketGateChannelHandler.java
License:Open Source License
/** * Gets called after the {@link io.netty.channel.ChannelHandler} was added to the actual context and it's ready to handle events. * * @param ctx/*from w w w . ja v a 2 s .c om*/ */ @Override public void handlerAdded(ChannelHandlerContext ctx) throws Exception { System.out.println("Handler added " + ctx); ctx.attr(webSocketGate.session).set(webSocketGate.createEmptySession()); super.handlerAdded(ctx); }
From source file:de.ruedigermoeller.reallive.client.wsgate.WebSocketGateChannelHandler.java
License:Open Source License
/** * Gets called after the {@link io.netty.channel.ChannelHandler} was removed from the actual context and it doesn't handle events * anymore./*from w ww . j a v a 2s. c om*/ * * @param ctx */ @Override public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { System.out.println("Handler removed " + ctx); ctx.attr(webSocketGate.session).set(null); super.handlerRemoved(ctx); }