From 054253fb90d54031b284997d73b69790f4676677 Mon Sep 17 00:00:00 2001 From: Redkale <22250530@qq.com> Date: Mon, 30 Oct 2017 19:19:31 +0800 Subject: [PATCH] =?UTF-8?q?AsyncConnection=E5=A2=9E=E5=8A=A0=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E6=9C=80=E5=90=8E=E4=B8=80=E6=AC=A1=E8=BF=9B=E8=A1=8C?= =?UTF-8?q?=E8=AF=BB=E5=86=99IO=E6=93=8D=E4=BD=9C=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/org/redkale/net/AsyncConnection.java | 25 ++++++++++++++++++++++++ src/org/redkale/net/Filter.java | 2 +- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/org/redkale/net/AsyncConnection.java b/src/org/redkale/net/AsyncConnection.java index 06db74e20..ec63bf175 100644 --- a/src/org/redkale/net/AsyncConnection.java +++ b/src/org/redkale/net/AsyncConnection.java @@ -26,12 +26,24 @@ public abstract class AsyncConnection implements AsynchronousByteChannel, AutoCl protected Object subobject; //用于存储绑定在Connection上的对象, 同attributes, 只绑定单个对象时尽量使用subobject而非attributes + protected volatile long readtime; + + protected volatile long writetime; + //关闭数 AtomicLong closedCounter = new AtomicLong(); //在线数 AtomicLong livingCounter = new AtomicLong(); + public final long getLastReadTime() { + return readtime; + } + + public final long getLastWriteTime() { + return writetime; + } + public abstract boolean isTCP(); public abstract SocketAddress getRemoteAddress(); @@ -209,6 +221,7 @@ public abstract class AsyncConnection implements AsynchronousByteChannel, AutoCl rs += channel.send(srcs[i], remoteAddress); if (i != offset) Thread.sleep(10); } + this.writetime = System.currentTimeMillis(); if (handler != null) handler.completed(rs, attachment); } catch (Exception e) { if (handler != null) handler.failed(e, attachment); @@ -219,6 +232,7 @@ public abstract class AsyncConnection implements AsynchronousByteChannel, AutoCl public void read(ByteBuffer dst, A attachment, CompletionHandler handler) { try { int rs = channel.read(dst); + this.readtime = System.currentTimeMillis(); if (handler != null) handler.completed(rs, attachment); } catch (IOException e) { if (handler != null) handler.failed(e, attachment); @@ -229,6 +243,7 @@ public abstract class AsyncConnection implements AsynchronousByteChannel, AutoCl public Future read(ByteBuffer dst) { try { int rs = channel.read(dst); + this.readtime = System.currentTimeMillis(); return CompletableFuture.completedFuture(rs); } catch (IOException e) { throw new RuntimeException(e); @@ -239,6 +254,7 @@ public abstract class AsyncConnection implements AsynchronousByteChannel, AutoCl public void write(ByteBuffer src, A attachment, CompletionHandler handler) { try { int rs = channel.send(src, remoteAddress); + this.writetime = System.currentTimeMillis(); if (handler != null) handler.completed(rs, attachment); } catch (IOException e) { if (handler != null) handler.failed(e, attachment); @@ -249,6 +265,7 @@ public abstract class AsyncConnection implements AsynchronousByteChannel, AutoCl public Future write(ByteBuffer src) { try { int rs = channel.send(src, remoteAddress); + this.writetime = System.currentTimeMillis(); return CompletableFuture.completedFuture(rs); } catch (IOException e) { throw new RuntimeException(e); @@ -359,6 +376,7 @@ public abstract class AsyncConnection implements AsynchronousByteChannel, AutoCl for (int i = offset; i < offset + length; i++) { rs += writeChannel.write(srcs[i]); } + this.writetime = System.currentTimeMillis(); if (handler != null) handler.completed(rs, attachment); } catch (IOException e) { if (handler != null) handler.failed(e, attachment); @@ -369,6 +387,7 @@ public abstract class AsyncConnection implements AsynchronousByteChannel, AutoCl public void read(ByteBuffer dst, A attachment, CompletionHandler handler) { try { int rs = readChannel.read(dst); + this.readtime = System.currentTimeMillis(); if (handler != null) handler.completed(rs, attachment); } catch (IOException e) { if (handler != null) handler.failed(e, attachment); @@ -379,6 +398,7 @@ public abstract class AsyncConnection implements AsynchronousByteChannel, AutoCl public Future read(ByteBuffer dst) { try { int rs = readChannel.read(dst); + this.readtime = System.currentTimeMillis(); return CompletableFuture.completedFuture(rs); } catch (IOException e) { throw new RuntimeException(e); @@ -389,6 +409,7 @@ public abstract class AsyncConnection implements AsynchronousByteChannel, AutoCl public void write(ByteBuffer src, A attachment, CompletionHandler handler) { try { int rs = writeChannel.write(src); + this.writetime = System.currentTimeMillis(); if (handler != null) handler.completed(rs, attachment); } catch (IOException e) { if (handler != null) handler.failed(e, attachment); @@ -399,6 +420,7 @@ public abstract class AsyncConnection implements AsynchronousByteChannel, AutoCl public Future write(ByteBuffer src) { try { int rs = writeChannel.write(src); + this.writetime = System.currentTimeMillis(); return CompletableFuture.completedFuture(rs); } catch (IOException e) { throw new RuntimeException(e); @@ -459,6 +481,7 @@ public abstract class AsyncConnection implements AsynchronousByteChannel, AutoCl @Override public void read(ByteBuffer dst, A attachment, CompletionHandler handler) { + this.readtime = System.currentTimeMillis(); if (readTimeoutSecond > 0) { channel.read(dst, readTimeoutSecond, TimeUnit.SECONDS, attachment, handler); } else { @@ -468,6 +491,7 @@ public abstract class AsyncConnection implements AsynchronousByteChannel, AutoCl @Override public void write(ByteBuffer src, A attachment, CompletionHandler handler) { + this.writetime = System.currentTimeMillis(); if (writeTimeoutSecond > 0) { channel.write(src, writeTimeoutSecond, TimeUnit.SECONDS, attachment, handler); } else { @@ -477,6 +501,7 @@ public abstract class AsyncConnection implements AsynchronousByteChannel, AutoCl @Override public void write(ByteBuffer[] srcs, int offset, int length, A attachment, final CompletionHandler handler) { + this.writetime = System.currentTimeMillis(); channel.write(srcs, offset, length, writeTimeoutSecond > 0 ? writeTimeoutSecond : 60, TimeUnit.SECONDS, attachment, new CompletionHandler() { diff --git a/src/org/redkale/net/Filter.java b/src/org/redkale/net/Filter.java index 49d9348d5..6b145f7d4 100644 --- a/src/org/redkale/net/Filter.java +++ b/src/org/redkale/net/Filter.java @@ -35,7 +35,7 @@ public abstract class Filter, P extends } @Override - public final int compareTo(Object o) { + public int compareTo(Object o) { if (!(o instanceof Filter)) return 1; Priority p1 = this.getClass().getAnnotation(Priority.class); Priority p2 = o.getClass().getAnnotation(Priority.class);