List of usage examples for io.netty.channel ChannelHandlerContext attr
@Deprecated @Override <T> Attribute<T> attr(AttributeKey<T> key);
From source file:at.yawk.dbus.protocol.codec.Local.java
/** * Generate a serial for the given channel. */// ww w. j av a 2s.c o m static int generateSerial(ChannelHandlerContext ctx) { int serial; Attribute<Integer> attr = ctx.attr(LAST_SERIAL); Integer lastSerial; do { lastSerial = attr.get(); serial = (lastSerial == null ? 0 : lastSerial) + 1; if (serial == 0) { serial = 1; } } while (!attr.compareAndSet(lastSerial, serial)); return serial; }
From source file:buildcraft.core.lib.network.ChannelHandler.java
License:Minecraft Mod Public
@Override public void handlerAdded(ChannelHandlerContext ctx) throws Exception { super.handlerAdded(ctx); ctx.attr(INBOUNDPACKETTRACKER).set(new ThreadLocal<WeakReference<FMLProxyPacket>>()); }
From source file:buildcraft.core.lib.network.ChannelHandler.java
License:Minecraft Mod Public
@Override protected void encode(ChannelHandlerContext ctx, Packet msg, List<Object> out) throws Exception { ByteBuf buffer = Unpooled.buffer();//from www . j a v a 2 s . c om Class<? extends Packet> clazz = msg.getClass(); byte discriminator = types.get(clazz); buffer.writeByte(discriminator); msg.writeData(buffer); FMLProxyPacket proxy = new FMLProxyPacket(buffer.copy(), ctx.channel().attr(NetworkRegistry.FML_CHANNEL).get()); WeakReference<FMLProxyPacket> ref = ctx.attr(INBOUNDPACKETTRACKER).get().get(); FMLProxyPacket old = ref == null ? null : ref.get(); if (old != null) { proxy.setDispatcher(old.getDispatcher()); } out.add(proxy); }
From source file:buildcraft.core.lib.network.ChannelHandler.java
License:Minecraft Mod Public
@Override protected void decode(ChannelHandlerContext ctx, FMLProxyPacket msg, List<Object> out) throws Exception { testMessageValidity(msg);//from w w w. ja v a2 s . c o m ByteBuf payload = msg.payload(); byte discriminator = payload.readByte(); Class<? extends Packet> clazz = discriminators.get(discriminator); if (clazz == null) { throw new NullPointerException( "Undefined message for discriminator " + discriminator + " in channel " + msg.channel()); } Packet newMsg = clazz.newInstance(); ctx.attr(INBOUNDPACKETTRACKER).get().set(new WeakReference<FMLProxyPacket>(msg)); newMsg.readData(payload.slice()); out.add(newMsg); }
From source file:cn.wantedonline.puppy.httpserver.component.BasePageDispatcher.java
License:Apache License
public ContextAttachment getAttach(ChannelHandlerContext ctx) { ctx.attr(HTTP_ATTACH_KEY).setIfAbsent(new ContextAttachment(ctx)); return ctx.attr(HTTP_ATTACH_KEY).get(); }
From source file:com.barchart.http.server.HttpRequestChannelHandler.java
License:BSD License
@Override public void channelRead0(final ChannelHandlerContext ctx, final FullHttpRequest msg) throws Exception { final RequestHandlerMapping mapping = config.getRequestMapping(msg.getUri()); String relativePath = msg.getUri(); if (mapping != null) { relativePath = relativePath.substring(mapping.path().length()); }/*from w w w . j a va 2 s . c om*/ // Create request/response final PooledServerRequest request = messagePool.getRequest(); // Handle 503 - sanity check, should be caught in acceptor if (request == null) { sendServerError(ctx, new ServerTooBusyException("Maximum concurrent connections reached")); return; } request.init(ctx.channel(), msg, relativePath); final RequestHandler handler = mapping == null ? null : mapping.handler(request); final PooledServerResponse response = messagePool.getResponse(); response.init(ctx, this, handler, request, config.logger()); if (mapping == null) { // No handler found, 404 response.setStatus(HttpResponseStatus.NOT_FOUND); } // Store in ChannelHandlerContext for future reference ctx.attr(ATTR_RESPONSE).set(response); try { // MJS: Dispatch an error if not found or authorized if (response.getStatus() == HttpResponseStatus.UNAUTHORIZED || response.getStatus() == HttpResponseStatus.NOT_FOUND) { config.errorHandler().onError(request, response, null); } else { handler.onRequest(request, response); } } catch (final Throwable t) { // Catch server errors response.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR); try { config.errorHandler().onError(request, response, t); } catch (final Throwable t2) { response.write(t.getClass() + " was thrown while processing this request. Additionally, " + t2.getClass() + " was thrown while handling this exception."); } config.logger().error(request, response, t); // Force request to end on exception, async handlers cannot allow // unchecked exceptions and still expect to return data if (!response.isFinished()) { response.finish(); } } finally { // If handler did not request async response, finish request if (!response.isFinished() && !response.isSuspended()) { response.finish(); } } }
From source file:com.barchart.http.server.HttpRequestChannelHandler.java
License:BSD License
@Override public void channelInactive(final ChannelHandlerContext ctx) { final PooledServerResponse response = ctx.attr(ATTR_RESPONSE).get(); if (response != null) { try {/*from w w w.java 2 s .co m*/ if (!response.isFinished()) { response.close(); final RequestHandler handler = response.handler(); if (handler != null) { handler.onAbort(response.request(), response); } } } finally { freeHandlers(ctx); } } }
From source file:com.barchart.http.server.HttpRequestChannelHandler.java
License:BSD License
@Override public void exceptionCaught(final ChannelHandlerContext ctx, final Throwable exception) throws Exception { final PooledServerResponse response = ctx.attr(ATTR_RESPONSE).get(); if (response != null) { try {//from ww w .jav a2 s.c o m try { if (!response.isFinished()) { response.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR); config.errorHandler().onError(response.request(), response, exception); response.close(); final RequestHandler handler = response.handler(); if (handler != null) { handler.onException(response.request(), response, exception); } } } finally { config.logger().error(response.request(), response, exception); } } finally { freeHandlers(ctx); } } }
From source file:com.barchart.http.server.HttpRequestChannelHandler.java
License:BSD License
/** * Free any request/response handlers related to the current channel handler * context.//from w w w . j a va 2s . c o m */ public void freeHandlers(final ChannelHandlerContext ctx) { final PooledServerResponse response = ctx.attr(ATTR_RESPONSE).getAndRemove(); if (response != null) { try { final RequestHandler handler = response.handler(); if (handler != null) { handler.onComplete(response.request(), response); } } finally { response.request().release(); messagePool.makeAvailable(response.request()); response.close(); messagePool.makeAvailable(response); } } }
From source file:com.barchart.netty.server.http.pipeline.HttpRequestChannelHandler.java
License:BSD License
private void handleRequest(final ChannelHandlerContext ctx, final FullHttpRequest msg) throws Exception { // Find request handler final HandlerMapping<RequestHandler> mapping = server.getRequestMapping(msg.getUri()); String relativePath = msg.getUri(); if (mapping != null) { relativePath = relativePath.substring(mapping.path().length()); }//from www .j ava 2s .com // Get an initialized request object from the pool final PooledHttpServerRequest request = requestPool.provision(ctx, msg, relativePath, this); // Handle 503 - sanity check, should be caught in acceptor if (request == null) { sendServerError(ctx, new ServerTooBusyException("Maximum concurrent connections reached")); return; } final RequestHandler handler = mapping == null ? null : mapping.factory().newHandler(); // Store in ChannelHandlerContext for future reference ctx.attr(ATTR_REQUEST).set(request); try { if (handler == null) { request.response().setStatus(HttpResponseStatus.NOT_FOUND); server.errorHandler().onError(request, null); } else { handler.handle(request); } } catch (final IOException i) { // Don't log IOExceptions (mostly client disconnects) sendError(request, i); } catch (final Throwable t) { // Log unchecked exceptions from handlers server.logger().error(request, t); sendError(request, t); } }