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,92 +285,93 @@ 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) {
if (writeByteTuple1Array != null) { while (hasRemain) { //必须要将buffer写完为止
final ByteBuffer buffer = pollWriteBuffer(); if (writeByteTuple1Array != null) {
if (buffer.remaining() >= writeByteTuple1Length + writeByteTuple2Length) { final ByteBuffer buffer = pollWriteBuffer();
buffer.put(writeByteTuple1Array, writeByteTuple1Offset, writeByteTuple1Length); if (buffer.remaining() >= writeByteTuple1Length + writeByteTuple2Length) {
if (writeByteTuple2Length > 0) { buffer.put(writeByteTuple1Array, writeByteTuple1Offset, writeByteTuple1Length);
buffer.put(writeByteTuple2Array, writeByteTuple2Offset, writeByteTuple2Length); if (writeByteTuple2Length > 0) {
if (writeByteTuple2Callback != null) { buffer.put(writeByteTuple2Array, writeByteTuple2Offset, writeByteTuple2Length);
writeByteTuple2Callback.accept(writeByteTuple2Attachment); if (writeByteTuple2Callback != null) {
writeByteTuple2Callback.accept(writeByteTuple2Attachment);
}
} }
} buffer.flip();
buffer.flip(); writeByteBuffer = buffer;
writeByteBuffer = buffer; writeByteTuple1Array = null;
writeByteTuple1Array = null; writeByteTuple1Offset = 0;
writeByteTuple1Offset = 0; writeByteTuple1Length = 0;
writeByteTuple1Length = 0; writeByteTuple2Array = null;
writeByteTuple2Array = null; writeByteTuple2Offset = 0;
writeByteTuple2Offset = 0; writeByteTuple2Length = 0;
writeByteTuple2Length = 0; writeByteTuple2Callback = null;
writeByteTuple2Callback = null; writeByteTuple2Attachment = null;
writeByteTuple2Attachment = null;
} else {
ByteBufferWriter writer = ByteBufferWriter.create(getWriteBufferSupplier(), buffer);
writer.put(writeByteTuple1Array, writeByteTuple1Offset, writeByteTuple1Length);
if (writeByteTuple2Length > 0) {
writer.put(writeByteTuple2Array, writeByteTuple2Offset, writeByteTuple2Length);
if (writeByteTuple2Callback != null) {
writeByteTuple2Callback.accept(writeByteTuple2Attachment);
}
}
final ByteBuffer[] buffers = writer.toBuffers();
writeByteBuffers = buffers;
writeOffset = 0;
writeLength = buffers.length;
writeByteTuple1Array = null;
writeByteTuple1Offset = 0;
writeByteTuple1Length = 0;
writeByteTuple2Array = null;
writeByteTuple2Offset = 0;
writeByteTuple2Length = 0;
writeByteTuple2Callback = null;
writeByteTuple2Attachment = null;
}
if (this.writeCompletionHandler == this.writeTimeoutCompletionHandler) {
if (writeByteBuffer == null) {
this.writeTimeoutCompletionHandler.buffers(writeByteBuffers);
} else { } else {
this.writeTimeoutCompletionHandler.buffer(writeByteBuffer); ByteBufferWriter writer = ByteBufferWriter.create(getWriteBufferSupplier(), buffer);
writer.put(writeByteTuple1Array, writeByteTuple1Offset, writeByteTuple1Length);
if (writeByteTuple2Length > 0) {
writer.put(writeByteTuple2Array, writeByteTuple2Offset, writeByteTuple2Length);
if (writeByteTuple2Callback != null) {
writeByteTuple2Callback.accept(writeByteTuple2Attachment);
}
}
final ByteBuffer[] buffers = writer.toBuffers();
writeByteBuffers = buffers;
writeOffset = 0;
writeLength = buffers.length;
writeByteTuple1Array = null;
writeByteTuple1Offset = 0;
writeByteTuple1Length = 0;
writeByteTuple2Array = null;
writeByteTuple2Offset = 0;
writeByteTuple2Length = 0;
writeByteTuple2Callback = null;
writeByteTuple2Attachment = null;
}
if (this.writeCompletionHandler == this.writeTimeoutCompletionHandler) {
if (writeByteBuffer == null) {
this.writeTimeoutCompletionHandler.buffers(writeByteBuffers);
} else {
this.writeTimeoutCompletionHandler.buffer(writeByteBuffer);
}
} }
} }
} int writeCount;
int writeCount; if (writeByteBuffer != null) {
if (writeByteBuffer != null) { writeCount = implWrite(writeByteBuffer);
writeCount = implWrite(writeByteBuffer); hasRemain = writeByteBuffer.hasRemaining();
hasRemain = writeByteBuffer.hasRemaining(); } else {
} else { writeCount = implWrite(writeByteBuffers, writeOffset, writeLength);
writeCount = implWrite(writeByteBuffers, writeOffset, writeLength); boolean remain = false;
boolean remain = false; for (int i = writeByteBuffers.length - 1; i >= writeOffset; i--) {
for (int i = writeByteBuffers.length - 1; i >= writeOffset; i--) { if (writeByteBuffers[i].hasRemaining()) {
if (writeByteBuffers[i].hasRemaining()) { remain = true;
remain = true; break;
break; }
} }
hasRemain = remain;
} }
hasRemain = remain; if (writeCount == 0) {
} if (hasRemain) {
if (writeCount == 0) { writeCompleted = false;
if (hasRemain) { writeTotal = totalCount;
writeCompleted = false; }
writeTotal = totalCount; break;
} else if (writeCount < 0) {
if (totalCount == 0) {
totalCount = writeCount;
}
break;
} else {
totalCount += writeCount;
} }
break; if (!hasRemain) {
} else if (writeCount < 0) { break;
if (totalCount == 0) {
totalCount = writeCount;
} }
break;
} else {
totalCount += writeCount;
}
if (!hasRemain) {
break;
} }
} }
@@ -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()) {