Skip to content

Commit

Permalink
Consider failing login task (FAForever#1664)
Browse files Browse the repository at this point in the history
make sure the login future is completed exceptionally if anything unsuspected happens.
Prepares FAForever#1663
  • Loading branch information
1-alex98 authored and Chris Haggan committed Apr 15, 2022
1 parent d3780ff commit fe3a1c1
Showing 1 changed file with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;

import javax.security.auth.login.LoginException;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
Expand Down Expand Up @@ -219,10 +219,10 @@ public CompletableFuture<LoginMessage> connectAndLogIn(String username, String p
this.password = password;

// TODO extract class?
fafConnectionTask = new Task<Void>() {
fafConnectionTask = new Task<>() {

@Override
protected Void call() throws Exception {
protected Void call() {
while (!isCancelled()) {
Server server = clientProperties.getServer();
String serverHost = server.getHost();
Expand Down Expand Up @@ -254,6 +254,12 @@ protected Void call() throws Exception {
if (isCancelled()) {
log.debug("Connection to FAF server has been closed");
} else {
if (loginFuture != null) {
loginFuture.completeExceptionally(new LoginFailedException("Lost connection to server during login"));
loginFuture = null;
fafConnectionTask.cancel();
return null;
}
log.warn("Lost connection to Server", e);
reconnectTimerService.incrementConnectionFailures();
reconnectTimerService.waitForReconnect();
Expand All @@ -263,6 +269,18 @@ protected Void call() throws Exception {
return null;
}

@Override
protected void failed() {
super.failed();
log.error("Server connection task failed", getException());
if (loginFuture != null) {
loginFuture.completeExceptionally(new LoginException("The server connection task failed, an internal error occurred"));
loginFuture = null;
}
IOUtils.closeQuietly(serverWriter);
IOUtils.closeQuietly(fafServerSocket);
}

@Override
protected void cancelled() {
IOUtils.closeQuietly(serverWriter);
Expand Down Expand Up @@ -464,10 +482,7 @@ private void parseServerObject(String jsonString) {
.forEach(consumer -> consumer.accept(serverMessage));
messageClass = messageClass.getSuperclass();
}
for (Class<?> type : ClassUtils.getAllInterfacesForClassAsSet(messageClass)) {
messageListeners.getOrDefault(messageClass, Collections.emptyList())
.forEach(consumer -> consumer.accept(serverMessage));
}

} catch (JsonSyntaxException e) {
log.warn("Could not deserialize message: " + jsonString, e);
}
Expand All @@ -489,6 +504,8 @@ private void onFafLoginSucceeded(LoginMessage loginServerMessage) {
if (loginFuture != null) {
loginFuture.complete(loginServerMessage);
loginFuture = null;
} else {
log.warn("Unexpected login message from server");
}
}

Expand Down

0 comments on commit fe3a1c1

Please sign in to comment.