Skip to content

Commit

Permalink
Merge pull request #4771 from kwvanderlinde/bugfix/4519-4542-4654-ser…
Browse files Browse the repository at this point in the history
…ver-init-and-shutdown

Refactor client-side and server-side state, split player database responsibilities, and enforce better lifecylce for servers and clients
  • Loading branch information
cwisniew authored May 7, 2024
2 parents 41479b0 + 392d148 commit 926c06a
Show file tree
Hide file tree
Showing 52 changed files with 1,066 additions and 1,224 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/
package net.rptools.clientserver.simple;

import java.util.concurrent.ExecutionException;
import net.rptools.clientserver.simple.connection.Connection;

public interface Handshake {
Expand Down Expand Up @@ -62,11 +61,6 @@ public interface Handshake {
*/
void removeObserver(HandshakeObserver observer);

/**
* Starts the handshake process.
*
* @throws ExecutionException when there is an exception in the background task.
* @throws InterruptedException when the background task is interrupted.
*/
void startHandshake() throws ExecutionException, InterruptedException;
/** Starts the handshake process. */
void startHandshake();
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@

/**
* @author drice
* <p>TODO To change the template for this generated type comment go to Window - Preferences -
* Java - Code Style - Code Templates
*/
public class SocketConnection extends AbstractConnection implements Connection {
/** Instance used for log messages. */
Expand All @@ -35,40 +33,44 @@ public class SocketConnection extends AbstractConnection implements Connection {
private String hostName;
private int port;

public SocketConnection(String id, String hostName, int port) throws IOException {
public SocketConnection(String id, String hostName, int port) {
this.id = id;
this.hostName = hostName;
this.port = port;
}

public SocketConnection(String id, Socket socket) throws IOException {
public SocketConnection(String id, Socket socket) {
this.id = id;
this.hostName = socket.getInetAddress().getHostName();
this.port = socket.getPort();
this.socket = socket;

initialize(socket);
}

private void initialize(Socket socket) throws IOException {
public String getId() {
return id;
}

private void initialize(Socket socket) {
this.socket = socket;
this.send = new SendThread(new BufferedOutputStream(socket.getOutputStream()));
this.receive = new ReceiveThread(this, socket.getInputStream());
this.send = new SendThread(socket);
this.receive = new ReceiveThread(socket);

this.send.start();
this.receive.start();
}

public String getId() {
return id;
}

@Override
public void open() throws IOException {
initialize(new Socket(hostName, port));
}

public void sendMessage(Object channel, byte[] message) {
addMessage(channel, message);
synchronized (send) {
send.notify();

if (send != null) {
synchronized (send) {
send.notify();
}
}
}

Expand Down Expand Up @@ -103,12 +105,12 @@ public String getError() {
// send thread
// /////////////////////////////////////////////////////////////////////////
private class SendThread extends Thread {
private final OutputStream out;
private final Socket socket;
private boolean stopRequested = false;

public SendThread(OutputStream out) {
public SendThread(Socket socket) {
setName("SocketConnection.SendThread");
this.out = out;
this.socket = socket;
}

public void requestStop() {
Expand All @@ -120,6 +122,15 @@ public void requestStop() {

@Override
public void run() {
final OutputStream out;
try {
out = new BufferedOutputStream(socket.getOutputStream());
} catch (IOException e) {
log.error("Unable to get socket output stream", e);
fireDisconnect();
return;
}

try {
while (!stopRequested && SocketConnection.this.isAlive()) {
try {
Expand Down Expand Up @@ -154,14 +165,12 @@ public void run() {
// receive thread
// /////////////////////////////////////////////////////////////////////////
private class ReceiveThread extends Thread {
private final SocketConnection conn;
private final InputStream in;
private final Socket socket;
private boolean stopRequested = false;

public ReceiveThread(SocketConnection conn, InputStream in) {
public ReceiveThread(Socket socket) {
setName("SocketConnection.ReceiveThread");
this.conn = conn;
this.in = in;
this.socket = socket;
}

public void requestStop() {
Expand All @@ -170,10 +179,19 @@ public void requestStop() {

@Override
public void run() {
while (!stopRequested && conn.isAlive()) {
final InputStream in;
try {
in = socket.getInputStream();
} catch (IOException e) {
log.error("Unable to get socket input stream", e);
SocketConnection.this.close();
return;
}

while (!stopRequested && SocketConnection.this.isAlive()) {
try {
byte[] message = conn.readMessage(in);
conn.dispatchCompressedMessage(conn.id, message);
byte[] message = SocketConnection.this.readMessage(in);
SocketConnection.this.dispatchCompressedMessage(SocketConnection.this.id, message);
} catch (IOException e) {
log.error(e);
fireDisconnect();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package net.rptools.clientserver.simple.server;

import java.util.*;
import java.util.concurrent.ExecutionException;
import net.rptools.clientserver.simple.DisconnectHandler;
import net.rptools.clientserver.simple.Handshake;
import net.rptools.clientserver.simple.HandshakeObserver;
Expand Down Expand Up @@ -129,7 +128,7 @@ public void handleDisconnect(Connection conn) {
fireClientDisconnect(conn);
}

protected void handleConnection(Connection conn) throws ExecutionException, InterruptedException {
protected void handleConnection(Connection conn) {
var handshake = handshakeProvider.getConnectionHandshake(conn);
handshake.addObserver(this);
// Make sure the client is allowed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutionException;
import net.rptools.clientserver.simple.MessageHandler;
import net.rptools.clientserver.simple.connection.SocketConnection;
import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -109,7 +108,7 @@ public void run() {
String id = nextClientId(s);
SocketConnection conn = new SocketConnection(id, s);
server.handleConnection(conn);
} catch (IOException | ExecutionException | InterruptedException e) {
} catch (IOException e) {
if (!suppressErrors) {
log.error(e.getMessage(), e);
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/net/rptools/clientserver/ConnectionFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/
package net.rptools.clientserver;

import java.io.IOException;
import net.rptools.clientserver.simple.MessageHandler;
import net.rptools.clientserver.simple.connection.Connection;
import net.rptools.clientserver.simple.connection.SocketConnection;
Expand All @@ -33,9 +32,10 @@ public static ConnectionFactory getInstance() {
return instance;
}

public Connection createConnection(String id, ServerConfig config) throws IOException {
if (!config.getUseWebRTC() || config.isPersonalServer())
public Connection createConnection(String id, ServerConfig config) {
if (!config.getUseWebRTC()) {
return new SocketConnection(id, config.getHostName(), config.getPort());
}

return new WebRTCConnection(
id,
Expand All @@ -49,9 +49,8 @@ public void onLoginError() {
}

public Server createServer(
ServerConfig config, HandshakeProvider handshake, MessageHandler messageHandler)
throws IOException {
if (!config.getUseWebRTC() || config.isPersonalServer()) {
ServerConfig config, HandshakeProvider handshake, MessageHandler messageHandler) {
if (!config.getUseWebRTC()) {
return new SocketServer(config.getPort(), handshake, messageHandler);
}

Expand All @@ -67,6 +66,7 @@ public void onLoginError() {

@Override
public void onUnexpectedClose() {
MapTool.disconnect();
MapTool.stopServer();
}
});
Expand Down
Loading

0 comments on commit 926c06a

Please sign in to comment.