|
|
@ -17,29 +17,31 @@ import java.util.concurrent.LinkedBlockingQueue; |
|
|
|
import java.util.concurrent.atomic.LongAdder; |
|
|
|
import java.util.concurrent.atomic.LongAdder; |
|
|
|
import java.util.regex.Pattern; |
|
|
|
import java.util.regex.Pattern; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
// MB Dev Notes:
|
|
|
|
* Thread blocks until MagicBane dispatch messages are |
|
|
|
// All outgoing Protocol messages to the player are managed through the MessageDispatcher.class.
|
|
|
|
* enqueued then processes them in FIFO order. The collection |
|
|
|
// All incoming Protocol messages from the player are managed by the Protocol.class.
|
|
|
|
* is thread safe. |
|
|
|
//
|
|
|
|
* <p> |
|
|
|
// A DispatchMessage is configured then wrapped in a Dispatch for a distribution list.
|
|
|
|
* Any large messages not time sensitive such as load object |
|
|
|
// A Dispatch can be submitted to the Dispatcher from any thread.
|
|
|
|
* sent to more than a single individual should be spawned |
|
|
|
//
|
|
|
|
* individually on a DispatchMessageThread. |
|
|
|
// Dispatches are interleaved between channels. This is to ensure
|
|
|
|
*/ |
|
|
|
// a combat or movement message is not delayed by spam clicking a
|
|
|
|
|
|
|
|
// larger message. Choose your channel wisely.
|
|
|
|
|
|
|
|
|
|
|
|
public class MessageDispatcher implements Runnable { |
|
|
|
public class MessageDispatcher implements Runnable { |
|
|
|
|
|
|
|
|
|
|
|
// Instance variables
|
|
|
|
// Instance variables
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked") // Cannot have arrays of generics in java.
|
|
|
|
|
|
|
|
private static final ConcurrentLinkedQueue<Dispatch>[] _messageQueue = new ConcurrentLinkedQueue[DispatchChannel.values().length]; |
|
|
|
private static final ConcurrentLinkedQueue<Dispatch>[] _messageQueue = new ConcurrentLinkedQueue[DispatchChannel.values().length]; |
|
|
|
private static final LinkedBlockingQueue<Boolean> _blockingQueue = new LinkedBlockingQueue<>(); |
|
|
|
private static final LinkedBlockingQueue<Boolean> _blockingQueue = new LinkedBlockingQueue<>(); |
|
|
|
|
|
|
|
|
|
|
|
// Class variables
|
|
|
|
// Class variables
|
|
|
|
|
|
|
|
|
|
|
|
public static volatile long[] messageCount = new long[DispatchChannel.values().length]; |
|
|
|
public static volatile long[] messageCount = new long[DispatchChannel.values().length]; |
|
|
|
public static LongAdder[] dispatchCount = new LongAdder[DispatchChannel.values().length]; |
|
|
|
public static LongAdder[] dispatchCount = new LongAdder[DispatchChannel.values().length]; |
|
|
|
|
|
|
|
|
|
|
|
// Performance metrics
|
|
|
|
// Performance metrics
|
|
|
|
|
|
|
|
|
|
|
|
public static volatile long[] maxRecipients = new long[DispatchChannel.values().length]; |
|
|
|
public static volatile long[] maxRecipients = new long[DispatchChannel.values().length]; |
|
|
|
public static LongAdder itemPoolSize = new LongAdder(); |
|
|
|
public static LongAdder itemPoolSize = new LongAdder(); |
|
|
|
private final Pattern filterPattern; // Unused, but just in case
|
|
|
|
private final Pattern filterPattern; // Unused, but just in case
|
|
|
@ -61,6 +63,35 @@ public class MessageDispatcher implements Runnable { |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public void run() { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
boolean shouldBlock; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while (true) { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
shouldBlock = true; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (DispatchChannel dispatchChannel : DispatchChannel.values()) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.messageDispatch = _messageQueue[dispatchChannel.getChannelID()].poll(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (this.messageDispatch != null) { |
|
|
|
|
|
|
|
DispatchMessage.serializeDispatch(this.messageDispatch); |
|
|
|
|
|
|
|
shouldBlock = false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (shouldBlock == true) |
|
|
|
|
|
|
|
shouldBlock = _blockingQueue.take(); |
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
|
|
Logger.error(e); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static void send(Dispatch messageDispatch, DispatchChannel dispatchChannel) { |
|
|
|
public static void send(Dispatch messageDispatch, DispatchChannel dispatchChannel) { |
|
|
|
|
|
|
|
|
|
|
|
// Don't queue up empty dispatches!
|
|
|
|
// Don't queue up empty dispatches!
|
|
|
@ -74,12 +105,11 @@ public class MessageDispatcher implements Runnable { |
|
|
|
// Update performance metrics
|
|
|
|
// Update performance metrics
|
|
|
|
|
|
|
|
|
|
|
|
messageCount[dispatchChannel.getChannelID()]++; |
|
|
|
messageCount[dispatchChannel.getChannelID()]++; |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static String getNetstatString() { |
|
|
|
public static String getNetstatString() { |
|
|
|
|
|
|
|
|
|
|
|
String outString = null; |
|
|
|
String outString; |
|
|
|
String newLine = System.getProperty("line.separator"); |
|
|
|
String newLine = System.getProperty("line.separator"); |
|
|
|
outString = "[LUA_NETSTA()]" + newLine; |
|
|
|
outString = "[LUA_NETSTA()]" + newLine; |
|
|
|
outString += "poolSize: " + itemPoolSize.longValue() + '\n'; |
|
|
|
outString += "poolSize: " + itemPoolSize.longValue() + '\n'; |
|
|
@ -94,37 +124,6 @@ public class MessageDispatcher implements Runnable { |
|
|
|
return outString; |
|
|
|
return outString; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public void run() { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
boolean shouldBlock; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while (true) { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
shouldBlock = true; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (DispatchChannel dispatchChannel : DispatchChannel.values()) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.messageDispatch = _messageQueue[dispatchChannel.getChannelID()].poll(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (this.messageDispatch != null) { |
|
|
|
|
|
|
|
DispatchMessage.serializeDispatch(this.messageDispatch); |
|
|
|
|
|
|
|
shouldBlock = false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (shouldBlock == true) |
|
|
|
|
|
|
|
shouldBlock = _blockingQueue.take(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
|
|
Logger.error(e); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// For Debugging:
|
|
|
|
// For Debugging:
|
|
|
|
//Logger.error("MessageDispatcher", messageDispatch.msg.getOpcodeAsString() + " sent to " + messageDispatch.playerList.size() + " players");
|
|
|
|
//Logger.error("MessageDispatcher", messageDispatch.msg.getOpcodeAsString() + " sent to " + messageDispatch.playerList.size() + " players");
|
|
|
|
} |
|
|
|
} |
|
|
|