/** * Internal method to test if the current channel is blocked. Should not be overridden. * * @return True if the current channel is blocked, else False */ protected boolean isBlocked(ChannelHandlerContext ctx) { return ctx.getAttachment() != null; }
static WebSocketServerHandshaker getHandshaker(ChannelHandlerContext ctx) { return (WebSocketServerHandshaker) ctx.getAttachment(); }
@Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { State state = (State) ctx.getAttachment(); state.lastReadTime = System.currentTimeMillis(); ctx.sendUpstream(e); }
@Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { State state = (State) ctx.getAttachment(); state.lastReadTime = System.currentTimeMillis(); ctx.sendUpstream(e); }
@Override public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { if (LOG.isTraceEnabled()) { LOG.trace("Channel disconnected " + e); } NettyServerCnxn cnxn = (NettyServerCnxn) ctx.getAttachment(); if (cnxn != null) { if (LOG.isTraceEnabled()) { LOG.trace("Channel disconnect caused close " + e); } cnxn.close(); } }
@Override public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception { LOG.warn("Exception caught " + e, e.getCause()); NettyServerCnxn cnxn = (NettyServerCnxn) ctx.getAttachment(); if (cnxn != null) { if (LOG.isDebugEnabled()) { LOG.debug("Closing " + cnxn); } cnxn.close(); } }
@Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { if (LOG.isTraceEnabled()) { LOG.trace("message received called " + e.getMessage()); } try { if (LOG.isDebugEnabled()) { LOG.debug("New message " + e.toString() + " from " + ctx.getChannel()); } NettyServerCnxn cnxn = (NettyServerCnxn)ctx.getAttachment(); synchronized(cnxn) { processMessage(e, cnxn); } } catch(Exception ex) { LOG.error("Unexpected exception in receive", ex); throw ex; } }
@Override public void writeComplete(ChannelHandlerContext ctx, WriteCompletionEvent e) throws Exception { if (e.getWrittenAmount() > 0) { State state = (State) ctx.getAttachment(); state.lastWriteTime = System.currentTimeMillis(); } ctx.sendUpstream(e); }
static ReadWriteStatus checkAttachment(ChannelHandlerContext ctx) { ReadWriteStatus rws = (ReadWriteStatus) ctx.getAttachment(); if (rws == null) { rws = new ReadWriteStatus(); ctx.setAttachment(rws); } return rws; }
private static State state(ChannelHandlerContext ctx) { State state; synchronized (ctx) { // FIXME: It could have been better if there is setAttachmentIfAbsent(). state = (State) ctx.getAttachment(); if (state != null) { return state; } state = new State(); ctx.setAttachment(state); } return state; }
@Override public Unmarshaller getUnmarshaller(ChannelHandlerContext ctx) throws Exception { Unmarshaller unmarshaller = (Unmarshaller) ctx.getAttachment(); if (unmarshaller == null) { unmarshaller = super.getUnmarshaller(ctx); ctx.setAttachment(unmarshaller); } return unmarshaller; }
private static State state(ChannelHandlerContext ctx) { State state; synchronized (ctx) { // FIXME: It could have been better if there is setAttachmentIfAbsent(). state = (State) ctx.getAttachment(); if (state != null) { return state; } state = new State(); ctx.setAttachment(state); } return state; }
public void run(Timeout timeout) throws Exception { if (timeout.isCancelled() || !ctx.getChannel().isOpen()) { return; } State state = (State) ctx.getAttachment(); long currentTime = System.currentTimeMillis(); long lastIoTime = Math.max(state.lastReadTime, state.lastWriteTime); long nextDelay = allIdleTimeMillis - (currentTime - lastIoTime); if (nextDelay <= 0) { // Both reader and writer are idle - set a new timeout and // notify the callback. state.allIdleTimeout = timer.newTimeout(this, allIdleTimeMillis, TimeUnit.MILLISECONDS); fireChannelIdle(ctx, IdleState.ALL_IDLE, lastIoTime); } else { // Either read or write occurred before the timeout - set a new // timeout with shorter delay. state.allIdleTimeout = timer.newTimeout(this, nextDelay, TimeUnit.MILLISECONDS); } } }
/** * Handle suspended reads */ protected boolean handleReadSuspend(ChannelHandlerContext ctx, ChannelEvent e) { if (e instanceof ChannelStateEvent) { ChannelStateEvent cse = (ChannelStateEvent) e; if (cse.getState() == ChannelState.INTEREST_OPS && (((Integer) cse.getValue()).intValue() & Channel.OP_READ) != 0) { // setReadable(true) requested boolean readSuspended = ctx.getAttachment() != null; if (readSuspended) { // Drop the request silently if MemoryAwareThreadPool has // set the flag. e.getFuture().setSuccess(); return true; } } } return false; } }
public void run(Timeout timeout) throws Exception { if (timeout.isCancelled() || !ctx.getChannel().isOpen()) { return; } State state = (State) ctx.getAttachment(); long currentTime = System.currentTimeMillis(); long lastReadTime = state.lastReadTime; long nextDelay = readerIdleTimeMillis - (currentTime - lastReadTime); if (nextDelay <= 0) { // Reader is idle - set a new timeout and notify the callback. state.readerIdleTimeout = timer.newTimeout(this, readerIdleTimeMillis, TimeUnit.MILLISECONDS); fireChannelIdle(ctx, IdleState.READER_IDLE, lastReadTime); } else { // Read occurred before the timeout - set a new timeout with shorter delay. state.readerIdleTimeout = timer.newTimeout(this, nextDelay, TimeUnit.MILLISECONDS); } } }
public void run(Timeout timeout) throws Exception { if (timeout.isCancelled() || !ctx.getChannel().isOpen()) { return; } State state = (State) ctx.getAttachment(); long currentTime = System.currentTimeMillis(); long lastWriteTime = state.lastWriteTime; long nextDelay = writerIdleTimeMillis - (currentTime - lastWriteTime); if (nextDelay <= 0) { // Writer is idle - set a new timeout and notify the callback. state.writerIdleTimeout = timer.newTimeout(this, writerIdleTimeMillis, TimeUnit.MILLISECONDS); fireChannelIdle(ctx, IdleState.WRITER_IDLE, lastWriteTime); } else { // Write occurred before the timeout - set a new timeout with shorter delay. state.writerIdleTimeout = timer.newTimeout(this, nextDelay, TimeUnit.MILLISECONDS); } } }
public void run(Timeout timeout) throws Exception { if (timeout.isCancelled()) { return; } if (!ctx.getChannel().isOpen()) { return; } State state = (State) ctx.getAttachment(); long currentTime = System.currentTimeMillis(); long nextDelay = timeoutMillis - (currentTime - state.lastReadTime); if (nextDelay <= 0) { // Read timed out - set a new timeout and notify the callback. state.timeout = timer.newTimeout(this, timeoutMillis, TimeUnit.MILLISECONDS); fireReadTimedOut(ctx); } else { // Read occurred before the timeout - set a new timeout with shorter delay. state.timeout = timer.newTimeout(this, nextDelay, TimeUnit.MILLISECONDS); } }
if (ctx.getAttachment() != null) {
@Override public void messageReceived(final ChannelHandlerContext ctx, MessageEvent messageEvent) throws Exception { if (messageEvent.getMessage() instanceof HttpRequest && ctx.getAttachment() != IGNORE_REQUEST) { handleHttpRequest(ctx, messageEvent, (HttpRequest) messageEvent.getMessage()); } else { super.messageReceived(ctx, messageEvent); } }
public static Closeable from(Protocol protocol, ChannelHandlerContext ctx) { return MDCBuilder.create() .addContext(from(ctx.getAttachment())) .addContext(MDCBuilder.PROTOCOL, protocol.getName()) .addContext(MDCBuilder.IP, retrieveIp(ctx)) .addContext(MDCBuilder.HOST, retrieveHost(ctx)) .build(); }