public void load() { // TODO: Eventually replace this? if (loadingStage == null) { loadingStage = LoadingStage.ALLOCATE_MEMORY; } if (loadingStage == LoadingStage.ALLOCATE_MEMORY) { Runtime runtime = Runtime.getRuntime(); int usedMemory = (int) ((runtime.totalMemory() - runtime.freeMemory()) / 1024L); long currentTime = TimeUtil.getCurrentTimeMillis(); if (lastgc == 0L) { lastgc = currentTime; } if (usedMemory > 16 * 1024 && currentTime - lastgc < 5000L) { if (currentTime - lastgc > 100L) { System.gc(); lastgc = currentTime; } } else { loadingStage = LoadingStage.CREATE_GAME_WORLD; } } if (loadingStage == LoadingStage.CREATE_GAME_WORLD) { // Initialize the world traversal maps, one for each plane. for (int i = 0; i < traversalMaps.length; i++) { traversalMaps[i] = new TraversalMap(104, 104); } loadingStage = LoadingStage.CONNECT_TO_UPDATE_SERVER; } if (loadingStage == LoadingStage.CONNECT_TO_UPDATE_SERVER) { if (fileIndexFactory == null) { fileIndexFactory = new FileIndexFactory(diskEntryWorker, remoteEntryWorker); } // Once the update table is complete we can continue if (fileIndexFactory.isUpdateTableComplete()) { loadingStage = LoadingStage.PREPARE_SOUND_ENGINE; } } if (loadingStage == LoadingStage.PREPARE_SOUND_ENGINE) {} }
private void establishUpdateConnection() { if (currentAttempts < remoteEntryWorker.getAttempts()) { // Set the update connection delay to wait 5 seconds for every failed attempt // greater than 1. updateConnectionDelay = 5 * 50 * (remoteEntryWorker.getAttempts() - 1); // Limit the next connection delay to be at maximum 60 seconds updateConnectionDelay = Math.min(updateConnectionDelay, 3000); // If the client build is out of date report it if (remoteEntryWorker.getAttempts() >= 2 && remoteEntryWorker.getStatus() == Handshake.STATUS_OUT_OF_DATE) { reportUpdateError("js5connect_outofdate"); state = ClientState.UPDATE_ERROR; return; } if (remoteEntryWorker.getAttempts() >= 4 && (state == ClientState.INITIAL_LOADING)) { if (remoteEntryWorker.getStatus() > 0) { reportUpdateError("js5connect"); } else { reportUpdateError("js5io"); } state = ClientState.UPDATE_ERROR; return; } } currentAttempts = remoteEntryWorker.getAttempts(); if (updateConnectionDelay > 0) { updateConnectionDelay--; } else { try { // Create the update server socket if (updateStage == 0) { updateServerSocketRequest = getSignlink().createSocket(remoteAddress, remotePort); updateStage++; } // Await for the update server socket request to finish if (updateStage == 1) { // Check if the update server socket request errored // if it did report the error and move along if (updateServerSocketRequest.errored()) { reportUpdateError(RemoteEntryWorker.ERROR_TIMEOUT); return; } // If the request finished increment the stage and move along if (updateServerSocketRequest.isFinished()) { updateStage++; } } // Write the update server handshake if (updateStage == 2) { // Create the update server socket stream updateServerStream = new SocketStream(updateServerSocketRequest.getResult(), getSignlink(), 5000); // Write the update server handshake to the server // 0: int8 -> update_server_hs // 1: int32 -> build Buffer buffer = new Buffer(5); buffer.putInt8(Handshake.OP_UPDATE_SERVER); buffer.putInt32(getBuild()); updateServerStream.write(buffer.array(), 0, buffer.position()); updateConnectionStartTime = TimeUtil.getCurrentTimeMillis(); // Increment the update server connection stage updateStage++; } // Await for the update server handshake response if (updateStage == 3) { // If we are not initially loading, and the stream has no available bytes wait // to read the status since we don't want to block the client. However after // thirty seconds from the time the handshake was written we assume that // the connection timed out. if (state != ClientState.INITIAL_LOADING && updateServerStream.available() < 1) { if (TimeUtil.getCurrentTimeMillis() - updateConnectionStartTime > 30000L) { reportUpdateError(RemoteEntryWorker.ERROR_TIMEOUT); return; } } else { // Read the status from the server, if it is anything // but okay report it as an error. int status = updateServerStream.read(); if (status != Handshake.STATUS_OKAY) { reportUpdateError(status); return; } // Increment the update server connection stage updateStage++; } } // Initialize the remote entry worker if (updateStage == 4) { boolean online = false; // TODO: Eventually fix this remoteEntryWorker.setupConnection(updateServerStream, online); updateServerSocketRequest = null; updateStage = 0; } } catch (IOException ex) { reportUpdateError(RemoteEntryWorker.ERROR_IO); } } }