Fix requests breaking on rejoining

This commit is contained in:
s809
2024-06-01 12:17:04 +05:00
parent a000afbc60
commit e08faa7943
3 changed files with 3 additions and 14 deletions
@@ -119,10 +119,8 @@ public abstract class NetworkEventSource
}
protected <TResponse extends TrackableMessage> CompletableFuture<TResponse> createRequest(PluginChannelSession session, TrackableMessage msg, Class<TResponse> responseClass)
protected <TResponse extends TrackableMessage> CompletableFuture<TResponse> createRequest(TrackableMessage msg, Class<TResponse> responseClass)
{
msg.setConnection(session);
CompletableFuture<TResponse> responseFuture = new CompletableFuture<>();
responseFuture.whenComplete((response, throwable) ->
{
@@ -76,7 +76,8 @@ public class PluginChannelSession extends NetworkEventSource
public <TResponse extends TrackableMessage> CompletableFuture<TResponse> sendRequest(TrackableMessage msg, Class<TResponse> responseClass)
{
CompletableFuture<TResponse> responseFuture = this.createRequest(this, msg, responseClass);
msg.setConnection(this);
CompletableFuture<TResponse> responseFuture = this.createRequest(msg, responseClass);
this.sendMessage(msg);
return responseFuture;
}
@@ -38,22 +38,12 @@ public abstract class TrackableMessage extends PluginChannelMessage
public long futureId = lastId.getAndIncrement()
| ((Objects.requireNonNull(SharedApi.getEnvironment()) == EWorldEnvironment.Server_Only ? 1 : 0) << 31);
private static final AtomicInteger lastContextId = new AtomicInteger();
private static final ConcurrentMap<PluginChannelSession, Integer> connectionToIdMap = new MapMaker().weakKeys().makeMap();
public void sendResponse(TrackableMessage responseMessage)
{
responseMessage.futureId = this.futureId;
this.session.sendMessage(responseMessage);
}
@Override
public void setConnection(PluginChannelSession connection)
{
super.setConnection(connection);
this.futureId |= (long) connectionToIdMap.computeIfAbsent(connection, k -> lastContextId.getAndIncrement()) << 32;
}
public void sendResponse(Exception e)
{
this.sendResponse(new ExceptionMessage(e));