/** * Attach this filter to the specified filter chain. It will search for the ThreadPoolFilter, and attach itself * before and after that filter. * * @param chain {@link IoFilterChain} to attach self to. */ public void attach(IoFilterChain chain) { String name = getThreadPoolFilterEntryName(chain.getAll()); chain.addBefore(name, getClass().getName() + ".add", new Add()); chain.addAfter(name, getClass().getName() + ".release", new Release()); }
public void buildFilterChain(IoFilterChain chain) throws Exception { chain.addFirst(ExecutorThreadModel.class.getName(), filter); } }
public void buildFilterChain(IoFilterChain chain) throws Exception { for (Iterator i = entries.iterator(); i.hasNext();) { Entry e = (Entry) i.next(); chain.addLast(e.getName(), e.getFilter()); } }
private void readSession(DatagramSessionImpl session) { ByteBuffer readBuf = ByteBuffer.allocate(session.getReadBufferSize()); try { int readBytes = session.getChannel().read(readBuf.buf()); if (readBytes > 0) { readBuf.flip(); ByteBuffer newBuf = ByteBuffer.allocate(readBuf.limit()); newBuf.put(readBuf); newBuf.flip(); session.increaseReadBytes(readBytes); session.getFilterChain().fireMessageReceived(session, newBuf); } } catch (IOException e) { session.getFilterChain().fireExceptionCaught(session, e); } finally { readBuf.release(); } }
private void notifyWriteTimeout(SocketSessionImpl session, long currentTime, long writeTimeout, long lastIoTime) { SelectionKey key = session.getSelectionKey(); if (writeTimeout > 0 && (currentTime - lastIoTime) >= writeTimeout && key != null && key.isValid() && (key.interestOps() & SelectionKey.OP_WRITE) != 0) { session.getFilterChain().fireExceptionCaught(session, new WriteTimeoutException()); } }
private void releaseWriteBuffers(SocketSessionImpl session) { Queue<WriteRequest> writeRequestQueue = session.getWriteRequestQueue(); WriteRequest req; if ((req = writeRequestQueue.poll()) != null) { ByteBuffer buf = (ByteBuffer) req.getMessage(); try { buf.release(); } catch (IllegalStateException e) { session.getFilterChain().fireExceptionCaught(session, e); } finally { // The first unwritten empty buffer must be // forwarded to the filter chain. if (buf.hasRemaining()) { req.getFuture().setWritten(false); } else { session.getFilterChain().fireMessageSent(session, req); } } // Discard others. while ((req = writeRequestQueue.poll()) != null) { try { ((ByteBuffer) req.getMessage()).release(); } catch (IllegalStateException e) { session.getFilterChain().fireExceptionCaught(session, e); } finally { req.getFuture().setWritten(false); } } } }
s.increaseWrittenMessages(); s.getRemoteSession().getFilterChain().fireMessageReceived( s.getRemoteSession(), messageCopy); s.getFilterChain().fireMessageSent(s, writeRequest); } finally { s.getLock().unlock();
session.getFilterChain().fireMessageSent(session, req); continue; session.getFilterChain().fireMessageSent(session, req);
@Override public void onPreAdd(IoFilterChain parent, String name, NextFilter nextFilter) throws Exception { if (parent.contains(ProtocolCodecFilter.class)) { throw new IllegalStateException( "A filter chain cannot contain more than one ProtocolCodecFilter."); } }
public void sessionClosed(NextFilter nextFilter, IoSession session) throws Exception { try { session.getHandler().sessionClosed(session); } finally { // Remove all filters. session.getFilterChain().clear(); } }
session.getFilterChain().fireSessionClosed(session);
private void readSession(DatagramChannel channel, RegistrationRequest req) throws Exception { ByteBuffer readBuf = ByteBuffer .allocate(((DatagramSessionConfig) req.config .getSessionConfig()).getReceiveBufferSize()); try { SocketAddress remoteAddress = channel.receive(readBuf.buf()); if (remoteAddress != null) { DatagramSessionImpl session = (DatagramSessionImpl) newSession( remoteAddress, req.address); readBuf.flip(); ByteBuffer newBuf = ByteBuffer.allocate(readBuf.limit()); newBuf.put(readBuf); newBuf.flip(); session.increaseReadBytes(newBuf.remaining()); session.getFilterChain().fireMessageReceived(session, newBuf); } } finally { readBuf.release(); } }
session.getFilterChain().fireMessageReceived(session, buf); buf = null; if (e instanceof IOException) scheduleRemove(session); session.getFilterChain().fireExceptionCaught(session, e); } finally { if (buf != null)
private void doAddNew() { Selector selector = this.selector; for (;;) { SocketSessionImpl session = newSessions.poll(); if (session == null) break; SocketChannel ch = session.getChannel(); try { ch.configureBlocking(false); session.setSelectionKey(ch.register(selector, SelectionKey.OP_READ, session)); // AbstractIoFilterChain.CONNECT_FUTURE is cleared inside here // in AbstractIoFilterChain.fireSessionOpened(). session.getServiceListeners().fireSessionCreated(session); } catch (IOException e) { // Clear the AbstractIoFilterChain.CONNECT_FUTURE attribute // and call ConnectFuture.setException(). session.getFilterChain().fireExceptionCaught(session, e); } } }
session.getFilterChain().fireMessageSent(session, req); continue;
private void flushSessions() { if (flushingSessions.size() == 0) return; for (;;) { DatagramSessionImpl session = flushingSessions.poll(); if (session == null) break; session.setScheduledForFlush(false); try { boolean flushedAll = flush(session); if (flushedAll && !session.getWriteRequestQueue().isEmpty() && !session.isScheduledForFlush()) { scheduleFlush(session); } } catch (IOException e) { session.getFilterChain().fireExceptionCaught(session, e); } } }
session.increaseWrittenMessages(); session.getFilterChain().fireMessageSent(session, req); continue; session.getFilterChain().fireMessageSent(session, req);
private void flushSessions() { if (flushingSessions.size() == 0) return; for (;;) { DatagramSessionImpl session = flushingSessions.poll(); if (session == null) break; session.setScheduledForFlush(false); try { boolean flushedAll = flush(session); if (flushedAll && !session.getWriteRequestQueue().isEmpty() && !session.isScheduledForFlush()) { scheduleFlush(session); } } catch (IOException e) { session.getFilterChain().fireExceptionCaught(session, e); } } }
private void doRemove() { for (;;) { SocketSessionImpl session = removingSessions.poll(); if (session == null) break; SocketChannel ch = session.getChannel(); SelectionKey key = session.getSelectionKey(); // Retry later if session is not yet fully initialized. // (In case that Session.close() is called before addSession() is processed) if (key == null) { scheduleRemove(session); break; } // skip if channel is already closed if (!key.isValid()) { continue; } try { key.cancel(); ch.close(); } catch (IOException e) { session.getFilterChain().fireExceptionCaught(session, e); } finally { releaseWriteBuffers(session); session.getServiceListeners().fireSessionDestroyed(session); } } }
session.getFilterChain().fireExceptionCaught(session, e);