Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor client-side and server-side state, split player database responsibilities, and enforce better lifecylce for servers and clients #4771

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading