fix event processing in network code
This commit is contained in:
@@ -362,45 +362,63 @@ public abstract class AbstractConnectionManager extends ControlledRunnable {
|
|||||||
|
|
||||||
selectedKeys = this.selector.selectedKeys().iterator();
|
selectedKeys = this.selector.selectedKeys().iterator();
|
||||||
|
|
||||||
if (selectedKeys.hasNext() == false)
|
// No need for the "if selectedKeys.hasNext() == false" check here
|
||||||
return;
|
|
||||||
|
|
||||||
while (selectedKeys.hasNext()) {
|
while (selectedKeys.hasNext()) {
|
||||||
thisKey = selectedKeys.next();
|
thisKey = selectedKeys.next();
|
||||||
|
|
||||||
//To shake out any issues
|
// To shake out any issues, logging null attachments
|
||||||
if (thisKey.attachment() == null)
|
if (thisKey.attachment() == null) {
|
||||||
Logger.error("Found null attachment! PANIC!");
|
Logger.error("Found null attachment! PANIC!");
|
||||||
|
selectedKeys.remove(); // Safely remove invalid keys from the set
|
||||||
|
continue; // Skip to the next key
|
||||||
|
}
|
||||||
|
|
||||||
if (thisKey.attachment() instanceof AbstractConnection)
|
// Check for task execution state and continue if already running
|
||||||
if (((AbstractConnection) thisKey.attachment()).execTask.get() == true)
|
if (thisKey.attachment() instanceof AbstractConnection) {
|
||||||
continue;
|
AbstractConnection connection = (AbstractConnection) thisKey.attachment();
|
||||||
|
if (connection.execTask.get()) {
|
||||||
selectedKeys.remove();
|
selectedKeys.remove(); // Safely remove the key from selected keys
|
||||||
|
continue; // Skip to the next key
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (thisKey.isValid() == false)
|
// Validity check for the key, move the removal after key validation
|
||||||
break; // Changed from continue
|
if (!thisKey.isValid()) {
|
||||||
else if (thisKey.isAcceptable())
|
Logger.warn("Invalid SelectionKey found. Skipping.");
|
||||||
|
selectedKeys.remove(); // Remove invalid keys after validity check
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Process the appropriate operation based on the key state
|
||||||
|
if (thisKey.isAcceptable()) {
|
||||||
this.acceptNewConnection(thisKey);
|
this.acceptNewConnection(thisKey);
|
||||||
else if (thisKey.isReadable())
|
} else if (thisKey.isReadable()) {
|
||||||
jm.submitJob(new ReadOperationHander(thisKey));
|
jm.submitJob(new ReadOperationHander(thisKey));
|
||||||
else if (thisKey.isWritable())
|
} else if (thisKey.isWritable()) {
|
||||||
jm.submitJob(new WriteOperationHander(thisKey));
|
jm.submitJob(new WriteOperationHander(thisKey));
|
||||||
else if (thisKey.isConnectable())
|
} else if (thisKey.isConnectable()) {
|
||||||
this.finishConnectingTo(thisKey);
|
this.finishConnectingTo(thisKey);
|
||||||
else
|
} else {
|
||||||
Logger.error("Unhandled keystate: " + thisKey.toString());
|
Logger.error("Unhandled keystate: " + thisKey.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Safely remove processed key
|
||||||
|
selectedKeys.remove();
|
||||||
|
|
||||||
} catch (CancelledKeyException cke) {
|
} catch (CancelledKeyException cke) {
|
||||||
Logger.error(this.getLocalNodeName(), cke);
|
Logger.error(this.getLocalNodeName(), cke);
|
||||||
this.disconnect(thisKey);
|
this.disconnect(thisKey);
|
||||||
|
selectedKeys.remove(); // Safely remove cancelled key
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Logger.error(this.getLocalNodeName(), e);
|
Logger.error(this.getLocalNodeName(), e);
|
||||||
|
selectedKeys.remove(); // Safely remove on error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected void connectTo(String host, int port) {
|
protected void connectTo(String host, int port) {
|
||||||
try {
|
try {
|
||||||
this.connectTo(InetAddress.getByName(host), port);
|
this.connectTo(InetAddress.getByName(host), port);
|
||||||
|
|||||||
Reference in New Issue
Block a user