侧边栏壁纸
博主头像
落叶人生博主等级

走进秋风,寻找秋天的落叶

  • 累计撰写 130562 篇文章
  • 累计创建 28 个标签
  • 累计收到 9 条评论
标签搜索

目 录CONTENT

文章目录

Mina源码阅读笔记(五)—Mina对连接的操作IoSession

2022-06-14 星期二 / 0 评论 / 0 点赞 / 117 阅读 / 13494 字

接上一篇《Mina的连接IoConnector》 IoSession是Mina管理两端的一个重要部分,也是Mina的核心,Session具有了生命周期的概念,它的生命周期和连接时紧密相关的,这点在

接上一篇《Mina的连接IoConnector》

IoSessionMina管理两端的一个重要部分,也是Mina的核心,Session具有了生命周期的概念,它的生命周期和连接时紧密相关的,这点在后面的介绍中会涉及。另外,好像hibernate中也有session也有生命周期(真的是好久没有用了,连hibernate有里session是干嘛的都想不起来了)。

在读源码之前,我们还是先了解下IoSession的作用和一些基本概念。IoSession的主要作用有如下一些:

l  管理连接。注意,这里的管理连接并不是直接去控制我们上次讲的最底层的连接acceptorconnector。如果acceptorconnector建立的一条管道,那session就是在管道内的管理者,他是没有办法将管道对半拆分开的,他只能从内部阻断两边的通信。管理连接还有部分就是可以配置缓冲区的大小,闲置时间等等。

l  存储信息。和web里的session一样,这里的session也有存储attribute的功能,不过一般来说,这里存储的都是和连接有关的东西,并不会像web开发一样存一些业务上的东西。

l  驱动读写操作。我不知道用驱动这个词是否合适,那个例子来说,session.write

l  统计功能。Session还记录了连接中的bytemessage等数量。

Session在使用中是通过ConnectionFuture获得的:

ConnectFuture future = connector.connect(new InetSocketAddress(					HOST, PORT));// 创建连接			future.awaitUninterruptibly();// 等待连接创建完成			session = future.getSession();// 获得session

说完作用,就是session的状态了,我们读源码的时候,也会跟着session的状态来读:

  • Connected : the session has been created and is available
  • Idle : the session hasn't processed any request for at least a period of time (this period is configurable)
    • Idle for read : no read has actually been made for a period of time
    • Idle for write : no write has actually been made for a period of time
    • Idle for both : no read nor write for a period of time
  • Closing : the session is being closed (the remaining messages are being flushed, cleaning up is not terminated)
  • Closed : The session is now closed, nothing else can be done to revive it.

注意,这里的最开始的connect,最后的closed这些都说的是acceptorconnector之间的操作,是他们的状态来影响session的状态。和hibernate不一样,那个的操作也是session自己的(session.close等),这里的session是没办法控制通道的。

了解完了基础的,我们来看看代码层面的实现,在org,apache.mina.core.session包中主要实现了IoSession的相关功能。我们从IoSession这个接口开始看起:

内容比较多,比之前的那几篇都难多了。我们还是按照上图画出来的方框对这些方法进行分分类,看看是不是跟我们之前分析的一样:

红色:得到一系列配置等。我们说了sessionmina的核心。

蓝色:驱动读写操作。

绿色:管理连接。

黑色:存储功能,是不是和JSP里的session很像。

橘色:统计数据。

有些没有框上的并不是说不属于这里面,而是有些我确实不了解,有些是比较难划分,具体的含义可以看源码中,都有说明。

了解完IoSession的最基本功能之后,我们要看看它的具体实现类AbstractIoSession。看具体实现之前,我们先看看这个类关联了哪些比较重要的引用:

private final IoHandler handler;    protected IoSessionConfig config;      private final IoService service;    private static final AttributeKey READY_READ_FUTURES_KEY = new AttributeKey(AbstractIoSession.class,            "readyReadFutures");private static final IoFutureListener<CloseFuture> SCHEDULED_COUNTER_RESETTER;  private static final WriteRequest CLOSE_REQUEST = new DefaultWriteRequest(new Object());    private IoSessionAttributeMap attributes;    private WriteRequestQueue writeRequestQueue;private WriteRequest currentWriteRequest;private final CloseFuture closeFuture = new DefaultCloseFuture(this);

在上面的代码中,我们有熟悉的handler,这里我们也可以稍微明确一下handlersession之间的关系了,每次我们在创建服务端和客户端的时候,都必须设置一个handler,如果不设置则报异常,handler里主要处理seesion各种状态时的业务。Service用来管理sessionCloseFutrue用来设置通道的关闭,在上面我们已经说过,session的关闭是通过closefuture来操作的。

这些成员变量中,除了我们见过的handler和future,凡是带有write的都来自org.apache.mina.core.write包,这个包作为一个内部的工具类,在session的写操作中起到辅助作用。其他类均来自org.apache.mina.core.session中,这些类组成都比较简单,但都是要了解AbstractIoSession之前,我们要对这里提到的这些对象有所了解。

首先是ioSessionConfig,和它的具体实现AbstractIoSessionCnfig:

从上面的图我们很容易就能看到mina的一些默认传输配置,当然这些数字都不是随便写的,为什么最小要64,最大是65535,我相信计算机网络相关课程里应该都会有涉及。

接下来是IoSessionAttributeMap接口,这个接口主要作用就是规定了get、set、remove Attribute的方法。注意存储在session中的变量是一种map关系的变量(key-value),所以我们也很容易明白这个接口命名时为什么后面要多个map出来。至于这个类的实现,它隐藏的很好,放在了一个内部类中。具体可以看IoSessionDataStructureFactory这个接口,这个接口主要是为了这个map形势提供数据结构和规定基本操作。至于这个session中map的底层实现则用了ConcurrentHashMap来做容器,这部分具体可以看内部类DefaultIoSessionAttributeMap。

还有一个就是AttributeKey,这个类主要重写equals方法和hashCode方法,为了将session中的key和对应的session联系起来。因为一个项目中可能有多个session,而不同session中的key可能会相同,所以在构造key和hash的时候会将session也考虑进去。

public AttributeKey(Class<?> source, String name) {        this.name = source.getName() + '.' + name + '@' + Integer.toHexString(this.hashCode());    }

现在我们可以看AbstractIoSession了。主要看读写操作,其他操作都是统计和配置稍稍看过即可:

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;    }

采用队列进行读取,这里只用了Queue,没有用concurrent中的那些同步队列,而是用了synchronized关键字来处理同步,主要是我们要明白这里要同步的不是队列里的内容,而是读的这个过程,session都是独立的,所以一个session内一个队列无论怎么抢还是能排除顺序的。所以对于读操作来说,主要是要保证在读一条的时候,不能有其他线程再读。

下面是写操作:

public WriteFuture write(Object message, SocketAddress remoteAddress) {        if (message == null) {            throw new IllegalArgumentException("Trying to write a null message : not allowed");        }        // We can't send a message to a connected session if we don't have        // the remote address        if (!getTransportMetadata().isConnectionless() && (remoteAddress != null)) {            throw new UnsupportedOperationException();        }        // If the session has been closed or is closing, we can't either        // send a message to the remote side. We generate a future        // containing an exception.        if (isClosing() || !isConnected()) {            WriteFuture future = new DefaultWriteFuture(this);            WriteRequest request = new DefaultWriteRequest(message, future, remoteAddress);            WriteException writeException = new WriteToClosedSessionException(request);            future.setException(writeException);            return future;        }        FileChannel openedFileChannel = null;        // TODO: remove this code as soon as we use InputStream        // instead of Object for the message.        try {            if ((message instanceof IoBuffer) && !((IoBuffer) message).hasRemaining()) {                // Nothing to write : probably an error in the user code                throw new IllegalArgumentException("message is empty. Forgot to call flip()?");            } else if (message instanceof FileChannel) {                FileChannel fileChannel = (FileChannel) message;                message = new DefaultFileRegion(fileChannel, 0, fileChannel.size());            } else if (message instanceof File) {                File file = (File) message;                openedFileChannel = new FileInputStream(file).getChannel();                message = new FilenameFileRegion(file, openedFileChannel, 0, openedFileChannel.size());            }        } catch (IOException e) {            ExceptionMonitor.getInstance().exceptionCaught(e);            return DefaultWriteFuture.newNotWrittenFuture(this, e);        }        // Now, we can write the message. First, create a future        WriteFuture writeFuture = new DefaultWriteFuture(this);        WriteRequest writeRequest = new DefaultWriteRequest(message, writeFuture, remoteAddress);        // Then, get the chain and inject the WriteRequest into it        IoFilterChain filterChain = getFilterChain();        filterChain.fireFilterWrite(writeRequest);        // TODO : This is not our business ! The caller has created a        // FileChannel,        // he has to close it !        if (openedFileChannel != null) {            // If we opened a FileChannel, it needs to be closed when the write            // has completed            final FileChannel finalChannel = openedFileChannel;            writeFuture.addListener(new IoFutureListener<WriteFuture>() {                public void operationComplete(WriteFuture future) {                    try {                        finalChannel.close();                    } catch (IOException e) {                        ExceptionMonitor.getInstance().exceptionCaught(e);                    }                }            });        }        // Return the WriteFuture.        return writeFuture;    }

这里面有个instanceof FileChannelFile是不是感到有点儿奇怪,mina不是写出去的是IoBuffer么,怎么现在又可以写文件了。Mina作为一个封装好的框架,自然可以直接做文件的传输,这里面会有相应的handler来处理这些业务。

Session部分最主要的就是了解他的生命周期以及相关联的那些引用,这里我们可以看到与读写最密切的就是Future了,所以这部分,就是我下篇会写的主题。


广告 广告

评论区