コード例 #1
0
ファイル: Player.cs プロジェクト: menohack/GlebForgeServer
 /// <summary>
 /// Copy constructor.
 /// </summary>
 /// <param name="copy">The Player object to copy into the new one.</param>
 public Player(Player copy)
 {
     this.Position = copy.Position;
     this.Velocity = copy.Velocity;
     this.Name = String.Copy(copy.Name);
     this.Password = String.Copy(copy.Password);
     this.loggedIn = copy.loggedIn;
 }
コード例 #2
0
 /// <summary>
 /// Updates player data.
 /// </summary>
 /// <param name="player"></param>
 public void UpdatePlayer(Player player)
 {
     Player result;
     if (dict.TryGetValue(player.Name, out result))
         dict.TryUpdate(player.Name, player, result);
 }
コード例 #3
0
 public bool TryUpdate(string key, Player newValue, Player comparisonValue)
 {
     return dict.TryUpdate(key, newValue, comparisonValue);
 }
コード例 #4
0
        public bool LogIn(string name)
        {
            Player player, updatedPlayer;
            if (!dict.TryGetValue(name, out player))
                throw new PlayerDoesNotExistException();

            if (player.LoggedIn)
                return false;
            else
            {
                updatedPlayer = new Player(player);
                updatedPlayer.LoggedIn = true;
                //If the update failed then the player has been modified (we are assuming
                //that LoggedIn was changed to false
                if (!dict.TryUpdate(name, updatedPlayer, player))
                    return false;
                else
                    return true;

            }
        }
コード例 #5
0
 public PlayerThread(Player player, bool networkDebug)
 {
     this.player = player;
     this.networkDebug = networkDebug;
 }
コード例 #6
0
        /// <summary>
        /// Spawn NUM_CONNECTIONS, some of which represent players that exist in the database,
        /// some of which represent players that do not exist. The threads connect and run until
        /// WAIT_BEFORE_DROP milliseconds pass, then all of them drop unexpectedly.
        /// </summary>
        public static void ConnectThenDrop()
        {
            Thread[] threads = new Thread[NUM_CONNECTIONS];
            PlayerThread[] pt = new PlayerThread[NUM_CONNECTIONS];
            Random rand = new Random();
            PlayerDatabase players = PlayerDatabase.Instance;
            players.CreateTestDatabase();
            int i = 0;
            for (; i < NUM_CONNECTIONS; i++)
            {
                Player player;

                if (rand.NextDouble() < FAKE_PLAYER_PROBABILITY)
                {
                    byte[] fakeName = new byte[rand.Next(MAX_NAME_LENGTH)];
                    byte[] fakePassword = new byte[128];
                    rand.NextBytes(fakeName);
                    rand.NextBytes(fakePassword);
                    player = new Player(new Position((float)(rand.NextDouble() * 800.0), (float)(rand.NextDouble() * 600.0)),
                        new Velocity((float)(rand.NextDouble() * 4.0 - 2.0), (float)(rand.NextDouble() * 4.0 - 2.0)),
                        ASCIIEncoding.ASCII.GetString(fakeName), ASCIIEncoding.ASCII.GetString(fakePassword));
                }
                else
                {
                    IList<Player> list = players.GetPlayers();
                    player = list[rand.Next(list.Count)];
                }

                pt[i] = new PlayerThread(player, false);
                threads[i] = new Thread(new ThreadStart(pt[i].SingleConnection));
                threads[i].Start();
            }

            //Set a boolean in each thread to true, causing the thread to return
            Thread.Sleep(WAIT_BEFORE_DROP);
            foreach (var p in pt)
                p.done = true;
        }
コード例 #7
0
ファイル: Server.cs プロジェクト: menohack/GlebForgeServer
            public void Receive(Server server)
            {
                try
                {
                    int length = server.ReadInt();
                    if (length > Player.MAX_PLAYER_NAME_LENGTH)
                        throw new ApplicationException(String.Format("Name too long: {0} characters", length));

                    String name = server.ReadString((uint)length);
                    Console.WriteLine("Player attempted to join with name: " + name);

                    server.player = players.FindPlayer(name);

                    if (server.player == null)
                        throw new ApplicationException("Player not found");
                    if (server.player.LoggedIn)
                        throw new ApplicationException("Player " + server.player.Name + " already logged in");

                    String password = server.ReadString(128);
                    if (!server.player.Password.Equals(password))
                        throw new ApplicationException(String.Format("Player gave invalid password {0}.", password));

                    Player updatedPlayer = new Player(server.player);
                    updatedPlayer.LoggedIn = true;
                    if (players.TryUpdate(name, updatedPlayer, server.player))
                        loggedIn = true;
                }
                catch (ApplicationException e)
                {
                    Console.WriteLine(e.Message);
                }
            }
コード例 #8
0
ファイル: Server.cs プロジェクト: menohack/GlebForgeServer
        /// <summary>
        /// This method runs the server by continually executing state transitions.
        /// </summary>
        private void Run()
        {
            try
            {
                stream = client.GetStream();
                StateContext context = new StateContext(this);

                while (true)
                    context.Next();
            }
            catch (Exception e)
            {
                //IOExceptions and TimeoutExceptions have the same behavior
                Console.WriteLine(e.Message);
                client.Close();

                Player updatedPlayer = new Player(player);
                updatedPlayer.LoggedIn = false;
                if (player != null && player.LoggedIn)
                    if (!players.TryUpdate(player.Name, updatedPlayer, player))
                        throw new Exception("F**k you");

                //if (player != null && player.LoggedIn)
                //	player.LoggedIn = false;
                closeServer(this);
                return;
            }
        }