Skip to content

Commit

Permalink
Faster disconnect on loss of connection
Browse files Browse the repository at this point in the history
This attempts to fix an issue, where certain players could sometimes not log in for up to 20 minutes after they lost connection to the server.
  • Loading branch information
neon-dev committed May 24, 2024
1 parent fc9cd94 commit 28a6357
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
35 changes: 16 additions & 19 deletions commons/src/com/aionemu/commons/network/AConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,6 @@ final void setKey(SelectionKey key) {
this.key = key;
}

/**
* Notify Dispatcher Selector that we want write some data here.
*/
protected final void enableWriteInterest() {
if (key.isValid()) {
key.interestOps(key.interestOps() | SelectionKey.OP_WRITE);
key.selector().wakeup();
}
}

/**
* @return SocketChannel representing this connection.
*/
Expand All @@ -115,8 +105,13 @@ public final void sendPacket(T serverPacket) {
if (pendingClose || closed)
return;

getSendMsgQueue().add(serverPacket);
enableWriteInterest();
if (isConnected()) {
getSendMsgQueue().add(serverPacket);
key.interestOps(key.interestOps() | SelectionKey.OP_WRITE);
key.selector().wakeup();
} else {
close();
}
}
}

Expand All @@ -140,16 +135,14 @@ public final void close(T closePacket) {
return;

pendingClose = true;
if (closePacket != null) {
if (closePacket != null || !isConnected())
getSendMsgQueue().clear();
if (closePacket != null && isConnected()) {
getSendMsgQueue().add(closePacket);
enableWriteInterest();
dispatcher.closeConnection(this);
} else {
dispatcher.closeConnection(this);
if (key.isValid())
key.selector().wakeup(); // notify dispatcher
key.interestOps(SelectionKey.OP_WRITE);
}
dispatcher.closeConnection(this);
key.selector().wakeup(); // notify dispatcher
}
}

Expand Down Expand Up @@ -179,6 +172,10 @@ final void disconnect(Executor dcExecutor) {
dcExecutor.execute(this::onDisconnect);
}

final boolean isConnected() {
return key.isValid();
}

/**
* @return True if this connection is pendingClose and not closed yet.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private void processPendingClose() {
synchronized (pendingClose) {
for (Iterator<AConnection<?>> iterator = pendingClose.iterator(); iterator.hasNext();) {
AConnection<?> connection = iterator.next();
if (connection.getSendMsgQueue().isEmpty() || !connection.getSocketChannel().isConnected()) {
if (connection.getSendMsgQueue().isEmpty() || !connection.isConnected()) {
closeConnectionImpl(connection);
iterator.remove();
}
Expand Down

0 comments on commit 28a6357

Please sign in to comment.