Skip to content

Commit

Permalink
Fix race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaming32 committed May 15, 2024
1 parent 74dc8d6 commit 10860c2
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/main/java/io/github/gaming32/worldhost/proxy/ProxyClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.netty.channel.local.LocalChannel;
import net.minecraft.server.network.ServerConnectionListener;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.util.function.Supplier;
Expand All @@ -24,6 +25,7 @@ public final class ProxyClient extends SimpleChannelInboundHandler<ByteBuf> {
private final long connectionId;
private final Supplier<ProxyPassthrough> proxy;

private ByteArrayOutputStream preActiveBuffer = new ByteArrayOutputStream();
private Channel channel;
private boolean closed;

Expand All @@ -41,9 +43,11 @@ public ProxyClient(
}

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
public synchronized void channelActive(ChannelHandlerContext ctx) throws Exception {
super.channelActive(ctx);
channel = ctx.channel();
send(preActiveBuffer.toByteArray());
preActiveBuffer = null;
WorldHost.LOGGER.info("Started proxy client from {}", remoteAddress);
}

Expand Down Expand Up @@ -107,7 +111,11 @@ public void close() {
}
}

public void send(byte[] message) {
public synchronized void send(byte[] message) {
if (channel == null) {
preActiveBuffer.writeBytes(message);
return;
}
if (channel.eventLoop().inEventLoop()) {
doSend(message);
} else {
Expand Down

0 comments on commit 10860c2

Please sign in to comment.