예제 #1
0
        public void ShouldRaiseReceiveLongMessage()
        {
            AutoResetEvent mutex           = new AutoResetEvent(false);
            int            Port            = new Random().Next(5000, 7000);
            bool           EventRaised     = false;
            string         Message         = "";
            string         ReceivedMessage = "";


            for (int i = 0; i < 100000; i++)
            {
                Message = Message + "a";
            }



            TCPNetworkProvider Provider = new TCPNetworkProvider(Port);

            Provider.MessageReceived += (object sender, EventArgs e) =>
            {
                Assert.IsInstanceOfType(e, typeof(NetworkMessageEvent));

                EventRaised     = true;
                ReceivedMessage = ((NetworkMessageEvent)e).ReceivedMessage();
                mutex.Set();
            };



            TcpClient  client         = new TcpClient();
            IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), Port);

            client.Connect(serverEndPoint);


            NetworkStream clientStream = client.GetStream();

            ASCIIEncoding encoder = new ASCIIEncoding();

            byte[] buffer = encoder.GetBytes(Message);

            clientStream.Write(buffer, 0, buffer.Length);
            clientStream.Flush();
            mutex.WaitOne();
            client.Close();
            Provider.Stop();
            Assert.IsTrue(EventRaised);
        }
예제 #2
0
        public void ShouldRaiseConnectEvent()
        {
            int            Port        = new Random().Next(5000, 50000);
            bool           EventRaised = false;
            AutoResetEvent mutex       = new AutoResetEvent(false);

            TCPNetworkProvider Provider = new TCPNetworkProvider(Port);

            Provider.ClientConnect += (object sender, EventArgs e) => {
                EventRaised = true;
                mutex.Set();
            };



            TcpClient  client         = new TcpClient();
            IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), Port);

            client.Connect(serverEndPoint);



            NetworkStream clientStream = client.GetStream();

            ASCIIEncoding encoder = new ASCIIEncoding();

            byte[] buffer = encoder.GetBytes("hi");

            clientStream.Write(buffer, 0, buffer.Length);
            clientStream.Flush();



            mutex.WaitOne();
            client.Close();

            Provider.Stop();

            Assert.IsTrue(EventRaised);
        }