public void ServerResponses()
        {
            EasyNetwork.Server server    = new EasyNetwork.Server("tcp://*:1982");
            EasyNetwork.Client clientOne = new EasyNetwork.Client("tcp://localhost:1982");
            EasyNetwork.Client clientTwo = new EasyNetwork.Client("tcp://localhost:1982");

            server.Start();
            clientOne.Start();
            clientTwo.Start();

            // Build and send a message to the server
            MyTestObject clientOneMessage = new MyTestObject();

            clientOneMessage.Value = 3.14159f;
            clientOneMessage.Text  = "Hello Server!";
            clientOne.Send <MyTestObject>(clientOneMessage);

            // Server receives, handles, and responds to message
            Object receivedObj = null;

            while (receivedObj == null)
            {
                Guid receivedId;
                receivedObj = server.Receive(out receivedId);

                if (receivedObj is MyTestObject)
                {
                    MyTestObject receivedMessage = receivedObj as MyTestObject;
                    MyTestObject responseMessage = new MyTestObject();
                    responseMessage.Value = receivedMessage.Value * 2;
                    responseMessage.Text  = "Howdy client!";

                    server.Send <MyTestObject>(receivedMessage, receivedId);
                }

                Thread.Sleep(100);
            }

            Thread.Sleep(1000);

            // Make sure the second client didn't receive anything
            Object clientTwoReceived = clientTwo.Receive();

            Assert.IsNull(clientTwoReceived);

            // The clientOne should have received the message destined for it
            Object clientReceived = clientOne.Receive();

            Assert.IsNotNull(clientReceived);
            Assert.IsTrue(clientReceived is MyTestObject);

            server.Stop();
            clientOne.Stop();
            clientTwo.Stop();
        }
示例#2
0
        public void ServerResponseEventHandlers()
        {
            EasyNetwork.Server server    = new EasyNetwork.Server("tcp://*:1982");
            EasyNetwork.Client clientOne = new EasyNetwork.Client("tcp://localhost:1982");
            EasyNetwork.Client clientTwo = new EasyNetwork.Client("tcp://localhost:1982");

            // Set up event handles
            serverObjectsReceived.Clear();
            server.DataReceived    += Server_DataReceived;
            clientOneDataReceived   = null;
            clientTwoDataReceived   = null;
            clientOne.DataReceived += ClientOne_DataReceived;
            clientTwo.DataReceived += ClientTwo_DataReceived;

            server.Start();
            clientOne.Start();
            clientTwo.Start();

            // Build and send a message to the server
            MyTestObject clientOneMessage = new MyTestObject();

            clientOneMessage.Value = 3.14159f;
            clientOneMessage.Text  = "Hello Server!";
            clientOne.Send <MyTestObject>(clientOneMessage);

            // Server receives, handles, and responds to message
            while (serverObjectsReceived.Count == 0)
            {
                Thread.Sleep(100);
            }

            Assert.IsTrue(serverObjectsReceived.Count > 0);
            Assert.IsTrue(serverObjectsReceived[0].Item1 is MyTestObject);
            MyTestObject receivedMessage = serverObjectsReceived[0].Item1 as MyTestObject;
            MyTestObject responseMessage = new MyTestObject();

            responseMessage.Value = receivedMessage.Value * 2;
            responseMessage.Text  = "Howdy client!";
            server.Send <MyTestObject>(receivedMessage, serverObjectsReceived[0].Item2);

            Thread.Sleep(1000);

            // Make sure the second client didn't receive anything
            Assert.IsNull(clientTwoDataReceived);

            // The clientOne should have received the message destined for it
            Assert.IsNotNull(clientOneDataReceived);
            Assert.IsTrue(clientOneDataReceived is MyTestObject);

            server.Stop();
            clientOne.Stop();
            clientTwo.Stop();
        }
示例#3
0
        public void PushToAllClients()
        {
            EasyNetwork.Server server = new EasyNetwork.Server("tcp://*:1982");
            server.DataReceived += Server_DataReceived;

            List <EasyNetwork.Client> clients = new List <EasyNetwork.Client>();
            const int NumClients = 10;

            for (int i = 0; i < NumClients; i++)
            {
                clients.Add(new EasyNetwork.Client("tcp://localhost:1982"));
                clients[clients.Count - 1].DataReceived += Clients_DataReceived;
            }

            server.Start();

            foreach (EasyNetwork.Client client in clients)
            {
                client.Start();
            }

            // Clients are registered with server after any messaging, so send something from each
            MyTestObject helloFromClient = new MyTestObject();

            helloFromClient.Value = 42.42f;
            helloFromClient.Text  = "Hello Server!";

            foreach (EasyNetwork.Client client in clients)
            {
                client.Send <MyTestObject>(helloFromClient);
            }

            // Server should keep track of each client after hearing from them
            while (server.ClientList.Count < NumClients)
            {
                Thread.Sleep(100);
            }

            Assert.AreEqual(NumClients, server.ClientList.Count);

            // Send something to each client
            foreach (Guid clientId in server.ClientList)
            {
                MyTestObject helloFromServer = new MyTestObject();
                helloFromServer.Value = 4.2f;
                helloFromServer.Text  = clientId.ToString();
                server.Send <MyTestObject>(helloFromServer, clientId);
            }

            // Wait until the appropriate number of responses are received by clients
            while (dataReceivedPerClient.Count < NumClients)
            {
                Thread.Sleep(100);
            }

            // Make sure each client has received their personalized messages from the server
            foreach (EasyNetwork.Client client in clients)
            {
                Object genericObject = dataReceivedPerClient[client.Id];
                Assert.IsNotNull(genericObject);

                if (genericObject is MyTestObject)
                {
                    MyTestObject receivedObject = genericObject as MyTestObject;
                    Assert.AreEqual(client.Id.ToString(), receivedObject.Text);
                }
            }

            server.Stop();

            foreach (EasyNetwork.Client client in clients)
            {
                client.Stop();
            }
        }
        public void ServerResponseEventHandlers()
        {
            EasyNetwork.Server server = new EasyNetwork.Server("tcp://*:1982");
            EasyNetwork.Client clientOne = new EasyNetwork.Client("tcp://localhost:1982");
            EasyNetwork.Client clientTwo = new EasyNetwork.Client("tcp://localhost:1982");

            // Set up event handles
            serverObjectsReceived.Clear();
            server.DataReceived += Server_DataReceived;
            clientOneDataReceived = null;
            clientTwoDataReceived = null;
            clientOne.DataReceived += ClientOne_DataReceived;
            clientTwo.DataReceived += ClientTwo_DataReceived;

            server.Start();
            clientOne.Start();
            clientTwo.Start();

            // Build and send a message to the server
            MyTestObject clientOneMessage = new MyTestObject();
            clientOneMessage.Value = 3.14159f;
            clientOneMessage.Text = "Hello Server!";
            clientOne.Send<MyTestObject>(clientOneMessage);

            // Server receives, handles, and responds to message
            while (serverObjectsReceived.Count == 0)
            {
                Thread.Sleep(100);
            }

            Assert.IsTrue(serverObjectsReceived.Count > 0);
            Assert.IsTrue(serverObjectsReceived[0].Item1 is MyTestObject);
            MyTestObject receivedMessage = serverObjectsReceived[0].Item1 as MyTestObject;
            MyTestObject responseMessage = new MyTestObject();
            responseMessage.Value = receivedMessage.Value * 2;
            responseMessage.Text = "Howdy client!";
            server.Send<MyTestObject>(receivedMessage, serverObjectsReceived[0].Item2);

            Thread.Sleep(1000);

            // Make sure the second client didn't receive anything
            Assert.IsNull(clientTwoDataReceived);

            // The clientOne should have received the message destined for it
            Assert.IsNotNull(clientOneDataReceived);
            Assert.IsTrue(clientOneDataReceived is MyTestObject);

            server.Stop();
            clientOne.Stop();
            clientTwo.Stop();
        }
        public void PushToAllClients()
        {
            EasyNetwork.Server server = new EasyNetwork.Server("tcp://*:1982");
            server.DataReceived += Server_DataReceived;

            List<EasyNetwork.Client> clients = new List<EasyNetwork.Client>();
            const int NumClients = 10;

            for (int i = 0; i < NumClients; i++)
            {
                clients.Add(new EasyNetwork.Client("tcp://localhost:1982"));
                clients[clients.Count - 1].DataReceived += Clients_DataReceived;
            }

            server.Start();

            foreach (EasyNetwork.Client client in clients)
            {
                client.Start();
            }

            // Clients are registered with server after any messaging, so send something from each
            MyTestObject helloFromClient = new MyTestObject();
            helloFromClient.Value = 42.42f;
            helloFromClient.Text = "Hello Server!";

            foreach (EasyNetwork.Client client in clients)
            {
                client.Send<MyTestObject>(helloFromClient);
            }

            // Server should keep track of each client after hearing from them
            while (server.ClientList.Count < NumClients)
            {
                Thread.Sleep(100);
            }

            Assert.AreEqual(NumClients, server.ClientList.Count);

            // Send something to each client
            foreach (Guid clientId in server.ClientList)
            {
                MyTestObject helloFromServer = new MyTestObject();
                helloFromServer.Value = 4.2f;
                helloFromServer.Text = clientId.ToString();
                server.Send<MyTestObject>(helloFromServer, clientId);
            }

            // Wait until the appropriate number of responses are received by clients
            while (dataReceivedPerClient.Count < NumClients)
            {
                Thread.Sleep(100);
            }

            // Make sure each client has received their personalized messages from the server
            foreach (EasyNetwork.Client client in clients)
            {
                Object genericObject = dataReceivedPerClient[client.Id];
                Assert.IsNotNull(genericObject);

                if (genericObject is MyTestObject)
                {
                    MyTestObject receivedObject = genericObject as MyTestObject;
                    Assert.AreEqual(client.Id.ToString(), receivedObject.Text);
                }
            }

            server.Stop();

            foreach (EasyNetwork.Client client in clients)
            {
                client.Stop();
            }
        }
        public void ServerResponses()
        {
            EasyNetwork.Server server = new EasyNetwork.Server("tcp://*:1982");
            EasyNetwork.Client clientOne = new EasyNetwork.Client("tcp://localhost:1982");
            EasyNetwork.Client clientTwo = new EasyNetwork.Client("tcp://localhost:1982");

            server.Start();
            clientOne.Start();
            clientTwo.Start();

            // Build and send a message to the server
            MyTestObject clientOneMessage = new MyTestObject();
            clientOneMessage.Value = 3.14159f;
            clientOneMessage.Text = "Hello Server!";
            clientOne.Send<MyTestObject>(clientOneMessage);

            // Server receives, handles, and responds to message
            Object receivedObj = null;
            while (receivedObj == null)
            {
                Guid receivedId;
                receivedObj = server.Receive(out receivedId);

                if (receivedObj is MyTestObject)
                {
                    MyTestObject receivedMessage = receivedObj as MyTestObject;
                    MyTestObject responseMessage = new MyTestObject();
                    responseMessage.Value = receivedMessage.Value * 2;
                    responseMessage.Text = "Howdy client!";

                    server.Send<MyTestObject>(receivedMessage, receivedId);
                }

                Thread.Sleep(100);
            }

            Thread.Sleep(1000);

            // Make sure the second client didn't receive anything
            Object clientTwoReceived = clientTwo.Receive();
            Assert.IsNull(clientTwoReceived);

            // The clientOne should have received the message destined for it
            Object clientReceived = clientOne.Receive();
            Assert.IsNotNull(clientReceived);
            Assert.IsTrue(clientReceived is MyTestObject);

            server.Stop();
            clientOne.Stop();
            clientTwo.Stop();
        }