예제 #1
0
        public void TestReceive()
        {
            MithrilServer server   = new MithrilServer();
            IPEndPoint    serverEp = new IPEndPoint(IPAddress.Parse("127.0.0.1"), server.Port);

            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            socket.Bind(new IPEndPoint(IPAddress.Any, 0));
            EndPoint fromEp = new IPEndPoint(IPAddress.Any, 0);

            Buffer buffer = new Buffer();

            buffer.WriteByte(Common.ICHI);
            buffer.WriteInt(410);

            socket.SendTo(buffer.Data, buffer.Count, SocketFlags.None, serverEp);
            buffer.Wipe();
            int numBytes = socket.ReceiveFrom(buffer.Data, buffer.Size, SocketFlags.None, ref fromEp);

            buffer.Set(numBytes);

            Assert.AreEqual(server.Port, ((IPEndPoint)fromEp).Port);
            Assert.AreEqual(Common.NI, buffer.ReadByte());
            Assert.AreEqual(420, buffer.ReadInt());

            int responseValue = buffer.ReadInt() + 10;

            buffer.Wipe();
            buffer.WriteByte(Common.SAN);
            buffer.WriteInt(responseValue);
            buffer.WriteInt(59);
            socket.SendTo(buffer.Data, buffer.Count, SocketFlags.None, serverEp);

            buffer.Wipe();
            numBytes = socket.ReceiveFrom(buffer.Data, buffer.Size, SocketFlags.None, ref fromEp);
            buffer.Set(numBytes);

            Assert.AreEqual(server.Port, ((IPEndPoint)fromEp).Port);
            Assert.AreEqual(Common.YON, buffer.ReadByte());
            Assert.AreEqual(69, buffer.ReadInt());

            Assert.IsTrue(server.IsConnected());

            server.Shutdown();

            buffer.Wipe();
            numBytes = socket.ReceiveFrom(buffer.Data, buffer.Size, SocketFlags.None, ref fromEp);
            buffer.Set(numBytes);

            Assert.AreEqual(server.Port, ((IPEndPoint)fromEp).Port);
            Assert.AreEqual(Common.DISCONNECT, buffer.ReadByte());
            Assert.AreEqual(Common.R_DISCONNECT, buffer.ReadByte());
        }
예제 #2
0
        public void TestOrdering()
        {
            MithrilServer server = new MithrilServer();

            server.Receive += OnReceive;
            IPEndPoint serverEp = new IPEndPoint(IPAddress.Parse("127.0.0.1"), server.Port);

            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            socket.Bind(new IPEndPoint(IPAddress.Any, 0));
            EndPoint fromEp = new IPEndPoint(IPAddress.Any, 0);

            Mithril.Buffer buffer = new Mithril.Buffer();
            buffer.WriteByte(Common.ICHI);
            buffer.WriteInt(410);

            socket.SendTo(buffer.Data, buffer.Count, SocketFlags.None, serverEp);
            buffer.Wipe();
            int numBytes = socket.ReceiveFrom(buffer.Data, buffer.Size, SocketFlags.None, ref fromEp);

            buffer.Set(numBytes);

            int responseValue = buffer.ReadInt() + 10;

            buffer.Wipe();
            buffer.WriteByte(Common.SAN);
            buffer.WriteInt(responseValue);
            buffer.WriteInt(59);
            socket.SendTo(buffer.Data, buffer.Count, SocketFlags.None, serverEp);

            buffer.Wipe();
            numBytes = socket.ReceiveFrom(buffer.Data, buffer.Size, SocketFlags.None, ref fromEp);
            buffer.Set(numBytes);

            buffer.Wipe();
            buffer.WriteByte(Common.RELIABLE);
            buffer.WriteByte(0);
            buffer.WriteInt(0);
            socket.SendTo(buffer.Data, buffer.Count, SocketFlags.None, serverEp);

            buffer.Wipe();
            buffer.WriteByte(Common.RELIABLE);
            buffer.WriteByte(2);
            buffer.WriteInt(2);
            socket.SendTo(buffer.Data, buffer.Count, SocketFlags.None, serverEp);

            buffer.Wipe();
            buffer.WriteByte(Common.RELIABLE);
            buffer.WriteByte(1);
            buffer.WriteInt(1);
            socket.SendTo(buffer.Data, buffer.Count, SocketFlags.None, serverEp);

            Thread.Sleep(100);

            Assert.AreEqual(0, data.Dequeue());
            Assert.AreEqual(1, data.Dequeue());
            Assert.AreEqual(2, data.Dequeue());

            buffer.Wipe();
            buffer.WriteByte(Common.RELIABLE);
            buffer.WriteByte(3);
            buffer.WriteInt(3);
            socket.SendTo(buffer.Data, buffer.Count, SocketFlags.None, serverEp);

            buffer.Wipe();
            buffer.WriteByte(Common.RELIABLE);
            buffer.WriteByte(5);
            buffer.WriteInt(5);
            socket.SendTo(buffer.Data, buffer.Count, SocketFlags.None, serverEp);

            Thread.Sleep(2_001);

            Assert.IsTrue(data.Count == 2);
            Assert.AreEqual(3, data.Dequeue());
            Assert.AreEqual(5, data.Dequeue());

            server.Shutdown();
        }
예제 #3
0
        public void TestReliable()
        {
            MithrilServer server = new MithrilServer();

            server.Receive += OnReceive;
            IPEndPoint serverEp = new IPEndPoint(IPAddress.Parse("127.0.0.1"), server.Port);

            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            socket.Bind(new IPEndPoint(IPAddress.Any, 0));
            EndPoint fromEp = new IPEndPoint(IPAddress.Any, 0);

            Mithril.Buffer buffer = new Mithril.Buffer();
            buffer.WriteByte(Common.ICHI);
            buffer.WriteInt(410);

            socket.SendTo(buffer.Data, buffer.Count, SocketFlags.None, serverEp);
            buffer.Wipe();
            int numBytes = socket.ReceiveFrom(buffer.Data, buffer.Size, SocketFlags.None, ref fromEp);

            buffer.Set(numBytes);

            int responseValue = buffer.ReadInt() + 10;

            buffer.Wipe();
            buffer.WriteByte(Common.SAN);
            buffer.WriteInt(responseValue);
            buffer.WriteInt(59);
            socket.SendTo(buffer.Data, buffer.Count, SocketFlags.None, serverEp);

            buffer.Wipe();
            numBytes = socket.ReceiveFrom(buffer.Data, buffer.Size, SocketFlags.None, ref fromEp);
            buffer.Set(numBytes);

            buffer.Wipe();
            buffer.WriteByte(Common.RELIABLE);
            buffer.WriteByte(0);
            buffer.WriteInt(0);
            socket.SendTo(buffer.Data, buffer.Count, SocketFlags.None, serverEp);
            awaitingAck.Add(0);

            buffer.Wipe();
            buffer.WriteByte(Common.RELIABLE);
            buffer.WriteByte(1);
            buffer.WriteInt(1);
            socket.SendTo(buffer.Data, buffer.Count, SocketFlags.None, serverEp);
            awaitingAck.Add(1);

            buffer.Wipe();
            buffer.WriteByte(Common.RELIABLE);
            buffer.WriteByte(2);
            buffer.WriteInt(2);
            socket.SendTo(buffer.Data, buffer.Count, SocketFlags.None, serverEp);
            awaitingAck.Add(2);

            buffer.Wipe();
            numBytes = socket.ReceiveFrom(buffer.Data, buffer.Size, SocketFlags.None, ref fromEp);
            buffer.Set(numBytes);
            Assert.AreEqual(Common.ACK, buffer.ReadByte());
            Assert.IsTrue(awaitingAck.Remove(buffer.ReadByte()));

            buffer.Wipe();
            numBytes = socket.ReceiveFrom(buffer.Data, buffer.Size, SocketFlags.None, ref fromEp);
            buffer.Set(numBytes);
            Assert.AreEqual(Common.ACK, buffer.ReadByte());
            Assert.IsTrue(awaitingAck.Remove(buffer.ReadByte()));

            buffer.Wipe();
            numBytes = socket.ReceiveFrom(buffer.Data, buffer.Size, SocketFlags.None, ref fromEp);
            buffer.Set(numBytes);
            Assert.AreEqual(Common.ACK, buffer.ReadByte());
            Assert.IsTrue(awaitingAck.Remove(buffer.ReadByte()));

            Assert.IsTrue(awaitingAck.Count == 0);

            buffer.Wipe();
            buffer.WriteInt(69);
            server.SendReliable(1, buffer);

            buffer.Wipe();
            numBytes = socket.ReceiveFrom(buffer.Data, buffer.Size, SocketFlags.None, ref fromEp);
            buffer.Set(numBytes);
            Assert.AreEqual(Common.RELIABLE, buffer.ReadByte());
            byte id = buffer.ReadByte();

            Assert.AreEqual(69, buffer.ReadInt());

            buffer.Wipe();
            buffer.WriteByte(Common.ACK);
            buffer.WriteByte(id);
            socket.SendTo(buffer.Data, buffer.Count, SocketFlags.None, serverEp);

            buffer.Wipe();
            buffer.WriteInt(420);
            server.SendReliable(1, buffer);

            for (int i = 0; i < 10; i++)
            {
                buffer.Wipe();
                numBytes = socket.ReceiveFrom(buffer.Data, buffer.Size, SocketFlags.None, ref fromEp);
                buffer.Set(numBytes);
                Assert.AreEqual(Common.RELIABLE, buffer.ReadByte());
                Assert.AreEqual(2, buffer.ReadByte());                 // expected packet id
                Assert.AreEqual(420, buffer.ReadInt());
            }

            buffer.Wipe();
            buffer.WriteInt(1337);
            server.SendReliable(1, buffer);

            buffer.Wipe();
            numBytes = socket.ReceiveFrom(buffer.Data, buffer.Size, SocketFlags.None, ref fromEp);
            buffer.Set(numBytes);
            Assert.AreEqual(Common.RELIABLE, buffer.ReadByte());
            Assert.AreEqual(3, buffer.ReadByte());
            Assert.AreEqual(1337, buffer.ReadInt());

            server.Shutdown();
        }