http<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">://blog.youkuaiyun.com/ljx8928358/article/details/7759024</span>
http://blog.youkuaiyun.com/cnicys/article/details/8535485
session就像mina框架的心脏,每一个client的连接到达server之后都会创建一个新的session,一直保存在内存单元中直到client 连接断开。
session 常常用来储存一些和连接相关的持久化信息,这些信息在server 进行处理的时候都是非常有用的,生命周期为整个会话
1、session的状态
通常来讲 mina 的session 有以下几个状态
1、Connected : client 连接上server 之后,这时 session 已经建立,并且该session 是有效的、可用的
2、Idle : 此时 session并没有处理任何的请求,并且这个时间超过了你设定的 session 进入空闲状态的那个时间点,idle又细分为三个状态
(1) idle for read 超过读通道设定的空闲时间
(2) idle for write 超过写通道设定的空闲时间
(3) idle for both 读写通道均超过设定空闲时间
3、Closing : 这时候session即将被关闭但是有没有完全关闭(对余下未处理完的消息进行 flushed 操作)
4、Closed : session已经完全关闭,并且这个状态时不可逆的
android客户端的使用:
//使用的CPU核心输
connector = new NioSocketConnector(Runtime.getRuntime().availableProcessors() + 1);
//设置为非延迟发送,为true则不组装成大包发送,收到东西马上发出
connector.getSessionConfig().setTcpNoDelay(true);
//设置输入缓冲区的大小
connector.getSessionConfig().setReceiveBufferSize(Global.SOCKET.READ_BUFFER_SIZE);
//设置输出缓冲区的大小
connector.getSessionConfig().setSendBufferSize(Global.SOCKET.READ_BUFFER_SIZE);
//读写 通道均在10 秒内无任何操作就进入空闲状态
connector.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 130);
//数据处理类
connector.getFilterChain().addLast(
"codec",
new ProtocolCodecFilter(new SimpleEncoder(),
new SimpleDecoder()));
//处理连接状态,返回处理后数据
connector.setHandler(new ServerSessionHandler());
//设置ip端口
connector.setDefaultRemoteAddress(new InetSocketAddress(ip, port));
ConnectFuture future = connector.connect();
处理连接:
class ServerSessionHandler extends IoHandlerAdapter { }
处理解析:
public class SimpleDecoder extends ProtocolDecoderAdapter {}
public class SimpleEncoder implements ProtocolEncoder {}
发送一个数据:
public void encode(IoSession session, Object obj, ProtocolEncoderOutput out)throws Exception {
out.write("hello!");
}
或
IoSession session
session.write(param);