/**
         * If exsisting player, send them the salt to their password
         * @param s socket to send the salt to
         * @param salt the salt to send
         */
        static void SendSalt(Socket s, String aSalt)
        {
            SaltSend ss = new SaltSend();

            ss.salt = aSalt;
            MemoryStream stream = ss.WriteData("This does not matter as it will not be used");

            s.Send(stream.ToArray());
        }
Esempio n. 2
0
        /**
         * Send the salt through the socket to the server
         */
        public void SendSalt()
        {
            Salt = Encryption.GetSalt();

            SaltSend ss = new SaltSend();

            ss.salt = Salt;
            MemoryStream outStream = ss.WriteData("");

            try
            {
                clientSocket.Send(outStream.ToArray());
            }
            catch { }
        }
        /**
         * The main loop for loging the player in, Some code duplication with recieve client process.
         * I felt it looked nicer and was easier if the login was kept apart from the other message types.
         * @param player A refernce to the player state so far, this will become fleshed out when the required details are forthcoming
         * @param bQuit primitives are passed by boolean by defualt.
         */
        static bool LoginSequence(ref Player player, ref bool bQuit, ref bool shouldDecrypt)
        {
            try
            {
                byte[] buffer = new byte[4096];
                int    result;

                result = player.Socket.Receive(buffer);

                if (result > 0)
                {
                    Msg m = Msg.DecodeStream(buffer, player.Salt, shouldDecrypt);

                    switch (m.mID)
                    {
                    //called when a player sends the username to the server, if the player is existing send back salt
                    case SaltRequest.ID:
                        SaltRequest SM = (SaltRequest)m;
                        lock (sqlWrapper)
                        {
                            //check to see if the salt value is valid
                            player.Salt = sqlWrapper.GetSalt(SM.message);
                            if (player.Salt != "")
                            {
                                SendSalt(player.Socket, player.Salt);
                                shouldDecrypt = true;
                            }
                            else
                            {
                                SendLoginResponse(player, "Failed No Such login", false);
                            }
                        }
                        break;

                    //called at the start of the create user. The client sends the salt at which point
                    //the server sends back a message saying its receivedd. The the encrypted createsuer can be proccessed.
                    case SaltSend.ID:
                    {
                        SaltSend ss = (SaltSend)m;
                        player.Salt = ss.salt;
                        SendSaltRecieved(player);
                        shouldDecrypt = true;
                    }
                    break;

                    //Called when the log in message arrives.
                    case LoginMessage.ID:
                    {
                        LoginMessage LM = (LoginMessage)m;
                        Console.WriteLine("Login request from: " + LM.name);

                        //queueing it with the rest and trapping the result of bool from another thread seem annoying
                        //Also it would have to have access to something back in this thread, which would also require a lock
                        //no way around locking something
                        lock (sqlWrapper)
                        {
                            if (sqlWrapper.GetPlayerLogin(ref player, LM.name, LM.password))
                            {
                                //successful log in
                                Console.WriteLine("Player: " + LM.name + "Logged in");
                                SendLoginResponse(player, "Success", true);
                                shouldDecrypt = true;

                                return(true);
                            }
                            else
                            {
                                //failed log in
                                Console.WriteLine("Player: " + LM.name + "Failed Login");
                                SendLoginResponse(player, "Failed to login", false);
                                shouldDecrypt = false;
                                return(false);
                            }
                        }
                    }

                    //Triggered if a create user message arrives
                    case CreateUser.ID:
                    {
                        //set up the new player that was created
                        CreateUser CM = (CreateUser)m;
                        player.PlayerName = CM.name;
                        Console.WriteLine("Create User recieved: " + CM.name);

                        lock (sqlWrapper)
                        {
                            //check to see if the new player is valid
                            if (sqlWrapper.AddPlayer(player, CM.password, player.Salt))
                            {
                                Console.Write(" created new player");
                                SendLoginResponse(player, "Success", true);
                                shouldDecrypt = true;
                                return(true);
                            }
                            else
                            {
                                Console.Write("Failed to create player");
                                SendLoginResponse(player, "Failed to Create player, username might already exist", false);
                                shouldDecrypt = false;
                                return(false);
                            }
                        }
                    }
                    }
                }
                return(false);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error loggin in: " + e);
                bQuit = true;
                return(false);
            }
        }
Esempio n. 4
0
        /**
         * Thread responsible for receiving all incoming data from the server
         * @param o form object reference
         */
        private void ClientReceive(Object o)
        {
            MudClient form = (MudClient)o;

            while (form.bConnected == true)
            {
                try
                {
                    byte[] buffer = new byte[4096];
                    int    result;

                    //listen for buffer
                    result = form.clientSocket.Receive(buffer);

                    if (result > 0)
                    {
                        //decode stream
                        Msg m = Msg.DecodeStream(buffer, Salt, ShouldDecrypt);

                        //Get message type and do approriate actiobn
                        if (m != null)
                        {
                            Console.Write("Got a message: ");
                            switch (m.mID)
                            {
                            case DungeonResponse.ID:
                            {
                                DungeonResponse dr = (DungeonResponse)m;

                                form.AddDungeonText(dr.response, true);
                            }
                            break;

                            case MapLayout.ID:
                            {
                                MapLayout ml = (MapLayout)m;
                                DrawQueue.Enqueue(() => ParseMap(ml.mapInfo));

                                break;
                            }

                            case LoginResponse.ID:
                            {
                                LoginResponse lm = (LoginResponse)m;
                                if (lm.loggedIn == "1")
                                {
                                    //Login was successful start decrypting and start main game
                                    loginScreen.LoginResponse(lm.message, true);
                                    ShouldDecrypt = true;
                                }
                                else
                                {
                                    //login failed, show failed message
                                    loginScreen.LoginResponse(lm.message, false);
                                    ShouldDecrypt = false;
                                }
                            }
                            break;

                            case PlayerLocations.ID:
                            {
                                PlayerLocations pl = (PlayerLocations)m;
                                DrawQueue.Enqueue(() => UpdatePlayerLocations(pl.LocationString));
                            }
                            break;

                            case UpdateChat.ID:
                            {
                                UpdateChat uc = (UpdateChat)m;
                                form.AddDungeonText(uc.message, false);
                            }
                            break;

                            case SaltSend.ID:
                            {
                                SaltSend ss = (SaltSend)m;
                                Salt = ss.salt;
                                SendLoginMessage();
                                break;
                            }

                            case SaltRequest.ID:
                            {
                                SaltRequest sr = (SaltRequest)m;
                                SendCreateUserMessage();
                                break;
                            }

                            default:
                                break;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    //If a discconect happens restart the service
                    form.bConnected = false;
                    Console.WriteLine(U.NL("Lost server!") + ex);
                    Application.Restart();
                    Environment.Exit(0);
                }
            }
        }