I used java to make simple chat program with AsynchronousChannelGroup, But some received data does not reach to completed. or reach very slow (1s ~ 2s). (I think it's random to client. Some client reach well to server but some client reach very slow and finally disconnect)
I try to get data callback way using code below
class socket
channel.read(readBuffer, this, new CompletionHandler<Integer, AsyncSocket>() {
@Override
public void completed(Integer size, AsyncSocket socket) {
ByteBuffer read = socket.readBuffer;
socket.callback.onRead(socket, read, read.limit() - read.position());
read.compact();
socket.channel.read(read, socket, this);
}
@Override
public void failed(Throwable e, AsyncSocket socket) {
// for close and output err
socket.closeSoft(new Exception(e));
}
});
class socket
public synchronized void write(ByteBuffer buffer) {
try {
while (buffer.hasRemaining()) {
channel.write(buffer).get();
}
} catch (Exception e) {
this.close(e);
}
}
class callback //server
public void onRead(AsyncSocket socket, ByteBuffer readBuffer, int size) {
// write every client using write() if all data transformed;
}
The callback omitted for some client when I try to access, and with a long time, err "The semaphore timeout" is showed in closeSoft method.
I think occurring condition is random and I cannot get any idea of this exception. Please help me. Thanks!