Java Code Examples for org.apache.mina.core.session.IoSession#isClosing()

The following examples show how to use org.apache.mina.core.session.IoSession#isClosing() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StalledSessionsFilter.java    From Openfire with Apache License 2.0 6 votes vote down vote up
@Override
public void filterWrite(NextFilter nextFilter, IoSession session, WriteRequest writeRequest)
        throws Exception {
    if (!session.isClosing()) {
        // Get number of pending requests
        long pendingBytes = session.getScheduledWriteBytes();
        if (pendingBytes > bytesCap) {
            // Get last time we were able to send something to the connected client
            long writeTime = session.getLastWriteTime();
            int pendingRequests = session.getScheduledWriteMessages();
            Log.debug("About to kill session with pendingBytes: " + pendingBytes + " pendingWrites: " +
                    pendingRequests + " lastWrite: " + new Date(writeTime) + "session: " + session);
            // Close the session and throw an exception
            session.close(false);
            throw new IOException("Closing session that seems to be stalled. Preventing OOM");
        }
    }
    // Call next filter (everything is fine)
    super.filterWrite(nextFilter, session, writeRequest);
}
 
Example 2
Source File: NetManager.java    From jane with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * 发送对象的底层入口. 可带监听器,并返回WriteFuture
 */
public static WriteFuture write(IoSession session, Object obj, IoFutureListener<?> listener)
{
	if (session.isClosing() || obj == null)
		return null;
	IoFilterChain ifc = session.getFilterChain();
	WriteFuture wf = new DefaultWriteFuture(session);
	if (listener != null)
		wf.addListener(listener);
	DefaultWriteRequest dwr = new DefaultWriteRequest(obj, wf);
	synchronized (session)
	{
		ifc.fireFilterWrite(dwr);
	}
	return wf;
}
 
Example 3
Source File: NetManager.java    From jane with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * 向某个连接发送请求
 * <p>
 * 此操作是异步的
 * @param onAnswer 回调对象,不能为null,用于在回复和超时时回调(超时则回复bean的参数为null)
 * @param timeout 超时时间(秒)
 * @return 如果连接已经失效则返回false且不会有回复和超时的回调, 否则返回true
 */
public <B extends Bean<B>> boolean ask(IoSession session, Bean<?> bean, int timeout, AnswerHandler<B> onAnswer)
{
	if (session.isClosing() || bean == null)
		return false;
	BeanContext<B> beanCtx = allocBeanContext(bean, session, onAnswer);
	if (!send0(session, bean))
	{
		if (_beanCtxMap.remove(bean.serial(), beanCtx))
		{
			beanCtx.session = null;
			beanCtx.askBean = null;
			beanCtx.answerHandler = null;
		}
		return false;
	}
	beanCtx.timeout = timeout;
	return true;
}
 
Example 4
Source File: NetManager.java    From jane with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * 向某个连接发送请求并返回CompletableFuture对象
 * <p>
 * 此操作是异步的
 * @return 如果连接已经失效则返回null, 如果请求超时则对返回的CompletableFuture对象调用get方法时返回null
 */
public <B extends Bean<B>> CompletableFuture<B> askAsync(IoSession session, Bean<?> bean, int timeout)
{
	if (session.isClosing() || bean == null)
		return null;
	CompletableFuture<B> cf = new CompletableFuture<>();
	BeanContext<B> beanCtx = allocBeanContext(bean, session, cf::complete);
	if (!send0(session, bean))
	{
		if (_beanCtxMap.remove(bean.serial(), beanCtx))
		{
			beanCtx.session = null;
			beanCtx.askBean = null;
			beanCtx.answerHandler = null;
		}
		return null;
	}
	beanCtx.timeout = timeout;
	return cf;
}
 
Example 5
Source File: HttpServerIoHandler.java    From game-server with MIT License 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public void messageSent(IoSession session, Object message) throws Exception {
	if (!session.isClosing()) {
		MsgUtil.close(session, "http isClosing");
	}
}
 
Example 6
Source File: ProxyFilter.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Receives data from the remote host, passes to the handler if a handshake is in progress, 
 * otherwise passes on transparently.
 * 
 * @param nextFilter the next filter in filter chain
 * @param session the session object
 * @param message the object holding the received data
 */
@Override
public void messageReceived(final NextFilter nextFilter, final IoSession session, final Object message)
        throws ProxyAuthException {
    ProxyLogicHandler handler = getProxyHandler(session);

    synchronized (handler) {
        IoBuffer buf = (IoBuffer) message;

        if (handler.isHandshakeComplete()) {
            // Handshake done - pass data on as-is
            nextFilter.messageReceived(session, buf);

        } else {
            LOGGER.debug(" Data Read: {} ({})", handler, buf);

            // Keep sending handshake data to the handler until we run out
            // of data or the handshake is finished
            while (buf.hasRemaining() && !handler.isHandshakeComplete()) {
                LOGGER.debug(" Pre-handshake - passing to handler");

                int pos = buf.position();
                handler.messageReceived(nextFilter, buf);

                // Data not consumed or session closing
                if (buf.position() == pos || session.isClosing()) {
                    return;
                }
            }

            // Pass on any remaining data to the next filter
            if (buf.hasRemaining()) {
                LOGGER.debug(" Passing remaining data to next filter");

                nextFilter.messageReceived(session, buf);
            }
        }
    }
}
 
Example 7
Source File: HttpCodec.java    From jane with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 发送HTTP的回复头和可选的数据部分
 * @param code 回复的HTTP状态码字符串. 如"404 Not Found";null表示"200 OK"
 * @param len
 * <li>len < 0: 使用chunked模式,后续发送若干个{@link #sendChunk},最后发送{@link #sendChunkEnd}
 * <li>len = 0: 由data参数的长度确定
 * <li>len > 0: 后续使用{@link #send}发送固定长度的数据
 * @param extraHead 额外发送的HTTP头. 可通过{@link #createExtraHead}创建,可传null表示无任何额外的头信息
 * @param data HTTP回复数据的内容. 有效范围是remain部分,null表示无数据
 */
@SuppressWarnings("null")
public static boolean sendHead(IoSession session, String code, long len, Octets extraHead, Octets data)
{
	if (session.isClosing())
		return false;
	int dataLen = (data != null ? data.remain() : 0);
	if (len == 0)
		len = dataLen;
	byte[] dateLine = getDateLine();
	int size = (code == null ? RES_HEAD_OK.length : 9 + Octets.marshalStrLen(code)) + dateLine.length +
			(len >= 0 ? RES_HEAD_CONT_LEN.length + getUIntLen(len) : RES_HEAD_CHUNKED.length) + 4;
	if (extraHead != null)
		size += extraHead.size();
	final int APPEND_DATA_MAX = 64;
	if (dataLen <= APPEND_DATA_MAX)
		size += dataLen;
	Octets buf = Octets.createSpace(size);
	if (code == null)
		buf.append(RES_HEAD_OK);
	else
		buf.append(RES_HEAD_OK, 0, 9).append(code);
	buf.append(dateLine);
	if (len >= 0)
		appendUInt(buf.append(RES_HEAD_CONT_LEN), len);
	else
		buf.append(RES_HEAD_CHUNKED);
	if (extraHead != null)
		buf.append(extraHead);
	buf.marshal4(0x0d0a0d0a);
	if (dataLen > 0 && dataLen <= APPEND_DATA_MAX)
		buf.append(data.array(), data.position(), dataLen);
	return send(session, buf) && (dataLen <= APPEND_DATA_MAX || send(session, data));
}
 
Example 8
Source File: NetManager.java    From jane with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 发送对象的底层入口
 */
public static boolean write(IoSession session, WriteRequest wr)
{
	if (session.isClosing() || wr == null)
		return false;
	IoFilterChain ifc = session.getFilterChain();
	synchronized (session)
	{
		ifc.fireFilterWrite(wr);
	}
	return true;
}
 
Example 9
Source File: NetManager.java    From jane with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static boolean write(IoSession session, Object obj)
{
	if (session.isClosing() || obj == null)
		return false;
	IoFilterChain ifc = session.getFilterChain();
	WriteRequest wr = (obj instanceof WriteRequest ? (WriteRequest)obj : new SimpleWriteRequest(obj));
	synchronized (session)
	{
		ifc.fireFilterWrite(wr);
	}
	return true;
}
 
Example 10
Source File: NetManager.java    From jane with GNU Lesser General Public License v3.0 5 votes vote down vote up
public boolean sendSafe(IoSession session, Bean<?> bean)
{
	if (session.isClosing() || bean == null)
		return false;
	bean.serial(0);
	RawBean rawbean = new RawBean(bean);
	SContext.current().addOnCommit(() -> send(session, rawbean));
	return true;
}
 
Example 11
Source File: NetManager.java    From jane with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 向某个连接发送bean
 * <p>
 * 此操作是异步的
 * @param bean 如果是RawBean类型,考虑到性能问题,在发送完成前不能修改其中的data对象
 * @param onSent 可设置一个回调对象,用于在发送成功后回调. null表示不回调
 * @return 如果连接已经失效则返回false, 否则返回true
 */
public <B extends Bean<B>> boolean send(IoSession session, B bean, Runnable onSent)
{
	if (bean == null)
		return false;
	bean.serial(0);
	if (onSent == null)
	{
		if (!write(session, bean))
			return false;
	}
	else
	{
		if (session.isClosing())
			return false;
		if (write(session, bean, future ->
		{
			try
			{
				onSent.run();
			}
			catch (Throwable e)
			{
				Log.error(e, "{}({}): callback exception: {}", _name, session.getId(), bean.typeName());
			}
		}) == null)
			return false;
	}
	if (_enableTrace)
		Log.trace("{}({}): send: {}:{}", _name, session.getId(), bean.typeName(), bean);
	return true;
}
 
Example 12
Source File: NetManager.java    From jane with GNU Lesser General Public License v3.0 5 votes vote down vote up
public <B extends Bean<B>> boolean sendSafe(IoSession session, B bean, Runnable callback)
{
	if (session.isClosing() || bean == null)
		return false;
	bean.serial(0);
	RawBean rawbean = new RawBean(bean);
	SContext.current().addOnCommit(() -> send(session, rawbean, callback));
	return true;
}
 
Example 13
Source File: NetManager.java    From jane with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 同ask, 区别仅仅是在事务成功后发送请求
 * <p>
 * 注意调用后修改参数bean会影响到实际的请求
 */
public <B extends Bean<B>> boolean askSafe(IoSession session, Bean<?> bean, AnswerHandler<B> onAnswer)
{
	if (session.isClosing() || bean == null)
		return false;
	SContext.current().addOnCommit(() -> ask(session, bean, onAnswer));
	return true;
}
 
Example 14
Source File: NetManager.java    From jane with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 同answer, 区别仅仅是在事务成功后回复
 */
public boolean answerSafe(IoSession session, Bean<?> askBean, Bean<?> answerBean)
{
	if (session.isClosing() || askBean == null || answerBean == null)
		return false;
	int askSerial = askBean.serial();
	answerBean.serial(askSerial > 0 ? -askSerial : 0);
	RawBean rawbean = new RawBean(answerBean);
	SContext.current().addOnCommit(() -> send0(session, rawbean));
	return true;
}
 
Example 15
Source File: NetManager.java    From jane with GNU Lesser General Public License v3.0 5 votes vote down vote up
public boolean answerSafe(IoSession session, int askSerial, Bean<?> answerBean)
{
	if (session.isClosing() || answerBean == null)
		return false;
	answerBean.serial(askSerial > 0 ? -askSerial : 0);
	RawBean rawbean = new RawBean(answerBean);
	SContext.current().addOnCommit(() -> send0(session, rawbean));
	return true;
}