AsyncNioConnection优化

This commit is contained in:
redkale
2023-01-30 13:53:03 +08:00
parent 823b89ec48
commit 7dc57c67ed

View File

@@ -35,7 +35,7 @@ abstract class AsyncNioConnection extends AsyncConnection {
protected CompletionHandler<Void, Object> connectCompletionHandler; protected CompletionHandler<Void, Object> connectCompletionHandler;
protected boolean connectPending; protected volatile boolean connectPending;
protected SelectionKey connectKey; protected SelectionKey connectKey;
@@ -48,7 +48,7 @@ abstract class AsyncNioConnection extends AsyncConnection {
protected CompletionHandler<Integer, ByteBuffer> readCompletionHandler; protected CompletionHandler<Integer, ByteBuffer> readCompletionHandler;
protected boolean readPending; protected volatile boolean readPending;
protected SelectionKey readKey; protected SelectionKey readKey;
@@ -88,7 +88,7 @@ abstract class AsyncNioConnection extends AsyncConnection {
protected CompletionHandler<Integer, Object> writeCompletionHandler; protected CompletionHandler<Integer, Object> writeCompletionHandler;
protected boolean writePending; protected volatile boolean writePending;
protected SelectionKey writeKey; protected SelectionKey writeKey;
@@ -285,11 +285,11 @@ abstract class AsyncNioConnection extends AsyncConnection {
public void doWrite(boolean direct) { public void doWrite(boolean direct) {
try { try {
this.writetime = System.currentTimeMillis(); this.writetime = System.currentTimeMillis();
final boolean invokeDirect = direct;
int totalCount = 0; int totalCount = 0;
boolean hasRemain = true; boolean hasRemain = true;
boolean writeCompleted = true; boolean writeCompleted = true;
while (invokeDirect && hasRemain) { //必须要将buffer写完为止 if (direct) {
while (hasRemain) { //必须要将buffer写完为止
if (writeByteTuple1Array != null) { if (writeByteTuple1Array != null) {
final ByteBuffer buffer = pollWriteBuffer(); final ByteBuffer buffer = pollWriteBuffer();
if (buffer.remaining() >= writeByteTuple1Length + writeByteTuple2Length) { if (buffer.remaining() >= writeByteTuple1Length + writeByteTuple2Length) {
@@ -373,6 +373,7 @@ abstract class AsyncNioConnection extends AsyncConnection {
break; break;
} }
} }
}
if (writeCompleted && (totalCount != 0 || !hasRemain)) { if (writeCompleted && (totalCount != 0 || !hasRemain)) {
handleWrite(writeTotal + totalCount, null); handleWrite(writeTotal + totalCount, null);
@@ -469,8 +470,6 @@ abstract class AsyncNioConnection extends AsyncConnection {
ByteBuffer bb; ByteBuffer bb;
int count;
@Override @Override
public synchronized int read() throws IOException { public synchronized int read() throws IOException {
if (bb == null || !bb.hasRemaining()) { if (bb == null || !bb.hasRemaining()) {