/** * Increase the number of bytes read on the data connection * @param increment The number of bytes written */ public void increaseReadDataBytes(int increment) { if (wrappedSession instanceof AbstractIoSession) { ((AbstractIoSession) wrappedSession).increaseReadBytes(increment, System.currentTimeMillis()); } }
/** * Increase the number of bytes written on the data connection * @param increment The number of bytes written */ public void increaseWrittenDataBytes(int increment) { if (wrappedSession instanceof AbstractIoSession) { ((AbstractIoSession) wrappedSession).increaseScheduledWriteBytes(increment); ((AbstractIoSession) wrappedSession).increaseWrittenBytes( increment, System.currentTimeMillis()); } }
/** * {@inheritDoc} */ public final CloseFuture close(boolean rightNow) { if (rightNow) { return closeNow(); } else { return closeOnFlush(); } }
/** * {@inheritDoc} */ public final CloseFuture close(boolean rightNow) { if (rightNow) { return close(); } return closeOnFlush(); }
@SuppressWarnings("unchecked") @Override public void filterWrite(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception { AbstractIoSession s = (AbstractIoSession) session; // Maintain counters. if (writeRequest.getMessage() instanceof IoBuffer) { IoBuffer buffer = (IoBuffer) writeRequest.getMessage(); // buffer.mark() call is now done in DefaultIoSessionDataStructureFactory$DefaultWriteRequestQueue.poll(). int remaining = buffer.remaining(); if (remaining == 0) { // Zero-sized buffer means the internal message // delimiter. s.increaseScheduledWriteMessages(); } else { s.increaseScheduledWriteBytes(remaining); } } else { s.increaseScheduledWriteMessages(); } s.getWriteRequestQueue().offer(s, writeRequest); if (!s.isWriteSuspended()) { s.getProcessor().flush(s); } }
@Override public void sessionClosed(NextFilter nextFilter, IoSession session) throws Exception { AbstractIoSession s = (AbstractIoSession) session; try { s.getHandler().sessionClosed(session); } finally { try { s.getWriteRequestQueue().dispose(session); } finally { try { s.getAttributeMap().dispose(session); } finally { try { // Remove all filters. session.getFilterChain().clear(); } finally { if (s.getConfig().isUseReadOperation()) { s.offerClosedReadFuture(); } } } } } }
private void read(S session) { IoSessionConfig config = session.getConfig(); int bufferSize = config.getReadBufferSize(); IoBuffer buf = IoBuffer.allocate(bufferSize); final boolean hasFragmentation = session.getTransportMetadata().hasFragmentation(); IoFilterChain filterChain = session.getFilterChain(); filterChain.fireMessageReceived(buf); buf = null; session.decreaseReadBufferSize(); } else if (readBytes == config.getReadBufferSize()) { session.increaseReadBufferSize(); IoFilterChain filterChain = session.getFilterChain(); filterChain.fireInputClosed(); IoFilterChain filterChain = session.getFilterChain(); filterChain.fireExceptionCaught(e);
final WriteRequestQueue writeRequestQueue = session.getWriteRequestQueue(); final int maxWrittenBytes = session.getConfig().getMaxReadBufferSize() + (session.getConfig().getMaxReadBufferSize() >>> 1); WriteRequest req = session.getCurrentWriteRequest(); if (req == null) { req = writeRequestQueue.poll(session); break; session.setCurrentWriteRequest(req); if (buf.remaining() == 0) { session.setCurrentWriteRequest(null); buf.reset(); session.getFilterChain().fireMessageSent(req); continue; destination = session.getRemoteAddress(); session.setCurrentWriteRequest(null); writtenBytes += localWrittenBytes; buf.reset(); session.getFilterChain().fireMessageSent(req); session.increaseWrittenBytes(writtenBytes, currentTime);
private boolean flushNow(T session, long currentTime) { if (!session.isConnected()) { scheduleRemove(session); return false; final boolean hasFragmentation = session.getTransportMetadata() .hasFragmentation(); .getWriteRequestQueue(); final int maxWrittenBytes = session.getConfig().getMaxReadBufferSize() + (session.getConfig().getMaxReadBufferSize() >>> 1); int writtenBytes = 0; WriteRequest req = null; do { req = session.getCurrentWriteRequest(); if (req == null) { req = writeRequestQueue.poll(session); session.setCurrentWriteRequest(req); req.getFuture().setException(e); IoFilterChain filterChain = session.getFilterChain(); filterChain.fireExceptionCaught(e); return false;
/** * @return a queue of ReadFuture */ private Queue<ReadFuture> getReadyReadFutures() { Queue<ReadFuture> readyReadFutures = (Queue<ReadFuture>) getAttribute(READY_READ_FUTURES_KEY); if (readyReadFutures == null) { readyReadFutures = new ConcurrentLinkedQueue<>(); Queue<ReadFuture> oldReadyReadFutures = (Queue<ReadFuture>) setAttributeIfAbsent(READY_READ_FUTURES_KEY, readyReadFutures); if (oldReadyReadFutures != null) { readyReadFutures = oldReadyReadFutures; } } return readyReadFutures; }
@Override public void exceptionCaught(NextFilter nextFilter, IoSession session, Throwable cause) throws Exception { AbstractIoSession s = (AbstractIoSession) session; try { s.getHandler().exceptionCaught(s, cause); } finally { if (s.getConfig().isUseReadOperation()) { s.offerFailedReadFuture(cause); } } }
@Override public void messageReceived(NextFilter nextFilter, IoSession session, Object message) throws Exception { AbstractIoSession s = (AbstractIoSession) session; if (!(message instanceof IoBuffer)) { s.increaseReadMessages(System.currentTimeMillis()); } else if (!((IoBuffer) message).hasRemaining()) { s.increaseReadMessages(System.currentTimeMillis()); } try { session.getHandler().messageReceived(s, message); } finally { if (s.getConfig().isUseReadOperation()) { s.offerReadFuture(message); } } }
/** * {@inheritDoc} */ public final ReadFuture read() { if (!getConfig().isUseReadOperation()) { throw new IllegalStateException("useReadOperation is not enabled."); } Queue<ReadFuture> readyReadFutures = getReadyReadFutures(); ReadFuture future; synchronized (readyReadFutures) { future = readyReadFutures.poll(); if (future != null) { if (future.isClosed()) { // Let other readers get notified. readyReadFutures.offer(future); } } else { future = new DefaultReadFuture(this); getWaitingReadFutures().offer(future); } } return future; }
/** * Find the processor associated to a session. If it hasen't be stored into * the session's attributes, pick a new processor and stores it. */ @SuppressWarnings("unchecked") private IoProcessor<S> getProcessor(S session) { IoProcessor<S> processor = (IoProcessor<S>) session.getAttribute(PROCESSOR); if (processor == null) { if (disposed || disposing) { throw new IllegalStateException("A disposed processor cannot be accessed."); } processor = pool[Math.abs((int) session.getId()) % pool.length]; if (processor == null) { throw new IllegalStateException("A disposed processor cannot be accessed."); } session.setAttributeIfAbsent(PROCESSOR, processor); } return processor; } }
/** * Decrease the counters of written messages and written bytes when a message has been written * * @param request The written message */ public final void decreaseScheduledBytesAndMessages(WriteRequest request) { Object message = request.getMessage(); if (message instanceof IoBuffer) { IoBuffer b = (IoBuffer) message; if (b.hasRemaining()) { increaseScheduledWriteBytes(-((IoBuffer) message).remaining()); } else { decreaseScheduledWriteMessages(); } } else { decreaseScheduledWriteMessages(); } }
/** * TODO Add method documentation */ public final void increaseWrittenBytes(int increment, long currentTime) { if (increment <= 0) { return; } writtenBytes += increment; lastWriteTime = currentTime; idleCountForBoth.set(0); idleCountForWrite.set(0); if (getService() instanceof AbstractIoService) { getService().getStatistics().increaseWrittenBytes(increment, currentTime); } increaseScheduledWriteBytes(-increment); }
/** * Increase the ReadBuffer size (it will double) */ public final void increaseReadBufferSize() { int newReadBufferSize = getConfig().getReadBufferSize() << 1; if (newReadBufferSize <= getConfig().getMaxReadBufferSize()) { getConfig().setReadBufferSize(newReadBufferSize); } else { getConfig().setReadBufferSize(getConfig().getMaxReadBufferSize()); } deferDecreaseReadBuffer = true; }
/** * Increase the number of written messages * * @param request The written message * @param currentTime The current tile */ public final void increaseWrittenMessages(WriteRequest request, long currentTime) { Object message = request.getMessage(); if (message instanceof IoBuffer) { IoBuffer b = (IoBuffer) message; if (b.hasRemaining()) { return; } } writtenMessages++; lastWriteTime = currentTime; if (getService() instanceof AbstractIoService) { ((AbstractIoService) getService()).getStatistics().increaseWrittenMessages(currentTime); } decreaseScheduledWriteMessages(); }
void increaseWrittenBytes0(int increment, long currentTime) { super.increaseWrittenBytes(increment, currentTime); }
/** * {@inheritDoc} */ public final Object getAttribute(Object key) { return getAttribute(key, null); }