示例#1
0
        public void ConnectionNewClientTest()
        {
            ConnectedClient connectedClient;

            Host host = new Host(IPAddress.Any);

            host.AddAcceptor(9000, (con, conClient) => new CommandHandler(con, conClient));

            try
            {
                host.Open();

                using (SimpleClient client = new SimpleClient())
                {
                    client.Connect();
                    Assert.AreEqual(1, host.ConnectedClientCount);

                    connectedClient = host.GetConnectedClientByID(client.ID);
                    Assert.AreEqual(1, connectedClient.HandlerCount);

                    client.DisconnectAsync().Wait();
                }
            }
            finally
            {
                host.Close();
            }
        }
示例#2
0
        public void ConnectionExistingClientTest()
        {
            ConnectedClient connectedClient;

            Host host = new Host(IPAddress.Any);

            host.AddAcceptor(9000, (con, conClient) => new CommandHandler(con, conClient));
            host.AddAcceptor(9001, (con, conClient) => new DataTransferHandler(con, conClient));

            try
            {
                host.Open();

                using (SimpleClient client = new SimpleClient())
                {
                    client.Connect();
                    Assert.AreEqual(1, host.ConnectedClientCount);

                    Connection connection = client.TakeConnection(9001);

                    connectedClient = host.GetConnectedClientByID(client.ID);
                    Assert.AreEqual(2, connectedClient.HandlerCount);

                    client.ReleaseConnection(connection);

                    client.DisconnectAsync().Wait();
                }
            }
            finally
            {
                host.Close();
            }
        }
示例#3
0
        public void AfterClientDisconnectTest()
        {
            ConnectedClient connectedClient;

            Host host = new Host(IPAddress.Any);

            host.AddAcceptor(9000, (con, conClient) => new CommandHandler(con, conClient));

            try
            {
                host.Open();

                using (SimpleClient client = new SimpleClient())
                {
                    client.Connect();
                    Assert.AreEqual(1, host.ConnectedClientCount);

                    connectedClient = host.GetConnectedClientByID(client.ID);
                    Assert.AreEqual(1, connectedClient.HandlerCount);

                    using (HostEventListener eventWait = new HostEventListener(host))
                    {
                        client.DisconnectAsync().Wait();

                        eventWait.SetStrategyUnlock(new UnlockIfRemoveLastHandler());

                        eventWait.Wait();
                        Assert.AreEqual(0, connectedClient.HandlerCount);

                        eventWait.Reset();

                        eventWait.SetStrategyUnlock(new UnlockIfRemoveLastClient());

                        eventWait.Wait();
                        Assert.AreEqual(0, host.ConnectedClientCount);
                    }
                }
            }
            finally
            {
                host.Close();
            }
        }
示例#4
0
        public void CommunicationObjectReceiveAndSendDataTest()
        {
            Host host = new Host(IPAddress.Any);

            host.AddAcceptor(9000, (con, conClient) => new CommandHandler(con, conClient));
            host.AddAcceptor(9001, (con, conClient) => new DataTransferHandler(con, conClient));

            try
            {
                host.Open();

                using (SimpleClient client = new SimpleClient())
                {
                    client.Connect();

                    short    commandCodeOld = 5;
                    short    errorCodeOld   = 4;
                    FileData fileDataOld    = new FileData("fileName.txt", 1024);

                    var receiveData = client.SendAndReceiveData(commandCodeOld, errorCodeOld, fileDataOld);

                    short    commandCodeNew = receiveData.Item1;
                    short    errorCodeNew   = receiveData.Item2;
                    FileData fileDataNew    = receiveData.Item3;

                    Assert.AreEqual(commandCodeOld, commandCodeNew);
                    Assert.AreEqual(errorCodeOld, errorCodeNew);
                    Assert.AreEqual(fileDataOld, fileDataNew);

                    client.DisconnectAsync().Wait();
                }
            }
            finally
            {
                host.Close();
            }
        }