私はNIO2サーバーを書いています.AsynchronousSocketChannelで非同期読み取り操作を行う必要があります。これらの操作はそれぞれ、整数の読み取りと、同じチャネルからのこの整数に等しいバイト数の読み取りで構成されています。問題は、チャネルに 2 つ以上の CompletionHandler を続けて配置し (複数の読み取り操作の要求があるため)、これらのハンドラーの最初が起動さcomplete()
れると、最初のハンドラーのメソッドでさらに読み取りコードが正しく機能しないことです。チャンネルに情報があると、ハンドラーは即座に起動されます。さらなる読み取りが問題complete()
なく完了するまで、チャネルをブロックするにはどうすればよいFuture
ですか? ハンドラーをソケットに配置してから他のタスクに渡す必要があるため、Future を使用できません。
for (final Map.Entry<String, AsynchronousSocketChannel> entry : ipSocketTable.entrySet()) {
try {
final AsynchronousSocketChannel outSocket = entry.getValue();
synchronized (outSocket) {
final ByteBuffer buf = ByteBuffer.allocateDirect(9);
outSocket.read(buf, null, new DataServerResponseHandler(buf, outSocket, resultTable, server, entry.getKey()));
}
} catch (Exception e) {
}
}
DataServerResponseHandler クラスは次のとおりです。
class DataServerResponseHandler implements CompletionHandler<Integer, Void> {
private ConcurrentHashMap<String, Boolean> resultTable = null;
private AsynchronousSocketChannel channel = null;
private TcpServer server;
private String ip;
private ByteBuffer msg;
public DataServerResponseHandler(ByteBuffer msg, AsynchronousSocketChannel channel,
ConcurrentHashMap<String, Boolean> resultTable, TcpServer server, String ip) {
this.msg = msg;
this.channel = channel;
this.resultTable = resultTable;
this.server = server;
this.ip = ip;
}
@Override
public void completed(Integer result, Void attachment) {
try {
msg.rewind();
int resultCode = msg.get() & 0xff;
int ipOne = msg.get() & 0xff;
int ipTwo = msg.get() & 0xff;
int ipThree = msg.get() & 0xff;
int ipFour = msg.get() & 0xff;
int length = msg.getInt();
msg.rewind();
ByteBuffer buf = ByteBuffer.allocateDirect(length);
channel.read(buf).get();
buf.rewind();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void failed(Throwable exc, Void attachment) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}