/// <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; } }
/// <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; }