import java.net.ServerSocket;import java.net.Socket;import java.io.IOException;/** * This is the core 'engine' of the runescape 317 client emulator. * * @author WinterLover <3 */public class Server implements Runnable{ /** * The number of milliseconds the server will * attempt to 'make under' every update loop. * * The number was changed from 500 to 600 because * 500 is too fast. You could even try going somewhere * between 650 and 700 for perfection, but 600 seems * alright. */ private static final int CYCLE = 600; /** * Port the ServerSocket will be bound to. * Change if you want. */ private final static int PORT = 43594; /** * The global player handler instance for the server. */ public static PlayerHandler playerHandler; /** * The server socket that receives all incoming * clients. */ private static ServerSocket clientListener; /** * Constructor that stats the thread for all incoming * connection receiving and respective client initialization * * Also instantiates server's global player handler. */ public Server() { new Thread(this).start(); playerHandler = new PlayerHandler(); } /** * Does primary looping and processing for whole server. If you are trying * to upgrade from a spin-off of cheezscape, just place all your 'somerandomclassname.process()''s * right next to 'playerHandler.process()' and your server will run the same. */ private boolean processing(int waitFails, long totalTimeSpent) { try { long beforeTime = System.currentTimeMillis(); playerHandler.process(); totalTimeSpent += System.currentTimeMillis() - beforeTime; if (totalTimeSpent >= CYCLE) { totalTimeSpent = CYCLE; if (++waitFails > 100) throw new RuntimeException("Machine is too slow to run this server!"); } return true; } catch (Exception e) { return false; } } /** * Entry point for application * * @exception InterruptedException if the Thread's sleeping is interrupted * @exception IOException if there is an exception when closing the <tt>ServerSocket</tt> instance */ public static void main(String args[]) throws InterruptedException, IOException { server server = new server(); int waitFails = 0; long totalTimeSpent = 0L; while (server.processing(waitFails, totalTimeSpent)) Thread.sleep(CYCLE - totalTimeSpent); server.playerHandler.destruct(); if (server.clientListener != null) server.clientListener.close(); server.clientListener = null; } /** * This is the client listener loop that really listens for all incoming * connections and then passes them onto the global player handler to be * processed and added to the client array. */ public void run() { try { clientListener = new ServerSocket(PORT); // previously had backlog set to 1 and default is 50 =P System.out.println("Server Started on port " + PORT + "!"); while (clientListener != null) { Socket socket = clientListener.accept(); socket.setTcpNoDelay(true); String connectingHost = socket.getInetAddress().getHostName(); System.out.println("Incoming connection from hostname " + connectingHost + " and on port " + socket.getPort()); playerHandler.newPlayerClient(socket, connectingHost); } } catch (IOException ioe) { misc.println("Error: Unable to startup listener on " + PORT + " - port already in use?"); } }}