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 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); } } }
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 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); } } }
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); } } } }
session.getFilterChain().fireExceptionCaught(session, e);
} catch (Throwable t) { session.getFilterChain().fireExceptionCaught(session, t); } finally { if (!success) {
if (e instanceof IOException) scheduleRemove(session); session.getFilterChain().fireExceptionCaught(session, e); } finally { if (buf != null)
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(); } }