diff --git a/src/org/redkale/net/AsyncConnection.java b/src/org/redkale/net/AsyncConnection.java index efaf84be7..11e819c55 100644 --- a/src/org/redkale/net/AsyncConnection.java +++ b/src/org/redkale/net/AsyncConnection.java @@ -291,7 +291,7 @@ public abstract class AsyncConnection implements AsynchronousByteChannel, AutoCl int rs = channel.read(dst); this.readtime = System.currentTimeMillis(); if (handler != null) handler.completed(rs, attachment); - } catch (IOException e) { + } catch (Exception e) { if (handler != null) handler.failed(e, attachment); } } @@ -307,7 +307,7 @@ public abstract class AsyncConnection implements AsynchronousByteChannel, AutoCl int rs = channel.read(dst); this.readtime = System.currentTimeMillis(); return CompletableFuture.completedFuture(rs); - } catch (IOException e) { + } catch (Exception e) { throw new RuntimeException(e); } } @@ -318,7 +318,7 @@ public abstract class AsyncConnection implements AsynchronousByteChannel, AutoCl int rs = channel.write(src); this.writetime = System.currentTimeMillis(); if (handler != null) handler.completed(rs, attachment); - } catch (IOException e) { + } catch (Exception e) { if (handler != null) handler.failed(e, attachment); } } @@ -329,7 +329,7 @@ public abstract class AsyncConnection implements AsynchronousByteChannel, AutoCl int rs = channel.read(src); this.writetime = System.currentTimeMillis(); return CompletableFuture.completedFuture(rs); - } catch (IOException e) { + } catch (Exception e) { throw new RuntimeException(e); } } diff --git a/src/org/redkale/net/ProtocolServer.java b/src/org/redkale/net/ProtocolServer.java index ceeb8b848..ccb7c0b2d 100644 --- a/src/org/redkale/net/ProtocolServer.java +++ b/src/org/redkale/net/ProtocolServer.java @@ -228,6 +228,17 @@ public abstract class ProtocolServer { public void accept() { final AsynchronousServerSocketChannel serchannel = this.serverChannel; serchannel.accept(null, new CompletionHandler() { + private boolean supportInited; + + private boolean supportTcpLay; + + private boolean supportAlive; + + private boolean supportReuse; + + private boolean supportRcv; + + private boolean supportSnd; @Override public void completed(final AsynchronousSocketChannel channel, Void attachment) { @@ -244,6 +255,27 @@ public abstract class ProtocolServer { AsyncConnection conn = AsyncConnection.create(channel, null, context); conn.livingCounter = livingCounter; conn.closedCounter = closedCounter; + try { + if (!supportInited) { + synchronized (this) { + if (!supportInited) { + supportInited = true; + final Set> options = channel.supportedOptions(); + supportTcpLay = options.contains(StandardSocketOptions.TCP_NODELAY); + supportAlive = options.contains(StandardSocketOptions.SO_KEEPALIVE); + supportReuse = options.contains(StandardSocketOptions.SO_REUSEADDR); + supportRcv = options.contains(StandardSocketOptions.SO_RCVBUF); + supportSnd = options.contains(StandardSocketOptions.SO_SNDBUF); + } + } + } + if (supportTcpLay) channel.setOption(StandardSocketOptions.TCP_NODELAY, true); + if (supportAlive) channel.setOption(StandardSocketOptions.SO_KEEPALIVE, true); + if (supportReuse) channel.setOption(StandardSocketOptions.SO_REUSEADDR, true); + if (supportRcv) channel.setOption(StandardSocketOptions.SO_RCVBUF, 16 * 1024); + if (supportSnd) channel.setOption(StandardSocketOptions.SO_SNDBUF, 16 * 1024); + } catch (IOException e) { + } context.runAsync(new PrepareRunner(context, conn, null, null)); }