public static FakeNetworkClient CreateFakeNetworkClient() { FakeNetworkClient client = new FakeNetworkClient(); _sessionManager.AddSession(client); long id = new Random().Next(0, 999999); AccountDTO account = new AccountDTO() { AccountId = id, Authority = AuthorityType.Admin, LastSession = 12345, Name = "test" + id, Password = "******" }; DAOFactory.AccountDAO.InsertOrUpdate(ref account); // register for account login ServiceFactory.Instance.CommunicationService.RegisterAccountLogin(account.Name, 12345); // OpenNosEntryPoint -> LoadCharacterList client.ReceivePacket("12345"); client.ReceivePacket(account.Name); client.ReceivePacket("test"); string clistStart = WaitForPacket(client); string clistEnd = WaitForPacket(client); // creation of character client.ReceivePacket($"Char_NEW {account.Name} 2 1 0 9"); List<string> clistAfterCreate = WaitForPackets(client, 3); CListPacket cListPacket = PacketFactory.Deserialize<CListPacket>(clistAfterCreate[1]); // select character client.ReceivePacket($"select {cListPacket.Slot}"); string okPacket = WaitForPacket(client); // start game client.ReceivePacket("game_start"); List<string> gameStartPacketsFirstPart = WaitForPackets(client, "p_clear"); List<string> gameStartPacketsSecondPart = WaitForPackets(client, "p_clear"); // wait 100 milliseconds to be sure initialization has been finished Thread.Sleep(100); return client; }
public static List<string> WaitForPackets(FakeNetworkClient client, int amount) { DateTime startTime = DateTime.Now; int receivedPackets = 0; List<string> packets = new List<string>(); while (receivedPackets < amount) { if (client.SentPackets.Count > 0) { packets.Add(client.SentPackets.Dequeue()); receivedPackets++; } // exit token if (startTime.AddSeconds(10) < DateTime.Now) { Assert.Fail($"Timed out while waiting for {amount} Packets"); return new List<string>(); } } return packets; }
public static List<string> WaitForPackets(FakeNetworkClient client, string lastPacketHeader, int timeout = 5) { DateTime startTime = DateTime.Now; List<string> packets = new List<string>(); while (true) { if (client.SentPackets.Count > 0) { string packet = client.SentPackets.Dequeue(); if (packet != null) { packets.Add(packet); if (packet.StartsWith(lastPacketHeader)) { return packets; } } } if (startTime.AddSeconds(timeout) <= DateTime.Now) { return packets; // timing out } } }
public static string WaitForPacket(FakeNetworkClient client, string packetHeader) { DateTime startTime = DateTime.Now; while (true) { if (client.SentPackets.Count > 0) { string packet = client.SentPackets.Dequeue(); Debug.WriteLine($"Dequeued {packet}"); if (packet != null && packet.StartsWith(packetHeader)) { return packet; } } // exit token if (startTime.AddSeconds(10) < DateTime.Now) { Assert.Fail($"Timed out while waiting for {packetHeader}"); return String.Empty; } } }