Skip to content

Commit

Permalink
De-static RPCService
Browse files Browse the repository at this point in the history
  • Loading branch information
Brutus5000 committed Dec 9, 2024
1 parent 10be8db commit 38c1074
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class IceAdapter implements Callable<Integer>, AutoCloseable, FafRpcCallb
private IceOptions iceOptions;

private GPGNetServer gpgNetServer;
private RPCService rpcService;

private final ExecutorService executor = ExecutorHolder.getExecutor();
private static final Lock lockGameSession = new ReentrantLock();
Expand Down Expand Up @@ -61,8 +62,11 @@ public void start() {

PeerIceModule.setForceRelay(iceOptions.isForceRelay());
gpgNetServer = new GPGNetServer();
gpgNetServer.init(iceOptions.getGpgnetPort(), iceOptions.getLobbyPort());
RPCService.init(iceOptions.getRpcPort(), this);
gpgNetServer.init(iceOptions.getGpgnetPort(), iceOptions.getLobbyPort(), rpcService);
rpcService.init(iceOptions.getRpcPort(), gpgNetServer, this);

PeerIceModule.setForceRelay(iceOptions.isForceRelay());
PeerIceModule.setRpcService(rpcService);

debug().startupComplete();
}
Expand All @@ -88,7 +92,7 @@ public void onConnectToPeer(String remotePlayerLogin, int remotePlayerId, boolea
if (gpgNetServer.isConnected()
&& gpgNetServer.getGameState().isPresent()
&& (gpgNetServer.getGameState().get() == GameState.LAUNCHING
|| GPGNetServer.getGameState().get() == GameState.ENDED)) {
|| gpgNetServer.getGameState().get() == GameState.ENDED)) {
log.warn("Game ended or in progress, ABORTING connectToPeer");
return;
}
Expand Down Expand Up @@ -146,7 +150,7 @@ public static void close(int status) {

onFAShutdown(); // will close gameSession aswell
INSTANCE.gpgNetServer.close();
RPCService.close();
INSTANCE.rpcService.close();
Debug.close();
TrayIcon.close();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class GPGNetServer implements AutoCloseable {

private int gpgnetPort;
private int lobbyPort;
private RPCService rpcService;
private ServerSocket serverSocket;
private volatile GPGNetClient currentClient;

Expand All @@ -49,8 +50,9 @@ public static void setLobbyInitMode(LobbyInitMode mode) {
INSTANCE.lobbyInitMode = mode;
}

public void init(int gpgnetPort, int lobbyPort) {
public void init(int gpgnetPort, int lobbyPort, RPCService rpcService) {
INSTANCE = this;
this.rpcService = rpcService;

if (gpgnetPort == 0) {
this.gpgnetPort = NetworkToolbox.findFreeTCPPort(20000, 65536);
Expand Down Expand Up @@ -103,7 +105,7 @@ private GPGNetClient(Socket socket) {
}
listenerThread = Thread.startVirtualThread(this::listenerThread);

RPCService.onConnectionStateChanged("Connected");
rpcService.onConnectionStateChanged("Connected");
log.info("GPGNetClient has connected");
}

Expand Down Expand Up @@ -145,7 +147,7 @@ private void processGpgnetMessage(String command, List<Object> args) {
"Received GPGNet message: {} {}",
command,
args.stream().map(Object::toString).collect(Collectors.joining(" ")));
RPCService.onGpgNetMessageReceived(command, args);
rpcService.onGpgNetMessageReceived(command, args);
}

/**
Expand Down Expand Up @@ -227,7 +229,7 @@ private void onGpgnetConnectionLost() {
clientFuture = new CompletableFuture<>();
}

RPCService.onConnectionStateChanged("Disconnected");
rpcService.onConnectionStateChanged("Disconnected");

IceAdapter.onFAShutdown();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.ice4j.TransportAddress;
import org.ice4j.ice.*;
Expand All @@ -28,6 +29,9 @@
@Getter
@Slf4j
public class PeerIceModule {
@Setter
private static RPCService rpcService;

private static boolean ALLOW_HOST = true;
private static boolean ALLOW_REFLEXIVE = true;
private static boolean ALLOW_RELAY = true;
Expand Down Expand Up @@ -85,7 +89,7 @@ public PeerIceModule(Peer peer) {
*/
private void setState(IceState newState) {
this.iceState = newState;
RPCService.onIceConnectionStateChanged(IceAdapter.getId(), peer.getRemoteId(), iceState.getMessage());
rpcService.onIceConnectionStateChanged(IceAdapter.getId(), peer.getRemoteId(), iceState.getMessage());
debug().peerStateChanged(this.peer);
}

Expand Down Expand Up @@ -200,7 +204,7 @@ a, new LongTermCredential(iceServer.getTurnUsername(), iceServer.getTurnCredenti
.map(it -> it.type().toString() + "(" + it.protocol() + ")")
.collect(Collectors.joining(", ")));
setState(AWAITING_CANDIDATES);
RPCService.onIceMsg(localCandidatesMessage);
rpcService.onIceMsg(localCandidatesMessage);

// Make sure to abort the connection process and reinitiate when we haven't received an answer to our offer in 6
// seconds, candidate packet was probably lost
Expand Down Expand Up @@ -358,7 +362,7 @@ private void startIce() {

// We are connected
connected = true;
RPCService.onConnected(IceAdapter.getId(), peer.getRemoteId(), true);
rpcService.onConnected(IceAdapter.getId(), peer.getRemoteId(), true);
setState(CONNECTED);

if (component.getSelectedPair().getLocalCandidate().getType() == CandidateType.RELAYED_CANDIDATE) {
Expand Down Expand Up @@ -403,7 +407,7 @@ public void onConnectionLost() {
if (connected) {
connected = false;
log.warn("{} ICE connection has been lost for peer", getLogPrefix());
RPCService.onConnected(IceAdapter.getId(), peer.getRemoteId(), false);
rpcService.onConnected(IceAdapter.getId(), peer.getRemoteId(), false);
}

setState(DISCONNECTED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,19 @@
import com.nbarraille.jjsonrpc.TcpServer;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import lombok.extern.slf4j.Slf4j;

/**
* Handles communication between client and adapter, opens a server for the client to connect to
*/
@Slf4j
public class RPCService {
public class RPCService implements AutoCloseable {

private static final ObjectMapper objectMapper = new ObjectMapper().registerModule(new JavaTimeModule());
private static TcpServer tcpServer;
private static volatile boolean skipRPCMessages = false;

public static void init(int port, FafRpcCallbacks callbacks) {
public void init(int port, GPGNetServer gpgNetServer, FafRpcCallbacks callbacks) {
Debug.RPC_PORT = port;
log.info("Creating RPC server on port {}", port);

Expand All @@ -39,7 +38,7 @@ public static void init(int port, FafRpcCallbacks callbacks) {
debug().rpcStarted(tcpServer.getFirstPeer());
tcpServer.getFirstPeer().thenAccept(firstPeer -> {
firstPeer.onConnectionLost(() -> {
GameState gameState = GPGNetServer.getGameState().orElse(null);
GameState gameState = gpgNetServer.getGameState().orElse(null);
if (gameState == GameState.LAUNCHING) {
skipRPCMessages = true;
log.warn("Lost connection to first RPC Peer. GameState: LAUNCHING, NOT STOPPING!");
Expand All @@ -58,19 +57,19 @@ public static void init(int port, FafRpcCallbacks callbacks) {
});
}

public static void onConnectionStateChanged(String newState) {
public void onConnectionStateChanged(String newState) {
if (!skipRPCMessages) {
getPeerOrWait().sendNotification("onConnectionStateChanged", Arrays.asList(newState));
}
}

public static void onGpgNetMessageReceived(String header, List<Object> chunks) {
public void onGpgNetMessageReceived(String header, List<Object> chunks) {
if (!skipRPCMessages) {
getPeerOrWait().sendNotification("onGpgNetMessageReceived", Arrays.asList(header, chunks));
}
}

public static void onIceMsg(CandidatesMessage candidatesMessage) {
public void onIceMsg(CandidatesMessage candidatesMessage) {
if (!skipRPCMessages) {
try {
getPeerOrWait()
Expand All @@ -86,15 +85,15 @@ public static void onIceMsg(CandidatesMessage candidatesMessage) {
}
}

public static void onIceConnectionStateChanged(long localPlayerId, long remotePlayerId, String state) {
public void onIceConnectionStateChanged(long localPlayerId, long remotePlayerId, String state) {
if (!skipRPCMessages) {
getPeerOrWait()
.sendNotification(
"onIceConnectionStateChanged", Arrays.asList(localPlayerId, remotePlayerId, state));
}
}

public static void onConnected(long localPlayerId, long remotePlayerId, boolean connected) {
public void onConnected(long localPlayerId, long remotePlayerId, boolean connected) {
if (!skipRPCMessages) {
getPeerOrWait().sendNotification("onConnected", Arrays.asList(localPlayerId, remotePlayerId, connected));
}
Expand All @@ -105,7 +104,7 @@ public static void onConnected(long localPlayerId, long remotePlayerId, boolean
*
* @return the currently connected peer (the client)
*/
public static JJsonPeer getPeerOrWait() {
public JJsonPeer getPeerOrWait() {
try {
return tcpServer.getFirstPeer().get();
} catch (Exception e) {
Expand All @@ -114,11 +113,8 @@ public static JJsonPeer getPeerOrWait() {
return null;
}

public static CompletableFuture<JJsonPeer> getPeerFuture() {
return tcpServer.getFirstPeer();
}

public static void close() {
@Override
public void close() {
tcpServer.stop();
}
}

0 comments on commit 38c1074

Please sign in to comment.