优化AsyncConnection
This commit is contained in:
@@ -16,6 +16,8 @@ import javax.net.ssl.SSLEngineResult.HandshakeStatus;
|
|||||||
import static javax.net.ssl.SSLEngineResult.HandshakeStatus.*;
|
import static javax.net.ssl.SSLEngineResult.HandshakeStatus.*;
|
||||||
import static javax.net.ssl.SSLEngineResult.Status.*;
|
import static javax.net.ssl.SSLEngineResult.Status.*;
|
||||||
import javax.net.ssl.*;
|
import javax.net.ssl.*;
|
||||||
|
import static javax.net.ssl.SSLEngineResult.HandshakeStatus.*;
|
||||||
|
import static javax.net.ssl.SSLEngineResult.Status.*;
|
||||||
import org.redkale.util.*;
|
import org.redkale.util.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -193,6 +195,14 @@ public abstract class AsyncConnection implements ChannelContext, Channel, AutoCl
|
|||||||
read(handler);
|
read(handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public final void startReadInIOThread(CompletionHandler<Integer, ByteBuffer> handler) {
|
||||||
|
if (inCurrThread()) {
|
||||||
|
startRead(handler);
|
||||||
|
} else {
|
||||||
|
execute(() -> startRead(handler));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public final void read(CompletionHandler<Integer, ByteBuffer> handler) {
|
public final void read(CompletionHandler<Integer, ByteBuffer> handler) {
|
||||||
if (sslEngine == null) {
|
if (sslEngine == null) {
|
||||||
readImpl(handler);
|
readImpl(handler);
|
||||||
@@ -201,6 +211,14 @@ public abstract class AsyncConnection implements ChannelContext, Channel, AutoCl
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public final void readInIOThread(CompletionHandler<Integer, ByteBuffer> handler) {
|
||||||
|
if (inCurrThread()) {
|
||||||
|
read(handler);
|
||||||
|
} else {
|
||||||
|
execute(() -> read(handler));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//src写完才会回调
|
//src写完才会回调
|
||||||
public final <A> void write(ByteBuffer src, A attachment, CompletionHandler<Integer, ? super A> handler) {
|
public final <A> void write(ByteBuffer src, A attachment, CompletionHandler<Integer, ? super A> handler) {
|
||||||
if (sslEngine == null) {
|
if (sslEngine == null) {
|
||||||
|
|||||||
@@ -228,11 +228,7 @@ public abstract class Response<C extends Context, R extends Request<C>> {
|
|||||||
AsyncConnection conn = removeChannel();
|
AsyncConnection conn = removeChannel();
|
||||||
if (conn != null && conn.protocolCodec != null) {
|
if (conn != null && conn.protocolCodec != null) {
|
||||||
this.responseConsumer.accept(this);
|
this.responseConsumer.accept(this);
|
||||||
if (conn.inCurrThread()) {
|
conn.readInIOThread(conn.protocolCodec);
|
||||||
conn.read(conn.protocolCodec);
|
|
||||||
} else {
|
|
||||||
conn.execute(() -> conn.read(conn.protocolCodec));
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
Supplier<Response> poolSupplier = this.responseSupplier;
|
Supplier<Response> poolSupplier = this.responseSupplier;
|
||||||
Consumer<Response> poolConsumer = this.responseConsumer;
|
Consumer<Response> poolConsumer = this.responseConsumer;
|
||||||
@@ -405,7 +401,11 @@ public abstract class Response<C extends Context, R extends Request<C>> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void completed(Integer result, A attachment) {
|
public void completed(Integer result, A attachment) {
|
||||||
channel.offerBuffer(buffer);
|
if (buffer != writeBuffer) {
|
||||||
|
channel.offerBuffer(buffer);
|
||||||
|
} else {
|
||||||
|
buffer.clear();
|
||||||
|
}
|
||||||
if (handler != null) {
|
if (handler != null) {
|
||||||
handler.completed(result, attachment);
|
handler.completed(result, attachment);
|
||||||
}
|
}
|
||||||
@@ -413,7 +413,11 @@ public abstract class Response<C extends Context, R extends Request<C>> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void failed(Throwable exc, A attachment) {
|
public void failed(Throwable exc, A attachment) {
|
||||||
channel.offerBuffer(buffer);
|
if (buffer != writeBuffer) {
|
||||||
|
channel.offerBuffer(buffer);
|
||||||
|
} else {
|
||||||
|
buffer.clear();
|
||||||
|
}
|
||||||
if (handler != null) {
|
if (handler != null) {
|
||||||
handler.failed(exc, attachment);
|
handler.failed(exc, attachment);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user