Skip to content

Commit

Permalink
improve DataForwardEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
Brokkonaut committed Sep 29, 2022
1 parent 23b6a99 commit 1a21c4b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
12 changes: 9 additions & 3 deletions src/main/java/de/cubeside/globalserver/ClientConnection.java
Expand Up @@ -359,12 +359,18 @@ public void sendPlayerOffline(String server, UUID uuid) {
}
}

public void sendData(String server, String channel, UUID targetUuid, String targetServer, byte[] data) {
public void sendData(ClientConnection fromServer, String channel, UUID targetUuid, ClientConnection targetServer, byte[] data) {
if (fromServer == null) {
throw new NullPointerException("fromServer");
}
if (channel == null) {
throw new NullPointerException("channel");
}
synchronized (this) {
if (os != null) {
try {
os.writeByte(ServerPacketType.DATA.ordinal());
os.writeUTF(server);
os.writeUTF(fromServer.getAccount());
os.writeUTF(channel);
int flags = (targetUuid != null ? 1 : 0) + (targetServer != null ? 2 : 0);
os.writeByte(flags);
Expand All @@ -373,7 +379,7 @@ public void sendData(String server, String channel, UUID targetUuid, String targ
os.writeLong(targetUuid.getLeastSignificantBits());
}
if (targetServer != null) {
os.writeUTF(targetServer);
os.writeUTF(targetServer.getAccount());
}
os.writeInt(data.length);
os.write(data);
Expand Down
28 changes: 24 additions & 4 deletions src/main/java/de/cubeside/globalserver/GlobalServer.java
Expand Up @@ -80,6 +80,7 @@ public class GlobalServer {
private ArrayList<ClientConnection> pendingConnections;

private ArrayList<ClientConnection> connections;
private HashMap<String, ClientConnection> connectionsByAccount;

private ConcurrentHashMap<String, ServerCommand> commands;

Expand Down Expand Up @@ -137,6 +138,7 @@ public GlobalServer() throws PluginLoadException {
}
pendingConnections = new ArrayList<>();
connections = new ArrayList<>();
connectionsByAccount = new HashMap<>();
commands = new ConcurrentHashMap<>();

addCommand(new HelpCommand());
Expand Down Expand Up @@ -215,6 +217,10 @@ public List<ClientConnection> getConnections() {
return Collections.unmodifiableList(connections);
}

public ClientConnection getConnection(String account) {
return connectionsByAccount.get(account);
}

void run() {
running = true;
int port = serverConfig.getPort();
Expand Down Expand Up @@ -275,6 +281,7 @@ void run() {
cc.closeConnection();
}
connections.clear();
connectionsByAccount.clear();

getEventBus().dispatchEvent(new GlobalServerStoppedEvent(this));

Expand Down Expand Up @@ -352,6 +359,7 @@ void removeConnection(ClientConnection connection) {
boolean wasOnline = connections.remove(connection);
pendingConnections.remove(connection);
if (wasOnline) {
connectionsByAccount.remove(connection.getAccount());
for (ClientConnection cc : connections) {
if (cc != connection) {
cc.sendServerOffline(connection.getAccount());
Expand Down Expand Up @@ -391,6 +399,7 @@ ClientConfig processLogin(ClientConnection connection, String account, byte[] pa

pendingConnections.remove(connection);
connections.add(connection);
connectionsByAccount.put(connection.getAccount(), connection);
connection.sendLoginResultAndActivateEncryption(true, config);
// send online servers
for (ClientConnection cc : connections) {
Expand Down Expand Up @@ -448,13 +457,14 @@ void processData(ClientConnection connection, String channel, UUID targetUuid, S
readLock.lock();
try {
HashSet<ClientConnection> targets = new HashSet<>();
ClientConnection targetServerConnection = targetServer == null ? null : connectionsByAccount.get(targetServer);
boolean fromRestricted = connection.getClient().isRestricted();
if (!fromRestricted || connection.getClient().getAllowedChannels().contains(channel)) {
for (ClientConnection cc : connections) {
if (cc != connection) {
boolean toRestricted = cc.getClient().isRestricted();
if (!toRestricted || cc.getClient().getAllowedChannels().contains(channel)) {
boolean explicitServer = targetServer != null && cc.getAccount().equals(targetServer);
boolean explicitServer = cc == targetServerConnection;
if (targetServer == null || explicitServer) {
boolean explicitPlayer = targetUuid != null && cc.hasPlayer(targetUuid);
if (toAllUnrestrictedServers || targetUuid == null || explicitPlayer) {
Expand All @@ -467,22 +477,32 @@ void processData(ClientConnection connection, String channel, UUID targetUuid, S
}
}
}
DataForwardEvent event = new DataForwardEvent(connection, targets, channel, targetUuid, targetServer, data, allowRestricted, toAllUnrestrictedServers);
DataForwardEvent event = new DataForwardEvent(connection, targets, channel, targetUuid, targetServerConnection, data, allowRestricted, toAllUnrestrictedServers);
getEventBus().dispatchEvent(event);
if (!event.isCancelled()) {
channel = event.getChannel();
targetUuid = event.getTargetUuid();
targetServer = event.getTargetServer();
targetServerConnection = event.getTargetServer();

data = event.getData();
for (ClientConnection target : event.getTargets()) {
target.sendData(connection.getAccount(), channel, targetUuid, targetServer, data);
target.sendData(connection, channel, targetUuid, targetServerConnection, data);
}
}
} finally {
readLock.unlock();
}
}

public void runWithReadLock(Runnable r) {
readLock.lock();
try {
r.run();
} finally {
readLock.unlock();
}
}

public static Console getConsole() {
return console;
}
Expand Down
Expand Up @@ -10,13 +10,13 @@ public class DataForwardEvent extends ClientConnectionEvent {
private HashSet<ClientConnection> targets;
private String channel;
private UUID targetUuid;
private String targetServer;
private ClientConnection targetServer;
private byte[] data;
private boolean allowRestricted;
private boolean toAllUnrestrictedServers;
private boolean cancelled = false;

public DataForwardEvent(ClientConnection source, HashSet<ClientConnection> targets, String channel, UUID targetUuid, String targetServer, byte[] data, boolean allowRestricted, boolean toAllUnrestrictedServers) {
public DataForwardEvent(ClientConnection source, HashSet<ClientConnection> targets, String channel, UUID targetUuid, ClientConnection targetServer, byte[] data, boolean allowRestricted, boolean toAllUnrestrictedServers) {
super(source);
this.targets = targets;
this.channel = channel;
Expand Down Expand Up @@ -61,11 +61,11 @@ public void setTargetUuid(UUID targetUuid) {
this.targetUuid = targetUuid;
}

public String getTargetServer() {
public ClientConnection getTargetServer() {
return targetServer;
}

public void setTargetServer(String targetServer) {
public void setTargetServer(ClientConnection targetServer) {
this.targetServer = targetServer;
}

Expand Down

0 comments on commit 1a21c4b

Please sign in to comment.