コード例 #1
0
 public DrawingEngine(UdpCommunicator c, Form1.Connectivity connectivity)
 {
     this.selected      = null;
     this.offlineShapes = new List <Shape>();
     this.com           = c;
     this.mode          = connectivity;
 }
コード例 #2
0
        static void Main(string[] args)
        {
            Tuple <string, string> userInput = GetUserInput();
            string domainName  = userInput.Item1;
            string dnsServerIp = userInput.Item2;

            try
            {
                for (int i = 0; i < 100; i++)
                {
                    // use IoC to resolve these...
                    IUdpCommunicator communicator = new UdpCommunicator();
                    IDnsString       dnsString    = new DnsString();
                    //IStringBuilderObjectPool stringBuilder = new StringBuilderObjectPool();
                    IDnsDatagramReader       reader           = new DnsDatagramReader(dnsString);
                    INetworkMessageProcessor messageProcessor = new DnsQueryMessageProcessor(reader, dnsString);
                    DnsResolver resolver = new DnsResolver(communicator, messageProcessor);

                    DnsResponseMessage response = resolver.Resolve(dnsServerIp, domainName);
                    PrintResult(response);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }
コード例 #3
0
        public void UdpCommunicator_ReceivingOfBadMessages()
        {
            // Setup a socket from which to send bad data
            var localEp = new IPEndPoint(IPAddress.Any, 0);
            var sender  = new UdpClient(localEp);

            var commReciever = new UdpCommunicator()
            {
                MinPort         = 10000,
                MaxPort         = 10999,
                Timeout         = 1000,
                EnvelopeHandler = ProcessEnvelope4
            };

            commReciever.Start();

            Random rand   = new Random();
            short  gameId = (short)rand.Next(1, 1000);

            byte[] bytesToSend     = Encoding.ASCII.GetBytes("Garabage");
            var    targetIpAddress = new IPEndPoint(IPAddress.Loopback, commReciever.Port);

            sender.Send(bytesToSend, bytesToSend.Length, targetIpAddress);

            // Give the receiver time to run
            Thread.Sleep(100);

            // No message should received
            Assert.IsNull(_lastIncomingEnvelope4);

            sender.Close();
            commReciever.Stop();
        }
コード例 #4
0
        public void TestSendAndReceive()
        {
            var blockComm1 = new ManualResetEvent(false);
            var blockComm2 = new ManualResetEvent(false);

            var expectedCount = 0;

            var comm1 = new UdpCommunicator(CommEndPoint1, Converter);
            var comm2 = new UdpCommunicator(CommEndPoint2, Converter);

            comm1.OnEnvelope += (Envelope env) =>
            {
                blockComm1.Set();
                Assert.True(env.MessageContent.SenderId == CommId2);
                Assert.True(env.MessageContent.ConversationId == ConvoId);
                Assert.True(env.MessageContent.MessageCount == expectedCount);

                expectedCount++;
            };

            comm2.OnEnvelope += (Envelope env) =>
            {
                blockComm2.Set();
                Assert.True(env.MessageContent.SenderId == CommId1);
                Assert.True(env.MessageContent.ConversationId == ConvoId);
                Assert.True(env.MessageContent.MessageCount == expectedCount);

                expectedCount++;
            };

            comm1.Start();
            comm2.Start();

            var startMessage = new Message()
            {
                SenderId       = CommId1,
                MessageId      = MessageType.UNKNOWN,
                ConversationId = ConvoId,
                MessageCount   = expectedCount
            };

            comm1.SendEnvelope(new Envelope(startMessage, CommEndPoint2));

            Assert.True(blockComm2.WaitOne(UdpCommunicator.Timeout * WaitFactor), "Communicator 2 never received message");

            var nextMessage = new Message()
            {
                SenderId       = CommId2,
                MessageId      = MessageType.UNKNOWN,
                ConversationId = ConvoId,
                MessageCount   = expectedCount
            };

            comm2.SendEnvelope(new Envelope(nextMessage, CommEndPoint1));

            Assert.True(blockComm1.WaitOne(UdpCommunicator.Timeout * WaitFactor), "Communicator 1 never received message");

            comm1.Stop();
            comm2.Stop();
        }
        public void UserApp_StartGameConversation_Test()
        {
            //--------------SET UP VARIABLES-------------------//
            RegistryData registryData = new RegistryData();

            RegistryData.GameInfo gameInfo = new RegistryData.GameInfo();

            TestAppWorker testAppWorker = new TestAppWorker(registryData);

            testAppWorker.StartTest();

            var gameManager = new UdpCommunicator()
            {
                MinPort         = 10000,
                MaxPort         = 10999,
                Timeout         = 1000,
                EnvelopeHandler = ProcessEnvelope1
            };

            gameManager.Start();

            IPEndPoint registryEp    = new IPEndPoint(IPAddress.Loopback, testAppWorker.commFacility.udpCommunicator.Port);
            IPEndPoint gameManagerEp = new IPEndPoint(IPAddress.Loopback, gameManager.Port);

            gameInfo.RemoteEndPoint = gameManagerEp;
            gameInfo.GameActive     = false;

            Assert.IsTrue(registryData.AddGame(gameInfo));

            StartGameMessage msg1 = new StartGameMessage(1);
            Envelope         env1 = new Envelope(msg1, registryEp);

            //--------------TEST INITIAL SET UP AND SEND INITIAL MESSAGE-------------------//
            Assert.IsNull(testAppWorker.commFacility.convDictionary.GetConv(msg1.convId));
            Assert.IsFalse(gameInfo.GameActive);

            gameManager.Send(env1);

            Thread.Sleep(1000);

            //--------------TEST OUTCOME-------------------//
            Assert.AreNotSame(msg1, _lastIncomingEnvelope1);

            // Make sure received message isn't null
            Assert.IsNotNull(_lastIncomingEnvelope1);
            Assert.IsNotNull(_lastIncomingEnvelope1.message);

            // Make sure received message is AckMessage
            Assert.AreEqual(msg1.convId, _lastIncomingEnvelope1.message.convId);
            AckMessage msg2 = _lastIncomingEnvelope1.message as AckMessage;

            Assert.IsNotNull(msg2);

            Assert.IsTrue(gameInfo.GameActive);
            Assert.IsNull(testAppWorker.commFacility.convDictionary.GetConv(msg1.convId));

            //--------------CLOSE EVERYTHING-------------------//
            testAppWorker.StopTest();
            gameManager.Stop();
        }
        public void UserApp_EndTurnConversation_Test()
        {
            TestAppWorker testAppWorker = new TestAppWorker();

            testAppWorker.StartTest();

            var fakeGameManager = new UdpCommunicator()
            {
                MinPort         = 10000,
                MaxPort         = 10999,
                Timeout         = 1000,
                EnvelopeHandler = ProcessEnvelope1
            };

            fakeGameManager.Start();

            IPEndPoint targetEndPoint = new IPEndPoint(IPAddress.Loopback, fakeGameManager.Port);
            Message    msg1           = new EndTurnMessage(1, 6);
            Envelope   env            = new Envelope(msg1, targetEndPoint);

            Assert.IsNull(testAppWorker.commFacility.convDictionary.GetConv(msg1.convId));

            testAppWorker.commFacility.Process(env);

            Assert.IsNotNull(testAppWorker.commFacility.convDictionary.GetConv(msg1.convId));

            Thread.Sleep(100);

            Assert.AreNotSame(msg1, _lastIncomingEnvelope1);
            Assert.IsNotNull(_lastIncomingEnvelope1);
            Assert.IsNotNull(_lastIncomingEnvelope1.message);
            Assert.AreEqual(msg1.msgId, _lastIncomingEnvelope1.message.msgId);
            Assert.AreEqual(msg1.convId, _lastIncomingEnvelope1.message.convId);
        }
コード例 #7
0
ファイル: Client.cs プロジェクト: arun21/pay-bit-forward
        public void Start()
        {
            var converter = new JsonMessageConverter();
            var comm      = new UdpCommunicator(new IPEndPoint(IPAddress.Any, 4000), converter);

            comm.Start();
            var router = new MessageRouter(comm);

            var content = new Content()
            {
                FileName    = "test_file.txt",
                ByteSize    = 1024,
                ContentHash = new byte[] { 0xDE, 0xAD, 0xBE, 0xEF },
                Description = "Test file",
                LocalPath   = "."
            };

            var chunks = new HashSet <int>(new List <int>()
            {
                0, 1, 2, 3
            });

            var worker = new ChunkReceiver(content, chunks, new Guid("11f684d9-53a5-4fd9-948f-5526e881f60c"));

            router.AddConversation(worker, new IPEndPoint(IPAddress.Loopback, 4001));

            Console.ReadLine();
            comm.Stop();
        }
コード例 #8
0
ファイル: JoinRoom.cs プロジェクト: hunterdean1/DrawingRoom
 public JoinRoom(Dictionary <int, Room> r, UdpCommunicator c)
 {
     InitializeComponent();
     //this.available = rooms;
     this.boxes           = r;
     this.roomsLabel.Text = RoomsToString();
     this.com             = c;
 }
コード例 #9
0
        public DrawingRoom(string s, IPEndPoint e, UdpCommunicator c)
        {
            InitializeComponent();

            this.self  = s;
            this.ownIp = e;
            this.com   = c;
        }
コード例 #10
0
        public void UdpCommunicator_Multicast()
        {
            var commSender = new UdpCommunicator()
            {
                MinPort         = 10000,
                MaxPort         = 10999,
                Timeout         = 1000,
                EnvelopeHandler = NoOp
            };

            commSender.Start();

            var commReciever = new UdpCommunicator()
            {
                MinPort         = 5000,
                MaxPort         = 5000,
                Timeout         = 1000,
                EnvelopeHandler = ProcessEnvelope5
            };

            commReciever.Start();

            Random rand   = new Random();
            short  gameId = (short)rand.Next(1, 1000);

            // Note, we can't test multiple receivers on the same host (localhost), because the
            // port number has to be the same for all receivers

            // Have the receivers join a Group Multicast address
            var multiCastAddress = new IPAddress(new byte[] { 224, 1, 1, 2 });

            commReciever.JoinMulticastGroup(multiCastAddress);

            // Send message to Group Multicast address
            var msg            = new StartGameMessage(gameId);
            var targetEndPoint = new IPEndPoint(IPAddress.Loopback, 5000);
            var env            = new Envelope(msg, targetEndPoint);

            commSender.Send(env);

            Thread.Sleep(100);

            Assert.AreNotSame(msg, _lastIncomingEnvelope5);
            Assert.IsNotNull(_lastIncomingEnvelope5);
            Assert.IsNotNull(_lastIncomingEnvelope5.message);
            Assert.AreEqual(msg.msgId, _lastIncomingEnvelope5.message.msgId);
            Assert.AreEqual(msg.convId, _lastIncomingEnvelope5.message.convId);
            StartGameMessage msg2 = _lastIncomingEnvelope5.message as StartGameMessage;

            Assert.IsNotNull(msg2);
            Assert.AreEqual(msg.GameId, msg2.GameId);

            commReciever.DropMulticastGroup(multiCastAddress);
            commReciever.Stop();
            commSender.Stop();
        }
        public void UserApp_JoinGameConversation_Test()
        {
            TestAppWorker testAppWorker = new TestAppWorker();

            testAppWorker.StartTest();

            var fakeRegistry = new UdpCommunicator()
            {
                MinPort         = 10000,
                MaxPort         = 10999,
                Timeout         = 1000,
                EnvelopeHandler = ProcessEnvelope1
            };

            fakeRegistry.Start();

            IPEndPoint targetEndPoint = new IPEndPoint(IPAddress.Loopback, fakeRegistry.Port);

            Message  msg1 = new RequestGameMessage();
            Envelope env  = new Envelope(msg1, targetEndPoint);

            Assert.IsNull(testAppWorker.commFacility.convDictionary.GetConv(msg1.convId));

            testAppWorker.commFacility.Process(env);

            Assert.IsNotNull(testAppWorker.commFacility.convDictionary.GetConv(msg1.convId));

            Thread.Sleep(100);

            Assert.AreNotSame(msg1, _lastIncomingEnvelope1);
            Assert.IsNotNull(_lastIncomingEnvelope1);
            Assert.IsNotNull(_lastIncomingEnvelope1.message);
            Assert.AreEqual(msg1.msgId, _lastIncomingEnvelope1.message.msgId);
            Assert.AreEqual(msg1.convId, _lastIncomingEnvelope1.message.convId);
            RequestGameMessage msg2 = _lastIncomingEnvelope1.message as RequestGameMessage;

            Assert.IsNotNull(msg2);

            GameInfoMessage msg3 = new GameInfoMessage(msg1.convId, 1, 1, "GMAddress", "UAAddress");

            Assert.AreNotSame((IPEndPoint)fakeRegistry._myUdpClient.Client.LocalEndPoint, _lastIncomingEnvelope1.remoteEndPoint);

            Envelope env2 = new Envelope(msg3, _lastIncomingEnvelope1.remoteEndPoint);

            fakeRegistry.Send(env2);

            Thread.Sleep(100);

            Assert.IsNull(testAppWorker.commFacility.convDictionary.GetConv(msg1.convId));

            fakeRegistry.Stop();
            testAppWorker.StopTest();
        }
コード例 #12
0
        public void Registry_JoinGameConversation_Test()
        {
            IPEndPoint registryEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 11000);

            RegistryData  registryData  = new RegistryData();
            TestAppWorker testAppWorker = new TestAppWorker(registryData, registryEndPoint);

            testAppWorker.StartTest();

            var comm1 = new UdpCommunicator()
            {
                MinPort         = 10000,
                MaxPort         = 10999,
                Timeout         = 1000,
                EnvelopeHandler = ProcessEnvelope1
            };

            comm1.Start();

            Message  msg1 = new RequestGameMessage();
            Envelope env  = new Envelope(msg1, registryEndPoint);

            Assert.IsNull(testAppWorker.commFacility.convDictionary.GetConv(msg1.convId));
            Assert.IsNull(registryData.GetAvailableGameManager());

            comm1.Send(env);

            Thread.Sleep(1000);

            /*
             * //The conversation happens to fast. After the sleep, the conv is already gone
             * Assert.IsNotNull(testAppWorker.commFacility.convDictionary.GetConv(msg1.convId));
             */

            Assert.AreNotSame(msg1, _lastIncomingEnvelope1);
            Assert.IsNotNull(_lastIncomingEnvelope1);
            Assert.IsNotNull(_lastIncomingEnvelope1.message);
            Assert.AreEqual(msg1.convId, _lastIncomingEnvelope1.message.convId);
            GameInfoMessage msg2 = _lastIncomingEnvelope1.message as GameInfoMessage;

            Assert.IsNotNull(msg2);

            Assert.IsNotNull(registryData.GetAvailableGameManager());
            Assert.IsNull(testAppWorker.commFacility.convDictionary.GetConv(msg1.convId));

            //-------------STOPPING------------//
            testAppWorker.StopTest();
            comm1.Stop();
        }
コード例 #13
0
        public void TestStartAndStop()
        {
            var blockComm = new ManualResetEvent(false);

            var comm = new UdpCommunicator(CommEndPoint1, Converter);

            Assert.False(comm.IsRunning);

            comm.Start();

            Thread.Yield();
            Assert.True(comm.IsRunning);

            comm.Stop();

            Thread.Yield();
            Assert.False(comm.IsRunning);
        }
コード例 #14
0
        public void UdpCommTest()
        {
            //Create 4 queue for both systems
            EnvelopeQueue inQ1  = new EnvelopeQueue();
            EnvelopeQueue outQ1 = new EnvelopeQueue();
            EnvelopeQueue inQ2  = new EnvelopeQueue();
            EnvelopeQueue outQ2 = new EnvelopeQueue();

            //Create the communicators
            UdpCommunicator udpCom1 = new UdpCommunicator(inQ1, outQ1, new IPEndPoint(IPAddress.Loopback, 0));
            UdpCommunicator udpCom2 = new UdpCommunicator(inQ2, outQ2, new IPEndPoint(IPAddress.Loopback, 0));

            //Start communicators
            udpCom1.startThreads();
            udpCom2.startThreads();

            //Create a message to send.
            AssignIdMessage mess = new AssignIdMessage(4, new byte[] { 0x1, 0x3 }, new byte[] { 0x1, 0x2 }, new Identifier(1, 1), new Identifier(1, 1));

            //Try to send a message between them
            Envelope env = new Envelope(mess, udpCom1.GetEndPoint(), udpCom2.GetEndPoint(), false);

            outQ1.Enqueue(env);

            //Wait for the message.
            Thread.Sleep(4000);

            //Make sure we received something.
            Assert.IsTrue(!inQ2.IsEmpty);

            //Check the message.
            Envelope tmpEnv;

            inQ2.TryDequeue(out tmpEnv);

            Assert.AreEqual(mess.getId(), ((AssignIdMessage)tmpEnv.Message).getId());


            udpCom1.closeCommunicator();
            udpCom2.closeCommunicator();
        }
コード例 #15
0
        public Form1()
        {
            InitializeComponent();
            seederDataGridView.CellFormatting += formatBytes;

            this.FormClosing += CleanUp;

            persistenceManager = new PersistenceManager();
            persistenceManager.Clear(PersistenceManager.StorageType.Remote);
            seederDataGridView.DataSource = persistenceManager.ReadContent().LocalContent;
            searchDataGridView.DataSource = persistenceManager.ReadContent().RemoteContent;

            converter = new JsonMessageConverter();
            comm      = new UdpCommunicator(new IPEndPoint(IPAddress.Any, Properties.Settings.Default.HostPort), converter);
            comm.Start();
            router = new MessageRouter(comm);

            router.OnConversationRequest += HandleNewConversation;

            RegistryEndpoint = new IPEndPoint(IPAddress.Parse(Properties.Settings.Default.RegistryAddress), Properties.Settings.Default.RegistryPort);
        }
コード例 #16
0
        public void UdpSendReceiveTest()
        {
            var        testGuid = Guid.NewGuid();
            IPEndPoint ep       = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 12000);
            Message    message  = new CanvasMessage()
            {
                ConversationId = new Tuple <Guid, short>(testGuid, 1),
                MessageNumber  = new Tuple <Guid, short>(testGuid, 1),
                CanvasId       = 1
            };
            Envelope envelope = new Envelope()
            {
                RemoteEP = ep, Message = message
            };
            UdpCommunicator communicator1 = new UdpCommunicator()
            {
                EnvelopeHandler = FirstEnvelope
            };

            communicator1.SetPort(12001);
            communicator1.Start();

            UdpCommunicator communicator2 = new UdpCommunicator()
            {
                EnvelopeHandler = SecondEnvelope
            };

            communicator2.SetPort(12000);
            communicator2.Start();

            Thread.Sleep(2000);
            communicator1.Send(envelope);
            Thread.Sleep(1000);

            Assert.IsNotNull(envelope2);
            Assert.IsNotNull(envelope2.Message);
            Assert.AreEqual(envelope.Message.ConversationId, envelope2.Message.ConversationId);
            Assert.AreEqual(envelope.Message.MessageNumber, envelope2.Message.MessageNumber);
            Assert.AreEqual((envelope.Message as CanvasMessage).CanvasId, (envelope2.Message as CanvasMessage).CanvasId);
        }
コード例 #17
0
ファイル: Seeder.cs プロジェクト: arun21/pay-bit-forward
        public void Start()
        {
            var converter = new JsonMessageConverter();
            var comm      = new UdpCommunicator(new IPEndPoint(IPAddress.Any, 4001), converter);

            comm.Start();
            var router = new MessageRouter(comm);

            router.OnConversationRequest += HandleNewConversation;

            ContentInfo = new Content()
            {
                FileName    = "test_file.txt",
                ByteSize    = 1024,
                ContentHash = new byte[] { 0xDE, 0xAD, 0xBE, 0xEF },
                Description = "Test file",
                LocalPath   = "TestData"
            };

            Console.ReadLine();
            comm.Stop();
        }
コード例 #18
0
        public void UdpCommunicator_SendingOfBadEnvelopes()
        {
            var commSender = new UdpCommunicator()
            {
                MinPort         = 10000,
                MaxPort         = 10999,
                Timeout         = 1000,
                EnvelopeHandler = NoOp
            };

            commSender.Start();

            var commReciever = new UdpCommunicator()
            {
                MinPort         = 10000,
                MaxPort         = 10999,
                Timeout         = 1000,
                EnvelopeHandler = ProcessEnvelope3
            };

            commReciever.Start();

            Random rand   = new Random();
            short  gameId = (short)rand.Next(1, 1000);

            // Send a message to a non-existant remote end point
            //     Expected behavior - no error, but no delivery
            var msg            = new StartGameMessage(gameId);
            var targetEndPoint = new IPEndPoint(IPAddress.Loopback, 1012);
            var env            = new Envelope(msg, targetEndPoint);
            var error          = commSender.Send(env);

            Assert.IsNull(error);
            Assert.IsNull(_lastIncomingEnvelope3);

            // Send a message to a remote end point with a 0 for the port
            //     Expected behavior - error
            env.remoteEndPoint = new IPEndPoint(IPAddress.Loopback, 0);
            error = commSender.Send(env);
            Assert.IsNotNull(error);
            Assert.IsNull(_lastIncomingEnvelope3);

            // Send a message to a remote end point with a 0.0.0.0 for the address
            //     Expected behavior - error
            env.remoteEndPoint = new IPEndPoint(IPAddress.Any, 1245);
            error = commSender.Send(env);
            Assert.IsNotNull(error);
            Assert.IsNull(_lastIncomingEnvelope3);

            // Send to a null remote end point
            //      Expected behavior -- error
            env.remoteEndPoint = null;
            error = commSender.Send(env);
            Assert.IsNotNull(error);
            Assert.IsNull(_lastIncomingEnvelope3);

            // Send a null message
            //      Expected behavior -- error
            env.message        = null;
            env.remoteEndPoint = new IPEndPoint(IPAddress.Loopback, 1012);
            error = commSender.Send(env);
            Assert.IsNotNull(error);
            Assert.IsNull(_lastIncomingEnvelope3);

            // Send a null envelope
            //      Expected behavior -- error
            error = commSender.Send(null);
            Assert.IsNotNull(error);
            Assert.IsNull(_lastIncomingEnvelope3);

            commSender.Stop();
            commReciever.Stop();
        }
コード例 #19
0
        public void UdpCommunicator_SimpleSendReceives()
        {
            var comm1 = new UdpCommunicator()
            {
                MinPort         = 10000,
                MaxPort         = 10999,
                Timeout         = 1000,
                EnvelopeHandler = ProcessEnvelope1
            };

            comm1.Start();

            var comm2 = new UdpCommunicator()
            {
                MinPort         = 10000,
                MaxPort         = 10999,
                Timeout         = 1000,
                EnvelopeHandler = ProcessEnvelope2
            };

            comm2.Start();

            Random rand   = new Random();
            short  gameId = (short)rand.Next(1, 1000);

            StartGameMessage msg            = new StartGameMessage(gameId);
            IPEndPoint       targetEndPoint = new IPEndPoint(IPAddress.Loopback, comm2.Port);
            Envelope         env            = new Envelope(msg, targetEndPoint);

            comm1.Send(env);

            Thread.Sleep(100);

            Assert.AreNotSame(msg, _lastIncomingEnvelope2);
            Assert.IsNotNull(_lastIncomingEnvelope2);
            Assert.IsNotNull(_lastIncomingEnvelope2.message);
            Assert.AreEqual(msg.msgId, _lastIncomingEnvelope2.message.msgId);
            Assert.AreEqual(msg.convId, _lastIncomingEnvelope2.message.convId);
            StartGameMessage msg2 = _lastIncomingEnvelope2.message as StartGameMessage;

            Assert.IsNotNull(msg2);
            Assert.AreEqual(msg.GameId, msg2.GameId);

            AckMessage msg3 = new AckMessage(msg2.convId, msg2.GameId);

            Assert.AreNotEqual(msg2.msgId, msg3.msgId);
            Assert.AreEqual(msg2.convId, msg3.convId);
            targetEndPoint = new IPEndPoint(IPAddress.Loopback, comm1.Port);
            Envelope env3 = new Envelope(msg3, targetEndPoint);

            comm2.Send(env3);

            Thread.Sleep(100);

            Assert.AreNotSame(msg3, _lastIncomingEnvelope1);
            Assert.IsNotNull(_lastIncomingEnvelope1);
            Assert.IsNotNull(_lastIncomingEnvelope1.message);
            Assert.AreEqual(msg3.msgId, _lastIncomingEnvelope1.message.msgId);
            Assert.AreEqual(msg3.convId, _lastIncomingEnvelope1.message.convId);
            AckMessage msg4 = _lastIncomingEnvelope1.message as AckMessage;

            Assert.IsNotNull(msg4);
            Assert.AreEqual(msg3.GameId, msg4.GameId);

            comm1.Stop();
            comm2.Stop();
        }
コード例 #20
0
 public Registry()
 {
     Converter    = new JsonMessageConverter();
     Communicator = new UdpCommunicator(new IPEndPoint(IPAddress.Any, 5000), Converter);
     Router       = new MessageRouter(Communicator);
 }
コード例 #21
0
        public void UserApp_StartGameConversation_Test()
        {
            //--------------SET UP VARIABLES-------------------//
            UserInfo userInfo = new UserInfo()
            {
                PlayerId        = 0,
                CurrentPlayerId = -1,
                AcceptUserInput = false
            };

            TestAppWorker testAppWorker = new TestAppWorker(userInfo);

            testAppWorker.StartTest();

            var gameManager = new UdpCommunicator()
            {
                MinPort         = 10000,
                MaxPort         = 10999,
                Timeout         = 1000,
                EnvelopeHandler = ProcessEnvelope1
            };

            gameManager.Start();

            IPEndPoint userAppEp = new IPEndPoint(IPAddress.Loopback, testAppWorker.commFacility.udpCommunicator.Port);

            testAppWorker.UserInfo.GameManagerEP = new IPEndPoint(IPAddress.Loopback, gameManager.Port);

            StartGameMessage msg1 = new StartGameMessage(1);
            Envelope         env1 = new Envelope(msg1, userAppEp);

            //--------------TEST INITIAL SET UP AND SEND INITIAL MESSAGE-------------------//
            Assert.IsNull(testAppWorker.commFacility.convDictionary.GetConv(msg1.convId));

            gameManager.Send(env1);

            Thread.Sleep(1000);

            //--------------TEST OUTCOME-------------------//
            Assert.IsTrue(testAppWorker.commFacility.convDictionary.GetConv(msg1.convId) is StartGameConversation);

            Assert.AreNotSame(msg1, _lastIncomingEnvelope1);

            // Make sure received message isn't null
            Assert.IsNotNull(_lastIncomingEnvelope1);
            Assert.IsNotNull(_lastIncomingEnvelope1.message);

            // Make sure received message is AckMessage
            Assert.AreEqual(msg1.convId, _lastIncomingEnvelope1.message.convId);
            AckMessage msg2 = _lastIncomingEnvelope1.message as AckMessage;

            Assert.IsNotNull(msg2);

            //--------------SEND START GAME STATE UPDATE MESSAGE-------------------//
            GameStateUpdateMessage msg3 = new GameStateUpdateMessage(msg1.convId, 1, 0, new short[4], new short[4], new short[84], 0, "TEST APP MOVE");
            Envelope env2 = new Envelope(msg3, _lastIncomingEnvelope1.remoteEndPoint);

            _lastIncomingEnvelope1 = null;

            // User should not know it is their turn
            Assert.IsFalse(userInfo.IsTurn);

            gameManager.Send(env2);

            Thread.Sleep(1000);

            //--------------TEST OUTCOME-------------------//
            // User should know it is their turn
            Assert.IsTrue(userInfo.IsTurn);

            Assert.AreNotSame(msg1, _lastIncomingEnvelope1);

            // Make sure received message isn't null
            Assert.IsNotNull(_lastIncomingEnvelope1);
            Assert.IsNotNull(_lastIncomingEnvelope1.message);

            // Make sure received message is AckMessage
            Assert.AreEqual(msg1.convId, _lastIncomingEnvelope1.message.convId);

            AckMessage msg4 = _lastIncomingEnvelope1.message as AckMessage;

            Assert.IsNotNull(msg4);

            //--------------SEND FIRST PLAYER GO ACK MESSAGE-------------------//
            AckMessage msg5 = new AckMessage(msg1.convId, msg1.GameId);
            Envelope   env3 = new Envelope(msg5, _lastIncomingEnvelope1.remoteEndPoint);

            _lastIncomingEnvelope1 = null;

            // Check user isn't able to input
            Assert.IsFalse(userInfo.AcceptUserInput);

            gameManager.Send(env3);

            Thread.Sleep(1000);

            //--------------TEST OUTCOME-------------------//
            // Check user is able to input
            Assert.IsTrue(userInfo.AcceptUserInput);

            // Check conversation ended
            Assert.IsNull(testAppWorker.commFacility.convDictionary.GetConv(msg1.convId));

            //--------------CLOSE EVERYTHING-------------------//
            testAppWorker.StopTest();
            gameManager.Stop();
        }
コード例 #22
0
        public void UdpAndTcpTest()
        {
            //Create 4 queue for both systems
            EnvelopeQueue inQ1A  = new EnvelopeQueue();
            EnvelopeQueue outQ1A = new EnvelopeQueue();
            EnvelopeQueue inQ2A  = new EnvelopeQueue();
            EnvelopeQueue outQ2A = new EnvelopeQueue();

            //Create the communicators
            TcpCommunicator tcpCom1 = new TcpCommunicator(inQ1A, outQ1A, new IPEndPoint(IPAddress.Loopback, 2210)); //Sender 1
            TcpCommunicator tcpCom2 = new TcpCommunicator(inQ2A, outQ2A, new IPEndPoint(IPAddress.Loopback, 2211)); //Reciever2

            //Create the communicators
            UdpCommunicator udpCom1 = new UdpCommunicator(inQ1A, outQ1A, new IPEndPoint(IPAddress.Loopback, 2212)); //Sender 1
            UdpCommunicator udpCom2 = new UdpCommunicator(inQ2A, outQ2A, new IPEndPoint(IPAddress.Loopback, 2213)); //Reciever2

            //Start communicator threads
            tcpCom1.startThreads();
            tcpCom2.startThreads();
            udpCom1.startThreads();
            udpCom2.startThreads();

            //Create the messages
            AssignIdMessage mess1 = new AssignIdMessage(1, new byte[] { 0x1, 0x3 }, new byte[] { 0x1, 0x2 }, new Identifier(1, 1), new Identifier(1, 1));
            AssignIdMessage mess2 = new AssignIdMessage(2, new byte[] { 0x1, 0x3 }, new byte[] { 0x1, 0x2 }, new Identifier(1, 1), new Identifier(1, 1));
            AssignIdMessage mess3 = new AssignIdMessage(3, new byte[] { 0x1, 0x3 }, new byte[] { 0x1, 0x2 }, new Identifier(1, 1), new Identifier(1, 1));
            AssignIdMessage mess4 = new AssignIdMessage(4, new byte[] { 0x1, 0x3 }, new byte[] { 0x1, 0x2 }, new Identifier(1, 1), new Identifier(1, 1));

            //Make the Envelopes
            Envelope env1 = new Envelope(mess1, udpCom1.GetEndPoint(), udpCom2.GetEndPoint(), false);
            Envelope env2 = new Envelope(mess2, udpCom1.GetEndPoint(), udpCom2.GetEndPoint(), false);
            Envelope env3 = new Envelope(mess3, tcpCom1.getListenerEndPoint(), tcpCom2.getListenerEndPoint(), true);
            Envelope env4 = new Envelope(mess4, tcpCom1.getListenerEndPoint(), tcpCom2.getListenerEndPoint(), true);


            //Put in sending Q.
            outQ1A.Enqueue(env1);
            outQ1A.Enqueue(env3);
            outQ1A.Enqueue(env2);
            outQ1A.Enqueue(env4);

            Thread.Sleep(3000); //Wait for messages to send.

            //Pull out messages
            Envelope tmpEnv1;
            Envelope tmpEnv2;
            Envelope tmpEnv3;
            Envelope tmpEnv4;

            //Is there something in the Q?
            Assert.IsTrue(!inQ2A.IsEmpty);

            inQ2A.TryDequeue(out tmpEnv1);
            inQ2A.TryDequeue(out tmpEnv2);
            inQ2A.TryDequeue(out tmpEnv3);
            inQ2A.TryDequeue(out tmpEnv4);

            Console.WriteLine(((AssignIdMessage)tmpEnv1.Message).getId());
            Assert.IsTrue(1 == ((AssignIdMessage)tmpEnv1.Message).getId() | 2 == ((AssignIdMessage)tmpEnv1.Message).getId() | 3 == ((AssignIdMessage)tmpEnv1.Message).getId() | 4 == ((AssignIdMessage)tmpEnv1.Message).getId());
            Assert.IsTrue(1 == ((AssignIdMessage)tmpEnv2.Message).getId() | 2 == ((AssignIdMessage)tmpEnv2.Message).getId() | 3 == ((AssignIdMessage)tmpEnv2.Message).getId() | 4 == ((AssignIdMessage)tmpEnv2.Message).getId());
            Assert.IsTrue(1 == ((AssignIdMessage)tmpEnv3.Message).getId() | 2 == ((AssignIdMessage)tmpEnv3.Message).getId() | 3 == ((AssignIdMessage)tmpEnv3.Message).getId() | 4 == ((AssignIdMessage)tmpEnv3.Message).getId());
            Assert.IsTrue(1 == ((AssignIdMessage)tmpEnv4.Message).getId() | 2 == ((AssignIdMessage)tmpEnv4.Message).getId() | 3 == ((AssignIdMessage)tmpEnv4.Message).getId() | 4 == ((AssignIdMessage)tmpEnv4.Message).getId());

            tcpCom1.closeCommunicator();
            tcpCom2.closeCommunicator();
            udpCom1.closeCommunicator();
            udpCom2.closeCommunicator();
        }
        public void GameManager_StartGameConversation_Test()
        {
            //--------------SET UP VARIABLES-------------------//
            string[]      args          = { "Test Arguments" };
            GameInfo      gameInfo      = new GameInfo();
            TestAppWorker testAppWorker = new TestAppWorker(args, gameInfo);

            testAppWorker.StartTest();

            var ua1 = new UdpCommunicator()
            {
                MinPort         = 10000,
                MaxPort         = 10999,
                Timeout         = 1000,
                EnvelopeHandler = ProcessEnvelope1
            };

            var ua2 = new UdpCommunicator()
            {
                MinPort         = 10000,
                MaxPort         = 10999,
                Timeout         = 1000,
                EnvelopeHandler = ProcessEnvelope2
            };

            var ua3 = new UdpCommunicator()
            {
                MinPort         = 10000,
                MaxPort         = 10999,
                Timeout         = 1000,
                EnvelopeHandler = ProcessEnvelope3
            };

            var ua4 = new UdpCommunicator()
            {
                MinPort         = 10000,
                MaxPort         = 10999,
                Timeout         = 1000,
                EnvelopeHandler = ProcessEnvelope4
            };

            var registry = new UdpCommunicator()
            {
                MinPort         = 10000,
                MaxPort         = 10999,
                Timeout         = 1000,
                EnvelopeHandler = ProcessEnvelope5
            };

            ua1.Start();
            ua2.Start();
            ua3.Start();
            ua4.Start();
            registry.Start();

            IPEndPoint ua1Ep      = new IPEndPoint(IPAddress.Loopback, ua1.Port);
            IPEndPoint ua2Ep      = new IPEndPoint(IPAddress.Loopback, ua2.Port);
            IPEndPoint ua3Ep      = new IPEndPoint(IPAddress.Loopback, ua3.Port);
            IPEndPoint ua4Ep      = new IPEndPoint(IPAddress.Loopback, ua4.Port);
            IPEndPoint registryEp = new IPEndPoint(IPAddress.Loopback, registry.Port);

            Assert.IsTrue(gameInfo.UserEndPoints.TryAdd(0, ua1Ep));
            Assert.IsTrue(gameInfo.UserEndPoints.TryAdd(1, ua2Ep));
            Assert.IsTrue(gameInfo.UserEndPoints.TryAdd(2, ua3Ep));
            Assert.IsTrue(gameInfo.UserEndPoints.TryAdd(3, ua4Ep));

            StartGameMessage msg1 = new StartGameMessage(1);
            Envelope         env1 = new Envelope(msg1, registryEp);

            //--------------TEST INITIAL SET UP AND START CONVERSATION-------------------//
            Assert.IsNull(testAppWorker.commFacility.convDictionary.GetConv(msg1.convId));

            testAppWorker.commFacility.Process(env1);

            Thread.Sleep(1000);

            //--------------TEST OUTCOME-------------------//
            Assert.IsTrue(testAppWorker.commFacility.convDictionary.GetConv(msg1.convId) is StartGameConversation);

            Assert.AreNotSame(msg1, _lastIncomingEnvelope1);
            Assert.AreNotSame(msg1, _lastIncomingEnvelope2);
            Assert.AreNotSame(msg1, _lastIncomingEnvelope3);
            Assert.AreNotSame(msg1, _lastIncomingEnvelope4);
            Assert.AreNotSame(msg1, _lastIncomingEnvelope5);

            // Make sure received messages aren't null
            Assert.IsNotNull(_lastIncomingEnvelope1);
            Assert.IsNotNull(_lastIncomingEnvelope1.message);

            Assert.IsNotNull(_lastIncomingEnvelope2);
            Assert.IsNotNull(_lastIncomingEnvelope2.message);

            Assert.IsNotNull(_lastIncomingEnvelope3);
            Assert.IsNotNull(_lastIncomingEnvelope3.message);

            Assert.IsNotNull(_lastIncomingEnvelope4);
            Assert.IsNotNull(_lastIncomingEnvelope4.message);

            Assert.IsNotNull(_lastIncomingEnvelope5);
            Assert.IsNotNull(_lastIncomingEnvelope5.message);

            // Make sure received messages are StartGameMessages
            Assert.AreEqual(msg1.convId, _lastIncomingEnvelope1.message.convId);
            StartGameMessage msg2 = _lastIncomingEnvelope1.message as StartGameMessage;

            Assert.IsNotNull(msg2);

            msg2 = _lastIncomingEnvelope2.message as StartGameMessage;
            Assert.IsNotNull(msg2);

            msg2 = _lastIncomingEnvelope3.message as StartGameMessage;
            Assert.IsNotNull(msg2);

            msg2 = _lastIncomingEnvelope4.message as StartGameMessage;
            Assert.IsNotNull(msg2);

            msg2 = _lastIncomingEnvelope5.message as StartGameMessage;
            Assert.IsNotNull(msg2);

            //--------------SEND START ACK MESSAGE-------------------//
            AckMessage msg3 = new AckMessage(msg1.convId, msg1.GameId);
            Envelope   env2 = new Envelope(msg3, _lastIncomingEnvelope1.remoteEndPoint);

            _lastIncomingEnvelope1 = null;
            _lastIncomingEnvelope2 = null;
            _lastIncomingEnvelope3 = null;
            _lastIncomingEnvelope4 = null;
            _lastIncomingEnvelope5 = null;

            ua1.Send(env2);
            ua2.Send(env2);
            ua3.Send(env2);
            ua4.Send(env2);
            registry.Send(env2);

            Thread.Sleep(1000);

            //--------------TEST OUTCOME-------------------//
            Assert.AreNotSame(msg1, _lastIncomingEnvelope1);
            Assert.AreNotSame(msg1, _lastIncomingEnvelope2);
            Assert.AreNotSame(msg1, _lastIncomingEnvelope3);
            Assert.AreNotSame(msg1, _lastIncomingEnvelope4);
            Assert.AreNotSame(msg1, _lastIncomingEnvelope5);

            // Make sure received messages aren't null, except registry's
            Assert.IsNotNull(_lastIncomingEnvelope1);
            Assert.IsNotNull(_lastIncomingEnvelope1.message);

            Assert.IsNotNull(_lastIncomingEnvelope2);
            Assert.IsNotNull(_lastIncomingEnvelope2.message);

            Assert.IsNotNull(_lastIncomingEnvelope3);
            Assert.IsNotNull(_lastIncomingEnvelope3.message);

            Assert.IsNotNull(_lastIncomingEnvelope4);
            Assert.IsNotNull(_lastIncomingEnvelope4.message);

            Assert.IsNull(_lastIncomingEnvelope5);

            // Make sure received messages are StartGameMessages
            Assert.AreEqual(msg1.convId, _lastIncomingEnvelope1.message.convId);

            GameStateUpdateMessage msg4;

            msg4 = _lastIncomingEnvelope1.message as GameStateUpdateMessage;
            Assert.IsNotNull(msg4);

            msg4 = _lastIncomingEnvelope2.message as GameStateUpdateMessage;
            Assert.IsNotNull(msg4);

            msg4 = _lastIncomingEnvelope3.message as GameStateUpdateMessage;
            Assert.IsNotNull(msg4);

            msg4 = _lastIncomingEnvelope4.message as GameStateUpdateMessage;
            Assert.IsNotNull(msg4);

            //--------------SEND UPDATE ACK MESSAGE-------------------//
            AckMessage msg5 = new AckMessage(msg1.convId, msg1.GameId);
            Envelope   env3 = new Envelope(msg5, _lastIncomingEnvelope1.remoteEndPoint);

            _lastIncomingEnvelope1 = null;
            _lastIncomingEnvelope2 = null;
            _lastIncomingEnvelope3 = null;
            _lastIncomingEnvelope4 = null;
            _lastIncomingEnvelope5 = null;

            ua1.Send(env3);
            ua2.Send(env3);
            ua3.Send(env3);
            ua4.Send(env3);

            Thread.Sleep(1000);

            //--------------TEST OUTCOME-------------------//
            Assert.AreNotSame(msg1, _lastIncomingEnvelope1);
            Assert.AreNotSame(msg1, _lastIncomingEnvelope2);
            Assert.AreNotSame(msg1, _lastIncomingEnvelope3);
            Assert.AreNotSame(msg1, _lastIncomingEnvelope4);
            Assert.AreNotSame(msg1, _lastIncomingEnvelope5);

            // Make sure all received messages are null except ua1's
            Assert.IsNotNull(_lastIncomingEnvelope1);
            Assert.IsNotNull(_lastIncomingEnvelope1.message);

            Assert.IsNull(_lastIncomingEnvelope2);

            Assert.IsNull(_lastIncomingEnvelope3);

            Assert.IsNull(_lastIncomingEnvelope4);

            Assert.IsNull(_lastIncomingEnvelope5);

            // Check conversation ended
            Assert.IsNull(testAppWorker.commFacility.convDictionary.GetConv(msg1.convId));

            //--------------CLOSE EVERYTHING-------------------//
            testAppWorker.StopTest();
            ua1.Stop();
            ua2.Stop();
            ua3.Stop();
            ua4.Stop();
            registry.Stop();
        }