示例#1
0
        public void Send_BytesSentShouldBeEqualToBytesReceived()
        {
            urSoket = new URSocket(socket);

            var listener = server.AcceptSocket();

            listener.ReceiveTimeout = Timeout;

            string testString = "Test";

            byte[] bytes = Encoding.ASCII.GetBytes(testString);

            var expected = bytes;

            urSoket.Send(bytes);

            byte[] buffer   = new byte[100];
            var    received = listener.Receive(buffer);

            byte[] actual = new byte[received];
            Buffer.BlockCopy(buffer, 0, actual, 0, received);

            Assert.Equal(expected.Length, actual.Length);
            for (int i = 0; i < expected.Length; i++)
            {
                Assert.Equal(expected[i], actual[i]);
            }
        }
示例#2
0
        public void Receive_ShouldNotReceiveWhenDisconnected()
        {
            urSoket       = new URSocket(socket);
            urSoket.State = ConnectionState.DISCONNECTED;

            var listener = server.AcceptSocket();

            listener.ReceiveTimeout = Timeout;

            string testString = "Test";

            byte[] bytes = Encoding.ASCII.GetBytes(testString);

            listener.Send(bytes);

            try
            {
                var resp = urSoket.Receive();
                Assert.Empty(resp);
            }
            catch (SocketException e)
            {
                Assert.Equal(SocketError.TimedOut, e.SocketErrorCode);
            }
        }
示例#3
0
        public void Send_ShouldNotSendBytesWhenDisconnected()
        {
            urSoket       = new URSocket(socket);
            urSoket.State = ConnectionState.DISCONNECTED;

            var listener = server.AcceptSocket();

            listener.ReceiveTimeout = Timeout;

            string testString = "Test";

            byte[] bytes = Encoding.ASCII.GetBytes(testString);

            urSoket.Send(bytes);

            byte[] buffer = new byte[100];

            try
            {
                int expected = 0;
                var actual   = listener.Receive(buffer);
                Assert.Equal(expected, actual);
            }
            catch (SocketException e)
            {
                // Actual: Socket exception code
                // Expected: TimeOut Exception
                Assert.Equal(SocketError.TimedOut, e.SocketErrorCode);
            }
        }
示例#4
0
        public void Ctor_ValidSocketShouldConnect()
        {
            urSoket = new URSocket(socket);

            var expected = ConnectionState.CONNECTED;

            var actual = urSoket.State;

            Assert.Equal(expected, actual);
        }
示例#5
0
        public void Ctor_NullSocketShouldDisconnect()
        {
            urSoket = new URSocket(null);

            var expected = ConnectionState.DISCONNECTED;

            var actual = urSoket.State;

            Assert.Equal(expected, actual);
        }
示例#6
0
        public void Close_ShouldDisconnected()
        {
            urSoket = new URSocket(socket);

            var expected = ConnectionState.DISCONNECTED;

            urSoket.Close();

            var actual = urSoket.State;

            Assert.Equal(expected, actual);
        }
        /// <summary>Initializes a new socket and starts the SocketListener. Creates a waiting thread to wait for the connection to be established.</summary>
        public void EstablishConnectionUR()
        {
            waitHandles[0]            = new AutoResetEvent(false);
            socket                    = new URSocket();
            socketThread              = new Thread(() => socket.RunSocketListener(logic.selectedUnit.urIpAddress, waitHandles[0])); // Creates a new thread to connect to the socket based on the selected Unit collected from logic
            socketThread.IsBackground = true;
            socketThread.Name         = "socketThread";
            socketThread.Start();

            Thread waitThread = new Thread(() =>
            {
                if (OnConnectionUR != null)
                {
                    OnConnectionUR(waitHandles[0].WaitOne(timeout, false));                     // Event is thrown when the AutoResetEvent is set or when timeout has run out.
                }
            });

            waitThread.IsBackground = true;
            waitThread.Name         = "waitThreadUR";
            waitThread.Start();
        }
示例#8
0
        public void Receive_ShouldReceiveSameBytes()
        {
            urSoket = new URSocket(socket);

            var listener = server.AcceptSocket();

            listener.ReceiveTimeout = Timeout;

            string testString = "Test";

            byte[] expected = Encoding.ASCII.GetBytes(testString);

            listener.Send(expected);

            var actual = urSoket.Receive();

            Assert.Equal(expected.Length, actual.Length);

            for (int i = 0; i < expected.Length; i++)
            {
                Assert.Equal(expected[i], actual[i]);
            }
        }