Files
prestonbane/src/engine/net/MessageDispatcher.java
T

129 lines
4.9 KiB
Java
Raw Normal View History

2022-04-30 09:41:17 -04:00
// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
// Magicbane Emulator Project © 2013 - 2022
// www.magicbane.com
package engine.net;
import engine.mbEnums.DispatchChannel;
2022-04-30 09:41:17 -04:00
import org.pmw.tinylog.Logger;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.LongAdder;
import java.util.regex.Pattern;
2024-04-26 08:09:07 -04:00
// 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.
//
2024-04-26 14:57:02 -04:00
// A DispatchMessage is configured (Protocol messsage) and wrapped in a Dispatch (distribution list).
// A Dispatch can be submitted to the MessageDispatcher for delivery from any thread.
2024-04-26 08:09:07 -04:00
//
// 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.
2022-04-30 09:41:17 -04:00
public class MessageDispatcher implements Runnable {
2024-04-26 15:00:03 -04:00
// Class variables
2022-04-30 09:41:17 -04:00
private static final ConcurrentLinkedQueue<Dispatch>[] _messageQueue = new ConcurrentLinkedQueue[DispatchChannel.values().length];
private static final LinkedBlockingQueue<Boolean> _blockingQueue = new LinkedBlockingQueue<>();
2023-07-15 09:23:48 -04:00
public static volatile long[] messageCount = new long[DispatchChannel.values().length];
public static LongAdder[] dispatchCount = new LongAdder[DispatchChannel.values().length];
2022-04-30 09:41:17 -04:00
// Performance metrics
2024-04-26 08:09:07 -04:00
2023-07-15 09:23:48 -04:00
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
private Dispatch messageDispatch;
2022-04-30 09:41:17 -04:00
public MessageDispatcher() {
// Create new FIFO queues for this network thread
2023-07-15 09:23:48 -04:00
2022-04-30 09:41:17 -04:00
for (DispatchChannel dispatchChannel : DispatchChannel.values()) {
_messageQueue[dispatchChannel.getChannelID()] = new ConcurrentLinkedQueue<>();
dispatchCount[dispatchChannel.getChannelID()] = new LongAdder();
}
2023-07-15 09:23:48 -04:00
2022-04-30 09:41:17 -04:00
filterPattern = Pattern.compile("[^\\p{ASCII}]");
2023-07-15 09:23:48 -04:00
Logger.info(" Dispatcher thread has started!");
2022-04-30 09:41:17 -04:00
}
2024-04-26 08:09:07 -04:00
@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);
}
}
}
2022-04-30 09:41:17 -04:00
public static void send(Dispatch messageDispatch, DispatchChannel dispatchChannel) {
2023-07-15 09:23:48 -04:00
2024-04-26 15:02:19 -04:00
// Use Dispatch.borrow() for a new dispatch.
// The Dispatch will be released by the system
2024-04-26 15:00:03 -04:00
// once delivered.
2022-04-30 09:41:17 -04:00
// Don't queue up empty dispatches!
2023-07-15 09:23:48 -04:00
2022-04-30 09:41:17 -04:00
if (messageDispatch.player == null)
return;
2023-07-15 09:23:48 -04:00
2022-04-30 09:41:17 -04:00
_messageQueue[dispatchChannel.getChannelID()].add(messageDispatch);
_blockingQueue.add(true);
2023-07-15 09:23:48 -04:00
2022-04-30 09:41:17 -04:00
// Update performance metrics
2023-07-15 09:23:48 -04:00
2022-04-30 09:41:17 -04:00
messageCount[dispatchChannel.getChannelID()]++;
}
2023-07-15 09:23:48 -04:00
public static String getNetstatString() {
2024-04-26 08:09:07 -04:00
String outString;
2023-07-15 09:23:48 -04:00
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");
}