ProtocolCodec优化
This commit is contained in:
@@ -9,7 +9,7 @@ import java.io.IOException;
|
|||||||
import java.net.*;
|
import java.net.*;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.channels.*;
|
import java.nio.channels.*;
|
||||||
import java.util.*;
|
import java.util.Set;
|
||||||
import java.util.concurrent.atomic.LongAdder;
|
import java.util.concurrent.atomic.LongAdder;
|
||||||
import java.util.function.*;
|
import java.util.function.*;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
@@ -133,17 +133,17 @@ class AsyncNioTcpProtocolServer extends ProtocolServer {
|
|||||||
final AsyncIOThread[] ioWriteThreads = ioGroup.ioWriteThreads;
|
final AsyncIOThread[] ioWriteThreads = ioGroup.ioWriteThreads;
|
||||||
int threads = ioReadThreads.length;
|
int threads = ioReadThreads.length;
|
||||||
int threadIndex = -1;
|
int threadIndex = -1;
|
||||||
|
Set<SelectionKey> keys = null;
|
||||||
while (!closed) {
|
while (!closed) {
|
||||||
try {
|
try {
|
||||||
int count = selector.select();
|
int count = selector.select();
|
||||||
if (count == 0) {
|
if (count == 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
Set<SelectionKey> keys = selector.selectedKeys();
|
if (keys == null) {
|
||||||
Iterator<SelectionKey> it = keys.iterator();
|
keys = selector.selectedKeys();
|
||||||
while (it.hasNext()) {
|
}
|
||||||
SelectionKey key = it.next();
|
for (SelectionKey key : keys) {
|
||||||
it.remove();
|
|
||||||
if (key.isAcceptable()) {
|
if (key.isAcceptable()) {
|
||||||
if (++threadIndex >= threads) {
|
if (++threadIndex >= threads) {
|
||||||
threadIndex = 0;
|
threadIndex = 0;
|
||||||
@@ -151,6 +151,7 @@ class AsyncNioTcpProtocolServer extends ProtocolServer {
|
|||||||
accept(key, ioReadThreads[threadIndex], ioWriteThreads[threadIndex]);
|
accept(key, ioReadThreads[threadIndex], ioWriteThreads[threadIndex]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
keys.clear();
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
server.logger.log(Level.SEVERE, "server accept error", t);
|
server.logger.log(Level.SEVERE, "server accept error", t);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,14 +18,14 @@ import java.util.logging.Level;
|
|||||||
*/
|
*/
|
||||||
class ProtocolCodec implements CompletionHandler<Integer, ByteBuffer> {
|
class ProtocolCodec implements CompletionHandler<Integer, ByteBuffer> {
|
||||||
|
|
||||||
private final AsyncConnection channel;
|
|
||||||
|
|
||||||
private final Context context;
|
private final Context context;
|
||||||
|
|
||||||
private final Supplier<Response> responseSupplier;
|
private final Supplier<Response> responseSupplier;
|
||||||
|
|
||||||
private final Consumer<Response> responseConsumer;
|
private final Consumer<Response> responseConsumer;
|
||||||
|
|
||||||
|
private AsyncConnection channel;
|
||||||
|
|
||||||
private Response resp;
|
private Response resp;
|
||||||
|
|
||||||
public ProtocolCodec(Context context, Supplier<Response> responseSupplier,
|
public ProtocolCodec(Context context, Supplier<Response> responseSupplier,
|
||||||
@@ -36,6 +36,15 @@ class ProtocolCodec implements CompletionHandler<Integer, ByteBuffer> {
|
|||||||
this.responseConsumer = responseConsumer;
|
this.responseConsumer = responseConsumer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void prepare() {
|
||||||
|
}
|
||||||
|
|
||||||
|
protected boolean recycle() {
|
||||||
|
this.channel = null;
|
||||||
|
this.resp = null;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
public ProtocolCodec response(Response resp) {
|
public ProtocolCodec response(Response resp) {
|
||||||
this.resp = resp;
|
this.resp = resp;
|
||||||
return this;
|
return this;
|
||||||
|
|||||||
Reference in New Issue
Block a user