示例#1
0
        /// <summary>
        /// Deserialize <seealso cref="#msgPatterns"/> and <seealso cref="#msgArguments"/>.
        /// </summary>
        /// <param name="in"> Stream. </param>
        /// <exception cref="IOException"> This should never happen. </exception>
        /// <exception cref="ClassNotFoundException"> This should never happen. </exception>
        private void DeSerializeMessages(ObjectInputStream @in)
        {
            // Step 1.
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int len = in.readInt();
            int len = @in.readInt();

            msgPatterns  = new List <Localizable>(len);
            msgArguments = new List <object[]>(len);
            // Step 2.
            for (int i = 0; i < len; i++)
            {
                // Step 3.
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final Localizable pat = (Localizable) in.readObject();
                Localizable pat = (Localizable)@in.readObject();
                msgPatterns.Add(pat);
                // Step 4.
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int aLen = in.readInt();
                int aLen = @in.readInt();
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final Object[] args = new Object[aLen];
                object[] args = new object[aLen];
                for (int j = 0; j < aLen; j++)
                {
                    // Step 5.
                    args[j] = @in.readObject();
                }
                msgArguments.Add(args);
            }
        }
示例#2
0
    // 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);
        }
    }
示例#3
0
        protected internal static string[] readStringMap(ObjectInputStream @in)
        {
            int num = @in.readInt();

            string[] array = new string[num];
            for (int i = 0; i < num; i++)
            {
                string text = (string)@in.readObject();
                array[i] = text;
            }
            return(array);
        }
示例#4
0
        private static Fst readFst(ObjectInputStream objectInputStream)
        {
            string[] array    = Fst.readStringMap(objectInputStream);
            string[] array2   = Fst.readStringMap(objectInputStream);
            int      num      = objectInputStream.readInt();
            Semiring semiring = (Semiring)objectInputStream.readObject();
            int      num2     = objectInputStream.readInt();
            Fst      fst      = new Fst(num2);

            fst.isyms    = array;
            fst.osyms    = array2;
            fst.semiring = semiring;
            for (int i = 0; i < num2; i++)
            {
                int   num3  = objectInputStream.readInt();
                State state = new State(num3 + 1);
                float num4  = objectInputStream.readFloat();
                if (num4 == fst.semiring.zero())
                {
                    num4 = fst.semiring.zero();
                }
                else if (num4 == fst.semiring.one())
                {
                    num4 = fst.semiring.one();
                }
                state.setFinalWeight(num4);
                state.id = objectInputStream.readInt();
                fst.states.add(state);
            }
            fst.setStart((State)fst.states.get(num));
            num2 = fst.getNumStates();
            for (int i = 0; i < num2; i++)
            {
                State state2 = fst.getState(i);
                for (int j = 0; j < state2.initialNumArcs - 1; j++)
                {
                    Arc arc = new Arc();
                    arc.setIlabel(objectInputStream.readInt());
                    arc.setOlabel(objectInputStream.readInt());
                    arc.setWeight(objectInputStream.readFloat());
                    arc.setNextState((State)fst.states.get(objectInputStream.readInt()));
                    state2.addArc(arc);
                }
            }
            return(fst);
        }
示例#5
0
        private static ImmutableFst readImmutableFst(ObjectInputStream objectInputStream)
        {
            string[]     isyms        = Fst.readStringMap(objectInputStream);
            string[]     osyms        = Fst.readStringMap(objectInputStream);
            int          num          = objectInputStream.readInt();
            Semiring     semiring     = (Semiring)objectInputStream.readObject();
            int          num2         = objectInputStream.readInt();
            ImmutableFst immutableFst = new ImmutableFst(num2);

            immutableFst.isyms    = isyms;
            immutableFst.osyms    = osyms;
            immutableFst.semiring = semiring;
            for (int i = 0; i < num2; i++)
            {
                int            num3           = objectInputStream.readInt();
                ImmutableState immutableState = new ImmutableState(num3 + 1);
                float          num4           = objectInputStream.readFloat();
                if (num4 == immutableFst.semiring.zero())
                {
                    num4 = immutableFst.semiring.zero();
                }
                else if (num4 == immutableFst.semiring.one())
                {
                    num4 = immutableFst.semiring.one();
                }
                immutableState.setFinalWeight(num4);
                immutableState.id = objectInputStream.readInt();
                immutableFst.states[immutableState.getId()] = immutableState;
            }
            immutableFst.setStart(immutableFst.states[num]);
            num2 = immutableFst.states.Length;
            for (int i = 0; i < num2; i++)
            {
                ImmutableState immutableState2 = immutableFst.states[i];
                for (int j = 0; j < immutableState2.initialNumArcs - 1; j++)
                {
                    Arc arc = new Arc();
                    arc.setIlabel(objectInputStream.readInt());
                    arc.setOlabel(objectInputStream.readInt());
                    arc.setWeight(objectInputStream.readFloat());
                    arc.setNextState(immutableFst.states[objectInputStream.readInt()]);
                    immutableState2.setArc(j, arc);
                }
            }
            return(immutableFst);
        }
示例#6
0
        /// <summary>
        /// Deserialize <seealso cref="#context"/>.
        /// </summary>
        /// <param name="in"> Stream. </param>
        /// <exception cref="IOException"> This should never happen. </exception>
        /// <exception cref="ClassNotFoundException"> This should never happen. </exception>
        private void DeSerializeContext(ObjectInputStream @in)
        {
            // Step 1.
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int len = in.readInt();
            int len = @in.readInt();

            context = new Dictionary <string, object>();
            for (int i = 0; i < len; i++)
            {
                // Step 2.
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final String key = (String) in.readObject();
                string key = (string)@in.readObject();
                // Step 3.
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final Object value = in.readObject();
                object value = @in.readObject();
                context[key] = value;
            }
        }
示例#7
0
    // 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);
        }
    }
示例#8
0
    // 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);
        }
    }
示例#9
0
 public virtual int readInt()
 {
     return(ois.readInt());
 }