Пример #1
0
        public void StartListenTest()
        {
            MyTcpServerBase target = new MyTcpServerBase(); // TODO: 初始化为适当的值

            target.StartListen(6600);
            Assert.IsTrue(target.IsListening);
            target.Dispose();
        }
Пример #2
0
        public void OnConnectTest()
        {
            var ower = TcpSnapshot.Snapshot().Where(item => item.Port == 6611).FirstOrDefault();

            if (ower != null)
            {
                ower.Kill();
            }

            MyTcpServerBase target = new MyTcpServerBase(); // TODO: 初始化为适当的值

            target.StartListen(6611);

            var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            socket.Connect(new System.Net.IPEndPoint(IPAddress.Loopback, 6611));
            Thread.Sleep(50);
            Assert.IsTrue(target.ConnectTimes == 1);
            Assert.IsTrue(target.AllSessions.Count() == 1);
            Assert.IsTrue(target.ExtraState.FreeSessionCount == 0);


            var socket2 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            socket2.Connect(new System.Net.IPEndPoint(IPAddress.Loopback, 6611));
            Thread.Sleep(50);
            Assert.IsTrue(target.ConnectTimes == 2);
            Assert.IsTrue(target.AllSessions.Count() == 2);
            Assert.IsTrue(target.ExtraState.FreeSessionCount == 0);


            socket.Dispose();
            Thread.Sleep(50);
            Assert.IsTrue(target.DisConnectTimes == 1);
            Assert.IsTrue(target.AllSessions.Count() == 1);
            Assert.IsTrue(target.ExtraState.FreeSessionCount == 1);

            socket2.Dispose();
            Thread.Sleep(50);
            Assert.IsTrue(target.DisConnectTimes == 2);
            Assert.IsTrue(target.AllSessions.Count() == 0);
            Assert.IsTrue(target.ExtraState.FreeSessionCount == 2);

            target.Dispose();
        }
Пример #3
0
        public void OnConnectTest()
        {
            MyTcpServerBase target = new MyTcpServerBase(); // TODO: 初始化为适当的值

            target.StartListen(6611);

            var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            socket.Connect(new System.Net.IPEndPoint(IPAddress.Loopback, 6611));
            Thread.Sleep(50);
            Assert.IsTrue(target.ConnectTimes == 1);
            Assert.IsTrue(target.AllSessions.Count() == 1);
            Assert.IsTrue(target.ExtraState.FreeSessionCount == 0);


            var socket2 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            socket2.Connect(new System.Net.IPEndPoint(IPAddress.Loopback, 6611));
            Thread.Sleep(50);
            Assert.IsTrue(target.ConnectTimes == 2);
            Assert.IsTrue(target.AllSessions.Count() == 2);
            Assert.IsTrue(target.ExtraState.FreeSessionCount == 0);


            socket.Dispose();
            Thread.Sleep(50);
            Assert.IsTrue(target.DisConnectTimes == 1);
            Assert.IsTrue(target.AllSessions.Count() == 1);
            Assert.IsTrue(target.ExtraState.FreeSessionCount == 1);

            socket2.Dispose();
            Thread.Sleep(50);
            Assert.IsTrue(target.DisConnectTimes == 2);
            Assert.IsTrue(target.AllSessions.Count() == 0);
            Assert.IsTrue(target.ExtraState.FreeSessionCount == 2);

            target.Dispose();
        }