Пример #1
0
        public void TestPokeNewValues()
        {
            MockTcpListener listener = new MockTcpListener() {
                Port = 36001
            };
            listener.BeginListener();

            MockTcpClient client = new MockTcpClient() {
                LastPacketReceived = new MockPacket() {
                    Packet = {
                        Stamp = DateTime.Now
                    }
                },
                LastPacketSent = new MockPacket() {
                    Packet = {
                        Stamp = DateTime.Now
                    }
                }
            };

            client.Setup(new ClientSetup() {
                Hostname = "localhost",
                Port = 36001
            });

            AutoResetEvent connectionWait = new AutoResetEvent(false);

            client.ConnectionStateChanged += (sender, state) => {
                if (state == ConnectionState.ConnectionReady) {
                    connectionWait.Set();
                }
            };

            client.Connect();

            Assert.IsTrue(connectionWait.WaitOne(1000));

            client.ConnectionState = ConnectionState.ConnectionLoggedIn;

            client.Poke();

            Assert.AreEqual(ConnectionState.ConnectionLoggedIn, client.ConnectionState);
        }
Пример #2
0
        // Process the client connection.
        protected static void AcceptTcpClientCallback(IAsyncResult ar)
        {
            // Get the listener that handles the client request.
            MockTcpListener listener = (MockTcpListener)ar.AsyncState;

            if (listener.Listener != null) {
                try {
                    // End the operation and display the received data on the console.
                    MockTcpClient client = new MockTcpClient(listener.Listener.EndAcceptTcpClient(ar));

                    // Listen for events on our new client
                    client.PacketReceived += listener.client_PacketReceived;
                    client.ConnectionStateChanged += listener.client_ConnectionStateChanged;

                    listener.Clients.Add(client);

                    // k, go. Now start reading.
                    client.BeginRead();

                    // Signal the calling thread to continue.
                    listener.Listener.BeginAcceptTcpClient(new AsyncCallback(AcceptTcpClientCallback), listener);
                }
                catch (Exception e) {
                    listener.OnException(e);
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Creates a listener, connects a client and ensures the connection is established. Just allows for easier
        /// tests with this drudgery taken care of.
        /// </summary>
        /// <param name="port"></param>
        /// <param name="listener"></param>
        /// <param name="client"></param>
        protected void CreateAndConnect(ushort port, out MockTcpListener listener, out MockTcpClient client)
        {
            listener = new MockTcpListener() {
                Port = port
            };

            listener.BeginListener();

            client = new MockTcpClient();

            client.Setup(new ClientSetup() {
                Hostname = "localhost",
                Port = port
            });

            client.Connect();

            AutoResetEvent connectionWait = new AutoResetEvent(false);

            Action<IClient, ConnectionState> connectionStateChangeHandler = (client1, state) => {
                if (state == ConnectionState.ConnectionReady) {
                    connectionWait.Set();
                }
            };

            client.ConnectionStateChanged += connectionStateChangeHandler;

            if (client.ConnectionState == ConnectionState.ConnectionReady) {
                connectionWait.Set();
            }

            connectionWait.WaitOne(1000);

            client.ConnectionState = ConnectionState.ConnectionLoggedIn;

            // Clean up before we give back control to the test.
            client.ConnectionStateChanged -= connectionStateChangeHandler;
        }
Пример #4
0
        public void TestPokeNulledValues()
        {
            MockTcpListener listener = new MockTcpListener() {
                Port = 36000
            };
            listener.BeginListener();

            MockTcpClient client = new MockTcpClient() {
                ConnectionState = ConnectionState.ConnectionLoggedIn
            };

            client.Setup(new ClientSetup() {
                Hostname = "localhost",
                Port = 36000
            });

            AutoResetEvent connectionWait = new AutoResetEvent(false);

            client.ConnectionStateChanged += (sender, state) => {
                if (state == ConnectionState.ConnectionReady) {
                    connectionWait.Set();
                }
            };

            client.Connect();

            Assert.IsTrue(connectionWait.WaitOne(1000));

            client.ConnectionState = ConnectionState.ConnectionLoggedIn;

            client.Poke();

            Assert.AreEqual(ConnectionState.ConnectionDisconnected, client.ConnectionState);
        }
Пример #5
0
        public void TestAcquireSequenceNumber()
        {
            MockTcpClient client = new MockTcpClient();

            Assert.AreEqual(1, client.AcquireSequenceNumber);
        }