Add 'unsafe' api for things like packet sending that may be implementation specific or break at any time

This commit is contained in:
md_5 2013-05-31 17:02:45 +10:00
parent 9fd69068ae
commit 639e5f3c1d
10 changed files with 93 additions and 32 deletions

View File

@ -37,6 +37,12 @@
<version>${project.version}</version> <version>${project.version}</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency>
<groupId>net.md-5</groupId>
<artifactId>bungeecord-protocol</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency> <dependency>
<groupId>org.yaml</groupId> <groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId> <artifactId>snakeyaml</artifactId>

View File

@ -1,6 +1,7 @@
package net.md_5.bungee.api.connection; package net.md_5.bungee.api.connection;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import net.md_5.bungee.protocol.packet.DefinedPacket;
/** /**
* A proxy connection is defined as a connection directly connected to a socket. * A proxy connection is defined as a connection directly connected to a socket.
@ -15,7 +16,7 @@ public interface Connection
* *
* @return the remote address * @return the remote address
*/ */
public InetSocketAddress getAddress(); InetSocketAddress getAddress();
/** /**
* Disconnects this end of the connection for the specified reason. If this * Disconnects this end of the connection for the specified reason. If this
@ -25,5 +26,23 @@ public interface Connection
* @param reason the reason shown to the player / sent to the server on * @param reason the reason shown to the player / sent to the server on
* disconnect * disconnect
*/ */
public void disconnect(String reason); void disconnect(String reason);
/**
* Get the unsafe methods of this class.
*
* @return the unsafe method interface
*/
Unsafe unsafe();
interface Unsafe
{
/**
* Send a packet to this connection.
*
* @param packet the packet to send
*/
void sendPacket(DefinedPacket packet);
}
} }

View File

@ -331,7 +331,7 @@ public class BungeeCord extends ProxyServer
{ {
for ( UserConnection con : connections.values() ) for ( UserConnection con : connections.values() )
{ {
con.sendPacket( packet ); con.unsafe().sendPacket( packet );
} }
} finally } finally
{ {

View File

@ -7,6 +7,7 @@ import lombok.RequiredArgsConstructor;
import lombok.Setter; import lombok.Setter;
import net.md_5.bungee.api.connection.Server; import net.md_5.bungee.api.connection.Server;
import net.md_5.bungee.netty.ChannelWrapper; import net.md_5.bungee.netty.ChannelWrapper;
import net.md_5.bungee.protocol.packet.DefinedPacket;
import net.md_5.bungee.protocol.packet.PacketFAPluginMessage; import net.md_5.bungee.protocol.packet.PacketFAPluginMessage;
import net.md_5.bungee.protocol.packet.PacketFFKick; import net.md_5.bungee.protocol.packet.PacketFFKick;
@ -21,11 +22,19 @@ public class ServerConnection implements Server
@Getter @Getter
@Setter @Setter
private boolean isObsolete; private boolean isObsolete;
private final Unsafe unsafe = new Unsafe()
{
@Override
public void sendPacket(DefinedPacket packet)
{
unsafe().sendPacket( packet );
}
};
@Override @Override
public void sendData(String channel, byte[] data) public void sendData(String channel, byte[] data)
{ {
ch.write( new PacketFAPluginMessage( channel, data ) ); unsafe().sendPacket( new PacketFAPluginMessage( channel, data ) );
} }
@Override @Override
@ -33,7 +42,7 @@ public class ServerConnection implements Server
{ {
if ( !ch.isClosed() ) if ( !ch.isClosed() )
{ {
ch.write( new PacketFFKick( reason ) ); unsafe().sendPacket( new PacketFFKick( reason ) );
ch.getHandle().eventLoop().schedule( new Runnable() ch.getHandle().eventLoop().schedule( new Runnable()
{ {
@Override @Override
@ -50,4 +59,10 @@ public class ServerConnection implements Server
{ {
return getInfo().getAddress(); return getInfo().getAddress();
} }
@Override
public Unsafe unsafe()
{
return unsafe;
}
} }

View File

@ -147,7 +147,7 @@ public class ServerConnector extends PacketHandler
modLogin = new Packet1Login( login.getEntityId(), login.getLevelType(), login.getGameMode(), (byte) login.getDimension(), login.getDifficulty(), login.getUnused(), modLogin = new Packet1Login( login.getEntityId(), login.getLevelType(), login.getGameMode(), (byte) login.getDimension(), login.getDifficulty(), login.getUnused(),
(byte) user.getPendingConnection().getListener().getTabListSize() ); (byte) user.getPendingConnection().getListener().getTabListSize() );
} }
user.sendPacket( modLogin ); user.unsafe().sendPacket( modLogin );
} else } else
{ {
bungee.getTabListHandler().onServerChange( user ); bungee.getTabListHandler().onServerChange( user );
@ -155,18 +155,18 @@ public class ServerConnector extends PacketHandler
Scoreboard serverScoreboard = user.getServerSentScoreboard(); Scoreboard serverScoreboard = user.getServerSentScoreboard();
for ( Objective objective : serverScoreboard.getObjectives() ) for ( Objective objective : serverScoreboard.getObjectives() )
{ {
user.sendPacket( new PacketCEScoreboardObjective( objective.getName(), objective.getValue(), (byte) 1 ) ); user.unsafe().sendPacket( new PacketCEScoreboardObjective( objective.getName(), objective.getValue(), (byte) 1 ) );
} }
for ( Team team : serverScoreboard.getTeams() ) for ( Team team : serverScoreboard.getTeams() )
{ {
user.sendPacket( new PacketD1Team( team.getName() ) ); user.unsafe().sendPacket( new PacketD1Team( team.getName() ) );
} }
serverScoreboard.clear(); serverScoreboard.clear();
user.sendDimensionSwitch(); user.sendDimensionSwitch();
user.setServerEntityId( login.getEntityId() ); user.setServerEntityId( login.getEntityId() );
user.sendPacket( new Packet9Respawn( login.getDimension(), login.getDifficulty(), login.getGameMode(), (short) 256, login.getLevelType() ) ); user.unsafe().sendPacket( new Packet9Respawn( login.getDimension(), login.getDifficulty(), login.getGameMode(), (short) 256, login.getLevelType() ) );
// Remove from old servers // Remove from old servers
user.getServer().setObsolete( true ); user.getServer().setObsolete( true );
@ -282,7 +282,7 @@ public class ServerConnector extends PacketHandler
} }
} }
user.sendPacket( pluginMessage ); // We have to forward these to the user, especially with Forge as stuff might break user.unsafe().sendPacket( pluginMessage ); // We have to forward these to the user, especially with Forge as stuff might break
if ( !sentMessages && user.getPendingConnection().getForgeLogin() != null ) if ( !sentMessages && user.getPendingConnection().getForgeLogin() != null )
{ {
for ( PacketFAPluginMessage message : user.getPendingConnection().getLoginMessages() ) for ( PacketFAPluginMessage message : user.getPendingConnection().getLoginMessages() )

View File

@ -88,6 +88,14 @@ public final class UserConnection implements ProxiedPlayer
@Getter @Getter
private String displayName; private String displayName;
/*========================================================================*/ /*========================================================================*/
private final Unsafe unsafe = new Unsafe()
{
@Override
public void sendPacket(DefinedPacket packet)
{
ch.write( packet );
}
};
public void init() public void init()
{ {
@ -100,11 +108,6 @@ public final class UserConnection implements ProxiedPlayer
} }
} }
public void sendPacket(DefinedPacket p)
{
ch.write( p );
}
public void sendPacket(byte[] b) public void sendPacket(byte[] b)
{ {
ch.write( b ); ch.write( b );
@ -134,8 +137,8 @@ public final class UserConnection implements ProxiedPlayer
void sendDimensionSwitch() void sendDimensionSwitch()
{ {
sendPacket( PacketConstants.DIM1_SWITCH ); unsafe().sendPacket( PacketConstants.DIM1_SWITCH );
sendPacket( PacketConstants.DIM2_SWITCH ); unsafe().sendPacket( PacketConstants.DIM2_SWITCH );
} }
public void connectNow(ServerInfo target) public void connectNow(ServerInfo target)
@ -220,7 +223,7 @@ public final class UserConnection implements ProxiedPlayer
if ( ch.getHandle().isActive() ) if ( ch.getHandle().isActive() )
{ {
bungee.getLogger().log( Level.INFO, "[" + getName() + "] disconnected with: " + reason ); bungee.getLogger().log( Level.INFO, "[" + getName() + "] disconnected with: " + reason );
sendPacket( new PacketFFKick( reason ) ); unsafe().sendPacket( new PacketFFKick( reason ) );
ch.getHandle().close(); ch.getHandle().close();
if ( server != null ) if ( server != null )
{ {
@ -239,7 +242,7 @@ public final class UserConnection implements ProxiedPlayer
@Override @Override
public void sendMessage(String message) public void sendMessage(String message)
{ {
sendPacket( new Packet3Chat( message ) ); unsafe().sendPacket( new Packet3Chat( message ) );
} }
@Override @Override
@ -254,7 +257,7 @@ public final class UserConnection implements ProxiedPlayer
@Override @Override
public void sendData(String channel, byte[] data) public void sendData(String channel, byte[] data)
{ {
sendPacket( new PacketFAPluginMessage( channel, data ) ); unsafe().sendPacket( new PacketFAPluginMessage( channel, data ) );
} }
@Override @Override
@ -322,6 +325,12 @@ public final class UserConnection implements ProxiedPlayer
@Override @Override
public void setTexturePack(TexturePackInfo pack) public void setTexturePack(TexturePackInfo pack)
{ {
sendPacket( new PacketFAPluginMessage( "MC|TPack", ( pack.getUrl() + "\00" + pack.getSize() ).getBytes() ) ); unsafe().sendPacket( new PacketFAPluginMessage( "MC|TPack", ( pack.getUrl() + "\00" + pack.getSize() ).getBytes() ) );
}
@Override
public Unsafe unsafe()
{
return unsafe;
} }
} }

View File

@ -36,6 +36,7 @@ import net.md_5.bungee.netty.CipherEncoder;
import net.md_5.bungee.netty.PacketDecoder; import net.md_5.bungee.netty.PacketDecoder;
import net.md_5.bungee.netty.PacketHandler; import net.md_5.bungee.netty.PacketHandler;
import net.md_5.bungee.protocol.Forge; import net.md_5.bungee.protocol.Forge;
import net.md_5.bungee.protocol.packet.DefinedPacket;
import net.md_5.bungee.protocol.packet.Packet1Login; import net.md_5.bungee.protocol.packet.Packet1Login;
import net.md_5.bungee.protocol.packet.Packet2Handshake; import net.md_5.bungee.protocol.packet.Packet2Handshake;
import net.md_5.bungee.protocol.packet.PacketCDClientStatus; import net.md_5.bungee.protocol.packet.PacketCDClientStatus;
@ -44,7 +45,6 @@ import net.md_5.bungee.protocol.packet.PacketFCEncryptionResponse;
import net.md_5.bungee.protocol.packet.PacketFDEncryptionRequest; import net.md_5.bungee.protocol.packet.PacketFDEncryptionRequest;
import net.md_5.bungee.protocol.packet.PacketFEPing; import net.md_5.bungee.protocol.packet.PacketFEPing;
import net.md_5.bungee.protocol.packet.PacketFFKick; import net.md_5.bungee.protocol.packet.PacketFFKick;
import net.md_5.bungee.protocol.Vanilla;
@RequiredArgsConstructor @RequiredArgsConstructor
public class InitialHandler extends PacketHandler implements PendingConnection public class InitialHandler extends PacketHandler implements PendingConnection
@ -63,6 +63,14 @@ public class InitialHandler extends PacketHandler implements PendingConnection
private List<PacketFAPluginMessage> loginMessages = new ArrayList<>(); private List<PacketFAPluginMessage> loginMessages = new ArrayList<>();
private State thisState = State.HANDSHAKE; private State thisState = State.HANDSHAKE;
private SecretKey sharedKey; private SecretKey sharedKey;
private final Unsafe unsafe = new Unsafe()
{
@Override
public void sendPacket(DefinedPacket packet)
{
unsafe().sendPacket( packet );
}
};
private static final PacketFAPluginMessage forgeMods = new PacketFAPluginMessage( "FML", new byte[] private static final PacketFAPluginMessage forgeMods = new PacketFAPluginMessage( "FML", new byte[]
{ {
0, 0, 0, 0, 0, 2 0, 0, 0, 0, 0, 2
@ -144,8 +152,8 @@ public class InitialHandler extends PacketHandler implements PendingConnection
} }
this.handshake = handshake; this.handshake = handshake;
ch.write( forgeMods ); unsafe().sendPacket( forgeMods );
ch.write( request = EncryptionUtil.encryptRequest() ); unsafe().sendPacket( request = EncryptionUtil.encryptRequest() );
thisState = State.ENCRYPT; thisState = State.ENCRYPT;
} }
@ -224,7 +232,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection
} }
thisState = InitialHandler.State.LOGIN; thisState = InitialHandler.State.LOGIN;
ch.write( new PacketFCEncryptionResponse( new byte[ 0 ], new byte[ 0 ] ) ); unsafe().sendPacket( new PacketFCEncryptionResponse( new byte[ 0 ], new byte[ 0 ] ) );
try try
{ {
Cipher encrypt = EncryptionUtil.getCipher( Cipher.ENCRYPT_MODE, sharedKey ); Cipher encrypt = EncryptionUtil.getCipher( Cipher.ENCRYPT_MODE, sharedKey );
@ -264,7 +272,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection
{ {
if ( !ch.isClosed() ) if ( !ch.isClosed() )
{ {
ch.write( new PacketFFKick( reason ) ); unsafe().sendPacket( new PacketFFKick( reason ) );
ch.close(); ch.close();
} }
} }
@ -293,6 +301,12 @@ public class InitialHandler extends PacketHandler implements PendingConnection
return (InetSocketAddress) ch.getHandle().remoteAddress(); return (InetSocketAddress) ch.getHandle().remoteAddress();
} }
@Override
public Unsafe unsafe()
{
return unsafe;
}
@Override @Override
public String toString() public String toString()
{ {

View File

@ -27,9 +27,9 @@ public class UpstreamBridge extends PacketHandler
this.bungee = bungee; this.bungee = bungee;
this.con = con; this.con = con;
BungeeCord.getInstance().addConnection( con );
bungee.getTabListHandler().onConnect( con ); bungee.getTabListHandler().onConnect( con );
con.sendPacket( BungeeCord.getInstance().registerChannels() ); BungeeCord.getInstance().addConnection( con );
con.unsafe().sendPacket( BungeeCord.getInstance().registerChannels() );
TexturePackInfo texture = con.getPendingConnection().getListener().getTexturePack(); TexturePackInfo texture = con.getPendingConnection().getListener().getTexturePack();
if ( texture != null ) if ( texture != null )

View File

@ -3,7 +3,6 @@ package net.md_5.bungee.tablist;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
import net.md_5.bungee.BungeeCord; import net.md_5.bungee.BungeeCord;
import net.md_5.bungee.UserConnection;
import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.TabListHandler; import net.md_5.bungee.api.TabListHandler;
import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.connection.ProxiedPlayer;
@ -17,10 +16,9 @@ public class Global implements TabListHandler
@Override @Override
public void onConnect(ProxiedPlayer player) public void onConnect(ProxiedPlayer player)
{ {
UserConnection con = (UserConnection) player;
for ( ProxiedPlayer p : ProxyServer.getInstance().getPlayers() ) for ( ProxiedPlayer p : ProxyServer.getInstance().getPlayers() )
{ {
con.sendPacket( new PacketC9PlayerListItem( p.getDisplayName(), true, (short) p.getPing() ) ); player.unsafe().sendPacket( new PacketC9PlayerListItem( p.getDisplayName(), true, (short) p.getPing() ) );
} }
BungeeCord.getInstance().broadcast( new PacketC9PlayerListItem( player.getDisplayName(), true, (short) player.getPing() ) ); BungeeCord.getInstance().broadcast( new PacketC9PlayerListItem( player.getDisplayName(), true, (short) player.getPing() ) );
} }

View File

@ -38,7 +38,7 @@ public class ServerUnique implements TabListHandler
{ {
for ( String username : usernames ) for ( String username : usernames )
{ {
( (UserConnection) player ).sendPacket( new PacketC9PlayerListItem( username, false, (short) 9999 ) ); player.unsafe().sendPacket( new PacketC9PlayerListItem( username, false, (short) 9999 ) );
} }
usernames.clear(); usernames.clear();
} }