Skip to content

Commit

Permalink
#3760: Fix possible NPE when trying to get encoder/decoder protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
BoomEaro authored and md-5 committed Nov 17, 2024
1 parent 4886c4b commit 7a42f12
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 34 deletions.
39 changes: 26 additions & 13 deletions proxy/src/main/java/net/md_5/bungee/ServerConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.net.SocketAddress;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import lombok.Data;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
Expand All @@ -32,7 +31,7 @@ public class ServerConnection implements Server
private final boolean forgeServer = false;
@Getter
private final Queue<KeepAliveData> keepAlives = new ArrayDeque<>();
private final Queue<DefinedPacket> packetQueue = new ConcurrentLinkedQueue<>();
private final Queue<DefinedPacket> packetQueue = new ArrayDeque<>();

private final Unsafe unsafe = new Unsafe()
{
Expand All @@ -45,23 +44,37 @@ public void sendPacket(DefinedPacket packet)

public void sendPacketQueued(DefinedPacket packet)
{
Protocol encodeProtocol = ch.getEncodeProtocol();
if ( !encodeProtocol.TO_SERVER.hasPacket( packet.getClass(), ch.getEncodeVersion() ) )
ch.scheduleIfNecessary( () ->
{
packetQueue.add( packet );
} else
{
unsafe().sendPacket( packet );
}
if ( ch.isClosed() )
{
return;
}
Protocol encodeProtocol = ch.getEncodeProtocol();
if ( !encodeProtocol.TO_SERVER.hasPacket( packet.getClass(), ch.getEncodeVersion() ) )
{
packetQueue.add( packet );
} else
{
unsafe().sendPacket( packet );
}
} );
}

public void sendQueuedPackets()
{
DefinedPacket packet;
while ( ( packet = packetQueue.poll() ) != null )
ch.scheduleIfNecessary( () ->
{
unsafe().sendPacket( packet );
}
if ( ch.isClosed() )
{
return;
}
DefinedPacket packet;
while ( ( packet = packetQueue.poll() ) != null )
{
unsafe().sendPacket( packet );
}
} );
}

@Override
Expand Down
40 changes: 27 additions & 13 deletions proxy/src/main/java/net/md_5/bungee/UserConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io.netty.util.internal.PlatformDependent;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.ArrayDeque;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
Expand All @@ -21,7 +22,6 @@
import java.util.Queue;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.logging.Level;
import lombok.Getter;
import lombok.NonNull;
Expand Down Expand Up @@ -146,7 +146,7 @@ public final class UserConnection implements ProxiedPlayer
@Setter
private ForgeServerHandler forgeServerHandler;
/*========================================================================*/
private final Queue<DefinedPacket> packetQueue = new ConcurrentLinkedQueue<>();
private final Queue<DefinedPacket> packetQueue = new ArrayDeque<>();
private final Unsafe unsafe = new Unsafe()
{
@Override
Expand Down Expand Up @@ -186,23 +186,37 @@ public void sendPacket(PacketWrapper packet)

public void sendPacketQueued(DefinedPacket packet)
{
Protocol encodeProtocol = ch.getEncodeProtocol();
if ( !encodeProtocol.TO_CLIENT.hasPacket( packet.getClass(), getPendingConnection().getVersion() ) )
ch.scheduleIfNecessary( () ->
{
packetQueue.add( packet );
} else
{
unsafe().sendPacket( packet );
}
if ( ch.isClosed() )
{
return;
}
Protocol encodeProtocol = ch.getEncodeProtocol();
if ( !encodeProtocol.TO_CLIENT.hasPacket( packet.getClass(), getPendingConnection().getVersion() ) )
{
packetQueue.add( packet );
} else
{
unsafe().sendPacket( packet );
}
} );
}

public void sendQueuedPackets()
{
DefinedPacket packet;
while ( ( packet = packetQueue.poll() ) != null )
ch.scheduleIfNecessary( () ->
{
unsafe().sendPacket( packet );
}
if ( ch.isClosed() )
{
return;
}
DefinedPacket packet;
while ( ( packet = packetQueue.poll() ) != null )
{
unsafe().sendPacket( packet );
}
} );
}

@Deprecated
Expand Down
36 changes: 28 additions & 8 deletions proxy/src/main/java/net/md_5/bungee/netty/ChannelWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,22 @@ public ChannelWrapper(ChannelHandlerContext ctx)

public Protocol getDecodeProtocol()
{
return ch.pipeline().get( MinecraftDecoder.class ).getProtocol();
return getMinecraftDecoder().getProtocol();
}

public void setDecodeProtocol(Protocol protocol)
{
ch.pipeline().get( MinecraftDecoder.class ).setProtocol( protocol );
getMinecraftDecoder().setProtocol( protocol );
}

public Protocol getEncodeProtocol()
{
return ch.pipeline().get( MinecraftEncoder.class ).getProtocol();

return getMinecraftEncoder().getProtocol();
}

public void setEncodeProtocol(Protocol protocol)
{
ch.pipeline().get( MinecraftEncoder.class ).setProtocol( protocol );
getMinecraftEncoder().setProtocol( protocol );
}

public void setProtocol(Protocol protocol)
Expand All @@ -69,13 +68,23 @@ public void setProtocol(Protocol protocol)

public void setVersion(int protocol)
{
ch.pipeline().get( MinecraftDecoder.class ).setProtocolVersion( protocol );
ch.pipeline().get( MinecraftEncoder.class ).setProtocolVersion( protocol );
getMinecraftDecoder().setProtocolVersion( protocol );
getMinecraftEncoder().setProtocolVersion( protocol );
}

public MinecraftDecoder getMinecraftDecoder()
{
return ch.pipeline().get( MinecraftDecoder.class );
}

public MinecraftEncoder getMinecraftEncoder()
{
return ch.pipeline().get( MinecraftEncoder.class );
}

public int getEncodeVersion()
{
return ch.pipeline().get( MinecraftEncoder.class ).getProtocolVersion();
return getMinecraftEncoder().getProtocolVersion();
}

public void write(Object packet)
Expand Down Expand Up @@ -223,4 +232,15 @@ public void updateComposite()
packetCompressor.setCompose( compressorCompose );
}
}

public void scheduleIfNecessary(Runnable task)
{
if ( ch.eventLoop().inEventLoop() )
{
task.run();
return;
}

ch.eventLoop().execute( task );
}
}

0 comments on commit 7a42f12

Please sign in to comment.