MB Dev Notes added to critical classes.

This commit is contained in:
2024-04-26 08:09:07 -04:00
parent 7d6407fbf5
commit 67bc13dcde
6 changed files with 91 additions and 67 deletions
+44 -45
View File
@@ -17,29 +17,31 @@ import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.LongAdder;
import java.util.regex.Pattern;
/**
* Thread blocks until MagicBane dispatch messages are
* enqueued then processes them in FIFO order. The collection
* is thread safe.
* <p>
* Any large messages not time sensitive such as load object
* sent to more than a single individual should be spawned
* individually on a DispatchMessageThread.
*/
// MB Dev Notes:
// All outgoing Protocol messages to the player are managed through the MessageDispatcher.class.
// All incoming Protocol messages from the player are managed by the Protocol.class.
//
// A DispatchMessage is configured then wrapped in a Dispatch for a distribution list.
// A Dispatch can be submitted to the Dispatcher from any thread.
//
// 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 {
// 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 LinkedBlockingQueue<Boolean> _blockingQueue = new LinkedBlockingQueue<>();
// Class variables
public static volatile long[] messageCount = new long[DispatchChannel.values().length];
public static LongAdder[] dispatchCount = new LongAdder[DispatchChannel.values().length];
// Performance metrics
public static volatile long[] maxRecipients = new long[DispatchChannel.values().length];
public static LongAdder itemPoolSize = new LongAdder();
private final Pattern filterPattern; // Unused, but just in case
@@ -61,39 +63,6 @@ public class MessageDispatcher implements Runnable {
}
public static void send(Dispatch messageDispatch, DispatchChannel dispatchChannel) {
// Don't queue up empty dispatches!
if (messageDispatch.player == null)
return;
_messageQueue[dispatchChannel.getChannelID()].add(messageDispatch);
_blockingQueue.add(true);
// Update performance metrics
messageCount[dispatchChannel.getChannelID()]++;
}
public static String getNetstatString() {
String outString = null;
String newLine = System.getProperty("line.separator");
outString = "[LUA_NETSTA()]" + newLine;
outString += "poolSize: " + itemPoolSize.longValue() + '\n';
for (DispatchChannel dispatchChannel : DispatchChannel.values()) {
outString += "Channel: " + dispatchChannel.name() + '\n';
outString += "Dispatches: " + dispatchCount[dispatchChannel.getChannelID()].longValue() + '\n';
outString += "Messages: " + messageCount[dispatchChannel.getChannelID()] + '\n';
outString += "maxRecipients: " + maxRecipients[dispatchChannel.getChannelID()] + '\n';
}
return outString;
}
@Override
public void run() {
@@ -112,12 +81,10 @@ public class MessageDispatcher implements Runnable {
DispatchMessage.serializeDispatch(this.messageDispatch);
shouldBlock = false;
}
}
if (shouldBlock == true)
shouldBlock = _blockingQueue.take();
} catch (Exception e) {
Logger.error(e);
}
@@ -125,6 +92,38 @@ public class MessageDispatcher implements Runnable {
}
}
public static void send(Dispatch messageDispatch, DispatchChannel dispatchChannel) {
// Don't queue up empty dispatches!
if (messageDispatch.player == null)
return;
_messageQueue[dispatchChannel.getChannelID()].add(messageDispatch);
_blockingQueue.add(true);
// Update performance metrics
messageCount[dispatchChannel.getChannelID()]++;
}
public static String getNetstatString() {
String outString;
String newLine = System.getProperty("line.separator");
outString = "[LUA_NETSTA()]" + newLine;
outString += "poolSize: " + itemPoolSize.longValue() + '\n';
for (DispatchChannel dispatchChannel : DispatchChannel.values()) {
outString += "Channel: " + dispatchChannel.name() + '\n';
outString += "Dispatches: " + dispatchCount[dispatchChannel.getChannelID()].longValue() + '\n';
outString += "Messages: " + messageCount[dispatchChannel.getChannelID()] + '\n';
outString += "maxRecipients: " + maxRecipients[dispatchChannel.getChannelID()] + '\n';
}
return outString;
}
// For Debugging:
//Logger.error("MessageDispatcher", messageDispatch.msg.getOpcodeAsString() + " sent to " + messageDispatch.playerList.size() + " players");
}
+15 -6
View File
@@ -1,8 +1,12 @@
// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
// Magicbane Emulator Project © 2013 - 2022
// www.magicbane.com
package engine.net;
/* This class defines Magicbane's application network protocol.
--> Name / Opcode / Message / Handler
*/
import engine.net.client.handlers.*;
import engine.net.client.msg.*;
@@ -16,6 +20,13 @@ import org.pmw.tinylog.Logger;
import java.lang.reflect.Constructor;
import java.util.HashMap;
// MB Dev Notes:
// This class defines the application network protocol used by SB.
// --> Name / Opcode / Message / Handler
//
// All incoming Protocol messages from the player are managed by the Protocol.class.
// All outgoing Protocol messages to the player are managed through the MessageDispatcher.class.
public enum Protocol {
NONE(0x0, null, null),
@@ -286,9 +297,7 @@ public enum Protocol {
public static boolean handleClientMsg(ClientNetMsg msg) {
// Main message handler for Magicbane. All messages
// incoming from the client are executed here for
// both the login and world servers.
// Process incoming Protocol message from client.
if (msg == null)
return false;