I have a server with NIO realization with selector. The realization is rather simple:
selector.select();
Set<SelectionKey> keys = selector.selectedKeys();
for (SelectionKey key : keys) {
if (! key.isValid()) {
continue;
}
UserConnection connection = users.get(key);
if (key.isReadable()) {
processReadableKey(key, connection);
} else if (key.isWritable()) {
processWritableKey(key, connection);
}
The thing is, when two packets (messages) arrives one after another almost immediately, selector reacts and processes the first one, but then it does not reacts and marks the corresponding key as "readable" to handle the second one.
When the next message arrives, selector handles it and after that it processes that "lost" packet.
I don't know, how to fix it. I tried to reduce channel buffer, and i tried to wake up selector by selector.wakeup()
, but it did not help, because the problem is connected with recognizing the key as "readable" immediately after hanling the first message in the couple.
Any ideas?