More junk management server code cleanup

This commit is contained in:
ceikry 2021-08-10 09:06:11 -05:00
parent d23b53ba6f
commit 1bd5cb44d8
6 changed files with 9 additions and 252 deletions

View file

@ -1,6 +1,5 @@
package ms;
import ms.classloader.ClassLoadServer;
import ms.net.NioReactor;
import ms.net.packet.WorldPacketRepository;
import ms.system.ShutdownSequence;
@ -100,7 +99,6 @@ public final class Management {
new Command("-rlcache", "Reloads launcher/client resource cache") {
@Override
public void run(String... args) {
ClassLoadServer.resetResourceCache();
System.out.println("Reloaded resource cache!");
}
},
@ -136,7 +134,6 @@ public final class Management {
SQLManager.init();
//NioReactor.configure(ServerConstants.PORT).start();
NioReactor.configure(5555).start();
new ClassLoadServer().start();
Runtime.getRuntime().addShutdownHook(new ShutdownSequence());
System.out.println("Status: ready.");
System.out.println("Use -commands for a list of commands!");

View file

@ -1,111 +0,0 @@
package ms.classloader;
import ms.Management;
import ms.ServerConstants;
import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.file.Files;
import java.util.HashMap;
/**
* ClassLoadServer
* @author Clayton Williams (Hope)
*/
public class ClassLoadServer extends Thread {
/**
* Listening port
*/
private static final int PORT = 5050;
/**
* serverSocket for incoming connections
*/
private ServerSocket serverSocket = null;
/**
* Holds classes and resources already added in the server
*/
protected final static HashMap<String, byte[]> resourceCache = new HashMap<String, byte[]>();
/**
* New socket
* @throws UnknownHostException
* @throws IOException
*/
public ClassLoadServer() {
try {
serverSocket = new ServerSocket(PORT);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Listen for incoming connections
*/
@Override
public void run() {
resetResourceCache();
// System.out.println("Listening on port " + PORT + "...");
while (Management.active) {
Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
//System.out.println("New Connection from : " + clientSocket.getInetAddress());
WorkerThread wcs = new WorkerThread(clientSocket);
wcs.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* Loads resources recursively from a starting root directory and adds the file bytes to our cache
* @param path
* @param pathRoot
*/
private static void loadResources(String path, String pathRoot) {
try {
path = ServerConstants.fixPath(null, path);
pathRoot = ServerConstants.fixPath(null, pathRoot);
File root = new File(path);
File[] list = root.listFiles();
if (list == null) return;
for (File f : list) {
if (f.isDirectory()) {
loadResources(f.getAbsolutePath(), pathRoot);
} else {
if (f.exists()) {
if (!f.getName().startsWith(".")) {
byte[] bytes = Files.readAllBytes(f.toPath());
String name = ServerConstants.fixPath(null, f.getAbsolutePath().substring(f.getAbsolutePath().lastIndexOf(pathRoot)));
//System.out.println("[ClassLoadServer] Loaded resource '" + f.getName() +
// "' Size: " + NumberFormat.getInstance().format(bytes.length) + " bytes");
resourceCache.put(name, bytes);
}
}
}
}
} catch(Exception e) {
e.printStackTrace();
}
}
/**
* Empties the class cache and loads resources
*/
public static void resetResourceCache() {
resourceCache.clear();
//loadResources("bin/org/keldagrim/launcher/", "org/arios/launcher/");
//loadResources("resources/", "org/keldagrim/launcher/");
//System.out.println("Loaded all resources!");
}
}

View file

@ -1,136 +0,0 @@
package ms.classloader;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Map;
import ms.ServerConstants;
import ms.system.OperatingSystem;
/**
* Worker thread for launcher connections
* @author Clayton Williams (Hope)
*
*/
public class WorkerThread extends Thread {
/**
* Our launcher connection
*/
private Socket launcher = null;
/**
* Input stream from launcher
*/
private ObjectInputStream is = null;
/**
* Output stream to launcher
*/
private ObjectOutputStream os = null;
/**
* The user's operating system type
*/
private OperatingSystem operatingSystem = OperatingSystem.WINDOWS;
/**
* Creates a new worker thread to handle the launcher requests
* @param socket
* @param classesCache
* @throws IOException
*/
public WorkerThread(Socket launcher) throws IOException {
super();
this.launcher = launcher;
try {
os = new ObjectOutputStream(new BufferedOutputStream(this.launcher.getOutputStream()));
os.flush();
is = new ObjectInputStream(new BufferedInputStream(launcher.getInputStream()));
} catch (IOException ioe) {
this.launcher.close();
}
}
/**
* Input Stream handler
*/
@SuppressWarnings("unused") //saving resource type for later
public void run() {
/* byte opcode = -1;
try {
while (true) {
opcode = is.readByte();
switch(opcode) {
case 1: //requests revision
operatingSystem = is.readUTF().contains("UNIX") ? OperatingSystem.UNIX : OperatingSystem.WINDOWS;
os.reset();
os.writeInt(ClassLoadServer.resourceCache.size());//#customresources
for (Map.Entry<String, byte[]> hm : ClassLoadServer.resourceCache.entrySet()) {
os.writeUTF(ServerConstants.fixPath(operatingSystem, (String) hm.getKey()));
os.writeInt(hm.getValue().length);
}
os.writeByte(1); //number of custom resources
//followed by 2 strings
//System.err.println(operatingSystem.name());
os.writeUTF(ServerConstants.fixPath(operatingSystem, "ms/launcher/arios-gamepack-530.jar"));
os.writeUTF("BINARY");
os.flush();
break;
case 2: //resource request
String resourceName = ServerConstants.fixPath(null, is.readUTF());
String resourceType = is.readUTF();
if (ClassLoadServer.resourceCache.containsKey(resourceName)) {
sendResource(ClassLoadServer.resourceCache.get(resourceName));
//System.out.println("Sent Resource: " + resourceName);
} else {
sendOpcode(-1);
//System.out.println("Could not send resource '" + resourceName + "'");
}
break;
default:
System.out.println("Unhandled opcode=" + opcode);
break;
}
}
} catch(Exception e) {
//System.out.println("Client force disconnect.");
} finally {
try {
is.close();
os.close();
launcher.close();
} catch (Exception e) {e.printStackTrace();}
}*/
}
/**
* send a byte array packet to the client
* @exception IOException file read error.
*/
protected void sendResource(byte [] bytes) throws IOException {
os.reset();
os.writeByte(2);
os.writeInt(bytes.length);
for (int i = 0; i < bytes.length; i++) {
os.writeByte(bytes[i]);
}
os.flush();
}
/**
* Send no data opcodes
* @param opcode
* @throws IOException
*/
protected void sendOpcode(int opcode) throws IOException {
os.reset();
os.writeByte(opcode);
os.flush();
}
}

View file

@ -67,6 +67,10 @@ public final class IoEventHandler {
ByteBuffer buffer = ByteBuffer.allocate(100_000);
IoSession session = (IoSession) key.attachment();
if (channel.read(buffer) == -1) {
if(session == null){
key.cancel();
return;
}
if(session.getGameServer() != null){
WorldDatabase.unRegister(session.getGameServer());
}
@ -87,9 +91,11 @@ public final class IoEventHandler {
*/
public void write(SelectionKey key) {
IoSession session = (IoSession) key.attachment();
if(session != null) {
key.interestOps(key.interestOps() & ~SelectionKey.OP_WRITE);
session.write();
}
}
/**
* Disconnects a connection.

View file

@ -96,6 +96,7 @@ public final class NioReactor implements Runnable {
SelectionKey key = keys.next();
keys.remove();
try {
if(key == null) continue;
if (!key.isValid() || !key.channel().isOpen()) {
key.cancel();
continue;

Binary file not shown.