|
|
@@ -18,11 +18,10 @@ import cn.hutool.socket.SocketConfig;
|
|
|
|
|
|
/**
|
|
|
* 基于AIO的Socket服务端实现
|
|
|
- *
|
|
|
- * @author looly
|
|
|
*
|
|
|
+ * @author looly
|
|
|
*/
|
|
|
-public class AioServer implements Closeable{
|
|
|
+public class AioServer implements Closeable {
|
|
|
private static final Log log = LogFactory.get();
|
|
|
private static AcceptHandler ACCEPT_HANDLER = new AcceptHandler();
|
|
|
|
|
|
@@ -30,11 +29,11 @@ public class AioServer implements Closeable{
|
|
|
private AsynchronousServerSocketChannel channel;
|
|
|
protected IoAction<ByteBuffer> ioAction;
|
|
|
protected SocketConfig config;
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* 构造
|
|
|
- *
|
|
|
+ *
|
|
|
* @param port 端口
|
|
|
*/
|
|
|
public AioServer(int port) {
|
|
|
@@ -43,9 +42,9 @@ public class AioServer implements Closeable{
|
|
|
|
|
|
/**
|
|
|
* 构造
|
|
|
- *
|
|
|
+ *
|
|
|
* @param address 地址
|
|
|
- * @param config {@link SocketConfig} 配置项
|
|
|
+ * @param config {@link SocketConfig} 配置项
|
|
|
*/
|
|
|
public AioServer(InetSocketAddress address, SocketConfig config) {
|
|
|
this.config = config;
|
|
|
@@ -54,7 +53,7 @@ public class AioServer implements Closeable{
|
|
|
|
|
|
/**
|
|
|
* 初始化
|
|
|
- *
|
|
|
+ *
|
|
|
* @param address 地址和端口
|
|
|
* @return this
|
|
|
*/
|
|
|
@@ -73,7 +72,7 @@ public class AioServer implements Closeable{
|
|
|
|
|
|
/**
|
|
|
* 开始监听
|
|
|
- *
|
|
|
+ *
|
|
|
* @param sync 是否阻塞
|
|
|
*/
|
|
|
public void start(boolean sync) {
|
|
|
@@ -88,9 +87,10 @@ public class AioServer implements Closeable{
|
|
|
* 设置 Socket 的 Option 选项<br>
|
|
|
* 选项见:{@link java.net.StandardSocketOptions}
|
|
|
*
|
|
|
- * @param <T> 选项泛型
|
|
|
- * @param name {@link SocketOption} 枚举
|
|
|
+ * @param <T> 选项泛型
|
|
|
+ * @param name {@link SocketOption} 枚举
|
|
|
* @param value SocketOption参数
|
|
|
+ * @return this
|
|
|
* @throws IOException IO异常
|
|
|
*/
|
|
|
public <T> AioServer setOption(SocketOption<T> name, T value) throws IOException {
|
|
|
@@ -100,7 +100,7 @@ public class AioServer implements Closeable{
|
|
|
|
|
|
/**
|
|
|
* 获取IO处理器
|
|
|
- *
|
|
|
+ *
|
|
|
* @return {@link IoAction}
|
|
|
*/
|
|
|
public IoAction<ByteBuffer> getIoAction() {
|
|
|
@@ -109,7 +109,7 @@ public class AioServer implements Closeable{
|
|
|
|
|
|
/**
|
|
|
* 设置IO处理器,单例存在
|
|
|
- *
|
|
|
+ *
|
|
|
* @param ioAction {@link IoAction}
|
|
|
* @return this;
|
|
|
*/
|
|
|
@@ -120,7 +120,7 @@ public class AioServer implements Closeable{
|
|
|
|
|
|
/**
|
|
|
* 获取{@link AsynchronousServerSocketChannel}
|
|
|
- *
|
|
|
+ *
|
|
|
* @return {@link AsynchronousServerSocketChannel}
|
|
|
*/
|
|
|
public AsynchronousServerSocketChannel getChannel() {
|
|
|
@@ -129,7 +129,7 @@ public class AioServer implements Closeable{
|
|
|
|
|
|
/**
|
|
|
* 处理接入的客户端
|
|
|
- *
|
|
|
+ *
|
|
|
* @return this
|
|
|
*/
|
|
|
public AioServer accept() {
|
|
|
@@ -139,7 +139,7 @@ public class AioServer implements Closeable{
|
|
|
|
|
|
/**
|
|
|
* 服务是否开启状态
|
|
|
- *
|
|
|
+ *
|
|
|
* @return 服务是否开启状态
|
|
|
*/
|
|
|
public boolean isOpen() {
|
|
|
@@ -168,9 +168,10 @@ public class AioServer implements Closeable{
|
|
|
}
|
|
|
|
|
|
// ------------------------------------------------------------------------------------- Private method start
|
|
|
+
|
|
|
/**
|
|
|
* 开始监听
|
|
|
- *
|
|
|
+ *
|
|
|
* @param sync 是否阻塞
|
|
|
* @throws IOException IO异常
|
|
|
*/
|