Add proper thread safety. Please let me know if this deadlocks your server.

This commit is contained in:
md_5 2013-02-10 18:35:09 +11:00
parent 36f3a7a7f2
commit b7ffd83675
1 changed files with 56 additions and 46 deletions

View File

@ -1,4 +1,4 @@
From 8794e1bc1ae9773a9f7c9943c7696a76e66691aa Mon Sep 17 00:00:00 2001 From 2624cc163bed8a94b851c7dbc186a304ce63bff7 Mon Sep 17 00:00:00 2001
From: md_5 <md_5@live.com.au> From: md_5 <md_5@live.com.au>
Date: Sun, 3 Feb 2013 10:24:33 +1100 Date: Sun, 3 Feb 2013 10:24:33 +1100
Subject: [PATCH] Netty Subject: [PATCH] Netty
@ -17,13 +17,13 @@ This commit is licensed under the Creative Commons Attribution-ShareAlike 3.0 Un
.../net/minecraft/server/PendingConnection.java | 15 +- .../net/minecraft/server/PendingConnection.java | 15 +-
.../net/minecraft/server/PlayerConnection.java | 2 +- .../net/minecraft/server/PlayerConnection.java | 2 +-
src/main/java/org/spigotmc/netty/CipherCodec.java | 65 ++++++ src/main/java/org/spigotmc/netty/CipherCodec.java | 65 ++++++
.../org/spigotmc/netty/NettyNetworkManager.java | 211 ++++++++++++++++++ .../org/spigotmc/netty/NettyNetworkManager.java | 221 ++++++++++++++++++
.../org/spigotmc/netty/NettyServerConnection.java | 105 +++++++++ .../org/spigotmc/netty/NettyServerConnection.java | 105 +++++++++
.../org/spigotmc/netty/NettySocketAdaptor.java | 248 +++++++++++++++++++++ .../org/spigotmc/netty/NettySocketAdaptor.java | 248 +++++++++++++++++++++
.../java/org/spigotmc/netty/PacketDecoder.java | 47 ++++ .../java/org/spigotmc/netty/PacketDecoder.java | 47 ++++
.../java/org/spigotmc/netty/PacketEncoder.java | 43 ++++ .../java/org/spigotmc/netty/PacketEncoder.java | 43 ++++
.../java/org/spigotmc/netty/PacketListener.java | 100 +++++++++ .../java/org/spigotmc/netty/PacketListener.java | 100 +++++++++
11 files changed, 841 insertions(+), 7 deletions(-) 11 files changed, 851 insertions(+), 7 deletions(-)
create mode 100644 src/main/java/org/spigotmc/netty/CipherCodec.java 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/NettyNetworkManager.java
create mode 100644 src/main/java/org/spigotmc/netty/NettyServerConnection.java create mode 100644 src/main/java/org/spigotmc/netty/NettyServerConnection.java
@ -208,10 +208,10 @@ index 0000000..cfc0535
+} +}
diff --git a/src/main/java/org/spigotmc/netty/NettyNetworkManager.java b/src/main/java/org/spigotmc/netty/NettyNetworkManager.java diff --git a/src/main/java/org/spigotmc/netty/NettyNetworkManager.java b/src/main/java/org/spigotmc/netty/NettyNetworkManager.java
new file mode 100644 new file mode 100644
index 0000000..effd1ee index 0000000..81aa42e
--- /dev/null --- /dev/null
+++ b/src/main/java/org/spigotmc/netty/NettyNetworkManager.java +++ b/src/main/java/org/spigotmc/netty/NettyNetworkManager.java
@@ -0,0 +1,211 @@ @@ -0,0 +1,221 @@
+package org.spigotmc.netty; +package org.spigotmc.netty;
+ +
+import io.netty.channel.Channel; +import io.netty.channel.Channel;
@ -247,8 +247,9 @@ index 0000000..effd1ee
+ private static final PrivateKey key = server.F().getPrivate(); + private static final PrivateKey key = server.F().getPrivate();
+ private static final NettyServerConnection serverConnection = (NettyServerConnection) server.ae(); + private static final NettyServerConnection serverConnection = (NettyServerConnection) server.ae();
+ /*========================================================================*/ + /*========================================================================*/
+ private Queue<Packet> syncPackets = new ConcurrentLinkedQueue<Packet>(); + private final Object mutex = new Object();
+ private volatile Channel channel; + private final Queue<Packet> syncPackets = new ConcurrentLinkedQueue<Packet>();
+ private Channel channel;
+ private SocketAddress address; + private SocketAddress address;
+ private Connection handler; + private Connection handler;
+ private SecretKey secret; + private SecretKey secret;
@ -258,35 +259,41 @@ index 0000000..effd1ee
+ +
+ @Override + @Override
+ public void channelActive(ChannelHandlerContext ctx) throws Exception { + public void channelActive(ChannelHandlerContext ctx) throws Exception {
+ // Channel and address groundwork first + synchronized (mutex) {
+ channel = ctx.channel(); + // Channel and address groundwork first
+ address = channel.remoteAddress(); + channel = ctx.channel();
+ // Then the socket adaptor + address = channel.remoteAddress();
+ socketAdaptor = NettySocketAdaptor.adapt((SocketChannel) channel); + // Then the socket adaptor
+ // Followed by their first handler + socketAdaptor = NettySocketAdaptor.adapt((SocketChannel) channel);
+ handler = new PendingConnection(server, this); + // Followed by their first handler
+ // Finally register the connection + handler = new PendingConnection(server, this);
+ serverConnection.pendingConnections.add((PendingConnection) handler); + // Finally register the connection
+ serverConnection.pendingConnections.add((PendingConnection) handler);
+ }
+ } + }
+ +
+ @Override + @Override
+ public void channelInactive(ChannelHandlerContext ctx) throws Exception { + public void channelInactive(ChannelHandlerContext ctx) throws Exception {
+ if (dcReason == null || dcArgs == null) { + synchronized (mutex) {
+ a("disconnect.endOfStream", new Object[0]); + if (dcReason == null || dcArgs == null) {
+ a("disconnect.endOfStream", new Object[0]);
+ }
+ // Remove channel reference to indicate we are done
+ channel = null;
+ } + }
+ // Remove channel reference to indicate we are done
+ channel = null;
+ } + }
+ +
+ @Override + @Override
+ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
+ // TODO: Remove this once we are more stable + synchronized (mutex) {
+ // Bukkit.getServer().getLogger().severe("======================= Start Netty Debug Log ======================="); + // TODO: Remove this once we are more stable
+ // Bukkit.getServer().getLogger().log(Level.SEVERE, "Error caught whilst handling " + channel, cause); + // Bukkit.getServer().getLogger().severe("======================= Start Netty Debug Log =======================");
+ // Bukkit.getServer().getLogger().severe("======================= End Netty Debug Log ======================="); + // Bukkit.getServer().getLogger().log(Level.SEVERE, "Error caught whilst handling " + channel, cause);
+ // Bukkit.getServer().getLogger().severe("======================= End Netty Debug Log =======================");
+ +
+ // Disconnect with generic reason + exception + // Disconnect with generic reason + exception
+ a("disconnect.genericReason", new Object[]{"Internal exception: " + cause}); + a("disconnect.genericReason", new Object[]{"Internal exception: " + cause});
+ }
+ } + }
+ +
+ @Override + @Override
@ -329,21 +336,20 @@ index 0000000..effd1ee
+ * @param packet the packet to queue + * @param packet the packet to queue
+ */ + */
+ public void queue(Packet packet) { + public void queue(Packet packet) {
+ // Only send if channel is still connected + synchronized (mutex) {
+ if (channel != null) { + // Only send if channel is still connected
+ // Process packet via handler + if (channel != null) {
+ packet = PacketListener.callQueued(this, handler, packet); + // Process packet via handler
+ // If handler indicates packet send + packet = PacketListener.callQueued(this, handler, packet);
+ if (packet != null) { + // If handler indicates packet send
+ channel.write(packet); + if (packet != null) {
+ channel.write(packet);
+ +
+ // If needed, check and prepare encryption phase + // If needed, check and prepare encryption phase
+ if (packet instanceof Packet252KeyResponse) { + if (packet instanceof Packet252KeyResponse) {
+ BufferedBlockCipher encrypt = NettyServerConnection.getCipher(true, secret); + BufferedBlockCipher encrypt = NettyServerConnection.getCipher(true, secret);
+ BufferedBlockCipher decrypt = NettyServerConnection.getCipher(false, secret); + BufferedBlockCipher decrypt = NettyServerConnection.getCipher(false, secret);
+ CipherCodec codec = 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); + channel.pipeline().addBefore("decoder", "cipher", codec);
+ } + }
+ } + }
@ -392,8 +398,10 @@ index 0000000..effd1ee
+ * close. Close and release all resources associated with this connection. + * close. Close and release all resources associated with this connection.
+ */ + */
+ public void d() { + public void d() {
+ if (channel != null) { + synchronized (mutex) {
+ channel.close(); + if (channel != null) {
+ channel.close();
+ }
+ } + }
+ } + }
+ +
@ -416,10 +424,12 @@ index 0000000..effd1ee
+ * exception which triggered the disconnect. + * exception which triggered the disconnect.
+ */ + */
+ public void a(String reason, Object... arguments) { + public void a(String reason, Object... arguments) {
+ if (channel != null) { + synchronized (mutex) {
+ dcReason = reason; + if (channel != null) {
+ dcArgs = arguments; + dcReason = reason;
+ channel.close(); + dcArgs = arguments;
+ channel.close();
+ }
+ } + }
+ } + }
+} +}