⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 abstractiosession.java

📁 mina是以Java实现的一个开源的网络程序框架
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    public final Object getCurrentWriteMessage() {        WriteRequest req = getCurrentWriteRequest();        if (req == null) {            return null;        }        return req.getMessage();    }    /**     * {@inheritDoc}     */    public final void setCurrentWriteRequest(WriteRequest currentWriteRequest) {        this.currentWriteRequest = currentWriteRequest;    }    /**     * TODO Add method documentation     */    public final void increaseReadBufferSize() {        int newReadBufferSize = getConfig().getReadBufferSize() << 1;        if (newReadBufferSize <= getConfig().getMaxReadBufferSize()) {            getConfig().setReadBufferSize(newReadBufferSize);        } else {            getConfig().setReadBufferSize(getConfig().getMaxReadBufferSize());        }        deferDecreaseReadBuffer = true;    }    /**     * TODO Add method documentation     */    public final void decreaseReadBufferSize() {        if (deferDecreaseReadBuffer) {            deferDecreaseReadBuffer = false;            return;        }        if (getConfig().getReadBufferSize() > getConfig().getMinReadBufferSize()) {            getConfig().setReadBufferSize(getConfig().getReadBufferSize() >>> 1);        }        deferDecreaseReadBuffer = true;    }    /**     * {@inheritDoc}     */    public final long getCreationTime() {        return creationTime;    }    /**     * {@inheritDoc}     */    public final long getLastIoTime() {        return Math.max(lastReadTime, lastWriteTime);    }    /**     * {@inheritDoc}     */    public final long getLastReadTime() {        return lastReadTime;    }    /**     * {@inheritDoc}     */    public final long getLastWriteTime() {        return lastWriteTime;    }    /**     * {@inheritDoc}     */    public final boolean isIdle(IdleStatus status) {        if (status == IdleStatus.BOTH_IDLE) {            return idleCountForBoth > 0;        }        if (status == IdleStatus.READER_IDLE) {            return idleCountForRead > 0;        }        if (status == IdleStatus.WRITER_IDLE) {            return idleCountForWrite > 0;        }        throw new IllegalArgumentException("Unknown idle status: " + status);    }    /**     * {@inheritDoc}     */    public final boolean isBothIdle() {        return isIdle(IdleStatus.BOTH_IDLE);    }    /**     * {@inheritDoc}     */    public final boolean isReaderIdle() {        return isIdle(IdleStatus.READER_IDLE);    }    /**     * {@inheritDoc}     */    public final boolean isWriterIdle() {        return isIdle(IdleStatus.WRITER_IDLE);    }    /**     * {@inheritDoc}     */    public final int getIdleCount(IdleStatus status) {        if (getConfig().getIdleTime(status) == 0) {            if (status == IdleStatus.BOTH_IDLE) {                idleCountForBoth = 0;            }            if (status == IdleStatus.READER_IDLE) {                idleCountForRead = 0;            }            if (status == IdleStatus.WRITER_IDLE) {                idleCountForWrite = 0;            }        }        if (status == IdleStatus.BOTH_IDLE) {            return idleCountForBoth;        }        if (status == IdleStatus.READER_IDLE) {            return idleCountForRead;        }        if (status == IdleStatus.WRITER_IDLE) {            return idleCountForWrite;        }        throw new IllegalArgumentException("Unknown idle status: " + status);    }    /**     * {@inheritDoc}     */    public final long getLastIdleTime(IdleStatus status) {        if (status == IdleStatus.BOTH_IDLE) {            return lastIdleTimeForBoth;        }        if (status == IdleStatus.READER_IDLE) {            return lastIdleTimeForRead;        }        if (status == IdleStatus.WRITER_IDLE) {            return lastIdleTimeForWrite;        }        throw new IllegalArgumentException("Unknown idle status: " + status);    }    /**     * TODO Add method documentation     */    public final void increaseIdleCount(IdleStatus status, long currentTime) {        if (status == IdleStatus.BOTH_IDLE) {            idleCountForBoth++;            lastIdleTimeForBoth = currentTime;        } else if (status == IdleStatus.READER_IDLE) {            idleCountForRead++;            lastIdleTimeForRead = currentTime;        } else if (status == IdleStatus.WRITER_IDLE) {            idleCountForWrite++;            lastIdleTimeForWrite = currentTime;        } else {            throw new IllegalArgumentException("Unknown idle status: " + status);        }    }    /**     * {@inheritDoc}     */    public final int getBothIdleCount() {        return getIdleCount(IdleStatus.BOTH_IDLE);    }    /**     * {@inheritDoc}     */    public final long getLastBothIdleTime() {        return getLastIdleTime(IdleStatus.BOTH_IDLE);    }    /**     * {@inheritDoc}     */    public final long getLastReaderIdleTime() {        return getLastIdleTime(IdleStatus.READER_IDLE);    }    /**     * {@inheritDoc}     */    public final long getLastWriterIdleTime() {        return getLastIdleTime(IdleStatus.WRITER_IDLE);    }    /**     * {@inheritDoc}     */    public final int getReaderIdleCount() {        return getIdleCount(IdleStatus.READER_IDLE);    }    /**     * {@inheritDoc}     */    public final int getWriterIdleCount() {        return getIdleCount(IdleStatus.WRITER_IDLE);    }    /**     * {@inheritDoc}     */    public SocketAddress getServiceAddress() {        IoService service = getService();        if (service instanceof IoAcceptor) {            return ((IoAcceptor) service).getLocalAddress();        } else {            return getRemoteAddress();        }    }    /**     * {@inheritDoc}     */    @Override    public final int hashCode() {        return super.hashCode();    }    /**     * {@inheritDoc}     * TODO This is a ridiculous implementation. Need to be replaced.     */    @Override    public final boolean equals(Object o) {        return super.equals(o);    }    /**     * {@inheritDoc}     */    @Override    public String toString() {        if (isConnected()||isClosing()) {            if (getService() instanceof IoAcceptor) {                return "(" + getIdAsString() + ": " + getServiceName() + ", server, " +                        getRemoteAddress() + " => " + getLocalAddress() + ')';            } else {                return "(" + getIdAsString() + ": " + getServiceName() + ", client, " +                        getLocalAddress() + " => " + getRemoteAddress() + ')';            }        } else {            return "Session disconnected ...";        }            }    /**     * TODO Add method documentation     */    private String getIdAsString() {        String id = Long.toHexString(getId()).toUpperCase();        // Somewhat inefficient, but it won't happen that often        // because an ID is often a big integer.        while (id.length() < 8) {            id = '0' + id; // padding        }        id = "0x" + id;        return id;    }    /**     * TODO Add method documentation     */    private String getServiceName() {        TransportMetadata tm = getTransportMetadata();        if (tm == null) {            return "null";        } else {            return tm.getProviderName() + ' ' + tm.getName();        }    }    /**     * Fires a {@link IoEventType#SESSION_IDLE} event to any applicable     * sessions in the specified collection.     *     * @param currentTime the current time (i.e. {@link System#currentTimeMillis()})     */    public static void notifyIdleness(Iterator<? extends IoSession> sessions, long currentTime) {        IoSession s = null;        while (sessions.hasNext()) {            s = sessions.next();            notifyIdleSession(s, currentTime);        }    }    /**     * Fires a {@link IoEventType#SESSION_IDLE} event if applicable for the     * specified {@code session}.     *     * @param currentTime the current time (i.e. {@link System#currentTimeMillis()})     */    public static void notifyIdleSession(IoSession session, long currentTime) {        notifyIdleSession0(                session, currentTime,                session.getConfig().getIdleTimeInMillis(IdleStatus.BOTH_IDLE),                IdleStatus.BOTH_IDLE, Math.max(                    session.getLastIoTime(),                    session.getLastIdleTime(IdleStatus.BOTH_IDLE)));        notifyIdleSession0(                session, currentTime,                session.getConfig().getIdleTimeInMillis(IdleStatus.READER_IDLE),                IdleStatus.READER_IDLE, Math.max(                    session.getLastReadTime(),                    session.getLastIdleTime(IdleStatus.READER_IDLE)));        notifyIdleSession0(                session, currentTime,                session.getConfig().getIdleTimeInMillis(IdleStatus.WRITER_IDLE),                IdleStatus.WRITER_IDLE, Math.max(                    session.getLastWriteTime(),                    session.getLastIdleTime(IdleStatus.WRITER_IDLE)));        notifyWriteTimeout(session, currentTime);    }    private static void notifyIdleSession0(            IoSession session, long currentTime,            long idleTime, IdleStatus status, long lastIoTime) {        if (idleTime > 0 && lastIoTime != 0                && currentTime - lastIoTime >= idleTime) {            session.getFilterChain().fireSessionIdle(status);        }    }    private static void notifyWriteTimeout(            IoSession session, long currentTime) {        long writeTimeout = session.getConfig().getWriteTimeoutInMillis();        if (writeTimeout > 0 &&                currentTime - session.getLastWriteTime() >= writeTimeout &&                !session.getWriteRequestQueue().isEmpty(session)) {            WriteRequest request = session.getCurrentWriteRequest();            if (request != null) {                session.setCurrentWriteRequest(null);                WriteTimeoutException cause = new WriteTimeoutException(request);                request.getFuture().setException(cause);                session.getFilterChain().fireExceptionCaught(cause);                // WriteException is an IOException, so we close the session.                session.close(true);            }        }    }            /**     * A queue which handles the CLOSE request.     *      * TODO : Check that when closing a session, all the pending     * requests are correctly sent.     */    private class CloseAwareWriteQueue implements WriteRequestQueue {        private final WriteRequestQueue q;        /**         * {@inheritDoc}         */        public CloseAwareWriteQueue(WriteRequestQueue q) {            this.q = q;        }        /**         * {@inheritDoc}         */        public synchronized WriteRequest poll(IoSession session) {            WriteRequest answer = q.poll(session);                        if (answer == CLOSE_REQUEST) {                AbstractIoSession.this.close();                dispose(session);                answer = null;            }                        return answer;        }        /**         * {@inheritDoc}         */        public void offer(IoSession session, WriteRequest e) {            q.offer(session, e);        }        /**         * {@inheritDoc}         */        public boolean isEmpty(IoSession session) {            return q.isEmpty(session);        }        /**         * {@inheritDoc}         */        public void clear(IoSession session) {            q.clear(session);        }        /**         * {@inheritDoc}         */        public void dispose(IoSession session) {            q.dispose(session);        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -