This commit is contained in:
Redkale
2018-05-07 12:44:55 +08:00
parent 0bf34781c5
commit d3c6ab8dc5
2 changed files with 36 additions and 4 deletions

View File

@@ -291,7 +291,7 @@ public abstract class AsyncConnection implements AsynchronousByteChannel, AutoCl
int rs = channel.read(dst); int rs = channel.read(dst);
this.readtime = System.currentTimeMillis(); this.readtime = System.currentTimeMillis();
if (handler != null) handler.completed(rs, attachment); if (handler != null) handler.completed(rs, attachment);
} catch (IOException e) { } catch (Exception e) {
if (handler != null) handler.failed(e, attachment); if (handler != null) handler.failed(e, attachment);
} }
} }
@@ -307,7 +307,7 @@ public abstract class AsyncConnection implements AsynchronousByteChannel, AutoCl
int rs = channel.read(dst); int rs = channel.read(dst);
this.readtime = System.currentTimeMillis(); this.readtime = System.currentTimeMillis();
return CompletableFuture.completedFuture(rs); return CompletableFuture.completedFuture(rs);
} catch (IOException e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
@@ -318,7 +318,7 @@ public abstract class AsyncConnection implements AsynchronousByteChannel, AutoCl
int rs = channel.write(src); int rs = channel.write(src);
this.writetime = System.currentTimeMillis(); this.writetime = System.currentTimeMillis();
if (handler != null) handler.completed(rs, attachment); if (handler != null) handler.completed(rs, attachment);
} catch (IOException e) { } catch (Exception e) {
if (handler != null) handler.failed(e, attachment); if (handler != null) handler.failed(e, attachment);
} }
} }
@@ -329,7 +329,7 @@ public abstract class AsyncConnection implements AsynchronousByteChannel, AutoCl
int rs = channel.read(src); int rs = channel.read(src);
this.writetime = System.currentTimeMillis(); this.writetime = System.currentTimeMillis();
return CompletableFuture.completedFuture(rs); return CompletableFuture.completedFuture(rs);
} catch (IOException e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }

View File

@@ -228,6 +228,17 @@ public abstract class ProtocolServer {
public void accept() { public void accept() {
final AsynchronousServerSocketChannel serchannel = this.serverChannel; final AsynchronousServerSocketChannel serchannel = this.serverChannel;
serchannel.accept(null, new CompletionHandler<AsynchronousSocketChannel, Void>() { serchannel.accept(null, new CompletionHandler<AsynchronousSocketChannel, Void>() {
private boolean supportInited;
private boolean supportTcpLay;
private boolean supportAlive;
private boolean supportReuse;
private boolean supportRcv;
private boolean supportSnd;
@Override @Override
public void completed(final AsynchronousSocketChannel channel, Void attachment) { public void completed(final AsynchronousSocketChannel channel, Void attachment) {
@@ -244,6 +255,27 @@ public abstract class ProtocolServer {
AsyncConnection conn = AsyncConnection.create(channel, null, context); AsyncConnection conn = AsyncConnection.create(channel, null, context);
conn.livingCounter = livingCounter; conn.livingCounter = livingCounter;
conn.closedCounter = closedCounter; conn.closedCounter = closedCounter;
try {
if (!supportInited) {
synchronized (this) {
if (!supportInited) {
supportInited = true;
final Set<SocketOption<?>> 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)); context.runAsync(new PrepareRunner(context, conn, null, null));
} }