/// <summary> /// Serialize <seealso cref="#context"/>. /// </summary> /// <param name="out"> Stream. </param> /// <exception cref="IOException"> This should never happen. </exception> private void SerializeContext(ObjectOutputStream @out) { // Step 1. //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final int len = context.size(); int len = context.Count; @out.writeInt(len); foreach (KeyValuePair <string, object> entry in context.SetOfKeyValuePairs()) { // Step 2. @out.writeObject(entry.Key); //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final Object value = entry.getValue(); object value = entry.Value; if (value is Serializable) { // Step 3a. @out.writeObject(value); } else { // Step 3b. @out.writeObject(NonSerializableReplacement(value)); } } }
private void writeStringMap(ObjectOutputStream objectOutputStream, string[] array) { objectOutputStream.writeInt(array.Length); for (int i = 0; i < array.Length; i++) { objectOutputStream.writeObject(array[i]); } }
// Connect to server address at port 1337 (main server) and get a new port on which to login or sign up. public void connectToMainServer(bool newAcct) { Socket cnxn = null; try { // 1st step: Connect to main server and send handshake. cnxn = new Socket(ip, 1337); ObjectOutputStream output = new ObjectOutputStream(cnxn.getOutputStream()); output.writeInt(handshake); output.flush(); // Must now send username. string username = newAcct ? usernameReg.text : usernameLogin.text; output.writeObject(username); output.flush(); // Receive whatever port the server sends (random or determined). cnxn.setSoTimeout(10000); // 10-sec timeout for input reads ObjectInputStream input = new ObjectInputStream(cnxn.getInputStream()); int nextPort = input.readInt(); // Close streams and connection. input.close(); output.close(); cnxn.close(); // We got a port now! At this point, either log in or sign up. if (newAcct) { signup(nextPort); } else { loginAndPlay(nextPort); } } catch (java.lang.Exception e) { // Display some kind of error window if there was a connection error (basically a Java exception). // If the socket is null, the connection attempt failed; otherwise, the connection timed out, or something else happened. StartMenuScript sms = (StartMenuScript)(startMenu.GetComponent(typeof(StartMenuScript))); if (cnxn == null) { sms.RaiseErrorWindow("Failed to connect. Check your connection settings. The main server may be down."); } else if (e.GetType() == typeof(SocketTimeoutException)) { sms.RaiseErrorWindow("Connection timed out. Check your connection. The main server may have gone down."); } else { sms.RaiseErrorWindow("An unknown exception occurred when trying to connect to the main server."); } } catch (System.Exception e) { // This handles C# exceptions. These shouldn't happen, which is why the errors are printed to the console (for us to test for ourselves). print("Encountered a C# exception:\n"); print(e.Message); } }
private void writeFst(ObjectOutputStream objectOutputStream) { this.writeStringMap(objectOutputStream, this.isyms); this.writeStringMap(objectOutputStream, this.osyms); objectOutputStream.writeInt(this.states.indexOf(this.start)); objectOutputStream.writeObject(this.semiring); objectOutputStream.writeInt(this.states.size()); HashMap hashMap = new HashMap(this.states.size(), 1f); int i; for (i = 0; i < this.states.size(); i++) { State state = (State)this.states.get(i); objectOutputStream.writeInt(state.getNumArcs()); objectOutputStream.writeFloat(state.getFinalWeight()); objectOutputStream.writeInt(state.getId()); hashMap.put(state, Integer.valueOf(i)); } i = this.states.size(); for (int j = 0; j < i; j++) { State state2 = (State)this.states.get(j); int numArcs = state2.getNumArcs(); for (int k = 0; k < numArcs; k++) { Arc arc = state2.getArc(k); objectOutputStream.writeInt(arc.getIlabel()); objectOutputStream.writeInt(arc.getOlabel()); objectOutputStream.writeFloat(arc.getWeight()); objectOutputStream.writeInt(((Integer)hashMap.get(arc.getNextState())).intValue()); } } }
/// <summary> /// Serialize <seealso cref="#msgPatterns"/> and <seealso cref="#msgArguments"/>. /// </summary> /// <param name="out"> Stream. </param> /// <exception cref="IOException"> This should never happen. </exception> private void SerializeMessages(ObjectOutputStream @out) { // Step 1. //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final int len = msgPatterns.size(); int len = msgPatterns.Count; @out.writeInt(len); // Step 2. for (int i = 0; i < len; i++) { //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final Localizable pat = msgPatterns.get(i); Localizable pat = msgPatterns[i]; // Step 3. @out.writeObject(pat); //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final Object[] args = msgArguments.get(i); object[] args = msgArguments[i]; //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final int aLen = args.length; int aLen = args.Length; // Step 4. @out.writeInt(aLen); for (int j = 0; j < aLen; j++) { if (args[j] is Serializable) { // Step 5a. @out.writeObject(args[j]); } else { // Step 5b. @out.writeObject(NonSerializableReplacement(args[j])); } } } }
public override void writeInt(int i) { output.writeInt(i); }
// This method allows the user to log in and play (if login is successful). Automatically called by either connectToMainServer() or signup(). public void loginAndPlay(int port) { // Check if user left either field blank. if (usernameLogin.text.Equals("") || passwordLogin.text.Equals("")) { loginErrorMessage.enabled = true; return; } // First, connect to the subserver at the given port. Socket cnxn = null; bool playerInGame = false; // tracks whether the player is in-game (i.e. not in the main menu) try { cnxn = new Socket(ip, port); ObjectOutputStream output = new ObjectOutputStream(cnxn.getOutputStream()); // Send handshake output.writeInt(handshake); output.flush(); // Now that we have connected and sent our handshake, we can send commands. // First: log in. output.writeInt(LOGIN); output.flush(); output.writeObject(usernameLogin.text); output.flush(); output.writeObject(passwordLogin.text); output.flush(); // Check if login was successful or failed ObjectInputStream input = new ObjectInputStream(cnxn.getInputStream()); cnxn.setSoTimeout(10000); if (input.readInt() != SPU.ServerResponse.LOGIN_OK.ordinal()) { // Login failed at this point. Disconnect from the server so the user can try again. loginErrorMessage.enabled = true; output.writeInt(DISCONNECT); output.flush(); input.close(); output.close(); cnxn.close(); return; } // At this point, login was successful. ((StartMenuScript)(startMenu.GetComponent(typeof(StartMenuScript)))).saveLogin(); loginErrorMessage.enabled = false; // Need to get the map first so the user can see stuff. First send the command, then receive the map and visibility. output.writeInt(GETCOND); output.flush(); int visibility = input.readInt(); SPU.Tile[][] map = (SPU.Tile[][])(input.readObject()); // Load the game and start necessary threads. isLoading = true; StartCoroutine("LoadGame"); while (isLoading) { ; } playerInGame = true; Destroy(GameObject.Find("Login Menu")); Destroy(GameObject.Find("Registration Menu")); Destroy(GameObject.Find("Main Menu Music")); // Create a thread to process user inputs (i.e. for the menu and for player movement) userInputThread = new System.Threading.Thread(new ThreadStart(new UserInputThread().ProcessInput)); userInputThread.Start(); while (!userInputThread.IsAlive) { ; //loop until thread activates } // TO DO MUCH LATER: Draw the map, using visibility to determine visible tiles, and put this in the new scene. // At this point, process move commands one at a time (or logout if the user chooses). while (cmd != LOGOUT) { // First write the command... output.writeInt(cmd); output.flush(); // ...then receive the updated visibility and map from the server. visibility = input.readInt(); map = (SPU.Tile[][])(input.readObject()); } // At this point, user is ready to log out (cmd == LOGOUT). output.writeInt(LOGOUT); output.flush(); // The game can just exit everything completely if the user is quitting to desktop. if (isQuitting) { Application.Quit(); } input.close(); output.close(); cnxn.close(); Destroy(GameObject.Find("BG Music")); isLoading = true; StartCoroutine("LoadMainMenu"); while (isLoading) { ; } Destroy(this); }catch (java.lang.Exception e) { // Deal with a failed connection attempt StartMenuScript sms = (StartMenuScript)(startMenu.GetComponent(typeof(StartMenuScript))); if (cnxn == null) { sms.RaiseErrorWindow("Failed to connect. Check your connection settings. The subserver may be down."); } else if (e.GetType() == typeof(SocketTimeoutException) && !playerInGame) { sms.RaiseErrorWindow("Connection timed out. Check your connection. The subserver may have gone down."); } else { // Return to the main menu, since connection has timed out. Destroy(startMenu); Destroy(GameObject.Find("BG Music")); userInputThread.Abort(); userInputThread.Join(); isLoading = true; StartCoroutine("LoadMainMenu"); while (isLoading) { ; } StartMenuScript sms2 = ((StartMenuScript)(GameObject.Find("Start Menu").GetComponent <StartMenuScript>())); sms2.RaiseErrorWindow("Connection timed out. Check your connection. The subserver may have gone down."); Destroy(this); } }catch (System.Exception e) { // This handles C# exceptions. These shouldn't happen, which is why the errors are printed to the console (for us to test for ourselves). print("Encountered a C# exception:\n"); print(e.StackTrace); } }
// Sign up on a given port. If registration is successful, the client will automatically log in for the user to begin playing. // Automatically called by connectToMainServer(). public void signup(int port) { // Check if user left either field blank. if (usernameReg.text.Equals("") || passwordReg.text.Equals("")) { regFailedMessage.enabled = true; return; } // First, connect to the subserver at the given port. Socket cnxn = null; bool acctCreated = false; try { cnxn = new Socket(ip, port); ObjectOutputStream output = new ObjectOutputStream(cnxn.getOutputStream()); // Send handshake output.writeInt(handshake); output.flush(); // Now that we have connected and sent our handshake, we can send commands. // Here we will just sign up, close the connection, and log in using the given name and PW. output.writeInt(SIGNUP); output.flush(); // Send username and PW. output.writeObject(usernameReg.text); output.flush(); output.writeObject(passwordReg.text); output.flush(); // Check if acc was created ObjectInputStream input = new ObjectInputStream(cnxn.getInputStream()); cnxn.setSoTimeout(10000); acctCreated = input.readInt() == SPU.ServerResponse.ACCOUNT_CREATE_OK.ordinal(); if (!acctCreated) { // Display an error message if registration failed. StartMenuScript sms = (StartMenuScript)(startMenu.GetComponent(typeof(StartMenuScript))); sms.RaiseErrorWindow("Account creation failed. That name may already be taken."); } // At this point, the user is (hopefully) signed up for the server on the given port. So, log in. // (Close connection and streams first!) output.close(); input.close(); cnxn.close(); regFailedMessage.enabled = false; // If registration succeeded, at this point the client will auto-login and start playing the game. if (acctCreated) { usernameLogin.text = usernameReg.text; passwordLogin.text = passwordReg.text; ((StartMenuScript)(startMenu.GetComponent(typeof(StartMenuScript)))).RegToLogin(); loginAndPlay(port); } }catch (java.lang.Exception e) { // Display some kind of error window if there was a connection error (basically a Java exception). // If the socket is null, the connection attempt failed; otherwise, the connection timed out, or something else happened. StartMenuScript sms = (StartMenuScript)(startMenu.GetComponent(typeof(StartMenuScript))); if (cnxn == null) { sms.RaiseErrorWindow("Failed to connect. Check your connection settings. The subserver may be down."); } else if (e.GetType() == typeof(SocketTimeoutException) && !acctCreated) { sms.RaiseErrorWindow("Connection timed out. Check your connection. The subserver may have gone down."); } else if (acctCreated) { sms.RaiseErrorWindow("Connection timed out, but registration was successful. The subserver may have gone down suddenly."); } else { sms.RaiseErrorWindow("An unknown exception occurred when trying to connect to the main server."); } }catch (System.Exception e) { // This handles C# exceptions. These shouldn't happen, which is why the errors are printed to the console (for us to test for ourselves). print("Encountered a C# exception:\n"); print(e.StackTrace); } }