Update to latest netty, fix event bus bug, comment and rework PacketDecoder to new netty for better performance

This commit is contained in:
md_5 2013-05-14 18:32:17 +10:00
parent 21a354fa75
commit b25c81daf3
5 changed files with 42 additions and 13 deletions

View File

@ -37,11 +37,17 @@ public class PluginManager
private final ProxyServer proxy; private final ProxyServer proxy;
/*========================================================================*/ /*========================================================================*/
private final Yaml yaml = new Yaml(); private final Yaml yaml = new Yaml();
@SuppressWarnings("unchecked") private final EventBus eventBus;
private final EventBus eventBus = new EventBus( ProxyServer.getInstance().getLogger(), Subscribe.class, EventHandler.class );
private final Map<String, Plugin> plugins = new HashMap<>(); private final Map<String, Plugin> plugins = new HashMap<>();
private final Map<String, Command> commandMap = new HashMap<>(); private final Map<String, Command> commandMap = new HashMap<>();
@SuppressWarnings("unchecked")
public PluginManager(ProxyServer proxy)
{
this.proxy = proxy;
eventBus = new EventBus( proxy.getLogger(), Subscribe.class, EventHandler.class );
}
/** /**
* Register a command so that it may be executed. * Register a command so that it may be executed.
* *

View File

@ -59,7 +59,7 @@
<properties> <properties>
<build.number>unknown</build.number> <build.number>unknown</build.number>
<netty.version>4.0.0.CR1</netty.version> <netty.version>4.0.0.CR2</netty.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> </properties>

View File

@ -53,13 +53,19 @@
<groupId>mysql</groupId> <groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId> <artifactId>mysql-connector-java</artifactId>
<version>5.1.24</version> <version>5.1.24</version>
<scope>compile</scope> <scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.17.1-GA</version>
<scope>runtime</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.xerial</groupId> <groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId> <artifactId>sqlite-jdbc</artifactId>
<version>3.7.2</version> <version>3.7.2</version>
<scope>compile</scope> <scope>runtime</scope>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@ -84,7 +84,7 @@ public class BungeeCord extends ProxyServer
* Thread pools. * Thread pools.
*/ */
public final ScheduledThreadPoolExecutor executors = new BungeeThreadPool( new ThreadFactoryBuilder().setNameFormat( "Bungee Pool Thread #%1$d" ).build() ); public final ScheduledThreadPoolExecutor executors = new BungeeThreadPool( new ThreadFactoryBuilder().setNameFormat( "Bungee Pool Thread #%1$d" ).build() );
public final MultithreadEventLoopGroup eventLoops = new NioEventLoopGroup( 0, new ThreadFactoryBuilder().setNameFormat( "Netty IO Thread #%1$d" ).build() ); public final MultithreadEventLoopGroup eventLoops = new NioEventLoopGroup( NioEventLoopGroup.DEFAULT_EVENT_LOOP_THREADS, new ThreadFactoryBuilder().setNameFormat( "Netty IO Thread #%1$d" ).build() );
/** /**
* locations.yml save thread. * locations.yml save thread.
*/ */

View File

@ -1,6 +1,7 @@
package net.md_5.bungee.netty; package net.md_5.bungee.netty;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.MessageBuf;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder; import io.netty.handler.codec.ReplayingDecoder;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
@ -25,13 +26,29 @@ public class PacketDecoder extends ReplayingDecoder<Void>
private int protocol; private int protocol;
@Override @Override
protected byte[] decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception protected void decode(ChannelHandlerContext ctx, ByteBuf in, MessageBuf<Object> out) throws Exception
{ {
// While we have enough data
while ( true )
{
// Store our start index
int startIndex = in.readerIndex(); int startIndex = in.readerIndex();
// Run packet through framer
PacketReader.readPacket( in, protocol ); PacketReader.readPacket( in, protocol );
byte[] buf = new byte[ in.readerIndex() - startIndex ]; // If we got this far, it means we have formed a packet, so lets grab the end index
int endIndex = in.readerIndex();
// Allocate a buffer big enough for all bytes we have read
byte[] buf = new byte[ endIndex - startIndex ];
// Go back to start index
in.readerIndex( startIndex ); in.readerIndex( startIndex );
// Drain all the bytes into our buffer
in.readBytes( buf, 0, buf.length ); in.readBytes( buf, 0, buf.length );
return buf; // Jump back to the end of this packet
in.readerIndex( endIndex );
// Checkpoint our state incase we don't have enough data for another packet
checkpoint();
// Store our decoded message
out.add( buf );
}
} }
} }