public CustomTest() { RUDPConnection s = new RUDPConnection(); RUDPConnection c = new RUDPConnection(); s.Listen("127.0.0.1", 80); c.Connect("127.0.0.1", 80); int counter = 0; //Listen for the next 500 packets s.OnPacketReceived += (RUDPPacket p) => { Console.WriteLine("##### " + Encoding.UTF8.GetString(p.Data, 0, p.Data.Length)); counter++; }; //Send test data for (int i = 0; i < 200; i++) { c.Send("test" + i.ToString()); } if (c.State != ConnectionState.CLOSED) { c.Disconnect(); } if (s.State != ConnectionState.CLOSED) { s.Disconnect(); } }
public override void Run() { bool finished = false; RUDPConnection s = new RUDPConnection(); RUDPConnection c = new RUDPConnection(); s.Listen("127.0.0.1", 80); c.Connect("127.0.0.1", 80); while (c.State != ConnectionState.OPEN) { Thread.Sleep(10); } Assert.AreEqual(ConnectionState.OPEN, c.State); byte[] buf = new byte[_packetSize * _multiplier]; Random r = new Random(DateTime.Now.Second); r.NextBytes(buf); int counter = 0; s.OnPacketReceived += (RUDPPacket p) => { Assert.IsTrue(p.Data.SequenceEqual(buf)); counter++; if (counter >= _packetMax) { finished = true; } }; c.OnSocketError += (IPEndPoint ep, Exception ex) => { Console.WriteLine("CLIENT ERROR {0}: {1}", ep, ex.Message); }; s.OnSocketError += (IPEndPoint ep, Exception ex) => { Console.WriteLine("SERVER ERROR {0}: {1}", ep, ex.Message); }; counter = 0; finished = false; for (int i = 0; i < _packetMax; i++) { c.Send(buf, (RUDPPacket p) => { Console.WriteLine("Packet {0} confirmed", p.Id); }); } while (!finished) { Thread.Sleep(10); } c.Status(); s.Status(); s.Disconnect(); c.Disconnect(); while (!(c.State == ConnectionState.CLOSED && s.State == ConnectionState.CLOSED)) { Thread.Sleep(10); } Assert.AreEqual(ConnectionState.CLOSED, s.State); Assert.AreEqual(ConnectionState.CLOSED, c.State); }
public void MultiPacketLarge() { int maxPackets = 100; bool finished = false; RUDPConnection s = new RUDPConnection(); RUDPConnection c = new RUDPConnection(); s.Listen("127.0.0.1", 80); c.Connect("127.0.0.1", 80); while (c.State != ConnectionState.OPEN) { Thread.Sleep(10); } Assert.AreEqual(ConnectionState.OPEN, c.State); byte[] buf = new byte[32 * 1024]; Random r = new Random(DateTime.Now.Second); r.NextBytes(buf); int counter = 0; s.OnPacketReceived += (RUDPPacket p) => { Assert.IsTrue(p.Data.SequenceEqual(buf)); counter++; if (counter >= maxPackets) { finished = true; } }; for (int i = 0; i < maxPackets; i++) { Thread.Sleep(1 * r.Next(0, 10)); c.Send(c.RemoteEndPoint, RUDPPacketType.DAT, RUDPPacketFlags.NUL, buf); } while (!finished) { Thread.Sleep(10); } counter = 0; finished = false; for (int i = 0; i < maxPackets; i++) { c.Send(c.RemoteEndPoint, RUDPPacketType.DAT, RUDPPacketFlags.NUL, buf); } while (!finished) { Thread.Sleep(10); } s.Disconnect(); c.Disconnect(); while (c.State != ConnectionState.CLOSED && s.State != ConnectionState.CLOSED) { Thread.Sleep(10); } Assert.AreEqual(ConnectionState.CLOSED, s.State); Assert.AreEqual(ConnectionState.CLOSED, c.State); }
public void MediumPacket() { int maxPackets = 500; bool finished = false; RUDPConnection s = new RUDPConnection(); RUDPConnection c = new RUDPConnection(); s.Listen("127.0.0.1", 80); c.Connect("127.0.0.1", 80); while (c.State != ConnectionState.OPEN) { Thread.Sleep(10); } Assert.AreEqual(ConnectionState.OPEN, c.State); int counter = 0; s.OnPacketReceived += (RUDPPacket p) => { Assert.AreEqual("SEQUENCEDDATAMEDIUMPACKETLENGTH" + counter, Encoding.ASCII.GetString(p.Data)); counter++; if (counter >= maxPackets) { finished = true; } }; Random r = new Random(DateTime.Now.Second); for (int i = 0; i < maxPackets; i++) { Thread.Sleep(1 * r.Next(0, 10)); c.Send("SEQUENCEDDATAMEDIUMPACKETLENGTH" + i.ToString()); } while (!finished) { Thread.Sleep(10); } counter = 0; finished = false; for (int i = 0; i < maxPackets; i++) { c.Send("SEQUENCEDDATAMEDIUMPACKETLENGTH" + i.ToString()); } while (!finished) { Thread.Sleep(10); } s.Disconnect(); c.Disconnect(); while (c.State != ConnectionState.CLOSED && s.State != ConnectionState.CLOSED) { Thread.Sleep(10); } Assert.AreEqual(ConnectionState.CLOSED, s.State); Assert.AreEqual(ConnectionState.CLOSED, c.State); }
public void PacketLossTestSync() { RUDPConnection s = new RUDPConnection(); RUDPConnection c = new RUDPConnection(); s.AsyncMode = false; c.AsyncMode = false; s.Listen("127.0.0.1", 80); c.Connect("127.0.0.1", 80); while (true) { c.Tick(); s.Tick(); if (c.State == ConnectionState.OPEN) { break; } Thread.Sleep(10); } Assert.AreEqual(ConnectionState.OPEN, c.State); int counter = 0; int maxPackets = 5; bool finished = false; s.OnPacketReceived += (RUDPPacket p) => { Assert.AreEqual(counter, int.Parse(Encoding.ASCII.GetString(p.Data))); counter++; if (counter >= maxPackets) { finished = true; } }; counter = 0; finished = false; for (int i = 0; i < maxPackets; i++) { c.Send(i.ToString()); } bool packetLoss = false; while (!finished) { c.Tick(); if (!packetLoss) { List <RUDPPacket> newPackets = new List <RUDPPacket>(); while (s._recvQueue.Count > 0) { newPackets.Add(s._recvQueue.Dequeue()); } newPackets.RemoveAt(0); s._recvQueue = new Queue <RUDPPacket>(newPackets); packetLoss = true; } Console.ReadKey(); s.Tick(); Console.ReadKey(); } s.Disconnect(); c.Disconnect(); while (true) { c.Tick(); s.Tick(); if (c.State == ConnectionState.CLOSED && s.State == ConnectionState.CLOSED) { break; } Thread.Sleep(10); } Assert.AreEqual(ConnectionState.CLOSED, s.State); Assert.AreEqual(ConnectionState.CLOSED, c.State); }