mirror of
https://gitlab.com/2009scape/2009scape.git
synced 2025-12-09 16:45:44 -07:00
More junk management server code cleanup
This commit is contained in:
parent
d23b53ba6f
commit
1bd5cb44d8
6 changed files with 9 additions and 252 deletions
|
|
@ -1,6 +1,5 @@
|
||||||
package ms;
|
package ms;
|
||||||
|
|
||||||
import ms.classloader.ClassLoadServer;
|
|
||||||
import ms.net.NioReactor;
|
import ms.net.NioReactor;
|
||||||
import ms.net.packet.WorldPacketRepository;
|
import ms.net.packet.WorldPacketRepository;
|
||||||
import ms.system.ShutdownSequence;
|
import ms.system.ShutdownSequence;
|
||||||
|
|
@ -100,7 +99,6 @@ public final class Management {
|
||||||
new Command("-rlcache", "Reloads launcher/client resource cache") {
|
new Command("-rlcache", "Reloads launcher/client resource cache") {
|
||||||
@Override
|
@Override
|
||||||
public void run(String... args) {
|
public void run(String... args) {
|
||||||
ClassLoadServer.resetResourceCache();
|
|
||||||
System.out.println("Reloaded resource cache!");
|
System.out.println("Reloaded resource cache!");
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
@ -136,7 +134,6 @@ public final class Management {
|
||||||
SQLManager.init();
|
SQLManager.init();
|
||||||
//NioReactor.configure(ServerConstants.PORT).start();
|
//NioReactor.configure(ServerConstants.PORT).start();
|
||||||
NioReactor.configure(5555).start();
|
NioReactor.configure(5555).start();
|
||||||
new ClassLoadServer().start();
|
|
||||||
Runtime.getRuntime().addShutdownHook(new ShutdownSequence());
|
Runtime.getRuntime().addShutdownHook(new ShutdownSequence());
|
||||||
System.out.println("Status: ready.");
|
System.out.println("Status: ready.");
|
||||||
System.out.println("Use -commands for a list of commands!");
|
System.out.println("Use -commands for a list of commands!");
|
||||||
|
|
|
||||||
|
|
@ -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!");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -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();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -67,6 +67,10 @@ public final class IoEventHandler {
|
||||||
ByteBuffer buffer = ByteBuffer.allocate(100_000);
|
ByteBuffer buffer = ByteBuffer.allocate(100_000);
|
||||||
IoSession session = (IoSession) key.attachment();
|
IoSession session = (IoSession) key.attachment();
|
||||||
if (channel.read(buffer) == -1) {
|
if (channel.read(buffer) == -1) {
|
||||||
|
if(session == null){
|
||||||
|
key.cancel();
|
||||||
|
return;
|
||||||
|
}
|
||||||
if(session.getGameServer() != null){
|
if(session.getGameServer() != null){
|
||||||
WorldDatabase.unRegister(session.getGameServer());
|
WorldDatabase.unRegister(session.getGameServer());
|
||||||
}
|
}
|
||||||
|
|
@ -87,8 +91,10 @@ public final class IoEventHandler {
|
||||||
*/
|
*/
|
||||||
public void write(SelectionKey key) {
|
public void write(SelectionKey key) {
|
||||||
IoSession session = (IoSession) key.attachment();
|
IoSession session = (IoSession) key.attachment();
|
||||||
key.interestOps(key.interestOps() & ~SelectionKey.OP_WRITE);
|
if(session != null) {
|
||||||
session.write();
|
key.interestOps(key.interestOps() & ~SelectionKey.OP_WRITE);
|
||||||
|
session.write();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -96,6 +96,7 @@ public final class NioReactor implements Runnable {
|
||||||
SelectionKey key = keys.next();
|
SelectionKey key = keys.next();
|
||||||
keys.remove();
|
keys.remove();
|
||||||
try {
|
try {
|
||||||
|
if(key == null) continue;
|
||||||
if (!key.isValid() || !key.channel().isOpen()) {
|
if (!key.isValid() || !key.channel().isOpen()) {
|
||||||
key.cancel();
|
key.cancel();
|
||||||
continue;
|
continue;
|
||||||
|
|
|
||||||
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue