Tweak to thread pool and safety.

This commit is contained in:
md_5 2013-02-10 10:30:58 +11:00
parent 86bb33ece7
commit 60cb2f461b

View File

@ -1,9 +1,9 @@
From 42978807473eb106e90adf9554dd76dfeb494f40 Mon Sep 17 00:00:00 2001
From b6830bc7718824f5ca347193d674f338178b6e31 Mon Sep 17 00:00:00 2001
From: md_5 <md_5@live.com.au>
Date: Sun, 3 Feb 2013 10:24:33 +1100
Subject: [PATCH] Netty
Implement an uber efficient network engine based on the Java NIO framework Netty. This is basically a complete rewrite of the Minecraft network engine with many distinct advantages. First and foremost, there will no longer be the horrid, and redundant case of 2, or even at times, 3 threads per a connection. Instead low level select/epoll based NIO is used. The number of threads used for network reading and writing will scale automatically to the number of cores for use on your server. In most cases this will be around 8 threads for a 4 core server, much better than the up to 1000 threads that could be in use at one time with the old engine. To facilitate asynchronous packet sending or receiving (currently only chat), a cached thread pool is kept handy. Currently this pool is unbounded, however at this stage we do not need to worry about servers being resource starved due to excessive spam or chat.
Implement an uber efficient network engine based on the Java NIO framework Netty. This is basically a complete rewrite of the Minecraft network engine with many distinct advantages. First and foremost, there will no longer be the horrid, and redundant case of 2, or even at times, 3 threads per a connection. Instead low level select/epoll based NIO is used. The number of threads used for network reading and writing will scale automatically to the number of cores for use on your server. In most cases this will be around 8 threads for a 4 core server, much better than the up to 1000 threads that could be in use at one time with the old engine. To facilitate asynchronous packet sending or receiving (currently only chat), a thread pool of 16 threads is kept handy.
== Plugin incompatibilities
As a side effect of this change, plugins which rely on very specific implementation level details within Minecraft are broken. At this point in time, TagAPI and ProtocolLib are affected. If you are a user of ProtocolLib you are advised to update to the latest build, where full support is enabled. If you are a user of TagAPI, support has not yet been added, so you will need to install the updated ProtocolLib so that TagAPI may use its functions.
== Stability
@ -17,13 +17,13 @@ This commit is licensed under the Creative Commons Attribution-ShareAlike 3.0 Un
.../net/minecraft/server/PendingConnection.java | 13 +-
.../net/minecraft/server/PlayerConnection.java | 2 +-
src/main/java/org/spigotmc/netty/CipherCodec.java | 65 ++++++
.../org/spigotmc/netty/NettyNetworkManager.java | 203 +++++++++++++++++
.../org/spigotmc/netty/NettyNetworkManager.java | 211 ++++++++++++++++++
.../org/spigotmc/netty/NettyServerConnection.java | 98 ++++++++
.../org/spigotmc/netty/NettySocketAdaptor.java | 248 +++++++++++++++++++++
.../java/org/spigotmc/netty/PacketDecoder.java | 52 +++++
.../java/org/spigotmc/netty/PacketEncoder.java | 43 ++++
.../java/org/spigotmc/netty/PacketListener.java | 100 +++++++++
11 files changed, 830 insertions(+), 6 deletions(-)
11 files changed, 838 insertions(+), 6 deletions(-)
create mode 100644 src/main/java/org/spigotmc/netty/CipherCodec.java
create mode 100644 src/main/java/org/spigotmc/netty/NettyNetworkManager.java
create mode 100644 src/main/java/org/spigotmc/netty/NettyServerConnection.java
@ -199,10 +199,10 @@ index 0000000..cfc0535
+}
diff --git a/src/main/java/org/spigotmc/netty/NettyNetworkManager.java b/src/main/java/org/spigotmc/netty/NettyNetworkManager.java
new file mode 100644
index 0000000..2ab0271
index 0000000..effd1ee
--- /dev/null
+++ b/src/main/java/org/spigotmc/netty/NettyNetworkManager.java
@@ -0,0 +1,203 @@
@@ -0,0 +1,211 @@
+package org.spigotmc.netty;
+
+import io.netty.channel.Channel;
@ -233,13 +233,13 @@ index 0000000..2ab0271
+ */
+public class NettyNetworkManager extends ChannelInboundMessageHandlerAdapter<Packet> implements INetworkManager {
+
+ private static final ExecutorService threadPool = Executors.newCachedThreadPool();
+ private static final ExecutorService threadPool = Executors.newFixedThreadPool(16);
+ private static final MinecraftServer server = MinecraftServer.getServer();
+ private static final PrivateKey key = server.F().getPrivate();
+ private static final NettyServerConnection serverConnection = (NettyServerConnection) server.ae();
+ /*========================================================================*/
+ private Queue<Packet> syncPackets = new ConcurrentLinkedQueue<Packet>();
+ private Channel channel;
+ private volatile Channel channel;
+ private SocketAddress address;
+ private Connection handler;
+ private SecretKey secret;
@ -320,15 +320,23 @@ index 0000000..2ab0271
+ * @param packet the packet to queue
+ */
+ public void queue(Packet packet) {
+ // Only send if channel is still connected
+ if (channel != null) {
+ // Process packet via handler
+ packet = PacketListener.callQueued(this, handler, packet);
+ // If handler indicates packet send
+ if (packet != null) {
+ channel.write(packet);
+
+ // If needed, check and prepare encryption phase
+ if (packet instanceof Packet252KeyResponse) {
+ BufferedBlockCipher encrypt = NettyServerConnection.getCipher(true, secret);
+ BufferedBlockCipher decrypt = NettyServerConnection.getCipher(false, secret);
+ channel.pipeline().addBefore("decoder", "cipher", new CipherCodec(encrypt, decrypt));
+ CipherCodec codec = new CipherCodec(encrypt, decrypt);
+ // Only add if the channel hasn't disconnected in the meantime
+ if (channel != null) {
+ channel.pipeline().addBefore("decoder", "cipher", codec);
+ }
+ }
+ }
+ }