public class SocketSelector extends Object implements Closeable
Modifier and Type | Field and Description |
---|---|
protected boolean |
isCheckTimeout |
protected ArraySet<SelectionKey> |
selectedKeys |
protected AtomicBoolean |
selecting |
protected Selector |
selector |
Constructor and Description |
---|
SocketSelector(EventRunner eventRunner,
boolean isCheckTimeout)
构造方法
|
Modifier and Type | Method and Description |
---|---|
void |
addEvent(int priority,
Runnable runnable)
向执行器中增加一个选择事件
|
void |
addIoEvent()
向执行器中增加一个选择事件
|
void |
checkReadTimeout()
读超时检查
|
void |
close()
选择器关闭方法
|
int |
dealException(SocketContext socketContext,
Exception e)
异常处理方法
|
EventRunner |
getEventRunner() |
int |
loadAndPrepare(IoSession session,
int readSize)
数据读取
|
int |
readFromChannel(SocketContext socketContext,
SelectableChannel selectableChannel)
通用封装的从通道读数据的方法
|
boolean |
register(SocketContext socketContext,
int ops)
注册一个 SocketContext 到选择器
|
boolean |
select()
事件选择业务
|
void |
tcpAccept(TcpServerSocket socketContext,
SocketChannel socketChannel)
Tcp 服务接受一个新的连接
|
int |
tcpReadFromChannel(TcpSocket socketContext,
SocketChannel socketChannel)
TCP 从通道读数据的方法
|
int |
tcpWriteToChannel(TcpSocket socketContext,
ByteBuffer buffer)
TCP 向通道写数据的方法
|
UdpSocket |
udpAccept(UdpServerSocket socketContext,
DatagramChannel datagramChannel,
SocketAddress address)
UDP 服务接受一个新的连接
|
int |
udpReadFromChannel(SocketContext<DatagramChannel,UdpSession> socketContext,
DatagramChannel datagramChannel)
UDP 从通道读数据的方法
|
int |
udpWriteToChannel(UdpSocket socketContext,
ByteBuffer buffer)
UDP 向通道写数据的方法
|
void |
unRegister(SelectionKey selectionKey)
在选择器中取消一个 SocketContext 的注册
|
int |
writeToChannel(SocketContext socketContext,
ByteBuffer buffer)
通用封装的向通道写数据的方法
|
protected Selector selector
protected boolean isCheckTimeout
protected ArraySet<SelectionKey> selectedKeys
protected AtomicBoolean selecting
public SocketSelector(EventRunner eventRunner, boolean isCheckTimeout) throws IOException
eventRunner
- 事件执行器isCheckTimeout
- 是否检查超时IOException
- IO 异常public EventRunner getEventRunner()
public boolean register(SocketContext socketContext, int ops)
socketContext
- SocketContext 对象ops
- 需要关注的操作public void unRegister(SelectionKey selectionKey)
selectionKey
- SocketContext 对象public void addIoEvent()
public void addEvent(int priority, Runnable runnable)
priority
- 指定的事件优先级, 越小优先级越高, 1-3 预留事件等级, 4:IO 事件, 5:EventProcess 事件, 6: Socket 注册/注销事件, 7-10 预留事件等级runnable
- 在事件选择前执行的方法public boolean select()
public void checkReadTimeout()
public void close()
close
in interface Closeable
close
in interface AutoCloseable
public int readFromChannel(SocketContext socketContext, SelectableChannel selectableChannel)
socketContext
- SocketContext 对象selectableChannel
- 读取的 SelectableChannel 对象public int writeToChannel(SocketContext socketContext, ByteBuffer buffer)
socketContext
- SocketContext 对象buffer
- 待写入的数据缓冲对象public void tcpAccept(TcpServerSocket socketContext, SocketChannel socketChannel)
socketContext
- SocketContext 对象socketChannel
- Socketchannel 对象public int tcpReadFromChannel(TcpSocket socketContext, SocketChannel socketChannel) throws IOException
socketContext
- TcpSocket 对象socketChannel
- 读取的 Socketchannel 对象IOException
- IO 异常public int tcpWriteToChannel(TcpSocket socketContext, ByteBuffer buffer) throws IOException
socketContext
- TcpSocket 对象buffer
- 待写入的数据缓冲对象IOException
- IO 异常public UdpSocket udpAccept(UdpServerSocket socketContext, DatagramChannel datagramChannel, SocketAddress address) throws IOException
socketContext
- UdpServerSocket 对象datagramChannel
- DatagramChannel 对象address
- 接受通道的地址信息IOException
- IO异常public int udpReadFromChannel(SocketContext<DatagramChannel,UdpSession> socketContext, DatagramChannel datagramChannel) throws IOException
socketContext
- SocketContext 对象datagramChannel
- 读取的 DatagramChannel 对象IOException
- IO 异public int udpWriteToChannel(UdpSocket socketContext, ByteBuffer buffer) throws IOException
socketContext
- UdpSocket 对象buffer
- 待写入的数据缓冲对象IOException
- IO 异public int loadAndPrepare(IoSession session, int readSize) throws IOException
session
- IoSession会话对象readSize
- 需要读取数据大小IOException
- IO 异常public int dealException(SocketContext socketContext, Exception e)
socketContext
- SocketContext 对象e
- Exception 异常对象Copyright © 2020 Voovan. All rights reserved.