Remove locking in favour of a volatile state variable.

This commit is contained in:
md_5 2013-02-13 12:22:25 +11:00
parent 58689d2251
commit 3bbde30ab0
1 changed files with 44 additions and 59 deletions

View File

@ -1,4 +1,4 @@
From 840a6fa0c4febdcdb5b9e0a7af30a7d04d7e3172 Mon Sep 17 00:00:00 2001 From d5543f1ee66f0f7e7ad7dad7220887d6014feb5f 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,14 +17,14 @@ 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 | 221 ++++++++++++++++++ .../org/spigotmc/netty/NettyNetworkManager.java | 206 +++++++++++++++++
.../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 | 63 ++++++ .../java/org/spigotmc/netty/PacketDecoder.java | 63 ++++++
.../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 +++++++++
src/main/java/org/spigotmc/netty/ReadState.java | 16 ++ src/main/java/org/spigotmc/netty/ReadState.java | 16 ++
12 files changed, 883 insertions(+), 7 deletions(-) 12 files changed, 868 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
@ -210,10 +210,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..81164a4 index 0000000..90c63e1
--- /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,221 @@ @@ -0,0 +1,206 @@
+package org.spigotmc.netty; +package org.spigotmc.netty;
+ +
+import io.netty.channel.Channel; +import io.netty.channel.Channel;
@ -249,8 +249,8 @@ index 0000000..81164a4
+ 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 final Object mutex = new Object();
+ private final Queue<Packet> syncPackets = new ConcurrentLinkedQueue<Packet>(); + private final Queue<Packet> syncPackets = new ConcurrentLinkedQueue<Packet>();
+ private volatile boolean connected;
+ private Channel channel; + private Channel channel;
+ private SocketAddress address; + private SocketAddress address;
+ private Connection handler; + private Connection handler;
@ -261,7 +261,6 @@ index 0000000..81164a4
+ +
+ @Override + @Override
+ public void channelActive(ChannelHandlerContext ctx) throws Exception { + public void channelActive(ChannelHandlerContext ctx) throws Exception {
+ synchronized (mutex) {
+ // Channel and address groundwork first + // Channel and address groundwork first
+ channel = ctx.channel(); + channel = ctx.channel();
+ address = channel.remoteAddress(); + address = channel.remoteAddress();
@ -270,33 +269,24 @@ index 0000000..81164a4
+ // Followed by their first handler + // Followed by their first handler
+ handler = new PendingConnection(server, this); + handler = new PendingConnection(server, this);
+ // Finally register the connection + // Finally register the connection
+ connected = true;
+ serverConnection.pendingConnections.add((PendingConnection) handler); + serverConnection.pendingConnections.add((PendingConnection) handler);
+ } + }
+ }
+ +
+ @Override + @Override
+ public void channelInactive(ChannelHandlerContext ctx) throws Exception { + public void channelInactive(ChannelHandlerContext ctx) throws Exception {
+ synchronized (mutex) {
+ if (dcReason == null || dcArgs == null) {
+ a("disconnect.endOfStream", new Object[0]); + a("disconnect.endOfStream", new Object[0]);
+ } + }
+ // 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 {
+ synchronized (mutex) {
+ // TODO: Remove this once we are more stable + // TODO: Remove this once we are more stable
+ // Bukkit.getServer().getLogger().severe("======================= Start Netty Debug Log ======================="); + // Bukkit.getServer().getLogger().severe("======================= Start Netty Debug Log =======================");
+ // Bukkit.getServer().getLogger().log(Level.SEVERE, "Error caught whilst handling " + channel, cause); + // Bukkit.getServer().getLogger().log(Level.SEVERE, "Error caught whilst handling " + channel, cause);
+ // Bukkit.getServer().getLogger().severe("======================= End Netty Debug Log ======================="); + // 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
+ public void messageReceived(ChannelHandlerContext ctx, final Packet msg) throws Exception { + public void messageReceived(ChannelHandlerContext ctx, final Packet msg) throws Exception {
@ -338,9 +328,8 @@ index 0000000..81164a4
+ * @param packet the packet to queue + * @param packet the packet to queue
+ */ + */
+ public void queue(Packet packet) { + public void queue(Packet packet) {
+ synchronized (mutex) {
+ // Only send if channel is still connected + // Only send if channel is still connected
+ if (channel != null) { + if (connected) {
+ // Process packet via handler + // Process packet via handler
+ packet = PacketListener.callQueued(this, handler, packet); + packet = PacketListener.callQueued(this, handler, packet);
+ // If handler indicates packet send + // If handler indicates packet send
@ -357,7 +346,6 @@ index 0000000..81164a4
+ } + }
+ } + }
+ } + }
+ }
+ +
+ /** + /**
+ * wakeThreads. In Vanilla this method will interrupt the network read and + * wakeThreads. In Vanilla this method will interrupt the network read and
@ -381,7 +369,7 @@ index 0000000..81164a4
+ } + }
+ +
+ // Disconnect via the handler - this performs all plugin related cleanup + logging + // Disconnect via the handler - this performs all plugin related cleanup + logging
+ if ((dcReason != null || dcArgs != null) && syncPackets.isEmpty()) { + if (!connected) {
+ handler.a(dcReason, dcArgs); + handler.a(dcReason, dcArgs);
+ } + }
+ } + }
@ -400,12 +388,11 @@ index 0000000..81164a4
+ * 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() {
+ synchronized (mutex) { + if (connected) {
+ if (channel != null) { + connected = false;
+ channel.close(); + channel.close();
+ } + }
+ } + }
+ }
+ +
+ /** + /**
+ * queueSize. Return the number of packets in the low priority queue. In a + * queueSize. Return the number of packets in the low priority queue. In a
@ -426,12 +413,10 @@ index 0000000..81164a4
+ * exception which triggered the disconnect. + * exception which triggered the disconnect.
+ */ + */
+ public void a(String reason, Object... arguments) { + public void a(String reason, Object... arguments) {
+ synchronized (mutex) { + if (connected) {
+ if (channel != null) {
+ dcReason = reason; + dcReason = reason;
+ dcArgs = arguments; + dcArgs = arguments;
+ channel.close(); + d();
+ }
+ } + }
+ } + }
+} +}